Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
10
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
dms3
go-merkledag
Commits
7cb580ad
Commit
7cb580ad
authored
Nov 13, 2015
by
Jeromy
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
rework editor creation and finalization
License: MIT Signed-off-by:
Jeromy
<
jeromyj@gmail.com
>
parent
b54c4fa0
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
65 additions
and
31 deletions
+65
-31
node.go
node.go
+3
-1
utils/diff.go
utils/diff.go
+3
-2
utils/utils.go
utils/utils.go
+54
-22
utils/utils_test.go
utils/utils_test.go
+5
-6
No files found.
node.go
View file @
7cb580ad
...
@@ -9,6 +9,8 @@ import (
...
@@ -9,6 +9,8 @@ import (
key
"github.com/ipfs/go-ipfs/blocks/key"
key
"github.com/ipfs/go-ipfs/blocks/key"
)
)
var
ErrLinkNotFound
=
fmt
.
Errorf
(
"no link by that name"
)
// Node represents a node in the IPFS Merkle DAG.
// Node represents a node in the IPFS Merkle DAG.
// nodes have opaque data and a set of navigable links.
// nodes have opaque data and a set of navigable links.
type
Node
struct
{
type
Node
struct
{
...
@@ -160,7 +162,7 @@ func (n *Node) GetNodeLink(name string) (*Link, error) {
...
@@ -160,7 +162,7 @@ func (n *Node) GetNodeLink(name string) (*Link, error) {
},
nil
},
nil
}
}
}
}
return
nil
,
ErrNotFound
return
nil
,
Err
Link
NotFound
}
}
func
(
n
*
Node
)
GetLinkedNode
(
ctx
context
.
Context
,
ds
DAGService
,
name
string
)
(
*
Node
,
error
)
{
func
(
n
*
Node
)
GetLinkedNode
(
ctx
context
.
Context
,
ds
DAGService
,
name
string
)
(
*
Node
,
error
)
{
...
...
utils/diff.go
View file @
7cb580ad
...
@@ -37,7 +37,7 @@ func (c *Change) String() string {
...
@@ -37,7 +37,7 @@ func (c *Change) String() string {
}
}
func
ApplyChange
(
ctx
context
.
Context
,
ds
dag
.
DAGService
,
nd
*
dag
.
Node
,
cs
[]
*
Change
)
(
*
dag
.
Node
,
error
)
{
func
ApplyChange
(
ctx
context
.
Context
,
ds
dag
.
DAGService
,
nd
*
dag
.
Node
,
cs
[]
*
Change
)
(
*
dag
.
Node
,
error
)
{
e
:=
NewDagEditor
(
ds
,
nd
)
e
:=
NewDagEditor
(
nd
,
ds
)
for
_
,
c
:=
range
cs
{
for
_
,
c
:=
range
cs
{
switch
c
.
Type
{
switch
c
.
Type
{
case
Add
:
case
Add
:
...
@@ -71,7 +71,8 @@ func ApplyChange(ctx context.Context, ds dag.DAGService, nd *dag.Node, cs []*Cha
...
@@ -71,7 +71,8 @@ func ApplyChange(ctx context.Context, ds dag.DAGService, nd *dag.Node, cs []*Cha
}
}
}
}
}
}
return
e
.
GetNode
(),
nil
return
e
.
Finalize
(
ds
)
}
}
func
Diff
(
ctx
context
.
Context
,
ds
dag
.
DAGService
,
a
,
b
*
dag
.
Node
)
[]
*
Change
{
func
Diff
(
ctx
context
.
Context
,
ds
dag
.
DAGService
,
a
,
b
*
dag
.
Node
)
[]
*
Change
{
...
...
utils/utils.go
View file @
7cb580ad
...
@@ -4,20 +4,41 @@ import (
...
@@ -4,20 +4,41 @@ import (
"errors"
"errors"
"strings"
"strings"
ds
"github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore"
syncds
"github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore/sync"
context
"github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
context
"github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
bstore
"github.com/ipfs/go-ipfs/blocks/blockstore"
bserv
"github.com/ipfs/go-ipfs/blockservice"
offline
"github.com/ipfs/go-ipfs/exchange/offline"
dag
"github.com/ipfs/go-ipfs/merkledag"
dag
"github.com/ipfs/go-ipfs/merkledag"
)
)
type
Editor
struct
{
type
Editor
struct
{
root
*
dag
.
Node
root
*
dag
.
Node
ds
dag
.
DAGService
// tmp is a temporary in memory (for now) dagstore for all of the
// intermediary nodes to be stored in
tmp
dag
.
DAGService
// src is the dagstore with *all* of the data on it, it is used to pull
// nodes from for modification (nil is a valid value)
src
dag
.
DAGService
}
func
NewMemoryDagService
()
dag
.
DAGService
{
// build mem-datastore for editor's intermediary nodes
bs
:=
bstore
.
NewBlockstore
(
syncds
.
MutexWrap
(
ds
.
NewMapDatastore
()))
bsrv
:=
bserv
.
New
(
bs
,
offline
.
Exchange
(
bs
))
return
dag
.
NewDAGService
(
bsrv
)
}
}
func
NewDagEditor
(
ds
dag
.
DAGService
,
root
*
dag
.
Node
)
*
Editor
{
// root is the node to be modified, source is the dagstore to pull nodes from (optional)
func
NewDagEditor
(
root
*
dag
.
Node
,
source
dag
.
DAGService
)
*
Editor
{
return
&
Editor
{
return
&
Editor
{
root
:
root
,
root
:
root
,
ds
:
ds
,
tmp
:
NewMemoryDagService
(),
src
:
source
,
}
}
}
}
...
@@ -26,7 +47,7 @@ func (e *Editor) GetNode() *dag.Node {
...
@@ -26,7 +47,7 @@ func (e *Editor) GetNode() *dag.Node {
}
}
func
(
e
*
Editor
)
GetDagService
()
dag
.
DAGService
{
func
(
e
*
Editor
)
GetDagService
()
dag
.
DAGService
{
return
e
.
ds
return
e
.
tmp
}
}
func
addLink
(
ctx
context
.
Context
,
ds
dag
.
DAGService
,
root
*
dag
.
Node
,
childname
string
,
childnd
*
dag
.
Node
)
(
*
dag
.
Node
,
error
)
{
func
addLink
(
ctx
context
.
Context
,
ds
dag
.
DAGService
,
root
*
dag
.
Node
,
childname
string
,
childnd
*
dag
.
Node
)
(
*
dag
.
Node
,
error
)
{
...
@@ -57,7 +78,7 @@ func addLink(ctx context.Context, ds dag.DAGService, root *dag.Node, childname s
...
@@ -57,7 +78,7 @@ func addLink(ctx context.Context, ds dag.DAGService, root *dag.Node, childname s
func
(
e
*
Editor
)
InsertNodeAtPath
(
ctx
context
.
Context
,
path
string
,
toinsert
*
dag
.
Node
,
create
func
()
*
dag
.
Node
)
error
{
func
(
e
*
Editor
)
InsertNodeAtPath
(
ctx
context
.
Context
,
path
string
,
toinsert
*
dag
.
Node
,
create
func
()
*
dag
.
Node
)
error
{
splpath
:=
strings
.
Split
(
path
,
"/"
)
splpath
:=
strings
.
Split
(
path
,
"/"
)
nd
,
err
:=
insertNodeAtPath
(
ctx
,
e
.
ds
,
e
.
root
,
splpath
,
toinsert
,
create
)
nd
,
err
:=
e
.
insertNodeAtPath
(
ctx
,
e
.
root
,
splpath
,
toinsert
,
create
)
if
err
!=
nil
{
if
err
!=
nil
{
return
err
return
err
}
}
...
@@ -65,27 +86,32 @@ func (e *Editor) InsertNodeAtPath(ctx context.Context, path string, toinsert *da
...
@@ -65,27 +86,32 @@ func (e *Editor) InsertNodeAtPath(ctx context.Context, path string, toinsert *da
return
nil
return
nil
}
}
func
insertNodeAtPath
(
ctx
context
.
Context
,
ds
dag
.
DAGService
,
root
*
dag
.
Node
,
path
[]
string
,
toinsert
*
dag
.
Node
,
create
func
()
*
dag
.
Node
)
(
*
dag
.
Node
,
error
)
{
func
(
e
*
Editor
)
insertNodeAtPath
(
ctx
context
.
Context
,
root
*
dag
.
Node
,
path
[]
string
,
toinsert
*
dag
.
Node
,
create
func
()
*
dag
.
Node
)
(
*
dag
.
Node
,
error
)
{
if
len
(
path
)
==
1
{
if
len
(
path
)
==
1
{
return
addLink
(
ctx
,
ds
,
root
,
path
[
0
],
toinsert
)
return
addLink
(
ctx
,
e
.
tmp
,
root
,
path
[
0
],
toinsert
)
}
}
nd
,
err
:=
root
.
GetLinkedNode
(
ctx
,
ds
,
path
[
0
])
nd
,
err
:=
root
.
GetLinkedNode
(
ctx
,
e
.
tmp
,
path
[
0
])
if
err
!=
nil
{
if
err
!=
nil
{
// if 'create' is true, we create directories on the way down as needed
// if 'create' is true, we create directories on the way down as needed
if
err
==
dag
.
ErrNotFound
&&
create
!=
nil
{
if
err
==
dag
.
Err
Link
NotFound
&&
create
!=
nil
{
nd
=
create
()
nd
=
create
()
}
else
{
err
=
nil
// no longer an error case
}
else
if
err
==
dag
.
ErrNotFound
{
nd
,
err
=
root
.
GetLinkedNode
(
ctx
,
e
.
src
,
path
[
0
])
}
if
err
!=
nil
{
return
nil
,
err
return
nil
,
err
}
}
}
}
ndprime
,
err
:=
insertNodeAtPath
(
ctx
,
ds
,
nd
,
path
[
1
:
],
toinsert
,
create
)
ndprime
,
err
:=
e
.
insertNodeAtPath
(
ctx
,
nd
,
path
[
1
:
],
toinsert
,
create
)
if
err
!=
nil
{
if
err
!=
nil
{
return
nil
,
err
return
nil
,
err
}
}
_
=
ds
.
Remove
(
root
)
_
=
e
.
tmp
.
Remove
(
root
)
_
=
root
.
RemoveNodeLink
(
path
[
0
])
_
=
root
.
RemoveNodeLink
(
path
[
0
])
err
=
root
.
AddNodeLinkClean
(
path
[
0
],
ndprime
)
err
=
root
.
AddNodeLinkClean
(
path
[
0
],
ndprime
)
...
@@ -93,7 +119,7 @@ func insertNodeAtPath(ctx context.Context, ds dag.DAGService, root *dag.Node, pa
...
@@ -93,7 +119,7 @@ func insertNodeAtPath(ctx context.Context, ds dag.DAGService, root *dag.Node, pa
return
nil
,
err
return
nil
,
err
}
}
_
,
err
=
ds
.
Add
(
root
)
_
,
err
=
e
.
tmp
.
Add
(
root
)
if
err
!=
nil
{
if
err
!=
nil
{
return
nil
,
err
return
nil
,
err
}
}
...
@@ -103,7 +129,7 @@ func insertNodeAtPath(ctx context.Context, ds dag.DAGService, root *dag.Node, pa
...
@@ -103,7 +129,7 @@ func insertNodeAtPath(ctx context.Context, ds dag.DAGService, root *dag.Node, pa
func
(
e
*
Editor
)
RmLink
(
ctx
context
.
Context
,
path
string
)
error
{
func
(
e
*
Editor
)
RmLink
(
ctx
context
.
Context
,
path
string
)
error
{
splpath
:=
strings
.
Split
(
path
,
"/"
)
splpath
:=
strings
.
Split
(
path
,
"/"
)
nd
,
err
:=
rmLink
(
ctx
,
e
.
ds
,
e
.
root
,
splpath
)
nd
,
err
:=
e
.
rmLink
(
ctx
,
e
.
root
,
splpath
)
if
err
!=
nil
{
if
err
!=
nil
{
return
err
return
err
}
}
...
@@ -111,7 +137,7 @@ func (e *Editor) RmLink(ctx context.Context, path string) error {
...
@@ -111,7 +137,7 @@ func (e *Editor) RmLink(ctx context.Context, path string) error {
return
nil
return
nil
}
}
func
rmLink
(
ctx
context
.
Context
,
ds
dag
.
DAGService
,
root
*
dag
.
Node
,
path
[]
string
)
(
*
dag
.
Node
,
error
)
{
func
(
e
*
Editor
)
rmLink
(
ctx
context
.
Context
,
root
*
dag
.
Node
,
path
[]
string
)
(
*
dag
.
Node
,
error
)
{
if
len
(
path
)
==
1
{
if
len
(
path
)
==
1
{
// base case, remove node in question
// base case, remove node in question
err
:=
root
.
RemoveNodeLink
(
path
[
0
])
err
:=
root
.
RemoveNodeLink
(
path
[
0
])
...
@@ -119,7 +145,7 @@ func rmLink(ctx context.Context, ds dag.DAGService, root *dag.Node, path []strin
...
@@ -119,7 +145,7 @@ func rmLink(ctx context.Context, ds dag.DAGService, root *dag.Node, path []strin
return
nil
,
err
return
nil
,
err
}
}
_
,
err
=
ds
.
Add
(
root
)
_
,
err
=
e
.
tmp
.
Add
(
root
)
if
err
!=
nil
{
if
err
!=
nil
{
return
nil
,
err
return
nil
,
err
}
}
...
@@ -127,17 +153,21 @@ func rmLink(ctx context.Context, ds dag.DAGService, root *dag.Node, path []strin
...
@@ -127,17 +153,21 @@ func rmLink(ctx context.Context, ds dag.DAGService, root *dag.Node, path []strin
return
root
,
nil
return
root
,
nil
}
}
nd
,
err
:=
root
.
GetLinkedNode
(
ctx
,
ds
,
path
[
0
])
nd
,
err
:=
root
.
GetLinkedNode
(
ctx
,
e
.
tmp
,
path
[
0
])
if
err
==
dag
.
ErrNotFound
{
nd
,
err
=
root
.
GetLinkedNode
(
ctx
,
e
.
src
,
path
[
0
])
}
if
err
!=
nil
{
if
err
!=
nil
{
return
nil
,
err
return
nil
,
err
}
}
nnode
,
err
:=
rmLink
(
ctx
,
ds
,
nd
,
path
[
1
:
])
nnode
,
err
:=
e
.
rmLink
(
ctx
,
nd
,
path
[
1
:
])
if
err
!=
nil
{
if
err
!=
nil
{
return
nil
,
err
return
nil
,
err
}
}
_
=
ds
.
Remove
(
root
)
_
=
e
.
tmp
.
Remove
(
root
)
_
=
root
.
RemoveNodeLink
(
path
[
0
])
_
=
root
.
RemoveNodeLink
(
path
[
0
])
err
=
root
.
AddNodeLinkClean
(
path
[
0
],
nnode
)
err
=
root
.
AddNodeLinkClean
(
path
[
0
],
nnode
)
...
@@ -145,7 +175,7 @@ func rmLink(ctx context.Context, ds dag.DAGService, root *dag.Node, path []strin
...
@@ -145,7 +175,7 @@ func rmLink(ctx context.Context, ds dag.DAGService, root *dag.Node, path []strin
return
nil
,
err
return
nil
,
err
}
}
_
,
err
=
ds
.
Add
(
root
)
_
,
err
=
e
.
tmp
.
Add
(
root
)
if
err
!=
nil
{
if
err
!=
nil
{
return
nil
,
err
return
nil
,
err
}
}
...
@@ -153,8 +183,10 @@ func rmLink(ctx context.Context, ds dag.DAGService, root *dag.Node, path []strin
...
@@ -153,8 +183,10 @@ func rmLink(ctx context.Context, ds dag.DAGService, root *dag.Node, path []strin
return
root
,
nil
return
root
,
nil
}
}
func
(
e
*
Editor
)
WriteOutputTo
(
ds
dag
.
DAGService
)
error
{
func
(
e
*
Editor
)
Finalize
(
ds
dag
.
DAGService
)
(
*
dag
.
Node
,
error
)
{
return
copyDag
(
e
.
GetNode
(),
e
.
ds
,
ds
)
nd
:=
e
.
GetNode
()
err
:=
copyDag
(
nd
,
e
.
tmp
,
ds
)
return
nd
,
err
}
}
func
copyDag
(
nd
*
dag
.
Node
,
from
,
to
dag
.
DAGService
)
error
{
func
copyDag
(
nd
*
dag
.
Node
,
from
,
to
dag
.
DAGService
)
error
{
...
...
utils/utils_test.go
View file @
7cb580ad
...
@@ -66,13 +66,12 @@ func assertNodeAtPath(t *testing.T, ds dag.DAGService, root *dag.Node, path stri
...
@@ -66,13 +66,12 @@ func assertNodeAtPath(t *testing.T, ds dag.DAGService, root *dag.Node, path stri
}
}
func
TestInsertNode
(
t
*
testing
.
T
)
{
func
TestInsertNode
(
t
*
testing
.
T
)
{
ds
:=
mdtest
.
Mock
()
root
:=
new
(
dag
.
Node
)
root
:=
new
(
dag
.
Node
)
e
:=
NewDagEditor
(
ds
,
root
)
e
:=
NewDagEditor
(
root
,
nil
)
testInsert
(
t
,
e
,
"a"
,
"anodefortesting"
,
false
,
""
)
testInsert
(
t
,
e
,
"a"
,
"anodefortesting"
,
false
,
""
)
testInsert
(
t
,
e
,
"a/b"
,
"data"
,
false
,
""
)
testInsert
(
t
,
e
,
"a/b"
,
"data"
,
false
,
""
)
testInsert
(
t
,
e
,
"a/b/c/d/e"
,
"blah"
,
false
,
"
merkledag: not found
"
)
testInsert
(
t
,
e
,
"a/b/c/d/e"
,
"blah"
,
false
,
"
no link by that name
"
)
testInsert
(
t
,
e
,
"a/b/c/d/e"
,
"foo"
,
true
,
""
)
testInsert
(
t
,
e
,
"a/b/c/d/e"
,
"foo"
,
true
,
""
)
testInsert
(
t
,
e
,
"a/b/c/d/f"
,
"baz"
,
true
,
""
)
testInsert
(
t
,
e
,
"a/b/c/d/f"
,
"baz"
,
true
,
""
)
testInsert
(
t
,
e
,
"a/b/c/d/f"
,
"bar"
,
true
,
""
)
testInsert
(
t
,
e
,
"a/b/c/d/f"
,
"bar"
,
true
,
""
)
...
@@ -92,7 +91,7 @@ func TestInsertNode(t *testing.T) {
...
@@ -92,7 +91,7 @@ func TestInsertNode(t *testing.T) {
func
testInsert
(
t
*
testing
.
T
,
e
*
Editor
,
path
,
data
string
,
create
bool
,
experr
string
)
{
func
testInsert
(
t
*
testing
.
T
,
e
*
Editor
,
path
,
data
string
,
create
bool
,
experr
string
)
{
child
:=
&
dag
.
Node
{
Data
:
[]
byte
(
data
)}
child
:=
&
dag
.
Node
{
Data
:
[]
byte
(
data
)}
ck
,
err
:=
e
.
ds
.
Add
(
child
)
ck
,
err
:=
e
.
tmp
.
Add
(
child
)
if
err
!=
nil
{
if
err
!=
nil
{
t
.
Fatal
(
err
)
t
.
Fatal
(
err
)
}
}
...
@@ -117,8 +116,8 @@ func testInsert(t *testing.T, e *Editor, path, data string, create bool, experr
...
@@ -117,8 +116,8 @@ func testInsert(t *testing.T, e *Editor, path, data string, create bool, experr
}
}
if
err
!=
nil
{
if
err
!=
nil
{
t
.
Fatal
(
err
)
t
.
Fatal
(
err
,
path
,
data
,
create
,
experr
)
}
}
assertNodeAtPath
(
t
,
e
.
ds
,
e
.
root
,
path
,
ck
)
assertNodeAtPath
(
t
,
e
.
tmp
,
e
.
root
,
path
,
ck
)
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment