mirror of
https://github.com/ipfs/kubo.git
synced 2025-08-06 19:44:01 +08:00
66 lines
1.3 KiB
Go
66 lines
1.3 KiB
Go
// 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"
|
|
key "gx/ipfs/QmYEoKZXHoAToWfhGF3vryhMn3WWhE1o2MasQ8uzY5iDi9/go-key"
|
|
)
|
|
|
|
var log = logging.Logger("blockset")
|
|
|
|
// BlockSet represents a mutable set of keyed blocks
|
|
type BlockSet interface {
|
|
AddBlock(key.Key)
|
|
RemoveBlock(key.Key)
|
|
HasKey(key.Key) bool
|
|
GetBloomFilter() bloom.Filter
|
|
|
|
GetKeys() []key.Key
|
|
}
|
|
|
|
func SimpleSetFromKeys(keys []key.Key) BlockSet {
|
|
sbs := &simpleBlockSet{blocks: make(map[key.Key]struct{})}
|
|
for _, k := range keys {
|
|
sbs.blocks[k] = struct{}{}
|
|
}
|
|
return sbs
|
|
}
|
|
|
|
func NewSimpleBlockSet() BlockSet {
|
|
return &simpleBlockSet{blocks: make(map[key.Key]struct{})}
|
|
}
|
|
|
|
type simpleBlockSet struct {
|
|
blocks map[key.Key]struct{}
|
|
}
|
|
|
|
func (b *simpleBlockSet) AddBlock(k key.Key) {
|
|
b.blocks[k] = struct{}{}
|
|
}
|
|
|
|
func (b *simpleBlockSet) RemoveBlock(k key.Key) {
|
|
delete(b.blocks, k)
|
|
}
|
|
|
|
func (b *simpleBlockSet) HasKey(k key.Key) bool {
|
|
_, has := b.blocks[k]
|
|
return has
|
|
}
|
|
|
|
func (b *simpleBlockSet) GetBloomFilter() bloom.Filter {
|
|
f := bloom.BasicFilter()
|
|
for k := range b.blocks {
|
|
f.Add([]byte(k))
|
|
}
|
|
return f
|
|
}
|
|
|
|
func (b *simpleBlockSet) GetKeys() []key.Key {
|
|
var out []key.Key
|
|
for k := range b.blocks {
|
|
out = append(out, k)
|
|
}
|
|
return out
|
|
}
|