-
Notifications
You must be signed in to change notification settings - Fork 0
/
traverse.go
34 lines (31 loc) · 925 Bytes
/
traverse.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
package pail
import (
"context"
"fmt"
"strings"
"github.com/storacha/go-pail/shard"
)
// Traverse from the passed shard block to the target shard block using the
// passed key. All traversed shards are returned, starting with the passed shard
// and ending with the target.
func traverse(ctx context.Context, shards *shard.Fetcher, shardBlock shard.BlockView, key string) ([]shard.BlockView, error) {
for _, e := range shardBlock.Value().Entries() {
k := e.Key()
v := e.Value()
if key == k {
break
}
if strings.HasPrefix(key, k) && v.Shard() != nil {
s, err := shards.Get(ctx, v.Shard())
if err != nil {
return nil, fmt.Errorf("getting shard %s: %w", v.Shard().String(), err)
}
path, err := traverse(ctx, shards, s, key[len(k):])
if err != nil {
return nil, err
}
return append([]shard.BlockView{shardBlock}, path...), nil
}
}
return []shard.BlockView{shardBlock}, nil
}