-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdag_store.go
74 lines (67 loc) · 1.75 KB
/
dag_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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package dagstore
import (
"context"
"fmt"
levelstore "github.com/CUIT-CBI/dag-store/store"
"github.com/ipfs/go-blockservice"
"github.com/ipfs/go-cid"
blockstore "github.com/ipfs/go-ipfs-blockstore"
chunk "github.com/ipfs/go-ipfs-chunker"
offline "github.com/ipfs/go-ipfs-exchange-offline"
"github.com/ipfs/go-merkledag"
"github.com/ipfs/go-unixfs/importer/helpers"
"github.com/ipfs/go-unixfs/importer/trickle"
uio "github.com/ipfs/go-unixfs/io"
"io"
)
type DagStore struct {
//store datastore.Batching
service blockservice.BlockService
}
func New(dir string) DagStore {
ds, err := levelstore.New(dir)
if err != nil {
fmt.Println(err)
}
bs := blockstore.NewBlockstore(ds)
exch := offline.Exchange(bs) // TODO: block交换策略
bserv := blockservice.New(bs, exch)
return DagStore{bserv}
}
func (ds *DagStore) Close() error {
if err := ds.service.Close(); err != nil {
return err
}
return nil
}
func (ds *DagStore) Add(ctx context.Context, reader io.Reader) (cid.Cid, error) {
dagService := merkledag.NewDAGService(ds.service)
params := helpers.DagBuilderParams{ // TODO:
Maxlinks: 10,
RawLeaves: true,
CidBuilder: cid.V0Builder{},
Dagserv: dagService,
NoCopy: false,
}
chunker, err := chunk.FromString(reader, "")
if err != nil {
return cid.Cid{}, err
}
db, err := params.New(chunker)
if err != nil {
return cid.Cid{}, err
}
root, err := trickle.Layout(db)
if err != nil {
return cid.Cid{}, err
}
return root.Cid(), ds.service.AddBlock(ctx, root)
}
func (ds *DagStore) Get(ctx context.Context, c cid.Cid) (io.WriterTo, error) {
dagService := merkledag.NewDAGService(ds.service)
node, err := dagService.Get(context.Background(), c)
if err != nil {
return nil, err
}
return uio.NewDagReader(ctx, node, dagService)
}