dbset.go 956 Bytes
Newer Older
1 2 3
package set

import (
Jeromy's avatar
Jeromy committed
4
	ds "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore"
5 6 7 8 9 10 11 12 13
	"github.com/jbenet/go-ipfs/blocks/bloom"
	"github.com/jbenet/go-ipfs/util"
)

type datastoreBlockSet struct {
	dstore ds.Datastore
	bset   BlockSet
}

14
func NewDBWrapperSet(d ds.Datastore, bset BlockSet) BlockSet {
15 16 17 18 19 20 21
	return &datastoreBlockSet{
		dstore: d,
		bset:   bset,
	}
}

func (d *datastoreBlockSet) AddBlock(k util.Key) {
22
	err := d.dstore.Put(k.DsKey(), []byte{})
23 24 25 26 27 28 29 30 31 32
	if err != nil {
		log.Error("blockset put error: %s", err)
	}

	d.bset.AddBlock(k)
}

func (d *datastoreBlockSet) RemoveBlock(k util.Key) {
	d.bset.RemoveBlock(k)
	if !d.bset.HasKey(k) {
33
		d.dstore.Delete(k.DsKey())
34 35 36 37 38 39 40 41 42 43 44
	}
}

func (d *datastoreBlockSet) HasKey(k util.Key) bool {
	return d.bset.HasKey(k)
}

func (d *datastoreBlockSet) GetBloomFilter() bloom.Filter {
	return d.bset.GetBloomFilter()
}

45 46
func (d *datastoreBlockSet) GetKeys() []util.Key {
	return d.bset.GetKeys()
47
}