-
Notifications
You must be signed in to change notification settings - Fork 0
/
set.go
60 lines (48 loc) · 1.22 KB
/
set.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
// package set contains various different types of 'BlockSet's
package set
import (
"github.com/ipfs/go-ipfs/blocks/bloom"
logging "gx/ipfs/QmSpJByNKFX1sCsHBEp3R73FL4NF6FnQTEGyNAXHm2GS52/go-log"
cid "gx/ipfs/QmV5gPoRsjN1Gid3LMdNZTyfCtP2DsvqEbMAmz82RmmiGk/go-cid"
)
var log = logging.Logger("blockset")
// BlockSet represents a mutable set of keyed blocks
type BlockSet interface {
AddBlock(*cid.Cid)
RemoveBlock(*cid.Cid)
HasKey(*cid.Cid) bool
GetBloomFilter() bloom.Filter
GetKeys() []*cid.Cid
}
func SimpleSetFromKeys(keys []*cid.Cid) BlockSet {
sbs := &simpleBlockSet{blocks: cid.NewSet()}
for _, k := range keys {
sbs.AddBlock(k)
}
return sbs
}
func NewSimpleBlockSet() BlockSet {
return &simpleBlockSet{blocks: cid.NewSet()}
}
type simpleBlockSet struct {
blocks *cid.Set
}
func (b *simpleBlockSet) AddBlock(k *cid.Cid) {
b.blocks.Add(k)
}
func (b *simpleBlockSet) RemoveBlock(k *cid.Cid) {
b.blocks.Remove(k)
}
func (b *simpleBlockSet) HasKey(k *cid.Cid) bool {
return b.blocks.Has(k)
}
func (b *simpleBlockSet) GetBloomFilter() bloom.Filter {
f := bloom.BasicFilter()
for _, k := range b.blocks.Keys() {
f.Add(k.Bytes())
}
return f
}
func (b *simpleBlockSet) GetKeys() []*cid.Cid {
return b.blocks.Keys()
}