-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathshardeddir.go
393 lines (345 loc) · 9.9 KB
/
shardeddir.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
package hamt
import (
"context"
"fmt"
bitfield "github.com/ipfs/go-bitfield"
"github.com/ipfs/go-unixfsnode/data"
"github.com/ipfs/go-unixfsnode/iter"
dagpb "github.com/ipld/go-codec-dagpb"
"github.com/ipld/go-ipld-prime"
"github.com/ipld/go-ipld-prime/schema"
)
const (
// HashMurmur3 is the multiformats identifier for Murmur3
HashMurmur3 uint64 = 0x22
)
var _ ipld.Node = UnixFSHAMTShard(nil)
var _ schema.TypedNode = UnixFSHAMTShard(nil)
var _ ipld.ADL = UnixFSHAMTShard(nil)
// UnixFSHAMTShared is an IPLD Prime Node that provides a read interface
// to a UnixFS HAMT
type UnixFSHAMTShard = *_UnixFSHAMTShard
type _UnixFSHAMTShard struct {
ctx context.Context
_substrate dagpb.PBNode
data data.UnixFSData
lsys *ipld.LinkSystem
bitfield bitfield.Bitfield
shardCache map[ipld.Link]*_UnixFSHAMTShard
cachedLength int64
}
// NewUnixFSHAMTShard attempts to construct a UnixFSHAMTShard node from the base protobuf node plus
// a decoded UnixFSData structure
func NewUnixFSHAMTShard(ctx context.Context, substrate dagpb.PBNode, data data.UnixFSData, lsys *ipld.LinkSystem) (ipld.Node, error) {
if err := validateHAMTData(data); err != nil {
return nil, err
}
shardCache := make(map[ipld.Link]*_UnixFSHAMTShard, substrate.FieldLinks().Length())
bf, err := bitField(data)
if err != nil {
return nil, err
}
return &_UnixFSHAMTShard{
ctx: ctx,
_substrate: substrate,
data: data,
lsys: lsys,
shardCache: shardCache,
bitfield: bf,
cachedLength: -1,
}, nil
}
// NewUnixFSHAMTShardWithPreload attempts to construct a UnixFSHAMTShard node from the base protobuf node plus
// a decoded UnixFSData structure, and then iterate through and load the full set of hamt shards.
func NewUnixFSHAMTShardWithPreload(ctx context.Context, substrate dagpb.PBNode, data data.UnixFSData, lsys *ipld.LinkSystem) (ipld.Node, error) {
n, err := NewUnixFSHAMTShard(ctx, substrate, data, lsys)
if err != nil {
return n, err
}
traverse := n.Length()
if traverse == -1 {
return n, fmt.Errorf("could not fully explore hamt during preload")
}
return n, nil
}
func (n UnixFSHAMTShard) Substrate() ipld.Node {
return n._substrate
}
func (n UnixFSHAMTShard) Kind() ipld.Kind {
return n._substrate.Kind()
}
// LookupByString looks for the key in the list of links with a matching name
func (n *_UnixFSHAMTShard) LookupByString(key string) (ipld.Node, error) {
hv := &hashBits{b: hash([]byte(key))}
return n.lookup(key, hv)
}
func (n UnixFSHAMTShard) lookup(key string, hv *hashBits) (dagpb.Link, error) {
log2 := log2Size(n.data)
maxPadLen := maxPadLength(n.data)
childIndex, err := hv.Next(log2)
if err != nil {
return nil, err
}
if n.hasChild(childIndex) {
pbLink, err := n.getChildLink(childIndex)
if err != nil {
return nil, err
}
isValue, err := isValueLink(pbLink, maxPadLen)
if err != nil {
return nil, err
}
if isValue {
if MatchKey(pbLink, key, maxPadLen) {
return pbLink.FieldHash(), nil
}
} else {
childNd, err := n.loadChild(pbLink)
if err != nil {
return nil, err
}
return childNd.lookup(key, hv)
}
}
return nil, schema.ErrNoSuchField{Type: nil /*TODO*/, Field: ipld.PathSegmentOfString(key)}
}
// AttemptHAMTShardFromNode attempts to read a HAMT shard from a general protobuf node
func AttemptHAMTShardFromNode(ctx context.Context, nd ipld.Node, lsys *ipld.LinkSystem) (UnixFSHAMTShard, error) {
// shortcut if node is already a hamt
hnd, ok := nd.(UnixFSHAMTShard)
if ok {
return hnd, nil
}
pbnd, ok := nd.(dagpb.PBNode)
if !ok {
return nil, fmt.Errorf("hamt.AttemptHAMTShardFromNode: %w", ErrNotProtobuf)
}
if !pbnd.FieldData().Exists() {
return nil, fmt.Errorf("hamt.AttemptHAMTShardFromNode: %w", ErrNotUnixFSNode)
}
data, err := data.DecodeUnixFSData(pbnd.FieldData().Must().Bytes())
if err != nil {
return nil, err
}
und, err := NewUnixFSHAMTShard(ctx, pbnd, data, lsys)
if err != nil {
return nil, err
}
return und.(UnixFSHAMTShard), nil
}
func (n UnixFSHAMTShard) loadChild(pbLink dagpb.PBLink) (UnixFSHAMTShard, error) {
cached, ok := n.shardCache[pbLink.FieldHash().Link()]
if ok {
return cached, nil
}
nd, err := n.lsys.Load(ipld.LinkContext{Ctx: n.ctx}, pbLink.FieldHash().Link(), dagpb.Type.PBNode)
if err != nil {
return nil, err
}
und, err := AttemptHAMTShardFromNode(n.ctx, nd, n.lsys)
if err != nil {
return nil, err
}
n.shardCache[pbLink.FieldHash().Link()] = und
return und, nil
}
func (n UnixFSHAMTShard) LookupByNode(key ipld.Node) (ipld.Node, error) {
ks, err := key.AsString()
if err != nil {
return nil, err
}
return n.LookupByString(ks)
}
func (n UnixFSHAMTShard) LookupByIndex(idx int64) (ipld.Node, error) {
return n._substrate.LookupByIndex(idx)
}
func (n UnixFSHAMTShard) LookupBySegment(seg ipld.PathSegment) (ipld.Node, error) {
return n.LookupByString(seg.String())
}
func (n UnixFSHAMTShard) MapIterator() ipld.MapIterator {
maxPadLen := maxPadLength(n.data)
listItr := &_UnixFSShardedDir__ListItr{
_substrate: n.FieldLinks().Iterator(),
maxPadLen: maxPadLen,
nd: n,
}
st := stringTransformer{maxPadLen: maxPadLen}
return iter.NewUnixFSDirMapIterator(listItr, st.transformNameNode)
}
type _UnixFSShardedDir__ListItr struct {
_substrate *dagpb.PBLinks__Itr
childIter *_UnixFSShardedDir__ListItr
nd UnixFSHAMTShard
maxPadLen int
total int64
}
func (itr *_UnixFSShardedDir__ListItr) Next() (int64, dagpb.PBLink, error) {
total := itr.total
itr.total++
next, err := itr.next()
if err != nil {
return -1, nil, err
}
if next == nil {
return -1, nil, nil
}
return total, next, nil
}
func (itr *_UnixFSShardedDir__ListItr) next() (dagpb.PBLink, error) {
if itr.childIter == nil {
if itr._substrate.Done() {
return nil, nil
}
_, next := itr._substrate.Next()
isValue, err := isValueLink(next, itr.maxPadLen)
if err != nil {
return nil, err
}
if isValue {
return next, nil
}
child, err := itr.nd.loadChild(next)
if err != nil {
return nil, err
}
itr.childIter = &_UnixFSShardedDir__ListItr{
_substrate: child._substrate.FieldLinks().Iterator(),
nd: child,
maxPadLen: maxPadLength(child.data),
}
}
_, next, err := itr.childIter.Next()
if itr.childIter.Done() {
// do this even on error to make sure we don't overrun a shard where the
// end is missing and the user is ignoring NotFound errors
itr.childIter = nil
}
if err != nil {
return nil, err
}
return next, nil
}
func (itr *_UnixFSShardedDir__ListItr) Done() bool {
return itr.childIter == nil && itr._substrate.Done()
}
// ListIterator returns an iterator which yields key-value pairs
// traversing the node.
// If the node kind is anything other than a list, nil will be returned.
//
// The iterator will yield every entry in the list; that is, it
// can be expected that itr.Next will be called node.Length times
// before itr.Done becomes true.
func (n UnixFSHAMTShard) ListIterator() ipld.ListIterator {
return nil
}
// Length returns the length of a list, or the number of entries in a map,
// or -1 if the node is not of list nor map kind.
func (n UnixFSHAMTShard) Length() int64 {
if n.cachedLength != -1 {
return n.cachedLength
}
maxPadLen := maxPadLength(n.data)
total := int64(0)
itr := n.FieldLinks().Iterator()
for !itr.Done() {
_, pbLink := itr.Next()
isValue, err := isValueLink(pbLink, maxPadLen)
if err != nil {
continue
}
if isValue {
total++
} else {
child, err := n.loadChild(pbLink)
if err != nil {
continue
}
total += child.Length()
}
}
n.cachedLength = total
return total
}
func (n UnixFSHAMTShard) IsAbsent() bool {
return false
}
func (n UnixFSHAMTShard) IsNull() bool {
return false
}
func (n UnixFSHAMTShard) AsBool() (bool, error) {
return n._substrate.AsBool()
}
func (n UnixFSHAMTShard) AsInt() (int64, error) {
return n._substrate.AsInt()
}
func (n UnixFSHAMTShard) AsFloat() (float64, error) {
return n._substrate.AsFloat()
}
func (n UnixFSHAMTShard) AsString() (string, error) {
return n._substrate.AsString()
}
func (n UnixFSHAMTShard) AsBytes() ([]byte, error) {
return n._substrate.AsBytes()
}
func (n UnixFSHAMTShard) AsLink() (ipld.Link, error) {
return n._substrate.AsLink()
}
func (n UnixFSHAMTShard) Prototype() ipld.NodePrototype {
// TODO: should this return something?
// probobly not until we write the write interfaces
return nil
}
// satisfy schema.TypedNode
func (UnixFSHAMTShard) Type() schema.Type {
return nil /*TODO:typelit*/
}
func (n UnixFSHAMTShard) Representation() ipld.Node {
return n._substrate.Representation()
}
// Native map accessors
func (n UnixFSHAMTShard) Iterator() *iter.UnixFSDir__Itr {
maxPadLen := maxPadLength(n.data)
listItr := &_UnixFSShardedDir__ListItr{
_substrate: n.FieldLinks().Iterator(),
maxPadLen: maxPadLen,
nd: n,
}
st := stringTransformer{maxPadLen: maxPadLen}
return iter.NewUnixFSDirIterator(listItr, st.transformNameNode)
}
func (n UnixFSHAMTShard) Lookup(key dagpb.String) dagpb.Link {
hv := &hashBits{b: hash([]byte(key.String()))}
link, err := n.lookup(key.String(), hv)
if err != nil {
return nil
}
return link
}
// direct access to the links and data
func (n UnixFSHAMTShard) FieldLinks() dagpb.PBLinks {
return n._substrate.FieldLinks()
}
func (n UnixFSHAMTShard) FieldData() dagpb.MaybeBytes {
return n._substrate.FieldData()
}
func (n UnixFSHAMTShard) getChildLink(childIndex int) (dagpb.PBLink, error) {
linkIndex := n.bitfield.OnesBefore(childIndex)
if linkIndex >= int(n.FieldLinks().Length()) || linkIndex < 0 {
return nil, ErrInvalidChildIndex
}
return n.FieldLinks().Lookup(int64(linkIndex)), nil
}
func (n UnixFSHAMTShard) hasChild(childIndex int) bool {
return n.bitfield.Bit(childIndex)
}
type stringTransformer struct {
maxPadLen int
}
func (s stringTransformer) transformNameNode(nd dagpb.String) dagpb.String {
nb := dagpb.Type.String.NewBuilder()
err := nb.AssignString(nd.String()[s.maxPadLen:])
if err != nil {
return nil
}
return nb.Build().(dagpb.String)
}