blockservice.go 3.7 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 54 55 56 57
		if err := s.Exchange.HasBlock(ctx, b); err != nil {
			// suppress error, as the client shouldn't care about bitswap.
			// the client only cares about the blockstore.Put.
			log.Errorf("Exchange.HasBlock error: %s", err)
		}
58
	}
59
	return k, nil
60 61 62 63
}

// GetBlock retrieves a particular block from the service,
// Getting it from the datastore using the key (hash).
Jeromy's avatar
Jeromy committed
64
func (s *BlockService) GetBlock(ctx context.Context, k u.Key) (*blocks.Block, error) {
Jeromy's avatar
Jeromy committed
65
	log.Debugf("BlockService GetBlock: '%s'", k)
66
	block, err := s.Blockstore.Get(k)
Jeromy's avatar
Jeromy committed
67
	if err == nil {
68 69 70
		return block, nil
		// TODO be careful checking ErrNotFound. If the underlying
		// implementation changes, this will break.
71
	} else if err == blockstore.ErrNotFound && s.Exchange != nil {
Jeromy's avatar
Jeromy committed
72
		log.Debug("Blockservice: Searching bitswap.")
73
		blk, err := s.Exchange.GetBlock(ctx, k)
Jeromy's avatar
Jeromy committed
74 75 76 77 78
		if err != nil {
			return nil, err
		}
		return blk, nil
	} else {
Jeromy's avatar
Jeromy committed
79
		log.Debug("Blockservice GetBlock: Not found.")
80
		return nil, ErrNotFound
81 82
	}
}
Jeromy's avatar
Jeromy committed
83

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

106
		rblocks, err := s.Exchange.GetBlocks(ctx, misses)
Jeromy's avatar
Jeromy committed
107 108 109 110
		if err != nil {
			log.Errorf("Error with GetBlocks: %s", err)
			return
		}
111

112 113 114 115 116 117
		for b := range rblocks {
			select {
			case out <- b:
			case <-ctx.Done():
				return
			}
Jeromy's avatar
Jeromy committed
118
		}
119 120
	}()
	return out
Jeromy's avatar
Jeromy committed
121 122
}

Jeromy's avatar
Jeromy committed
123
// DeleteBlock deletes a block in the blockservice from the datastore
Jeromy's avatar
Jeromy committed
124
func (s *BlockService) DeleteBlock(k u.Key) error {
125
	return s.Blockstore.DeleteBlock(k)
Jeromy's avatar
Jeromy committed
126
}