blockservice.go 3.97 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 10 11
	context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
	blocks "github.com/ipfs/go-ipfs/blocks"
	"github.com/ipfs/go-ipfs/blocks/blockstore"
12
	key "github.com/ipfs/go-ipfs/blocks/key"
13
	exchange "github.com/ipfs/go-ipfs/exchange"
Jeromy's avatar
Jeromy committed
14
	logging "github.com/ipfs/go-ipfs/vendor/go-log-v1.0.0"
15 16
)

Jeromy's avatar
Jeromy committed
17
var log = logging.Logger("blockservice")
18

19
var ErrNotFound = errors.New("blockservice: key not found")
Jeromy's avatar
Jeromy committed
20

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

// NewBlockService creates a BlockService with given datastore instance.
31
func New(bs blockstore.Blockstore, rem exchange.Interface) *BlockService {
Jeromy's avatar
Jeromy committed
32
	if rem == nil {
Jeromy's avatar
Jeromy committed
33
		log.Warning("blockservice running in local (offline) mode.")
Jeromy's avatar
Jeromy committed
34
	}
35 36

	return &BlockService{
37 38
		Blockstore: bs,
		Exchange:   rem,
39
	}
40 41 42
}

// AddBlock adds a particular block to the service, Putting it into the datastore.
43
// TODO pass a context into this if the remote.HasBlock is going to remain here.
44
func (s *BlockService) AddBlock(b *blocks.Block) (key.Key, error) {
45
	k := b.Key()
Brian Tiger Chow's avatar
Brian Tiger Chow committed
46
	err := s.Blockstore.Put(b)
Jeromy's avatar
Jeromy committed
47 48 49
	if err != nil {
		return k, err
	}
50
	if err := s.Exchange.HasBlock(context.TODO(), b); err != nil {
51
		return "", errors.New("blockservice is closed")
52
	}
53
	return k, nil
54 55
}

56 57 58 59 60 61 62 63
func (s *BlockService) AddBlocks(bs []*blocks.Block) ([]key.Key, error) {
	err := s.Blockstore.PutMany(bs)
	if err != nil {
		return nil, err
	}

	var ks []key.Key
	for _, b := range bs {
64
		if err := s.Exchange.HasBlock(context.TODO(), b); err != nil {
65 66 67 68 69 70 71
			return nil, errors.New("blockservice is closed")
		}
		ks = append(ks, b.Key())
	}
	return ks, nil
}

72 73
// GetBlock retrieves a particular block from the service,
// Getting it from the datastore using the key (hash).
74
func (s *BlockService) GetBlock(ctx context.Context, k key.Key) (*blocks.Block, error) {
Jeromy's avatar
Jeromy committed
75
	log.Debugf("BlockService GetBlock: '%s'", k)
76
	block, err := s.Blockstore.Get(k)
Jeromy's avatar
Jeromy committed
77
	if err == nil {
78
		return block, nil
Jeromy's avatar
Jeromy committed
79 80 81
	}

	if err == blockstore.ErrNotFound && s.Exchange != nil {
82 83
		// TODO be careful checking ErrNotFound. If the underlying
		// implementation changes, this will break.
Jeromy's avatar
Jeromy committed
84
		log.Debug("Blockservice: Searching bitswap.")
85
		blk, err := s.Exchange.GetBlock(ctx, k)
Jeromy's avatar
Jeromy committed
86
		if err != nil {
87 88 89
			if err == blockstore.ErrNotFound {
				return nil, ErrNotFound
			}
Jeromy's avatar
Jeromy committed
90 91 92
			return nil, err
		}
		return blk, nil
Jeromy's avatar
Jeromy committed
93 94 95 96
	}

	log.Debug("Blockservice GetBlock: Not found.")
	if err == blockstore.ErrNotFound {
97
		return nil, ErrNotFound
98
	}
Jeromy's avatar
Jeromy committed
99 100

	return nil, err
101
}
Jeromy's avatar
Jeromy committed
102

103 104 105
// GetBlocks gets a list of blocks asynchronously and returns through
// the returned channel.
// NB: No guarantees are made about order.
106
func (s *BlockService) GetBlocks(ctx context.Context, ks []key.Key) <-chan *blocks.Block {
107
	out := make(chan *blocks.Block, 0)
108
	go func() {
109
		defer close(out)
110
		var misses []key.Key
111
		for _, k := range ks {
112
			hit, err := s.Blockstore.Get(k)
113
			if err != nil {
114
				misses = append(misses, k)
115
				continue
116
			}
117
			log.Debug("Blockservice: Got data in datastore.")
118 119 120 121 122
			select {
			case out <- hit:
			case <-ctx.Done():
				return
			}
123
		}
Jeromy's avatar
Jeromy committed
124

125
		rblocks, err := s.Exchange.GetBlocks(ctx, misses)
Jeromy's avatar
Jeromy committed
126
		if err != nil {
127
			log.Debugf("Error with GetBlocks: %s", err)
Jeromy's avatar
Jeromy committed
128 129
			return
		}
130

131 132 133 134 135 136
		for b := range rblocks {
			select {
			case out <- b:
			case <-ctx.Done():
				return
			}
Jeromy's avatar
Jeromy committed
137
		}
138 139
	}()
	return out
Jeromy's avatar
Jeromy committed
140 141
}

Jeromy's avatar
Jeromy committed
142
// DeleteBlock deletes a block in the blockservice from the datastore
143
func (s *BlockService) DeleteBlock(k key.Key) error {
144
	return s.Blockstore.DeleteBlock(k)
Jeromy's avatar
Jeromy committed
145
}
146 147 148

func (s *BlockService) Close() error {
	log.Debug("blockservice is shutting down...")
149
	return s.Exchange.Close()
150
}