-
Notifications
You must be signed in to change notification settings - Fork 0
/
bloom_cache.go
158 lines (133 loc) · 3.57 KB
/
bloom_cache.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
package blockstore
import (
"github.com/ipfs/go-ipfs/blocks"
key "github.com/ipfs/go-ipfs/blocks/key"
bloom "gx/ipfs/QmWQ2SJisXwcCLsUXLwYCKSfyExXjFRW2WbBH5sqCUnwX5/bbloom"
context "gx/ipfs/QmZy2y8t9zQH2a1b8q2ZSLKp17ATuJoCNxxyMFG5qFExpt/go-net/context"
"sync/atomic"
)
// bloomCached returns Blockstore that caches Has requests using Bloom filter
// Size is size of bloom filter in bytes
func bloomCached(bs Blockstore, ctx context.Context, bloomSize, hashCount int) (*bloomcache, error) {
bl, err := bloom.New(float64(bloomSize), float64(hashCount))
if err != nil {
return nil, err
}
bc := &bloomcache{blockstore: bs, bloom: bl}
bc.Invalidate()
go bc.Rebuild(ctx)
return bc, nil
}
type bloomcache struct {
bloom *bloom.Bloom
active int32
// This chan is only used for testing to wait for bloom to enable
rebuildChan chan struct{}
blockstore Blockstore
// Statistics
hits uint64
misses uint64
}
func (b *bloomcache) Invalidate() {
b.rebuildChan = make(chan struct{})
atomic.StoreInt32(&b.active, 0)
}
func (b *bloomcache) BloomActive() bool {
return atomic.LoadInt32(&b.active) != 0
}
func (b *bloomcache) Rebuild(ctx context.Context) {
evt := log.EventBegin(ctx, "bloomcache.Rebuild")
defer evt.Done()
ch, err := b.blockstore.AllKeysChan(ctx)
if err != nil {
log.Errorf("AllKeysChan failed in bloomcache rebuild with: %v", err)
return
}
finish := false
for !finish {
select {
case key, ok := <-ch:
if ok {
b.bloom.AddTS([]byte(key)) // Use binary key, the more compact the better
} else {
finish = true
}
case <-ctx.Done():
log.Warning("Cache rebuild closed by context finishing.")
return
}
}
close(b.rebuildChan)
atomic.StoreInt32(&b.active, 1)
}
func (b *bloomcache) DeleteBlock(k key.Key) error {
if has, ok := b.hasCached(k); ok && !has {
return ErrNotFound
}
return b.blockstore.DeleteBlock(k)
}
// if ok == false has is inconclusive
// if ok == true then has respons to question: is it contained
func (b *bloomcache) hasCached(k key.Key) (has bool, ok bool) {
if k == "" {
// Return cache invalid so call to blockstore
// in case of invalid key is forwarded deeper
return false, false
}
if b.BloomActive() {
blr := b.bloom.HasTS([]byte(k))
if blr == false { // not contained in bloom is only conclusive answer bloom gives
return false, true
}
}
return false, false
}
func (b *bloomcache) Has(k key.Key) (bool, error) {
if has, ok := b.hasCached(k); ok {
return has, nil
}
return b.blockstore.Has(k)
}
func (b *bloomcache) Get(k key.Key) (blocks.Block, error) {
if has, ok := b.hasCached(k); ok && !has {
return nil, ErrNotFound
}
return b.blockstore.Get(k)
}
func (b *bloomcache) Put(bl blocks.Block) error {
if has, ok := b.hasCached(bl.Key()); ok && has {
return nil
}
err := b.blockstore.Put(bl)
if err == nil {
b.bloom.AddTS([]byte(bl.Key()))
}
return err
}
func (b *bloomcache) PutMany(bs []blocks.Block) error {
var good []blocks.Block
for _, block := range bs {
if has, ok := b.hasCached(block.Key()); !ok || (ok && !has) {
good = append(good, block)
}
}
err := b.blockstore.PutMany(bs)
if err == nil {
for _, block := range bs {
b.bloom.AddTS([]byte(block.Key()))
}
}
return err
}
func (b *bloomcache) AllKeysChan(ctx context.Context) (<-chan key.Key, error) {
return b.blockstore.AllKeysChan(ctx)
}
func (b *bloomcache) GCLock() Unlocker {
return b.blockstore.(GCBlockstore).GCLock()
}
func (b *bloomcache) PinLock() Unlocker {
return b.blockstore.(GCBlockstore).PinLock()
}
func (b *bloomcache) GCRequested() bool {
return b.blockstore.(GCBlockstore).GCRequested()
}