blockservice.go 2.47 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
	mh "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multihash"
Jeromy's avatar
Jeromy committed
10
	"github.com/op/go-logging"
11

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

Jeromy's avatar
Jeromy committed
17 18
var log = logging.MustGetLogger("blockservice")

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

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

// 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()
	dsk := ds.NewKey(string(k))
Jeromy's avatar
Jeromy committed
41
	log.Debug("storing [%s] in datastore", k.Pretty())
42 43
	// TODO(brian): define a block datastore with a Put method which accepts a
	// block parameter
Jeromy's avatar
Jeromy committed
44 45 46 47
	err := s.Datastore.Put(dsk, b.Data)
	if err != nil {
		return k, err
	}
48
	if s.Remote != nil {
49 50
		ctx := context.TODO()
		err = s.Remote.HasBlock(ctx, *b)
51
	}
Jeromy's avatar
Jeromy committed
52
	return k, err
53 54 55 56 57
}

// 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
58
	log.Debug("BlockService GetBlock: '%s'", k.Pretty())
59 60
	dsk := ds.NewKey(string(k))
	datai, err := s.Datastore.Get(dsk)
Jeromy's avatar
Jeromy committed
61
	if err == nil {
Jeromy's avatar
Jeromy committed
62
		log.Debug("Blockservice: Got data in datastore.")
Jeromy's avatar
Jeromy committed
63 64 65 66 67 68 69 70
		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
71
	} else if err == ds.ErrNotFound && s.Remote != nil {
Jeromy's avatar
Jeromy committed
72
		log.Debug("Blockservice: Searching bitswap.")
73 74
		ctx, _ := context.WithTimeout(context.TODO(), 5*time.Second)
		blk, err := s.Remote.Block(ctx, k)
Jeromy's avatar
Jeromy committed
75 76 77 78 79
		if err != nil {
			return nil, err
		}
		return blk, nil
	} else {
Jeromy's avatar
Jeromy committed
80
		log.Debug("Blockservice GetBlock: Not found.")
Jeromy's avatar
Jeromy committed
81
		return nil, u.ErrNotFound
82 83
	}
}