Skip to content

Commit

Permalink
Add a NodeAdder interface and have Batch implement it
Browse files Browse the repository at this point in the history
This adds a NodeAdder interface (which partially implements
a DAGService), and adjusts the Batch type to satisfy it.
  • Loading branch information
hsanjuan committed Oct 25, 2018
1 parent 2f245e4 commit f48f39f
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 15 deletions.
2 changes: 1 addition & 1 deletion .gx/lastpubver
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.6.0: QmdDXJs4axxefSPgK6Y1QhpJWKuDPnGJiqgq4uncb4rFHL
0.7.0: QmdVNBLt7RMYnZwqBQJeexmSbTEDzERjBQUfs5McuPfEtB
14 changes: 11 additions & 3 deletions batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,13 @@ func (t *Batch) asyncCommit() {
}

// Add adds a node to the batch and commits the batch if necessary.
func (t *Batch) Add(nd Node) error {
func (t *Batch) Add(ctx context.Context, nd Node) error {
return t.AddMany(ctx, []Node{nd})
}

// Add many calls Add for every given Node, thus batching and
// commiting them as needed.
func (t *Batch) AddMany(ctx context.Context, nodes []Node) error {
if t.err != nil {
return t.err
}
Expand All @@ -115,8 +121,10 @@ func (t *Batch) Add(nd Node) error {
return t.err
}

t.nodes = append(t.nodes, nd)
t.size += len(nd.RawData())
t.nodes = append(t.nodes, nodes...)
for _, nd := range nodes {
t.size += len(nd.RawData())
}

if t.size > t.opts.maxSize || len(t.nodes) > t.opts.maxNodes {
t.asyncCommit()
Expand Down
2 changes: 1 addition & 1 deletion batch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func TestBatch(t *testing.T) {
// It would be great if we could use *many* different nodes here
// but we can't add any dependencies and I don't feel like adding
// any more testing code.
if err := b.Add(new(EmptyNode)); err != nil {
if err := b.Add(ctx, new(EmptyNode)); err != nil {
t.Fatal(err)
}
}
Expand Down
22 changes: 13 additions & 9 deletions merkledag.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,18 @@ type NodeGetter interface {
GetMany(context.Context, []cid.Cid) <-chan *NodeOption
}

// NodeAdder adds nodes to a DAG.
type NodeAdder interface {
// Add adds a node to this DAG.
Add(context.Context, Node) error

// AddMany adds many nodes to this DAG.
//
// Consider using the Batch NodeAdder (`NewBatch`) if you make
// extensive use of this function.
AddMany(context.Context, []Node) error
}

// NodeGetters can optionally implement this interface to make finding linked
// objects faster.
type LinkGetter interface {
Expand All @@ -41,21 +53,13 @@ type LinkGetter interface {
// DAGService is an IPFS Merkle DAG service.
type DAGService interface {
NodeGetter

// Add adds a node to this DAG.
Add(context.Context, Node) error
NodeAdder

// Remove removes a node from this DAG.
//
// Remove returns no error if the requested node is not present in this DAG.
Remove(context.Context, cid.Cid) error

// AddMany adds many nodes to this DAG.
//
// Consider using NewBatch instead of calling this directly if you need
// to add an unbounded number of nodes to avoid buffering too much.
AddMany(context.Context, []Node) error

// RemoveMany removes many nodes from this DAG.
//
// It returns success even if the nodes were not present in the DAG.
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@
"license": "",
"name": "go-ipld-format",
"releaseCmd": "git commit -a -m \"gx publish $VERSION\"",
"version": "0.6.0"
"version": "0.7.0"
}

0 comments on commit f48f39f

Please sign in to comment.