-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgcs.go
387 lines (322 loc) · 10.7 KB
/
gcs.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
package disttopk
import (
"encoding/binary"
"fmt"
"io"
"math"
// "sort"
)
type Gcs struct {
*CountMinHash
Data *HashValueSlice
}
/*
type HashValueSlice struct {
hvs []uint32
}
func NewHashValueSlice() *HashValueSlice {
return &HashValueSlice{make([]uint32, 0)}
}
func (p HashValueSlice) Len() int { return len(p.hvs) }
func (p HashValueSlice) Less(i, j int) bool { return p.hvs[i] < p.hvs[j] }
func (p *HashValueSlice) Swap(i, j int) { p.hvs[i], p.hvs[j] = p.hvs[j], p.hvs[i] }
func (p *HashValueSlice) GetSlice() []uint32 { return p.hvs }
func (t *HashValueSlice) Insert(v uint32) {
if !t.Contains(v) {
t.hvs = append(t.hvs, v)
sort.Sort(t)
}
}
func (t *HashValueSlice) Merge(n *HashValueSlice) {
for _, v := range n.hvs {
t.Insert(v)
}
}
func (t *HashValueSlice) Contains(value uint32) bool {
ret := sort.Search(len(t.hvs), func(i int) bool { return t.hvs[i] >= value })
if ret < len(t.hvs) && t.hvs[ret] == value {
return true
}
return false
}*/
type HashValueSlice struct {
hvs map[uint32]bool
}
func NewHashValueSlice() *HashValueSlice {
return &HashValueSlice{make(map[uint32]bool)}
}
func NewHashValueSliceLen(n int) *HashValueSlice {
return &HashValueSlice{make(map[uint32]bool, n)}
}
func (p *HashValueSlice) Len() int { return len(p.hvs) }
func (p *HashValueSlice) GetSlice() []uint32 {
slice := make([]uint32, 0, p.Len())
for k, _ := range p.hvs {
slice = append(slice, k)
}
return slice
}
func (p *HashValueSlice) Eval(f func(uint32)) {
for k, _ := range p.hvs {
f(k)
}
}
func (t *HashValueSlice) Insert(v uint32) {
t.hvs[v] = true
}
func (t *HashValueSlice) Remove(v uint32) {
delete(t.hvs, v)
}
func (t *HashValueSlice) InsertAll(n *HashValueSlice) {
for k, _ := range n.hvs {
t.Insert(k)
}
}
func (t *HashValueSlice) Contains(value uint32) bool {
return t.hvs[value]
}
/*
func EstimateEpsGcs(N_est int, n_est int, penalty_bits int, NumTransfers int) float64 {
//TODO change! -- this is base on the bloom filter approximation with k != 1
//for compressed filters, needs to change.
//for m = size of bloom, p = size of each record sent as false pos, s = # times filter sent across the wire
//total (t) = s * m + (N-n) * eps * p
// m = n * 1.44 * log_2(1/eps) = n * 1.44 * 1/ln(2) * ln (1/eps)
// dt/deps = s * n * 1.44 * 1/ln(2) * 1/(1/eps) * (-1) 1/eps^2 + (N-n) * p
// 0 = -1 * s * n * 1.44 / ln (2) * 1 / eps + (N-n) * p
// (s * n * (1.44 / ln (2))) / ((N -n) * p) = eps
// eps = s * 1.44 / (N/n -1) * p * ln (2)
//fmt.Printf("N %v n %v penalty %v, NumTransfers %v\n", N_est, n_est, penalty_bits, NumTransfers)
eps := (2.0 * 1.44) / (float64(penalty_bits) * math.Log(2) * (float64(N_est/n_est) - 1.0))
//fmt.Println("Eps", eps, "N_est", N_est, "n_est", n_est)
return eps
}
func EstimateEpsGcsAdjuster(N_est int, n_est int, penalty_bits int, NumTransfers int, adjuster float64) float64 {
//TODO change! -- this is base on the bloom filter approximation with k != 1
//for compressed filters, needs to change.
//for m = size of bloom, p = size of each record sent as false pos, s = # times filter sent across the wire, A = probability filter will be used
//total (t) = s * m + (N-n) * eps * p * A
// m = n * 1.44 * log_2(1/eps) = n * 1.44 * 1/ln(2) * ln (1/eps)
// dt/deps = s * n * 1.44 * 1/ln(2) * 1/(1/eps) * (-1) 1/eps^2 + (N-n) * p * A
// 0 = -1 * s * n * 1.44 / ln (2) * 1 / eps + (N-n) * p * A
// (s * n * (1.44 / ln (2))) / ((N -n) * p * A) = eps
// eps = s * 1.44 / (N/n -1) * p * ln (2) * A
//fmt.Printf("N %v n %v penalty %v, NumTransfers %v\n", N_est, n_est, penalty_bits, NumTransfers)
eps := (2.0 * 1.44) / (float64(penalty_bits) * adjuster * math.Log(2) * (float64(N_est/n_est) - 1.0))
m := float64(n_est) * 1.44 * (1.0 / math.Log(2)) * math.Log(1.0/eps)
sketch := 2.0 * m
penalty := (float64(N_est) - float64(n_est)) * float64(penalty_bits) * adjuster * eps
fmt.Println("Eps debug: eps", eps, "sketch", sketch, "penalty", penalty)
return eps
}
*/
func EstimateEpsGcsAlt(n_est int, penalty_bits int, numNodes int, items int, numTransfers int, adjuster float64, itemslocallist int) float64 {
//TODO change! -- this is base on the bloom filter approximation with k != 1
//for compressed filters, needs to change.
//for m = size of bloom, p = size of each record sent as false pos, s = # times filter sent across the wire, A = probability filter will be used
// x = num nodes, l = items per node
//total (t) = s*x*m + (l-n) * eps * p * x
// m = n * 1.44 * log_2(1/eps) = n * 1.44 * 1/ln(2) * ln (1/eps)
// dt/deps = s*x*n * 1.44 * 1/ln(2) * 1/(1/eps) * (-1) 1/eps^2 + (l-n) * p * x
// 0 = -1 * s * x * n * 1.44 / ln (2) * 1 / eps + (l-n) * p * x
// ( s* x *n * (1.44 / ln (2))) / ((l -n) * p * x) = eps
// eps = s * 1.44 / (l/n -1) * p * ln (2)
//fmt.Println("n_est", n_est, "penalty_bits", penalty_bits, "numNodes", numNodes, "itemspn", itemsPerNode)
//size_adj := 0.8
//eps := (float64(numTransfers) * size_adj * 1.44) / (float64(penalty_bits) * adjuster * math.Log(2) * (float64(items/n_est) - 1.0))
//k == avg number of nodes that have each item
//total (t) = s*n*m + (U-x) * eps * p * A * K
// m = x * 1.44 * log_2(1/eps) = x * 1.44 * 1/ln(2) * ln (1/eps)
// dt/deps = s * n *x * 1.44 * 1/ln(2) * 1/(1/eps) * (-1) 1/eps^2 + (U-x) * p * A
// 0 = -1 * s * n * x * 1.44 / ln (2) * 1 / eps + (U-x) * p * A
// ( s* n *x * (1.44 / ln (2))) / ((U -x) * p* A) = eps
// eps = s * n * 1.44 / (U/x -1) * p * ln (2)
avg_nodes_per_item := (float64(numNodes) * float64(itemslocallist)) / float64(items)
size_adj := 0.8
eps := (float64(numTransfers) * float64(numNodes) * size_adj * 1.44) / (float64(penalty_bits) * avg_nodes_per_item * adjuster * math.Log(2) * (float64(items/n_est) - 1.0))
/*
effective_m := float64(n_est) * size_adj * 1.44 * (1.0 / math.Log(2)) * math.Log(1.0/eps)
sketch := (effective_m + (9.0 * 8.0)) * float64(numNodes) //9 bytes is the overhead
penalty := (float64(itemsPerNode) - float64(n_est)) * float64(penalty_bits) * float64(numNodes) * eps * adjuster
fmt.Println("Eps debug: eps", eps, "sketch per transfer ", sketch, "bits ", sketch/8, "bytes\n penalty", penalty, "bits", penalty/8, "bytes sum", sketch+penalty, "bits", (sketch+penalty)/8, "bytes")
actual_m := MakePowerOf2(EstimateMGcs(n_est, eps))
eps_test := float64(n_est) / float64(actual_m)
effective_m = float64(n_est) * size_adj * 1.44 * (1.0 / math.Log(2)) * math.Log(1.0/eps_test)
sketch = (effective_m + (9.0 * 8.0)) * float64(numNodes) //9 bytes is the overhead
penalty = (float64(itemsPerNode) - float64(n_est)) * float64(penalty_bits) * float64(numNodes) * eps_test * adjuster
fmt.Println("ipn", itemsPerNode, "n_est", n_est, "eps_test", eps_test)
penalty_items := (float64(itemsPerNode) - float64(n_est)) * eps_test
fmt.Println("Eps debug: m ", actual_m, effective_m, "eps", eps, "sketch per transfer", sketch, "bits ", sketch/8, "bytes\npenalty", penalty, "bits", penalty/8, " bytes. Items sent as fp ", penalty_items, "/node. Sum", sketch+penalty, "bits", (sketch+penalty)/8, "bytes")
*/
return eps
}
func EstimateMGcs(n int, eps float64) int {
if eps >= 1 {
return 0
}
// eps=(1-e^(kn/m))^k
//For k=1:
//eps=(1-e^(n/m))
//e^(-n/m) = 1-eps
//-n/m = ln (1-eps)
//m = -n/ln(1-eps)
//by the taylor series for eps in our range ln(1-eps) ~ -eps. so
//m ~ n/eps
m := int(float64(n) / eps)
return m
}
func NewGcs(m int) *Gcs {
//fmt.Println("Making GCS of size M=", m)
/*
m == 0 should be valid
if m < 1 {
panic(fmt.Sprintf("Wrong size %v", m))
}*/
if m == 0 {
panic("M cannot be 0")
}
if m > math.MaxUint32 {
panic("m bigger than uint32")
}
s := Gcs{
NewCountMinHash(1, m),
NewHashValueSlice(),
}
return &s
}
func (b *Gcs) CreateNew() *Gcs {
return NewGcs(b.Columns)
}
func (b *Gcs) GetInfo() string {
return fmt.Sprintf("Gcs modulus %v, hash values set %v", b.Columns, b.Data.Len())
}
func (b *Gcs) ByteSize() int {
return (b.Data.Len() * 4)
}
func (b *Gcs) Len() int {
return (b.Data.Len())
}
func (s *Gcs) AddString(key string) {
s.Add([]byte(key))
}
func (s *Gcs) AddInt(key int) {
tmp := make([]byte, 16)
binary.PutUvarint(tmp, uint64(key))
s.Add(tmp)
}
func (b *Gcs) Add(id []byte) {
if b.Columns == 0 {
return
}
index := b.GetIndexNoOffset(id, 0)
b.Data.Insert(index)
}
func (b *Gcs) HashValues() *HashValueSlice {
return b.Data
}
func (b *Gcs) GetM() uint {
return uint(b.Columns)
}
func (t *Gcs) SubtractGcs(other *Gcs) {
if other == nil {
panic("Other is nil")
}
if t.Columns != other.Columns {
panic(fmt.Sprintf("SNH", t.Columns, other.Columns))
}
other.Data.Eval(func(hv uint32) { t.Data.Remove(hv) })
}
func (s *Gcs) NumberHashes() int {
return s.Hashes
}
func (s *Gcs) QueryHashValues(hvs []uint32) bool {
if len(hvs) < s.NumberHashes() {
panic("wrong num idx")
}
cols := s.Columns
if cols == 0 {
return true
}
index := hvs[0] % uint32(cols)
return s.Data.Contains(index)
}
func (s *Gcs) QueryHashValue(hv uint32) bool {
if s.NumberHashes() != 1 {
panic("wrong num idx")
}
cols := s.Columns
if cols == 0 {
return true
}
index := hv % uint32(cols)
return s.Data.Contains(index)
}
func (s *Gcs) Query(key []byte) bool {
if s.Columns == 0 {
return true
}
index := s.GetIndexNoOffset(key, 0)
return s.Data.Contains(index)
}
func (s *Gcs) GetCompressedSizeExpensive() int {
buf, err := SerializeObject(s)
if err != nil {
panic(err)
}
return len(buf)
}
func (p *Gcs) Serialize(w io.Writer) error {
m := p.CountMinHash.Columns
if err := SerializeIntAsU32(w, &m); err != nil {
return err
}
array := make([]int, p.Data.Len())
for i, v := range p.Data.GetSlice() {
array[i] = int(v)
}
return GolumbEncodeWriter(w, array)
}
func (p *Gcs) Deserialize(r io.Reader) error {
m := int(0)
if err := DeserializeIntAsU32(r, &m); err != nil {
return err
}
p.CountMinHash = NewCountMinHash(1, m)
array, err := GolumbDecodeReader(r)
if err != nil {
return err
}
p.Data = NewHashValueSliceLen(len(array))
for i, _ := range array {
p.Data.Insert(uint32(array[i]))
}
return nil
}
/*
func orderGcsMerge(left, right *Gcs) *Gcs {
//fmt.Println("Merging, ", left.Columns, right.Columns)
// this merge merges larger into smaller, you can also imagine merging smaller into larger
if left.Columns == right.Columns {
left.Data.InsertAll(right.Data)
return left
} else {
if right.Columns < left.Columns || right.Columns%left.Columns != 0 {
panic("Gcs not mergeable")
}
right.Data.Eval(func(v uint32) { left.Data.Insert(v % uint32(right.Columns)) })
return left
}
}
func GcsMerge(left, right *Gcs) *Gcs {
lc := left.Columns
rc := right.Columns
if lc > rc {
return orderGcsMerge(right, left)
}
return orderGcsMerge(left, right)
}
// this can change either gcs or both
func (t *Gcs) Merge(tomerge *Gcs) *Gcs {
return GcsMerge(t, tomerge)
}
*/