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