offline.go 1.79 KB
Newer Older
Jeromy's avatar
Jeromy committed
1 2 3
// package offline implements an object that implements the exchange
// interface but returns nil values to every request.
package offline
4 5

import (
6 7
	"context"

8 9 10 11
	blocks "github.com/ipfs/go-block-format"
	cid "github.com/ipfs/go-cid"
	blockstore "github.com/ipfs/go-ipfs-blockstore"
	exchange "github.com/ipfs/go-ipfs-exchange-interface"
12 13
)

14 15
func Exchange(bs blockstore.Blockstore) exchange.Interface {
	return &offlineExchange{bs: bs}
16 17 18 19
}

// offlineExchange implements the Exchange interface but doesn't return blocks.
// For use in offline mode.
20 21 22
type offlineExchange struct {
	bs blockstore.Blockstore
}
23

Jeromy's avatar
Jeromy committed
24
// GetBlock returns nil to signal that a block could not be retrieved for the
25 26
// given key.
// NB: This function may return before the timeout expires.
27
func (e *offlineExchange) GetBlock(_ context.Context, k cid.Cid) (blocks.Block, error) {
28
	return e.bs.Get(k)
29 30 31
}

// HasBlock always returns nil.
32
func (e *offlineExchange) HasBlock(b blocks.Block) error {
33
	return e.bs.Put(b)
34
}
35 36 37

// Close always returns nil.
func (_ *offlineExchange) Close() error {
38 39
	// NB: exchange doesn't own the blockstore's underlying datastore, so it is
	// not responsible for closing it.
40 41
	return nil
}
Jeromy's avatar
Jeromy committed
42

43
func (e *offlineExchange) GetBlocks(ctx context.Context, ks []cid.Cid) (<-chan blocks.Block, error) {
44
	out := make(chan blocks.Block)
45 46
	go func() {
		defer close(out)
47
		var misses []cid.Cid
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
		for _, k := range ks {
			hit, err := e.bs.Get(k)
			if err != nil {
				misses = append(misses, k)
				// a long line of misses should abort when context is cancelled.
				select {
				// TODO case send misses down channel
				case <-ctx.Done():
					return
				default:
					continue
				}
			}
			select {
			case out <- hit:
			case <-ctx.Done():
				return
			}
		}
	}()
	return out, nil
Jeromy's avatar
Jeromy committed
69
}
70 71 72 73

func (e *offlineExchange) IsOnline() bool {
	return false
}