-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathget.go
41 lines (35 loc) · 955 Bytes
/
get.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
package pail
import (
"context"
"errors"
"github.com/ipld/go-ipld-prime"
"github.com/storacha/go-pail/block"
"github.com/storacha/go-pail/shard"
)
var ErrNotFound = errors.New("not found")
// Get the stored value for the given key from the bucket. If the key is not
// found, [ErrNotFound] is returned as the error value.
func Get(ctx context.Context, blocks block.Fetcher, root ipld.Link, key string) (ipld.Link, error) {
shards := shard.NewFetcher(blocks)
rshard, err := shards.GetRoot(ctx, root)
if err != nil {
return nil, err
}
path, err := traverse(ctx, shards, shard.AsBlock(rshard), key)
if err != nil {
return nil, err
}
target := path[len(path)-1]
skey := key[len(target.Value().Prefix()):] // key within the shard
var entry shard.Entry
for _, e := range target.Value().Entries() {
if e.Key() == skey {
entry = e
break
}
}
if entry == nil {
return nil, ErrNotFound
}
return entry.Value().Value(), nil
}