-
Notifications
You must be signed in to change notification settings - Fork 101
/
Copy pathart_with_bloom.go
70 lines (61 loc) · 1.78 KB
/
art_with_bloom.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
package index
import (
"sync"
"github.com/ByteStorage/FlyDB/db/data"
"github.com/ByteStorage/FlyDB/lib/bloom"
art "github.com/Clement-Jean/go-art"
)
// AdaptiveRadixTreeWithBloom Adaptive Radix Tree Index
// The following link is the ART library written by go.
// If you need to know more about it, please go to the corresponding warehouse.
// https://github.com/Clement-Jean/go-art
type AdaptiveRadixTreeWithBloom struct {
tree art.Tree[[]byte, *data.LogRecordPst]
lock *sync.RWMutex
filter *bloom.Filter
}
// NewARTWithBloom Initializes the adaptive radix tree index
func NewARTWithBloom() *AdaptiveRadixTreeWithBloom {
return &AdaptiveRadixTreeWithBloom{
tree: art.NewAlphaSortedTree[[]byte, *data.LogRecordPst](),
lock: new(sync.RWMutex),
filter: bloom.NewBloomFilter(1000, 0.01),
}
}
func (artree *AdaptiveRadixTreeWithBloom) Put(key []byte, pst *data.LogRecordPst) bool {
artree.lock.Lock()
defer artree.lock.Unlock()
artree.tree.Insert(key, pst)
artree.filter.Add(key)
return true
}
func (artree *AdaptiveRadixTreeWithBloom) Get(key []byte) *data.LogRecordPst {
if !artree.filter.MayContainItem(key) {
return nil
}
artree.lock.RLock()
defer artree.lock.RUnlock()
value, found := artree.tree.Search(key)
if !found {
return nil
}
return value
}
func (artree *AdaptiveRadixTreeWithBloom) Delete(key []byte) bool {
if !artree.filter.MayContainItem(key) {
return false
}
artree.lock.Lock()
defer artree.lock.Unlock()
return artree.tree.Delete(key)
}
func (artree *AdaptiveRadixTreeWithBloom) Size() int {
artree.lock.RLock()
defer artree.lock.RUnlock()
return artree.tree.Size()
}
func (artree *AdaptiveRadixTreeWithBloom) Iterator(reverse bool) Iterator {
artree.lock.RLock()
defer artree.lock.RUnlock()
return NewARTreeIterator(artree.tree, reverse)
}