blockservice.go 2.48 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"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
10
	logging "github.com/jbenet/go-ipfs/Godeps/_workspace/src/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
}

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

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