Commit cc91da6f authored by Kevin Atkinson's avatar Kevin Atkinson

Make blocks.Block an interface.

License: MIT
Signed-off-by: default avatarKevin Atkinson <k@kevina.org>
parent 0b8ac0fc
......@@ -30,9 +30,9 @@ var ErrNotFound = errors.New("blockstore: block not found")
type Blockstore interface {
DeleteBlock(key.Key) error
Has(key.Key) (bool, error)
Get(key.Key) (*blocks.Block, error)
Put(*blocks.Block) error
PutMany([]*blocks.Block) error
Get(key.Key) (blocks.Block, error)
Put(blocks.Block) error
PutMany([]blocks.Block) error
AllKeysChan(ctx context.Context) (<-chan key.Key, error)
}
......@@ -73,7 +73,7 @@ type blockstore struct {
gcreqlk sync.Mutex
}
func (bs *blockstore) Get(k key.Key) (*blocks.Block, error) {
func (bs *blockstore) Get(k key.Key) (blocks.Block, error) {
maybeData, err := bs.datastore.Get(k.DsKey())
if err == ds.ErrNotFound {
return nil, ErrNotFound
......@@ -89,7 +89,7 @@ func (bs *blockstore) Get(k key.Key) (*blocks.Block, error) {
return blocks.NewBlockWithHash(bdata, mh.Multihash(k))
}
func (bs *blockstore) Put(block *blocks.Block) error {
func (bs *blockstore) Put(block blocks.Block) error {
k := block.Key().DsKey()
// Has is cheaper than Put, so see if we already have it
......@@ -97,10 +97,10 @@ func (bs *blockstore) Put(block *blocks.Block) error {
if err == nil && exists {
return nil // already stored.
}
return bs.datastore.Put(k, block.Data)
return bs.datastore.Put(k, block.Data())
}
func (bs *blockstore) PutMany(blocks []*blocks.Block) error {
func (bs *blockstore) PutMany(blocks []blocks.Block) error {
t, err := bs.datastore.Batch()
if err != nil {
return err
......@@ -112,7 +112,7 @@ func (bs *blockstore) PutMany(blocks []*blocks.Block) error {
continue
}
err = t.Put(k, b.Data)
err = t.Put(k, b.Data())
if err != nil {
return err
}
......
......@@ -40,7 +40,7 @@ func TestPutThenGetBlock(t *testing.T) {
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(block.Data, blockFromBlockstore.Data) {
if !bytes.Equal(block.Data(), blockFromBlockstore.Data()) {
t.Fail()
}
}
......
......@@ -34,11 +34,11 @@ func (w *writecache) Has(k key.Key) (bool, error) {
return w.blockstore.Has(k)
}
func (w *writecache) Get(k key.Key) (*blocks.Block, error) {
func (w *writecache) Get(k key.Key) (blocks.Block, error) {
return w.blockstore.Get(k)
}
func (w *writecache) Put(b *blocks.Block) error {
func (w *writecache) Put(b blocks.Block) error {
k := b.Key()
if _, ok := w.cache.Get(k); ok {
return nil
......@@ -49,8 +49,8 @@ func (w *writecache) Put(b *blocks.Block) error {
return w.blockstore.Put(b)
}
func (w *writecache) PutMany(bs []*blocks.Block) error {
var good []*blocks.Block
func (w *writecache) PutMany(bs []blocks.Block) error {
var good []blocks.Block
for _, b := range bs {
if _, ok := w.cache.Get(b.Key()); !ok {
good = append(good, b)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment