Skip to content

Commit

Permalink
Fail to construct preload hamt shards when traversal fails
Browse files Browse the repository at this point in the history
Fix #54
  • Loading branch information
willscott committed Jul 6, 2023
1 parent 364a549 commit 2450f69
Showing 1 changed file with 24 additions and 7 deletions.
31 changes: 24 additions & 7 deletions hamt/shardeddir.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,13 @@ func NewUnixFSHAMTShardWithPreload(ctx context.Context, substrate dagpb.PBNode,
return n, err
}

traverse := n.Length()
traverse, err := n.(*_UnixFSHAMTShard).length()
if traverse == -1 {
return n, fmt.Errorf("could not fully explore hamt during preload")
}
if err != nil {
return n, err
}

return n, nil
}
Expand Down Expand Up @@ -261,9 +264,9 @@ func (n UnixFSHAMTShard) ListIterator() ipld.ListIterator {

// 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 {
func (n UnixFSHAMTShard) length() (int64, error) {
if n.cachedLength != -1 {
return n.cachedLength
return n.cachedLength, nil
}
maxPadLen := maxPadLength(n.data)
total := int64(0)
Expand All @@ -272,20 +275,34 @@ func (n UnixFSHAMTShard) Length() int64 {
_, pbLink := itr.Next()
isValue, err := isValueLink(pbLink, maxPadLen)
if err != nil {
continue
return 0, err
}
if isValue {
total++
} else {
child, err := n.loadChild(pbLink)
if err != nil {
continue
return 0, err
}
total += child.Length()
cl, err := child.length()
if err != nil {
return 0, err
}
total += cl
}
}
n.cachedLength = total
return total
return total, 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 {
len, err := n.length()
if err != nil {
return 0
}
return len
}

func (n UnixFSHAMTShard) IsAbsent() bool {
Expand Down

0 comments on commit 2450f69

Please sign in to comment.