set.go 1.13 KB
Newer Older
1 2 3 4 5 6 7
package set

import (
	"github.com/jbenet/go-ipfs/blocks/bloom"
	"github.com/jbenet/go-ipfs/util"
)

8 9
var log = util.Logger("blockset")

10 11 12 13 14
type BlockSet interface {
	AddBlock(util.Key)
	RemoveBlock(util.Key)
	HasKey(util.Key) bool
	GetBloomFilter() bloom.Filter
15 16 17 18 19 20 21 22 23 24 25 26

	GetKeys() []util.Key
}

func SimpleSetFromKeys(keys []util.Key) BlockSet {
	sbs := &simpleBlockSet{blocks: make(map[util.Key]struct{})}
	for _, k := range keys {
		sbs.blocks[k] = struct{}{}
	}
	return sbs
}

27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
func NewSimpleBlockSet() BlockSet {
	return &simpleBlockSet{blocks: make(map[util.Key]struct{})}
}

type simpleBlockSet struct {
	blocks map[util.Key]struct{}
}

func (b *simpleBlockSet) AddBlock(k util.Key) {
	b.blocks[k] = struct{}{}
}

func (b *simpleBlockSet) RemoveBlock(k util.Key) {
	delete(b.blocks, k)
}

func (b *simpleBlockSet) HasKey(k util.Key) bool {
	_, has := b.blocks[k]
	return has
}

func (b *simpleBlockSet) GetBloomFilter() bloom.Filter {
	f := bloom.BasicFilter()
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
50
	for k := range b.blocks {
51 52 53 54
		f.Add([]byte(k))
	}
	return f
}
55 56 57

func (b *simpleBlockSet) GetKeys() []util.Key {
	var out []util.Key
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
58
	for k := range b.blocks {
59 60 61 62
		out = append(out, k)
	}
	return out
}