Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 9d18f13

Browse files
committedNov 10, 2015
rework editor creation and finalization
License: MIT Signed-off-by: Jeromy <jeromyj@gmail.com>
1 parent 51ad061 commit 9d18f13

File tree

7 files changed

+54
-42
lines changed

7 files changed

+54
-42
lines changed
 

‎core/commands/add.go

+12-20
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,11 @@ import (
66
"path"
77

88
"github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/cheggaaa/pb"
9-
ds "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore"
10-
syncds "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore/sync"
119
cxt "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
1210

13-
bstore "github.com/ipfs/go-ipfs/blocks/blockstore"
14-
bserv "github.com/ipfs/go-ipfs/blockservice"
1511
cmds "github.com/ipfs/go-ipfs/commands"
1612
files "github.com/ipfs/go-ipfs/commands/files"
1713
core "github.com/ipfs/go-ipfs/core"
18-
offline "github.com/ipfs/go-ipfs/exchange/offline"
1914
importer "github.com/ipfs/go-ipfs/importer"
2015
"github.com/ipfs/go-ipfs/importer/chunk"
2116
dag "github.com/ipfs/go-ipfs/merkledag"
@@ -108,7 +103,7 @@ remains to be implemented.
108103
hidden, _, _ := req.Option(hiddenOptionName).Bool()
109104
chunker, _, _ := req.Option(chunkerOptionName).String()
110105

111-
e := dagutils.NewDagEditor(NewMemoryDagService(), newDirNode())
106+
e := dagutils.NewDagEditor(newDirNode())
112107
if hash {
113108
nilnode, err := core.NewNode(n.Context(), &core.BuildCfg{
114109
//TODO: need this to be true or all files
@@ -172,21 +167,25 @@ remains to be implemented.
172167
return err
173168
}
174169

175-
if !hash {
176-
// copy intermediary nodes from editor to our actual dagservice
177-
err := e.WriteOutputTo(n.DAG)
178-
if err != nil {
179-
log.Error("WRITE OUT: ", err)
180-
return err
181-
}
170+
if hash {
171+
return nil
182172
}
183173

174+
// copy intermediary nodes from editor to our actual dagservice
175+
_, err := e.Finalize(n.DAG)
176+
if err != nil {
177+
log.Error("WRITE OUT: ", err)
178+
return err
179+
}
180+
181+
// rootnode will optionally wrap the output
184182
rootnd, err := fileAdder.RootNode()
185183
if err != nil {
186184
return err
187185
}
188186

189187
return pinRoot(rootnd)
188+
190189
}
191190

192191
go func() {
@@ -284,13 +283,6 @@ remains to be implemented.
284283
Type: AddedObject{},
285284
}
286285

287-
func NewMemoryDagService() dag.DAGService {
288-
// build mem-datastore for editor's intermediary nodes
289-
bs := bstore.NewBlockstore(syncds.MutexWrap(ds.NewMapDatastore()))
290-
bsrv := bserv.New(bs, offline.Exchange(bs))
291-
return dag.NewDAGService(bsrv)
292-
}
293-
294286
// Internal structure for holding the switches passed to the `add` call
295287
type adder struct {
296288
ctx cxt.Context

‎core/commands/object.go

+10-4
Original file line numberDiff line numberDiff line change
@@ -599,14 +599,17 @@ func rmLinkCaller(req cmds.Request, root *dag.Node) (key.Key, error) {
599599

600600
path := req.Arguments()[2]
601601

602-
e := dagutils.NewDagEditor(nd.DAG, root)
602+
e := dagutils.NewDagEditor(root)
603603

604604
err = e.RmLink(req.Context(), path)
605605
if err != nil {
606606
return "", err
607607
}
608608

609-
nnode := e.GetNode()
609+
nnode, err := e.Finalize(nd.DAG)
610+
if err != nil {
611+
return "", err
612+
}
610613

611614
return nnode.Key()
612615
}
@@ -636,7 +639,7 @@ func addLinkCaller(req cmds.Request, root *dag.Node) (key.Key, error) {
636639
}
637640
}
638641

639-
e := dagutils.NewDagEditor(nd.DAG, root)
642+
e := dagutils.NewDagEditor(root)
640643

641644
childnd, err := nd.DAG.Get(req.Context(), childk)
642645
if err != nil {
@@ -648,7 +651,10 @@ func addLinkCaller(req cmds.Request, root *dag.Node) (key.Key, error) {
648651
return "", err
649652
}
650653

651-
nnode := e.GetNode()
654+
nnode, err := e.Finalize(nd.DAG)
655+
if err != nil {
656+
return "", err
657+
}
652658

653659
return nnode.Key()
654660
}

‎core/corehttp/gateway_handler.go

+8-2
Original file line numberDiff line numberDiff line change
@@ -321,14 +321,20 @@ func (i *gatewayHandler) putHandler(w http.ResponseWriter, r *http.Request) {
321321
return
322322
}
323323

324-
e := dagutils.NewDagEditor(i.node.DAG, rnode)
324+
e := dagutils.NewDagEditor(rnode)
325325
err = e.InsertNodeAtPath(ctx, newPath, newnode, uio.NewEmptyDirectory)
326326
if err != nil {
327327
webError(w, "putHandler: InsertNodeAtPath failed", err, http.StatusInternalServerError)
328328
return
329329
}
330330

331-
newkey, err = e.GetNode().Key()
331+
nnode, err := e.Finalize(i.node.DAG)
332+
if err != nil {
333+
webError(w, "putHandler: could not get node", err, http.StatusInternalServerError)
334+
return
335+
}
336+
337+
newkey, err = nnode.Key()
332338
if err != nil {
333339
webError(w, "putHandler: could not get key of edited node", err, http.StatusInternalServerError)
334340
return

‎merkledag/utils/diff.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ func (c *Change) String() string {
3737
}
3838

3939
func ApplyChange(ctx context.Context, ds dag.DAGService, nd *dag.Node, cs []*Change) (*dag.Node, error) {
40-
e := NewDagEditor(ds, nd)
40+
e := NewDagEditor(nd)
4141
for _, c := range cs {
4242
switch c.Type {
4343
case Add:
@@ -71,7 +71,8 @@ func ApplyChange(ctx context.Context, ds dag.DAGService, nd *dag.Node, cs []*Cha
7171
}
7272
}
7373
}
74-
return e.GetNode(), nil
74+
75+
return e.Finalize(ds)
7576
}
7677

7778
func Diff(ctx context.Context, ds dag.DAGService, a, b *dag.Node) []*Change {

‎merkledag/utils/utils.go

+18-4
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,13 @@ import (
44
"errors"
55
"strings"
66

7+
ds "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore"
8+
syncds "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore/sync"
79
context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
810

11+
bstore "github.com/ipfs/go-ipfs/blocks/blockstore"
12+
bserv "github.com/ipfs/go-ipfs/blockservice"
13+
offline "github.com/ipfs/go-ipfs/exchange/offline"
914
dag "github.com/ipfs/go-ipfs/merkledag"
1015
)
1116

@@ -14,10 +19,17 @@ type Editor struct {
1419
ds dag.DAGService
1520
}
1621

17-
func NewDagEditor(ds dag.DAGService, root *dag.Node) *Editor {
22+
func NewMemoryDagService() dag.DAGService {
23+
// build mem-datastore for editor's intermediary nodes
24+
bs := bstore.NewBlockstore(syncds.MutexWrap(ds.NewMapDatastore()))
25+
bsrv := bserv.New(bs, offline.Exchange(bs))
26+
return dag.NewDAGService(bsrv)
27+
}
28+
29+
func NewDagEditor(root *dag.Node) *Editor {
1830
return &Editor{
1931
root: root,
20-
ds: ds,
32+
ds: NewMemoryDagService(),
2133
}
2234
}
2335

@@ -152,8 +164,10 @@ func rmLink(ctx context.Context, ds dag.DAGService, root *dag.Node, path []strin
152164
return root, nil
153165
}
154166

155-
func (e *Editor) WriteOutputTo(ds dag.DAGService) error {
156-
return copyDag(e.GetNode(), e.ds, ds)
167+
func (e *Editor) Finalize(ds dag.DAGService) (*dag.Node, error) {
168+
nd := e.GetNode()
169+
err := copyDag(nd, e.ds, ds)
170+
return nd, err
157171
}
158172

159173
func copyDag(nd *dag.Node, from, to dag.DAGService) error {

‎merkledag/utils/utils_test.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,8 @@ func assertNodeAtPath(t *testing.T, ds dag.DAGService, root *dag.Node, path stri
6666
}
6767

6868
func TestInsertNode(t *testing.T) {
69-
ds := mdtest.Mock()
7069
root := new(dag.Node)
71-
e := NewDagEditor(ds, root)
70+
e := NewDagEditor(root)
7271

7372
testInsert(t, e, "a", "anodefortesting", false, "")
7473
testInsert(t, e, "a/b", "data", false, "")

‎tar/format.go

+2-8
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ func ImportTar(r io.Reader, ds dag.DAGService) (*dag.Node, error) {
4646
root := new(dag.Node)
4747
root.Data = []byte("ipfs/tar")
4848

49-
e := dagutil.NewDagEditor(ds, root)
49+
e := dagutil.NewDagEditor(root)
5050

5151
for {
5252
h, err := tr.Next()
@@ -91,13 +91,7 @@ func ImportTar(r io.Reader, ds dag.DAGService) (*dag.Node, error) {
9191
}
9292
}
9393

94-
root = e.GetNode()
95-
_, err = ds.Add(root)
96-
if err != nil {
97-
return nil, err
98-
}
99-
100-
return root, nil
94+
return e.Finalize(ds)
10195
}
10296

10397
// adds a '-' to the beginning of each path element so we can use 'data' as a

0 commit comments

Comments
 (0)
Please sign in to comment.