blockservice.go 3.51 KB
Newer Older
1 2 3
// package blockservice implements a BlockService interface that provides
// a single GetBlock/AddBlock interface that seamlessly retrieves data either
// locally or from a remote peer through the exchange.
4 5 6
package blockservice

import (
7
	"errors"
8 9
	"fmt"

10
	context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
11
	blocks "github.com/jbenet/go-ipfs/blocks"
12
	"github.com/jbenet/go-ipfs/blocks/blockstore"
13
	exchange "github.com/jbenet/go-ipfs/exchange"
14 15 16
	u "github.com/jbenet/go-ipfs/util"
)

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
17
var log = u.Logger("blockservice")
18
var ErrNotFound = errors.New("blockservice: key not found")
Jeromy's avatar
Jeromy committed
19

20 21
// BlockService is a hybrid block datastore. It stores data in a local
// datastore and may retrieve data from a remote Exchange.
22 23
// It uses an internal `datastore.Datastore` instance to store values.
type BlockService struct {
24 25
	// TODO don't expose underlying impl details
	Blockstore blockstore.Blockstore
26
	Exchange   exchange.Interface
27 28 29
}

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

// AddBlock adds a particular block to the service, Putting it into the datastore.
41
// TODO pass a context into this if the remote.HasBlock is going to remain here.
42 43
func (s *BlockService) AddBlock(b *blocks.Block) (u.Key, error) {
	k := b.Key()
Brian Tiger Chow's avatar
Brian Tiger Chow committed
44
	err := s.Blockstore.Put(b)
Jeromy's avatar
Jeromy committed
45 46 47
	if err != nil {
		return k, err
	}
48

49 50
	// TODO this operation rate-limits blockservice operations, we should
	// consider moving this to an sync process.
51
	if s.Exchange != nil {
52
		ctx := context.TODO()
53
		err = s.Exchange.HasBlock(ctx, b)
54
	}
Jeromy's avatar
Jeromy committed
55
	return k, err
56 57 58 59
}

// GetBlock retrieves a particular block from the service,
// Getting it from the datastore using the key (hash).
Jeromy's avatar
Jeromy committed
60
func (s *BlockService) GetBlock(ctx context.Context, k u.Key) (*blocks.Block, error) {
Jeromy's avatar
Jeromy committed
61
	log.Debugf("BlockService GetBlock: '%s'", k)
62
	block, err := s.Blockstore.Get(k)
Jeromy's avatar
Jeromy committed
63
	if err == nil {
64 65 66
		return block, nil
		// TODO be careful checking ErrNotFound. If the underlying
		// implementation changes, this will break.
67
	} else if err == blockstore.ErrNotFound && s.Exchange != nil {
Jeromy's avatar
Jeromy committed
68
		log.Debug("Blockservice: Searching bitswap.")
69
		blk, err := s.Exchange.GetBlock(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.")
76
		return nil, ErrNotFound
77 78
	}
}
Jeromy's avatar
Jeromy committed
79

80 81 82
// GetBlocks gets a list of blocks asynchronously and returns through
// the returned channel.
// NB: No guarantees are made about order.
83
func (s *BlockService) GetBlocks(ctx context.Context, ks []u.Key) <-chan *blocks.Block {
84
	out := make(chan *blocks.Block, 0)
85
	go func() {
86 87
		defer close(out)
		var misses []u.Key
88
		for _, k := range ks {
89
			hit, err := s.Blockstore.Get(k)
90
			if err != nil {
91
				misses = append(misses, k)
92
				continue
93
			}
94
			log.Debug("Blockservice: Got data in datastore.")
95 96 97 98 99
			select {
			case out <- hit:
			case <-ctx.Done():
				return
			}
100
		}
Jeromy's avatar
Jeromy committed
101

102
		rblocks, err := s.Exchange.GetBlocks(ctx, misses)
Jeromy's avatar
Jeromy committed
103 104 105 106
		if err != nil {
			log.Errorf("Error with GetBlocks: %s", err)
			return
		}
107

108 109 110 111 112 113
		for b := range rblocks {
			select {
			case out <- b:
			case <-ctx.Done():
				return
			}
Jeromy's avatar
Jeromy committed
114
		}
115 116
	}()
	return out
Jeromy's avatar
Jeromy committed
117 118
}

Jeromy's avatar
Jeromy committed
119
// DeleteBlock deletes a block in the blockservice from the datastore
Jeromy's avatar
Jeromy committed
120
func (s *BlockService) DeleteBlock(k u.Key) error {
121
	return s.Blockstore.DeleteBlock(k)
Jeromy's avatar
Jeromy committed
122
}