Unverified Commit c96181f0 authored by Hector Sanjuan's avatar Hector Sanjuan Committed by GitHub

Merge pull request #46 from ipfs/fix/batchds

Make Batch implement a NodeAdder interface
parents 2f245e49 4f06ec02
0.6.0: QmdDXJs4axxefSPgK6Y1QhpJWKuDPnGJiqgq4uncb4rFHL
0.7.0: QmdVNBLt7RMYnZwqBQJeexmSbTEDzERjBQUfs5McuPfEtB
......@@ -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
}
......@@ -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()
......
......@@ -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)
}
}
......
......@@ -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 {
......@@ -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.
......
......@@ -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"
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment