blocks.go 1.78 KB
Newer Older
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
1 2 3
package blocks

import (
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
4 5 6 7
	"fmt"
	ds "github.com/jbenet/datastore.go"
	u "github.com/jbenet/go-ipfs/util"
	mh "github.com/jbenet/go-multihash"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
8 9
)

Juan Batiz-Benet's avatar
go lint  
Juan Batiz-Benet committed
10
// Block is the ipfs blocks service. It is the way
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
11
// to retrieve blocks by the higher level ipfs modules
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
12 13 14 15 16
type Block struct {
	Multihash mh.Multihash
	Data      []byte
}

Juan Batiz-Benet's avatar
go lint  
Juan Batiz-Benet committed
17
// NewBlock creates a Block object from opaque data. It will hash the data.
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
18 19 20 21 22 23 24 25
func NewBlock(data []byte) (*Block, error) {
	h, err := u.Hash(data)
	if err != nil {
		return nil, err
	}
	return &Block{Data: data, Multihash: h}, nil
}

Juan Batiz-Benet's avatar
go lint  
Juan Batiz-Benet committed
26
// Key returns the block's Multihash as a Key value.
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
27 28 29 30
func (b *Block) Key() u.Key {
	return u.Key(b.Multihash)
}

Juan Batiz-Benet's avatar
go lint  
Juan Batiz-Benet committed
31 32
// BlockService is a block datastore.
// It uses an internal `datastore.Datastore` instance to store values.
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
33
type BlockService struct {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
34 35 36 37
	Datastore ds.Datastore
	// Remote *bitswap.BitSwap // eventually.
}

Juan Batiz-Benet's avatar
go lint  
Juan Batiz-Benet committed
38
// NewBlockService creates a BlockService with given datastore instance.
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
39 40 41 42 43 44 45
func NewBlockService(d ds.Datastore) (*BlockService, error) {
	if d == nil {
		return nil, fmt.Errorf("BlockService requires valid datastore")
	}
	return &BlockService{Datastore: d}, nil
}

Juan Batiz-Benet's avatar
go lint  
Juan Batiz-Benet committed
46
// AddBlock adds a particular block to the service, Putting it into the datastore.
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
47 48 49 50
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)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
51 52
}

Juan Batiz-Benet's avatar
go lint  
Juan Batiz-Benet committed
53 54
// GetBlock retrieves a particular block from the service,
// Getting it from the datastore using the key (hash).
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
func (s *BlockService) GetBlock(k u.Key) (*Block, error) {
	dsk := ds.NewKey(string(k))
	datai, err := s.Datastore.Get(dsk)
	if err != nil {
		return nil, err
	}

	data, ok := datai.([]byte)
	if !ok {
		return nil, fmt.Errorf("data associated with %s is not a []byte", k)
	}

	return &Block{
		Multihash: mh.Multihash(k),
		Data:      data,
	}, nil
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
71
}