blockservice.go 2.4 KB
Newer Older
1 2 3 4
package blockservice

import (
	"fmt"
Jeromy's avatar
Jeromy committed
5
	"time"
6

7
	context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
8
	ds "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/datastore.go"
9 10
	mh "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multihash"

11
	blocks "github.com/jbenet/go-ipfs/blocks"
12
	exchange "github.com/jbenet/go-ipfs/exchange"
13 14 15
	u "github.com/jbenet/go-ipfs/util"
)

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
16
var log = u.Logger("blockservice")
Jeromy's avatar
Jeromy committed
17

18 19 20 21
// BlockService is a block datastore.
// It uses an internal `datastore.Datastore` instance to store values.
type BlockService struct {
	Datastore ds.Datastore
22
	Remote    exchange.Interface
23 24 25
}

// NewBlockService creates a BlockService with given datastore instance.
26
func NewBlockService(d ds.Datastore, rem exchange.Interface) (*BlockService, error) {
27 28 29
	if d == nil {
		return nil, fmt.Errorf("BlockService requires valid datastore")
	}
Jeromy's avatar
Jeromy committed
30
	if rem == nil {
Jeromy's avatar
Jeromy committed
31
		log.Warning("blockservice running in local (offline) mode.")
Jeromy's avatar
Jeromy committed
32 33
	}
	return &BlockService{Datastore: d, Remote: rem}, nil
34 35 36 37 38
}

// AddBlock adds a particular block to the service, Putting it into the datastore.
func (s *BlockService) AddBlock(b *blocks.Block) (u.Key, error) {
	k := b.Key()
Jeromy's avatar
Jeromy committed
39
	log.Debug("blockservice: storing [%s] in datastore", k.Pretty())
40 41
	// TODO(brian): define a block datastore with a Put method which accepts a
	// block parameter
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
42
	err := s.Datastore.Put(k.DsKey(), b.Data)
Jeromy's avatar
Jeromy committed
43 44 45
	if err != nil {
		return k, err
	}
46
	if s.Remote != nil {
47 48
		ctx := context.TODO()
		err = s.Remote.HasBlock(ctx, *b)
49
	}
Jeromy's avatar
Jeromy committed
50
	return k, err
51 52 53 54 55
}

// GetBlock retrieves a particular block from the service,
// Getting it from the datastore using the key (hash).
func (s *BlockService) GetBlock(k u.Key) (*blocks.Block, error) {
Jeromy's avatar
Jeromy committed
56
	log.Debug("BlockService GetBlock: '%s'", k.Pretty())
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
57
	datai, err := s.Datastore.Get(k.DsKey())
Jeromy's avatar
Jeromy committed
58
	if err == nil {
Jeromy's avatar
Jeromy committed
59
		log.Debug("Blockservice: Got data in datastore.")
Jeromy's avatar
Jeromy committed
60 61 62 63 64 65 66 67
		bdata, ok := datai.([]byte)
		if !ok {
			return nil, fmt.Errorf("data associated with %s is not a []byte", k)
		}
		return &blocks.Block{
			Multihash: mh.Multihash(k),
			Data:      bdata,
		}, nil
68
	} else if err == ds.ErrNotFound && s.Remote != nil {
Jeromy's avatar
Jeromy committed
69
		log.Debug("Blockservice: Searching bitswap.")
70 71
		ctx, _ := context.WithTimeout(context.TODO(), 5*time.Second)
		blk, err := s.Remote.Block(ctx, k)
Jeromy's avatar
Jeromy committed
72 73 74 75 76
		if err != nil {
			return nil, err
		}
		return blk, nil
	} else {
Jeromy's avatar
Jeromy committed
77
		log.Debug("Blockservice GetBlock: Not found.")
Jeromy's avatar
Jeromy committed
78
		return nil, u.ErrNotFound
79 80
	}
}