offline.go 1.85 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
	"github.com/ipfs/go-ipfs/blocks/blockstore"
	exchange "github.com/ipfs/go-ipfs/exchange"
10
	blocks "gx/ipfs/QmbJUay5h1HtzhJb5QQk2t26yCnJksHynvhcqp18utBPqG/go-block-format"
Jeromy's avatar
Jeromy committed
11

12
	cid "gx/ipfs/QmNw61A6sJoXMeP37mJRtQZdNhj5e3FdjoTN3v4FyE96Gk/go-cid"
13 14
)

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

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

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

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

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

44
func (e *offlineExchange) GetBlocks(ctx context.Context, ks []*cid.Cid) (<-chan blocks.Block, error) {
45
	out := make(chan blocks.Block)
46 47
	go func() {
		defer close(out)
48
		var misses []*cid.Cid
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
		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
70
}
71 72 73 74

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