1
0
mirror of https://github.com/ipfs/kubo.git synced 2025-09-09 19:32:24 +08:00
link errors left:
- protocol buffers output is not lint-friendly
This commit is contained in:
Juan Batiz-Benet
2014-07-21 08:05:27 -07:00
parent b5b2e8ae77
commit f718bd6a92
17 changed files with 134 additions and 37 deletions

View File

@ -7,14 +7,14 @@ import (
mh "github.com/jbenet/go-multihash"
)
// Blocks is the ipfs blocks service. It is the way
// Block is the ipfs blocks service. It is the way
// to retrieve blocks by the higher level ipfs modules
type Block struct {
Multihash mh.Multihash
Data []byte
}
// NewBlock creates a Block object from opaque data. It will hash the data.
func NewBlock(data []byte) (*Block, error) {
h, err := u.Hash(data)
if err != nil {
@ -23,15 +23,19 @@ func NewBlock(data []byte) (*Block, error) {
return &Block{Data: data, Multihash: h}, nil
}
// Key returns the block's Multihash as a Key value.
func (b *Block) Key() u.Key {
return u.Key(b.Multihash)
}
// BlockService is a block datastore.
// It uses an internal `datastore.Datastore` instance to store values.
type BlockService struct {
Datastore ds.Datastore
// Remote *bitswap.BitSwap // eventually.
}
// NewBlockService creates a BlockService with given datastore instance.
func NewBlockService(d ds.Datastore) (*BlockService, error) {
if d == nil {
return nil, fmt.Errorf("BlockService requires valid datastore")
@ -39,12 +43,15 @@ func NewBlockService(d ds.Datastore) (*BlockService, error) {
return &BlockService{Datastore: d}, nil
}
// AddBlock adds a particular block to the service, Putting it into the datastore.
func (s *BlockService) AddBlock(b *Block) (u.Key, error) {
k := b.Key()
dsk := ds.NewKey(string(k))
return k, s.Datastore.Put(dsk, b.Data)
}
// GetBlock retrieves a particular block from the service,
// Getting it from the datastore using the key (hash).
func (s *BlockService) GetBlock(k u.Key) (*Block, error) {
dsk := ds.NewKey(string(k))
datai, err := s.Datastore.Get(dsk)