forked from ipfs/go-graphsync
-
Notifications
You must be signed in to change notification settings - Fork 0
/
querypreparer.go
175 lines (163 loc) · 5.25 KB
/
querypreparer.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package responsemanager
import (
"context"
"io"
"math"
"github.com/ipfs/go-cid"
ipld "github.com/ipld/go-ipld-prime"
"github.com/ipld/go-ipld-prime/datamodel"
cidlink "github.com/ipld/go-ipld-prime/linking/cid"
"github.com/ipld/go-ipld-prime/traversal"
"github.com/libp2p/go-libp2p/core/peer"
graphsync "github.com/filecoin-project/boost-graphsync"
"github.com/filecoin-project/boost-graphsync/cidset"
"github.com/filecoin-project/boost-graphsync/dedupkey"
"github.com/filecoin-project/boost-graphsync/donotsendfirstblocks"
"github.com/filecoin-project/boost-graphsync/ipldutil"
gsmsg "github.com/filecoin-project/boost-graphsync/message"
"github.com/filecoin-project/boost-graphsync/panics"
"github.com/filecoin-project/boost-graphsync/responsemanager/queryexecutor"
"github.com/filecoin-project/boost-graphsync/responsemanager/responseassembler"
)
type errorString string
func (e errorString) Error() string {
return string(e)
}
const errInvalidRequest = errorString("request not valid")
type queryPreparer struct {
requestHooks RequestHooks
linkSystem ipld.LinkSystem
// maximum number of links to traverse per request. A value of zero = infinity, or no limit=
maxLinksPerRequest uint64
panicCallback panics.CallBackFn
}
func (qe *queryPreparer) prepareQuery(
ctx context.Context,
p peer.ID,
request gsmsg.GraphSyncRequest,
responseStream responseassembler.ResponseStream,
signals queryexecutor.ResponseSignals) (ipld.BlockReadOpener, ipldutil.Traverser, bool, error) {
result := qe.requestHooks.ProcessRequestHooks(p, request)
var isPaused bool
err := responseStream.Transaction(func(rb responseassembler.ResponseBuilder) error {
for _, extension := range result.Extensions {
rb.SendExtensionData(extension)
}
if result.Err != nil {
rb.FinishWithError(graphsync.RequestFailedUnknown)
return result.Err
} else if !result.IsValidated {
rb.FinishWithError(graphsync.RequestRejected)
return errInvalidRequest
} else if result.IsPaused {
rb.PauseRequest()
isPaused = true
}
return nil
})
if err != nil {
return nil, nil, false, err
}
if err := processDedupByKey(request, responseStream); err != nil {
return nil, nil, false, err
}
if err := processDoNoSendCids(request, responseStream); err != nil {
return nil, nil, false, err
}
if err := processDoNotSendFirstBlocks(request, responseStream); err != nil {
return nil, nil, false, err
}
rootLink := cidlink.Link{Cid: request.Root()}
linkSystem := result.CustomLinkSystem
if linkSystem.StorageReadOpener == nil {
linkSystem = qe.linkSystem
}
var budget *traversal.Budget
if qe.maxLinksPerRequest > 0 {
budget = &traversal.Budget{
NodeBudget: math.MaxInt64,
LinkBudget: int64(qe.maxLinksPerRequest),
}
}
traverser := ipldutil.TraversalBuilder{
Root: rootLink,
Selector: request.Selector(),
LinkSystem: linkSystem,
Chooser: result.CustomChooser,
Budget: budget,
PanicCallback: qe.panicCallback,
Visitor: func(p traversal.Progress, n datamodel.Node, vr traversal.VisitReason) error {
if vr != traversal.VisitReason_SelectionMatch {
return nil
}
if lbn, ok := n.(datamodel.LargeBytesNode); ok {
s, err := lbn.AsLargeBytes()
if err != nil {
log.Warnf("error %s in AsLargeBytes at path %s", err.Error(), p.Path)
}
_, err = io.Copy(io.Discard, s)
if err != nil {
log.Warnf("error %s reading bytes from reader at path %s", err.Error(), p.Path)
}
}
return nil
},
}.Start(ctx)
return linkSystem.StorageReadOpener, traverser, isPaused, nil
}
func processDedupByKey(request gsmsg.GraphSyncRequest, responseStream responseassembler.ResponseStream) error {
dedupData, has := request.Extension(graphsync.ExtensionDeDupByKey)
if !has {
return nil
}
key, err := dedupkey.DecodeDedupKey(dedupData)
if err != nil {
_ = responseStream.Transaction(func(rb responseassembler.ResponseBuilder) error {
rb.FinishWithError(graphsync.RequestFailedUnknown)
return nil
})
return err
}
responseStream.DedupKey(key)
return nil
}
func processDoNoSendCids(request gsmsg.GraphSyncRequest, responseStream responseassembler.ResponseStream) error {
doNotSendCidsData, has := request.Extension(graphsync.ExtensionDoNotSendCIDs)
if !has {
return nil
}
cidSet, err := cidset.DecodeCidSet(doNotSendCidsData)
if err != nil {
_ = responseStream.Transaction(func(rb responseassembler.ResponseBuilder) error {
rb.FinishWithError(graphsync.RequestFailedUnknown)
return nil
})
return err
}
links := make([]ipld.Link, 0, cidSet.Len())
err = cidSet.ForEach(func(c cid.Cid) error {
links = append(links, cidlink.Link{Cid: c})
return nil
})
if err != nil {
return err
}
responseStream.IgnoreBlocks(links)
return nil
}
func processDoNotSendFirstBlocks(request gsmsg.GraphSyncRequest, responseStream responseassembler.ResponseStream) error {
doNotSendFirstBlocksData, has := request.Extension(graphsync.ExtensionsDoNotSendFirstBlocks)
if !has {
return nil
}
skipCount, err := donotsendfirstblocks.DecodeDoNotSendFirstBlocks(doNotSendFirstBlocksData)
if err != nil {
_ = responseStream.Transaction(func(rb responseassembler.ResponseBuilder) error {
rb.FinishWithError(graphsync.RequestFailedUnknown)
return nil
})
return err
}
responseStream.SkipFirstBlocks(skipCount)
return nil
}