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
e787a157
Commit
e787a157
authored
Jan 25, 2018
by
Steven Allen
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
make code-climate happier
License: MIT Signed-off-by:
Steven Allen
<
steven@stebalien.com
>
parent
2fbf40a0
Changes
5
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
19 additions
and
6 deletions
+19
-6
node.go
node.go
+4
-2
test/utils.go
test/utils.go
+2
-0
utils/diff.go
utils/diff.go
+1
-0
utils/utils.go
utils/utils.go
+10
-2
utils/utils_test.go
utils/utils_test.go
+2
-2
No files found.
node.go
View file @
e787a157
...
...
@@ -124,7 +124,7 @@ func (n *ProtoNode) AddRawLink(name string, l *node.Link) error {
return
nil
}
// Remove a link on this node by the given name
// Remove
NodeLink removes
a link on this node by the given name
.
func
(
n
*
ProtoNode
)
RemoveNodeLink
(
name
string
)
error
{
n
.
encoded
=
nil
good
:=
make
([]
*
node
.
Link
,
0
,
len
(
n
.
links
))
...
...
@@ -146,7 +146,7 @@ func (n *ProtoNode) RemoveNodeLink(name string) error {
return
nil
}
//
R
eturn a copy of the link with given name
//
GetNodeLink r
eturn
s
a copy of the link with
the
given name
.
func
(
n
*
ProtoNode
)
GetNodeLink
(
name
string
)
(
*
node
.
Link
,
error
)
{
for
_
,
l
:=
range
n
.
links
{
if
l
.
Name
==
name
{
...
...
@@ -160,6 +160,7 @@ func (n *ProtoNode) GetNodeLink(name string) (*node.Link, error) {
return
nil
,
ErrLinkNotFound
}
// GetLinkedProtoNode returns a copy of the ProtoNode with the given name.
func
(
n
*
ProtoNode
)
GetLinkedProtoNode
(
ctx
context
.
Context
,
ds
node
.
DAGService
,
name
string
)
(
*
ProtoNode
,
error
)
{
nd
,
err
:=
n
.
GetLinkedNode
(
ctx
,
ds
,
name
)
if
err
!=
nil
{
...
...
@@ -174,6 +175,7 @@ func (n *ProtoNode) GetLinkedProtoNode(ctx context.Context, ds node.DAGService,
return
pbnd
,
nil
}
// GetLinkedNode returns a copy of the IPLD Node with the given name.
func
(
n
*
ProtoNode
)
GetLinkedNode
(
ctx
context
.
Context
,
ds
node
.
DAGService
,
name
string
)
(
node
.
Node
,
error
)
{
lnk
,
err
:=
n
.
GetNodeLink
(
name
)
if
err
!=
nil
{
...
...
test/utils.go
View file @
e787a157
...
...
@@ -11,10 +11,12 @@ import (
node
"gx/ipfs/Qme5bWv7wtjUNGsK2BNGVUFPKiuxWrsqrtvYwCLRw8YFES/go-ipld-format"
)
// Mock returns a new thread-safe, mock DAGService.
func
Mock
()
node
.
DAGService
{
return
dag
.
NewDAGService
(
Bserv
())
}
// Bserv returns a new, thread-safe, mock BlockService.
func
Bserv
()
bsrv
.
BlockService
{
bstore
:=
blockstore
.
NewBlockstore
(
dssync
.
MutexWrap
(
ds
.
NewMapDatastore
()))
return
bsrv
.
New
(
bstore
,
offline
.
Exchange
(
bstore
))
...
...
utils/diff.go
View file @
e787a157
...
...
@@ -37,6 +37,7 @@ func (c *Change) String() string {
}
}
// ApplyChange applies the requested changes to the given node in the given dag.
func
ApplyChange
(
ctx
context
.
Context
,
ds
node
.
DAGService
,
nd
*
dag
.
ProtoNode
,
cs
[]
*
Change
)
(
*
dag
.
ProtoNode
,
error
)
{
e
:=
NewDagEditor
(
nd
,
ds
)
for
_
,
c
:=
range
cs
{
...
...
utils/utils.go
View file @
e787a157
...
...
@@ -27,6 +27,7 @@ type Editor struct {
src
node
.
DAGService
}
// NewMemoryDagService returns a new, thread-safe in-memory DAGService.
func
NewMemoryDagService
()
node
.
DAGService
{
// build mem-datastore for editor's intermediary nodes
bs
:=
bstore
.
NewBlockstore
(
syncds
.
MutexWrap
(
ds
.
NewMapDatastore
()))
...
...
@@ -34,7 +35,10 @@ func NewMemoryDagService() node.DAGService {
return
dag
.
NewDAGService
(
bsrv
)
}
// root is the node to be modified, source is the dagstore to pull nodes from (optional)
// NewDagEditor returns an ProtoNode editor.
//
// * root is the node to be modified
// * source is the dagstore to pull nodes from (optional)
func
NewDagEditor
(
root
*
dag
.
ProtoNode
,
source
node
.
DAGService
)
*
Editor
{
return
&
Editor
{
root
:
root
,
...
...
@@ -43,17 +47,19 @@ func NewDagEditor(root *dag.ProtoNode, source node.DAGService) *Editor {
}
}
// GetNode returns the a copy of the root node being edited.
func
(
e
*
Editor
)
GetNode
()
*
dag
.
ProtoNode
{
return
e
.
root
.
Copy
()
.
(
*
dag
.
ProtoNode
)
}
// GetDagService returns the DAGService used by this editor.
func
(
e
*
Editor
)
GetDagService
()
node
.
DAGService
{
return
e
.
tmp
}
func
addLink
(
ctx
context
.
Context
,
ds
node
.
DAGService
,
root
*
dag
.
ProtoNode
,
childname
string
,
childnd
node
.
Node
)
(
*
dag
.
ProtoNode
,
error
)
{
if
childname
==
""
{
return
nil
,
errors
.
New
(
"cannot create link with no name
!
"
)
return
nil
,
errors
.
New
(
"cannot create link with no name"
)
}
// ensure that the node we are adding is in the dagservice
...
...
@@ -188,6 +194,8 @@ func (e *Editor) rmLink(ctx context.Context, root *dag.ProtoNode, path []string)
return
root
,
nil
}
// Finalize writes the new DAG to the given DAGService and returns the modified
// root node.
func
(
e
*
Editor
)
Finalize
(
ctx
context
.
Context
,
ds
node
.
DAGService
)
(
*
dag
.
ProtoNode
,
error
)
{
nd
:=
e
.
GetNode
()
err
:=
copyDag
(
ctx
,
nd
,
e
.
tmp
,
ds
)
...
...
utils/utils_test.go
View file @
e787a157
...
...
@@ -70,8 +70,8 @@ func TestInsertNode(t *testing.T) {
testInsert
(
t
,
e
,
"a/b/c/d/f"
,
"baz"
,
true
,
""
)
testInsert
(
t
,
e
,
"a/b/c/d/f"
,
"bar"
,
true
,
""
)
testInsert
(
t
,
e
,
""
,
"bar"
,
true
,
"cannot create link with no name
!
"
)
testInsert
(
t
,
e
,
"////"
,
"slashes"
,
true
,
"cannot create link with no name
!
"
)
testInsert
(
t
,
e
,
""
,
"bar"
,
true
,
"cannot create link with no name"
)
testInsert
(
t
,
e
,
"////"
,
"slashes"
,
true
,
"cannot create link with no name"
)
c
:=
e
.
GetNode
()
.
Cid
()
...
...
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