-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathw3rc.go
84 lines (71 loc) · 2.5 KB
/
w3rc.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
79
80
81
82
83
84
package w3rc
import (
"context"
"github.com/ipfs-shipyard/w3rc/contentrouting/delegated"
"github.com/ipfs-shipyard/w3rc/exchange"
"github.com/ipfs-shipyard/w3rc/exchange/bitswap"
"github.com/ipfs-shipyard/w3rc/exchange/filecoinretrieval"
"github.com/ipfs-shipyard/w3rc/planning"
"github.com/ipfs/go-cid"
logging "github.com/ipfs/go-log/v2"
"github.com/ipld/go-ipld-prime"
"github.com/ipld/go-ipld-prime/datamodel"
)
var log = logging.Logger("w3rc")
// NewSession creates a Session with given configuration.
// A session represents a set of related queries for content addressed data.
// Connections to peers may stay open for the life of a session.
func NewSession(ls ipld.LinkSystem, opts ...Option) (Session, error) {
conf := config{}
if err := apply(&conf, opts...); err != nil {
return nil, err
}
if err := applyDefaults(ls, &conf); err != nil {
return nil, err
}
router, err := delegated.NewDelegatedHTTP(conf.indexerURL)
if err != nil {
return nil, err
}
session := simpleSession{
ls: ls,
router: router,
mux: exchange.DefaultMux(),
scheduler: planning.NewSimpleScheduler(),
}
dt := conf.dt
session.mux.Register(filecoinretrieval.NewFilecoinExchange(nil, conf.host, dt))
session.mux.Register(bitswap.NewBitswaExchange(conf.host, &ls))
return &session, nil
}
// ResultChan provides progress updates from a call to `GetStream`
type ResultChan chan ProgressResult
// A ProgressResult is an individual update from a call to `GetStream`
// The result will either have a status of `Error` and an Error set,
// or will have a node and path set.
// The ResultChan a result is sent down will close after an error result
// or 'complete' result is sent.
type ProgressResult struct {
Status
Error error
ipld.Path
ipld.Node
}
// Status is a code of the type of an individual Progress Result
type Status uint8
// These are valid status codes
const (
ERROR Status = iota
INPROGRESS
COMPLETE
)
// A Session is able to fetch content addressed data.
type Session interface {
// Get returns a dag rooted at root. If selector is `nil`, the single block
// of the root will be assumed. If the full dag under root is desired, (following links)
// `CommonSelector_MatchAllRecursively` should be provided.
Get(ctx context.Context, root cid.Cid, selector datamodel.Node) (ipld.Node, error)
// TODO: GetStream is not yet implemented - should follow logic of get but with incremental responses.
//GetStream(ctx context.Context, root cid.Cid, selector datamodel.Node) ResultChan
Close() error
}