@cryptotaxi247 / kubo / commits / 85eb8812f

Port dag commansds to CoreAPI

License: MIT Signed-off-by: Łukasz Magiera <magik6k@gmail.com>

Łukasz Magiera committed Jan 22, 2019 at 21:01 UTC 85eb8812faeb508666b39de776c85172048fb650
5 files changed +93 -33
core/commands/dag/dag.go
+18 -28
@@ -4,10 +4,11 @@ import (
4 "fmt"
5 "io"
6 "math"
7 + "strings"
8
9 "github.com/ipfs/go-ipfs/core/commands/cmdenv"
10 + "github.com/ipfs/go-ipfs/core/coreapi/interface"
11 "github.com/ipfs/go-ipfs/core/coredag"
10 - "github.com/ipfs/go-ipfs/pin"
12
13 path "gx/ipfs/QmNYPETsdAu2uQ1k9q9S1jYEGURaLHV6cbYRSVFVRftpF8/go-path"
14 cid "gx/ipfs/QmR8BauakNcBa3RbE4nbQu76PDiJgoQgz8AJdhJuiU4TAw/go-cid"
@@ -65,7 +66,7 @@ into an object of the specified format.
66 cmdkit.StringOption("hash", "Hash function to use").WithDefault(""),
67 },
68 Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
68 - nd, err := cmdenv.GetNode(env)
69 + api, err := cmdenv.GetApi(env, req)
70 if err != nil {
71 return err
72 }
@@ -87,12 +88,11 @@ into an object of the specified format.
88 }
89 }
90
90 - cids := cid.NewSet()
91 - b := ipld.NewBatch(req.Context, nd.DAG)
92 -
91 + var adder ipld.NodeAdder = api.Dag()
92 if dopin {
94 - defer nd.Blockstore.PinLock().Unlock()
93 + adder = api.Dag().Pinning()
94 }
95 + b := ipld.NewBatch(req.Context, adder)
96
97 it := req.Files.Entries()
98 for it.Next() {
@@ -116,7 +116,6 @@ into an object of the specified format.
116 }
117
118 cid := nds[0].Cid()
119 - cids.Add(cid)
119 if err := res.Emit(&OutputObject{Cid: cid}); err != nil {
120 return err
121 }
@@ -129,17 +128,6 @@ into an object of the specified format.
128 return err
129 }
130
132 - if dopin {
133 - cids.ForEach(func(c cid.Cid) error {
134 - nd.Pinning.PinWithMode(c, pin.Recursive)
135 - return nil
136 - })
137 -
138 - err := nd.Pinning.Flush()
139 - if err != nil {
140 - return err
141 - }
142 - }
131 return nil
132 },
133 Type: OutputObject{},
@@ -167,27 +155,29 @@ format.
155 cmdkit.StringArg("ref", true, false, "The object to get").EnableStdin(),
156 },
157 Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
170 - nd, err := cmdenv.GetNode(env)
158 + api, err := cmdenv.GetApi(env, req)
159 if err != nil {
160 return err
161 }
162
175 - p, err := path.ParsePath(req.Arguments[0])
163 + p, err := iface.ParsePath(req.Arguments[0])
164 if err != nil {
165 return err
166 }
167
180 - lastCid, rem, err := nd.Resolver.ResolveToLastNode(req.Context, p)
168 + rp, err := api.ResolvePath(req.Context, p)
169 if err != nil {
170 return err
171 }
184 - obj, err := nd.DAG.Get(req.Context, lastCid)
172 +
173 + obj, err := api.Dag().Get(req.Context, rp.Cid())
174 if err != nil {
175 return err
176 }
177
178 var out interface{} = obj
190 - if len(rem) > 0 {
179 + if len(rp.Remainder()) > 0 {
180 + rem := strings.Split(rp.Remainder(), "/")
181 final, _, err := obj.Resolve(rem)
182 if err != nil {
183 return err
@@ -210,24 +200,24 @@ var DagResolveCmd = &cmds.Command{
200 cmdkit.StringArg("ref", true, false, "The path to resolve").EnableStdin(),
201 },
202 Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
213 - nd, err := cmdenv.GetNode(env)
203 + api, err := cmdenv.GetApi(env, req)
204 if err != nil {
205 return err
206 }
207
218 - p, err := path.ParsePath(req.Arguments[0])
208 + p, err := iface.ParsePath(req.Arguments[0])
209 if err != nil {
210 return err
211 }
212
223 - lastCid, rem, err := nd.Resolver.ResolveToLastNode(req.Context, p)
213 + rp, err := api.ResolvePath(req.Context, p)
214 if err != nil {
215 return err
216 }
217
218 return cmds.EmitOnce(res, &ResolveOutput{
229 - Cid: lastCid,
230 - RemPath: path.Join(rem),
219 + Cid: rp.Cid(),
220 + RemPath: rp.Remainder(),
221 })
222 },
223 Encoders: cmds.EncoderMap{
core/coreapi/coreapi.go
+8 -4
@@ -18,12 +18,13 @@ import (
18 "errors"
19 "fmt"
20 "github.com/ipfs/go-ipfs/core"
21 - coreiface "github.com/ipfs/go-ipfs/core/coreapi/interface"
22 - "github.com/ipfs/go-ipfs/core/coreapi/interface/options"
21 "github.com/ipfs/go-ipfs/namesys"
22 "github.com/ipfs/go-ipfs/pin"
23 "github.com/ipfs/go-ipfs/repo"
24
25 + coreiface "github.com/ipfs/go-ipfs/core/coreapi/interface"
26 + "github.com/ipfs/go-ipfs/core/coreapi/interface/options"
27 +
28 ci "gx/ipfs/QmNiJiXwWE3kRhZrC5ej3kSjWHm337pYfhjLGSCDNKJP2s/go-libp2p-crypto"
29 "gx/ipfs/QmP2g3VxmC7g7fyRJDj1VJ72KHZbJ9UW24YjSWEj1XTb4H/go-ipfs-exchange-interface"
30 pstore "gx/ipfs/QmPiemjiKBC9VA7vZF82m4x1oygtg2c2YVqag8PX7dN1BD/go-libp2p-peerstore"
@@ -96,8 +97,11 @@ func (api *CoreAPI) Block() coreiface.BlockAPI {
97 }
98
99 // Dag returns the DagAPI interface implementation backed by the go-ipfs node
99 -func (api *CoreAPI) Dag() ipld.DAGService {
100 - return api.dag
100 +func (api *CoreAPI) Dag() coreiface.APIDagService {
101 + return &dagAPI{
102 + api.dag,
103 + api,
104 + }
105 }
106
107 // Name returns the NameAPI interface implementation backed by the go-ipfs node
core/coreapi/dag.go new
+53
@@ -0,0 +1,53 @@
1 +package coreapi
2 +
3 +import (
4 + "context"
5 +
6 + "github.com/ipfs/go-ipfs/pin"
7 +
8 + cid "gx/ipfs/QmR8BauakNcBa3RbE4nbQu76PDiJgoQgz8AJdhJuiU4TAw/go-cid"
9 + ipld "gx/ipfs/QmcKKBwfz6FyQdHR2jsXrrF6XeSBXYL86anmWNewpFpoF5/go-ipld-format"
10 +)
11 +
12 +type dagAPI struct {
13 + ipld.DAGService
14 +
15 + core *CoreAPI
16 +}
17 +
18 +type pinningAdder CoreAPI
19 +
20 +func (adder *pinningAdder) Add(ctx context.Context, nd ipld.Node) error {
21 + defer adder.blockstore.PinLock().Unlock()
22 +
23 + if err := adder.dag.Add(ctx, nd); err != nil {
24 + return err
25 + }
26 +
27 + adder.pinning.PinWithMode(nd.Cid(), pin.Recursive)
28 +
29 + return adder.pinning.Flush()
30 +}
31 +
32 +func (adder *pinningAdder) AddMany(ctx context.Context, nds []ipld.Node) error {
33 + defer adder.blockstore.PinLock().Unlock()
34 +
35 + if err := adder.dag.AddMany(ctx, nds); err != nil {
36 + return err
37 + }
38 +
39 + cids := cid.NewSet()
40 +
41 + for _, nd := range nds {
42 + c := nd.Cid()
43 + if cids.Visit(c) {
44 + adder.pinning.PinWithMode(c, pin.Recursive)
45 + }
46 + }
47 +
48 + return adder.pinning.Flush()
49 +}
50 +
51 +func (api *dagAPI) Pinning() ipld.NodeAdder {
52 + return (*pinningAdder)(api.core)
53 +}
core/coreapi/interface/coreapi.go
+1 -1
@@ -19,7 +19,7 @@ type CoreAPI interface {
19 Block() BlockAPI
20
21 // Dag returns an implementation of Dag API
22 - Dag() ipld.DAGService
22 + Dag() APIDagService
23
24 // Name returns an implementation of Name API
25 Name() NameAPI
core/coreapi/interface/dag.go new
+13
@@ -0,0 +1,13 @@
1 +package iface
2 +
3 +import (
4 + ipld "gx/ipfs/QmcKKBwfz6FyQdHR2jsXrrF6XeSBXYL86anmWNewpFpoF5/go-ipld-format"
5 +)
6 +
7 +// APIDagService extends ipld.DAGService
8 +type APIDagService interface {
9 + ipld.DAGService
10 +
11 + // Pinning returns special NodeAdder which recursively pins added nodes
12 + Pinning() ipld.NodeAdder
13 +}