blockservice.go 2.42 KB
Newer Older
1 2 3 4 5
package blockservice

import (
	"fmt"

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

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

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

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

// NewBlockService creates a BlockService with given datastore instance.
25
func NewBlockService(d ds.Datastore, rem exchange.Interface) (*BlockService, error) {
26 27 28
	if d == nil {
		return nil, fmt.Errorf("BlockService requires valid datastore")
	}
Jeromy's avatar
Jeromy committed
29
	if rem == nil {
Jeromy's avatar
Jeromy committed
30
		log.Warning("blockservice running in local (offline) mode.")
Jeromy's avatar
Jeromy committed
31 32
	}
	return &BlockService{Datastore: d, Remote: rem}, nil
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()
38
	log.Debug("blockservice: storing [%s] in datastore", k)
39 40
	// TODO(brian): define a block datastore with a Put method which accepts a
	// block parameter
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
41
	err := s.Datastore.Put(k.DsKey(), b.Data)
Jeromy's avatar
Jeromy committed
42 43 44
	if err != nil {
		return k, err
	}
45
	if s.Remote != nil {
46 47
		ctx := context.TODO()
		err = s.Remote.HasBlock(ctx, *b)
48
	}
Jeromy's avatar
Jeromy committed
49
	return k, err
50 51 52 53
}

// GetBlock retrieves a particular block from the service,
// Getting it from the datastore using the key (hash).
Jeromy's avatar
Jeromy committed
54
func (s *BlockService) GetBlock(ctx context.Context, k u.Key) (*blocks.Block, error) {
55
	log.Debug("BlockService GetBlock: '%s'", k)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
56
	datai, err := s.Datastore.Get(k.DsKey())
Jeromy's avatar
Jeromy committed
57
	if err == nil {
Jeromy's avatar
Jeromy committed
58
		log.Debug("Blockservice: Got data in datastore.")
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 {
Jeromy's avatar
Jeromy committed
68
		log.Debug("Blockservice: Searching bitswap.")
69
		blk, err := s.Remote.Block(ctx, k)
Jeromy's avatar
Jeromy committed
70 71 72 73 74
		if err != nil {
			return nil, err
		}
		return blk, nil
	} else {
Jeromy's avatar
Jeromy committed
75
		log.Debug("Blockservice GetBlock: Not found.")
Jeromy's avatar
Jeromy committed
76
		return nil, u.ErrNotFound
77 78
	}
}
Jeromy's avatar
Jeromy committed
79 80 81 82

func (s *BlockService) DeleteBlock(k u.Key) error {
	return s.Datastore.Delete(k.DsKey())
}