-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathstore.go
58 lines (49 loc) · 1.46 KB
/
store.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
package blox
import (
"bytes"
"context"
"io"
"github.com/ipfs/go-cid"
"github.com/ipfs/go-datastore"
format "github.com/ipfs/go-ipld-format"
"github.com/ipld/go-ipld-prime"
cidlink "github.com/ipld/go-ipld-prime/linking/cid"
"github.com/multiformats/go-multicodec"
)
var lp = cidlink.LinkPrototype{
Prefix: cid.Prefix{
Version: 1,
Codec: uint64(multicodec.DagCbor),
MhType: uint64(multicodec.Blake3),
MhLength: -1,
},
}
func (p *Blox) Store(ctx context.Context, n ipld.Node) (ipld.Link, error) {
return p.ls.Store(ipld.LinkContext{Ctx: ctx}, lp, n)
}
func (p *Blox) Load(ctx context.Context, l ipld.Link, np ipld.NodePrototype) (ipld.Node, error) {
return p.ls.Load(ipld.LinkContext{Ctx: ctx}, l, np)
}
func (p *Blox) Has(ctx context.Context, l ipld.Link) (bool, error) {
return p.ds.Has(ctx, toDatastoreKey(l))
}
func (p *Blox) blockWriteOpener(ctx ipld.LinkContext) (io.Writer, ipld.BlockWriteCommitter, error) {
buf := bytes.NewBuffer(nil)
return buf, func(l ipld.Link) error {
return p.ds.Put(ctx.Ctx, toDatastoreKey(l), buf.Bytes())
}, nil
}
func (p *Blox) blockReadOpener(ctx ipld.LinkContext, l ipld.Link) (io.Reader, error) {
val, err := p.ds.Get(ctx.Ctx, toDatastoreKey(l))
switch err {
case nil:
return bytes.NewBuffer(val), nil
case datastore.ErrNotFound:
return nil, format.ErrNotFound{Cid: l.(cidlink.Link).Cid}
default:
return nil, err
}
}
func toDatastoreKey(l ipld.Link) datastore.Key {
return datastore.NewKey(l.Binary())
}