kubo/blockstore/blockstore.go
Juan Batiz-Benet 7c5679536c bugfix: use consistent interface
We'll want a `type blocks.Block interface {}` later, but
for now, make sure Blockstore uses ptrs for both Get and Put.

+ fix NewBlock output compile error
2014-10-07 21:32:17 -07:00

44 lines
894 B
Go

package blockstore
import (
"errors"
ds "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/datastore.go"
blocks "github.com/jbenet/go-ipfs/blocks"
u "github.com/jbenet/go-ipfs/util"
)
var ValueTypeMismatch = errors.New("The retrieved value is not a Block")
type Blockstore interface {
Get(u.Key) (*blocks.Block, error)
Put(*blocks.Block) error
}
func NewBlockstore(d ds.Datastore) Blockstore {
return &blockstore{
datastore: d,
}
}
type blockstore struct {
datastore ds.Datastore
}
func (bs *blockstore) Get(k u.Key) (*blocks.Block, error) {
maybeData, err := bs.datastore.Get(k.DsKey())
if err != nil {
return nil, err
}
bdata, ok := maybeData.([]byte)
if !ok {
return nil, ValueTypeMismatch
}
return blocks.NewBlock(bdata), nil
}
func (bs *blockstore) Put(block *blocks.Block) error {
return bs.datastore.Put(block.Key().DsKey(), block.Data)
}