blockservice.go 2.76 KB
Newer Older
1 2 3
package blockservice

import (
4
	"errors"
5 6
	"fmt"

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/go-datastore"
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")
17
var ErrNotFound = errors.New("blockservice: key not found")
Jeromy's avatar
Jeromy committed
18

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()
40
	log.Debug("blockservice: storing [%s] in datastore", k)
41 42
	// TODO(brian): define a block datastore with a Put method which accepts a
	// block parameter
43 44 45 46 47

	// check if we have it before adding. this is an extra read, but large writes
	// are more expensive.
	// TODO(jbenet) cheaper has. https://github.com/jbenet/go-datastore/issues/6
	has, err := s.Datastore.Has(k.DsKey())
Jeromy's avatar
Jeromy committed
48 49 50
	if err != nil {
		return k, err
	}
51 52 53 54 55 56 57
	if !has {
		err := s.Datastore.Put(k.DsKey(), b.Data)
		if err != nil {
			return k, err
		}
	}

58
	if s.Remote != nil {
59 60
		ctx := context.TODO()
		err = s.Remote.HasBlock(ctx, *b)
61
	}
Jeromy's avatar
Jeromy committed
62
	return k, err
63 64 65 66
}

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

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