forked from ETHFSx/go-ipfs-exchange-offline
-
Notifications
You must be signed in to change notification settings - Fork 0
/
offline.go
78 lines (68 loc) · 1.94 KB
/
offline.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// package offline implements an object that implements the exchange
// interface but returns nil values to every request.
package offline
import (
"context"
blocks "github.com/ipfs/go-block-format"
cid "github.com/ipfs/go-cid"
blockstore "github.com/ipfs/go-ipfs-blockstore"
exchange "github.com/IPFS-eX/go-ipfs-exchange-interface"
"github.com/libp2p/go-libp2p-core/peer"
)
func Exchange(bs blockstore.Blockstore) exchange.Interface {
return &offlineExchange{bs: bs}
}
// offlineExchange implements the Exchange interface but doesn't return blocks.
// For use in offline mode.
type offlineExchange struct {
bs blockstore.Blockstore
}
func (_ *offlineExchange) Push(ctx context.Context, num uint32, peer peer.ID, c cid.Cid) error {
return nil
}
// GetBlock returns nil to signal that a block could not be retrieved for the
// given key.
// NB: This function may return before the timeout expires.
func (e *offlineExchange) GetBlock(_ context.Context, k cid.Cid) (blocks.Block, error) {
return e.bs.Get(k)
}
// HasBlock always returns nil.
func (e *offlineExchange) HasBlock(b blocks.Block) error {
return e.bs.Put(b)
}
// Close always returns nil.
func (_ *offlineExchange) Close() error {
// NB: exchange doesn't own the blockstore's underlying datastore, so it is
// not responsible for closing it.
return nil
}
func (e *offlineExchange) GetBlocks(ctx context.Context, ks []cid.Cid) (<-chan blocks.Block, error) {
out := make(chan blocks.Block)
go func() {
defer close(out)
var misses []cid.Cid
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
}
func (e *offlineExchange) IsOnline() bool {
return false
}