blockservice.go 2.37 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 16 17 18 19
	u "github.com/jbenet/go-ipfs/util"
)

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

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

// 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
38
	u.DOut("storing [%s] in datastore\n", k.Pretty())
39 40
	// TODO(brian): define a block datastore with a Put method which accepts a
	// block parameter
Jeromy's avatar
Jeromy committed
41 42 43 44
	err := s.Datastore.Put(dsk, b.Data)
	if err != nil {
		return k, err
	}
45
	if s.Remote != nil {
46
		err = s.Remote.HasBlock(*b)
47
	}
Jeromy's avatar
Jeromy committed
48
	return k, err
49 50 51 52 53
}

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