github.com/keltia/go-ipfs@v0.3.8-0.20150909044612-210793031c63/blocks/set/set.go (about) 1 // package set contains various different types of 'BlockSet's 2 package set 3 4 import ( 5 "github.com/ipfs/go-ipfs/blocks/bloom" 6 key "github.com/ipfs/go-ipfs/blocks/key" 7 "github.com/ipfs/go-ipfs/util" 8 ) 9 10 var log = util.Logger("blockset") 11 12 // BlockSet represents a mutable set of keyed blocks 13 type BlockSet interface { 14 AddBlock(key.Key) 15 RemoveBlock(key.Key) 16 HasKey(key.Key) bool 17 GetBloomFilter() bloom.Filter 18 19 GetKeys() []key.Key 20 } 21 22 func SimpleSetFromKeys(keys []key.Key) BlockSet { 23 sbs := &simpleBlockSet{blocks: make(map[key.Key]struct{})} 24 for _, k := range keys { 25 sbs.blocks[k] = struct{}{} 26 } 27 return sbs 28 } 29 30 func NewSimpleBlockSet() BlockSet { 31 return &simpleBlockSet{blocks: make(map[key.Key]struct{})} 32 } 33 34 type simpleBlockSet struct { 35 blocks map[key.Key]struct{} 36 } 37 38 func (b *simpleBlockSet) AddBlock(k key.Key) { 39 b.blocks[k] = struct{}{} 40 } 41 42 func (b *simpleBlockSet) RemoveBlock(k key.Key) { 43 delete(b.blocks, k) 44 } 45 46 func (b *simpleBlockSet) HasKey(k key.Key) bool { 47 _, has := b.blocks[k] 48 return has 49 } 50 51 func (b *simpleBlockSet) GetBloomFilter() bloom.Filter { 52 f := bloom.BasicFilter() 53 for k := range b.blocks { 54 f.Add([]byte(k)) 55 } 56 return f 57 } 58 59 func (b *simpleBlockSet) GetKeys() []key.Key { 60 var out []key.Key 61 for k := range b.blocks { 62 out = append(out, k) 63 } 64 return out 65 }