-
Notifications
You must be signed in to change notification settings - Fork 23
/
bitmaps.go
456 lines (379 loc) · 8.72 KB
/
bitmaps.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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
package basalt
import (
"encoding/binary"
"fmt"
"io"
"sync"
"github.com/RoaringBitmap/roaring"
"github.com/smallnest/log"
)
// OP bitmaps operations
type OP byte
const (
BmOpAdd OP = 1
BmOpAddMany = 2
BmOpRemove = 3
BmOpDrop = 4
BmOpClear = 5
)
// Bitmaps contains all bitmaps of namespace.
type Bitmaps struct {
mu sync.RWMutex
bitmaps map[string]*Bitmap
writeCallback func(op OP, value string)
}
// NewBitmaps creates a Bitmaps.
func NewBitmaps() *Bitmaps {
return &Bitmaps{
bitmaps: make(map[string]*Bitmap),
}
}
// Bitmap is the goroutine-safe bitmap.
type Bitmap struct {
mu sync.RWMutex
bitmap *roaring.Bitmap
}
// Add adds a value.
func (bs *Bitmaps) Add(name string, v uint32, callback bool) {
if bs.writeCallback != nil && callback {
bs.writeCallback(BmOpAdd, fmt.Sprintf("%s,%d", name, v))
return
}
bs.mu.Lock()
bm := bs.bitmaps[name]
if bm == nil {
bm = &Bitmap{
bitmap: roaring.NewBitmap(),
}
bs.bitmaps[name] = bm
}
bs.mu.Unlock()
bm.mu.Lock()
bm.bitmap.Add(v)
bm.mu.Unlock()
}
// AddMany adds multiple values.
func (bs *Bitmaps) AddMany(name string, v []uint32, callback bool) {
if bs.writeCallback != nil && callback {
bs.writeCallback(BmOpAddMany, fmt.Sprintf("%s,%d", name, ints2str(v)))
return
}
bs.mu.Lock()
bm := bs.bitmaps[name]
if bm == nil {
bm = &Bitmap{
bitmap: roaring.NewBitmap(),
}
bs.bitmaps[name] = bm
}
bs.mu.Unlock()
bm.mu.Lock()
bm.bitmap.AddMany(v)
bm.mu.Unlock()
}
// Remove removes a value.
func (bs *Bitmaps) Remove(name string, v uint32, callback bool) {
if bs.writeCallback != nil && callback {
bs.writeCallback(BmOpRemove, fmt.Sprintf("%s,%d", name, v))
return
}
bs.mu.Lock()
bm := bs.bitmaps[name]
if bm == nil {
bm = &Bitmap{
bitmap: roaring.NewBitmap(),
}
bs.bitmaps[name] = bm
}
bs.mu.Unlock()
bm.mu.Lock()
bm.bitmap.Remove(v)
bm.mu.Unlock()
}
// RemoveBitmap removes a bitmap.
func (bs *Bitmaps) RemoveBitmap(name string, callback bool) {
if bs.writeCallback != nil && callback {
bs.writeCallback(BmOpDrop, name)
return
}
bs.mu.Lock()
delete(bs.bitmaps, name)
bs.mu.Unlock()
}
// ClearBitmap clear a bitmap.
func (bs *Bitmaps) ClearBitmap(name string, callback bool) {
if bs.writeCallback != nil && callback {
bs.writeCallback(BmOpClear, name)
return
}
bs.mu.RLock()
bm := bs.bitmaps[name]
if bm == nil {
bs.mu.RUnlock()
return
}
bs.mu.RUnlock()
bm.bitmap.Clear()
}
// Exists checks whether a value exists.
func (bs *Bitmaps) Exists(name string, v uint32) bool {
bs.mu.RLock()
bm := bs.bitmaps[name]
if bm == nil {
bs.mu.RUnlock()
return false
}
bs.mu.RUnlock()
bm.mu.RLock()
existed := bm.bitmap.Contains(v)
bm.mu.RUnlock()
return existed
}
// Card returns the number of integers contained in the bitmap.
func (bs *Bitmaps) Card(name string) uint64 {
bs.mu.RLock()
bm := bs.bitmaps[name]
if bm == nil {
bs.mu.RUnlock()
return 0
}
bs.mu.RUnlock()
bm.mu.RLock()
num := bm.bitmap.GetCardinality()
bm.mu.RUnlock()
return num
}
type Stats struct {
Cardinality uint64
Containers uint64
ArrayContainers uint64
ArrayContainerBytes uint64
ArrayContainerValues uint64
BitmapContainers uint64
BitmapContainerBytes uint64
BitmapContainerValues uint64
RunContainers uint64
RunContainerBytes uint64
RunContainerValues uint64
}
// Stats gets the stats of named bitmap.
func (bs *Bitmaps) Stats(name string) Stats {
bs.mu.RLock()
bm := bs.bitmaps[name]
if bm == nil {
bs.mu.RUnlock()
return Stats{}
}
bs.mu.RUnlock()
bm.mu.RLock()
stats := bm.bitmap.Stats()
bm.mu.RUnlock()
return Stats(stats)
}
func (bs *Bitmaps) intersection(names ...string) *roaring.Bitmap {
var bms []*roaring.Bitmap
bs.mu.RLock()
for _, name := range names {
bm := bs.bitmaps[name]
if bm == nil {
bs.mu.RUnlock()
return nil
}
bms = append(bms, bm.bitmap)
}
bs.mu.RUnlock()
return roaring.ParAnd(0, bms...)
}
// Inter computes the intersection (AND) of all provided bitmaps.
func (bs *Bitmaps) Inter(names ...string) []uint32 {
bm := bs.intersection(names...)
if bm == nil {
return nil
}
return bm.ToArray()
}
// InterStore computes the intersection (AND) of all provided bitmaps and save to destination.
func (bs *Bitmaps) InterStore(destination string, names ...string) uint64 {
bm := bs.intersection(names...)
if bm == nil {
return 0
}
bs.mu.Lock()
bs.bitmaps[destination] = &Bitmap{bitmap: bm}
bs.mu.Unlock()
return bm.GetCardinality()
}
func (bs *Bitmaps) union(names ...string) *roaring.Bitmap {
var bms []*roaring.Bitmap
bs.mu.RLock()
for _, name := range names {
bm := bs.bitmaps[name]
if bm != nil {
bms = append(bms, bm.bitmap)
}
}
bs.mu.RUnlock()
return roaring.ParHeapOr(0, bms...)
}
// Union computes the union (OR) of all provided bitmaps.
func (bs *Bitmaps) Union(names ...string) []uint32 {
bm := bs.union(names...)
return bm.ToArray()
}
// UnionStore computes the union (OR) of all provided bitmaps and store to destination.
func (bs *Bitmaps) UnionStore(destination string, names ...string) uint64 {
bm := bs.union(names...)
bs.mu.Lock()
bs.bitmaps[destination] = &Bitmap{bitmap: bm}
bs.mu.Unlock()
return bm.GetCardinality()
}
func (bs *Bitmaps) xor(name1, name2 string) *roaring.Bitmap {
var rbm1, rbm2 *roaring.Bitmap
bs.mu.RLock()
bm1 := bs.bitmaps[name1]
if bm1 == nil {
rbm1 = roaring.NewBitmap()
} else {
rbm1 = bm1.bitmap
}
bm2 := bs.bitmaps[name2]
if bm2 == nil {
rbm2 = roaring.NewBitmap()
} else {
rbm2 = bm2.bitmap
}
bs.mu.RUnlock()
return roaring.Xor(rbm1, rbm2)
}
// Xor computes the symmetric difference between two bitmaps and returns the result
func (bs *Bitmaps) Xor(name1, name2 string) []uint32 {
bm := bs.xor(name1, name2)
return bm.ToArray()
}
// XorStore computes the symmetric difference between two bitmaps and save the result to destination.
func (bs *Bitmaps) XorStore(destination, name1, name2 string) uint64 {
bm := bs.xor(name1, name2)
bs.mu.Lock()
bs.bitmaps[destination] = &Bitmap{bitmap: bm}
bs.mu.Unlock()
return bm.GetCardinality()
}
func (bs *Bitmaps) diff(name1, name2 string) *roaring.Bitmap {
var rbm1, rbm2 *roaring.Bitmap
bs.mu.RLock()
bm1 := bs.bitmaps[name1]
if bm1 == nil {
rbm1 = roaring.NewBitmap()
} else {
rbm1 = bm1.bitmap
}
bm2 := bs.bitmaps[name2]
if bm2 == nil {
rbm2 = roaring.NewBitmap()
} else {
rbm2 = bm2.bitmap
}
bs.mu.RUnlock()
return roaring.AndNot(rbm1, rbm2)
}
// Diff computes the difference between two bitmaps and returns the result.
func (bs *Bitmaps) Diff(name1, name2 string) []uint32 {
bm := bs.diff(name1, name2)
return bm.ToArray()
}
// DiffStore computes the difference between two bitmaps and save the result to destination.
func (bs *Bitmaps) DiffStore(destination, name1, name2 string) uint64 {
bm := bs.diff(name1, name2)
bs.mu.Lock()
bs.bitmaps[destination] = &Bitmap{bitmap: bm}
bs.mu.Unlock()
return bm.GetCardinality()
}
// Save saves bitmaps to the io.Writer.
func (bs *Bitmaps) Save(w io.Writer) error {
var keys []string
bs.mu.RLock()
for k := range bs.bitmaps {
keys = append(keys, k)
}
bs.mu.RUnlock()
for _, k := range keys {
bs.mu.RLock()
bm := bs.bitmaps[k]
bs.mu.RUnlock()
if bm != nil {
if err := bs.saveBitmap(w, k, bm.bitmap); err != nil {
return err
}
}
}
return nil
}
func (bs *Bitmaps) saveBitmap(w io.Writer, name string, bm *roaring.Bitmap) error {
if bm == nil {
return nil
}
err := binary.Write(w, binary.LittleEndian, uint32(len(name)))
if err != nil {
log.Errorf("failed to write len of name %s: %v", name, err)
return err
}
_, err = w.Write([]byte(name))
if err != nil {
log.Errorf("failed to write name %s: %v", name, err)
return err
}
bs.mu.RLock()
pBitmap := bm.Clone()
bs.mu.RUnlock()
_, err = pBitmap.WriteTo(w)
if err != nil {
log.Errorf("failed to write bitmap %s: %v", name, err)
return err
}
return nil
}
// Read restores bitmaps from a io.Reader.
func (bs *Bitmaps) Read(r io.Reader) error {
for {
name, bm, err := readBitmap(r)
if err == io.EOF {
return nil
}
if err != nil {
return err
}
b := &Bitmap{
bitmap: bm,
}
bs.mu.Lock()
bs.bitmaps[name] = b
bs.mu.Unlock()
}
}
func readBitmap(r io.Reader) (name string, bm *roaring.Bitmap, err error) {
var l uint32
err = binary.Read(r, binary.LittleEndian, &l)
if err != nil {
if err == io.EOF {
return "", nil, err
}
log.Errorf("failed to read len of name: %v", err)
return "", nil, err
}
var data = make([]byte, int(l))
_, err = io.ReadFull(r, data)
if err != nil {
log.Errorf("failed to read name: %v", err)
return "", nil, err
}
name = string(data)
bm = roaring.NewBitmap()
_, err = bm.ReadFrom(r)
if err != nil {
log.Errorf("failed to read name %s: %v", name, err)
return "", nil, err
}
return name, bm, nil
}