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
767a3634
Commit
767a3634
authored
Jun 26, 2015
by
Jeromy
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
use batching transaction interface from datastore
License: MIT Signed-off-by:
Jeromy
<
jeromyj@gmail.com
>
parent
306c9d1f
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
44 additions
and
0 deletions
+44
-0
merkledag.go
merkledag.go
+44
-0
No files found.
merkledag.go
View file @
767a3634
...
...
@@ -26,6 +26,8 @@ type DAGService interface {
// nodes of the passed in node.
GetDAG
(
context
.
Context
,
*
Node
)
[]
NodeGetter
GetNodes
(
context
.
Context
,
[]
key
.
Key
)
[]
NodeGetter
Batch
()
*
Batch
}
func
NewDAGService
(
bs
*
bserv
.
BlockService
)
DAGService
{
...
...
@@ -62,6 +64,10 @@ func (n *dagService) Add(nd *Node) (key.Key, error) {
return
n
.
Blocks
.
AddBlock
(
b
)
}
func
(
n
*
dagService
)
Batch
()
*
Batch
{
return
&
Batch
{
ds
:
n
,
MaxSize
:
8
*
1024
*
1024
}
}
// AddRecursive adds the given node and all child nodes to the BlockService
func
(
n
*
dagService
)
AddRecursive
(
nd
*
Node
)
error
{
_
,
err
:=
n
.
Add
(
nd
)
...
...
@@ -269,3 +275,41 @@ func (np *nodePromise) Get(ctx context.Context) (*Node, error) {
}
return
np
.
cache
,
nil
}
type
Batch
struct
{
ds
*
dagService
blocks
[]
*
blocks
.
Block
size
int
MaxSize
int
}
func
(
t
*
Batch
)
Add
(
nd
*
Node
)
(
key
.
Key
,
error
)
{
d
,
err
:=
nd
.
Encoded
(
false
)
if
err
!=
nil
{
return
""
,
err
}
b
:=
new
(
blocks
.
Block
)
b
.
Data
=
d
b
.
Multihash
,
err
=
nd
.
Multihash
()
if
err
!=
nil
{
return
""
,
err
}
k
:=
key
.
Key
(
b
.
Multihash
)
t
.
blocks
=
append
(
t
.
blocks
,
b
)
t
.
size
+=
len
(
b
.
Data
)
if
t
.
size
>
t
.
MaxSize
{
return
k
,
t
.
Commit
()
}
return
k
,
nil
}
func
(
t
*
Batch
)
Commit
()
error
{
_
,
err
:=
t
.
ds
.
Blocks
.
AddBlocks
(
t
.
blocks
)
t
.
blocks
=
nil
t
.
size
=
0
return
err
}
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