Commit 44a78014 authored by Steven Allen's avatar Steven Allen

add contexts to Add/Remove methods

We'll need these for slower/remote datastores.
parent f10b5dd9
package format package format
import ( import (
"context"
"runtime" "runtime"
cid "github.com/ipfs/go-cid" cid "github.com/ipfs/go-cid"
...@@ -71,7 +72,7 @@ func (t *Batch) asyncCommit() { ...@@ -71,7 +72,7 @@ func (t *Batch) asyncCommit() {
} }
} }
go func(b []Node) { go func(b []Node) {
err := t.ds.AddMany(b) err := t.ds.AddMany(context.Background(), b)
t.commitResults <- err t.commitResults <- err
}(t.nodes) }(t.nodes)
......
...@@ -41,14 +41,14 @@ func (d *testDag) GetMany(ctx context.Context, cids []*cid.Cid) <-chan *NodeOpti ...@@ -41,14 +41,14 @@ func (d *testDag) GetMany(ctx context.Context, cids []*cid.Cid) <-chan *NodeOpti
return out return out
} }
func (d *testDag) Add(node Node) error { func (d *testDag) Add(ctx context.Context, node Node) error {
d.mu.Lock() d.mu.Lock()
defer d.mu.Unlock() defer d.mu.Unlock()
d.nodes[node.Cid().KeyString()] = node d.nodes[node.Cid().KeyString()] = node
return nil return nil
} }
func (d *testDag) AddMany(nodes []Node) error { func (d *testDag) AddMany(ctx context.Context, nodes []Node) error {
d.mu.Lock() d.mu.Lock()
defer d.mu.Unlock() defer d.mu.Unlock()
for _, n := range nodes { for _, n := range nodes {
...@@ -57,14 +57,14 @@ func (d *testDag) AddMany(nodes []Node) error { ...@@ -57,14 +57,14 @@ func (d *testDag) AddMany(nodes []Node) error {
return nil return nil
} }
func (d *testDag) Remove(c *cid.Cid) error { func (d *testDag) Remove(ctx context.Context, c *cid.Cid) error {
d.mu.Lock() d.mu.Lock()
defer d.mu.Unlock() defer d.mu.Unlock()
delete(d.nodes, c.KeyString()) delete(d.nodes, c.KeyString())
return nil return nil
} }
func (d *testDag) RemoveMany(cids []*cid.Cid) error { func (d *testDag) RemoveMany(ctx context.Context, cids []*cid.Cid) error {
d.mu.Lock() d.mu.Lock()
defer d.mu.Unlock() defer d.mu.Unlock()
for _, c := range cids { for _, c := range cids {
......
...@@ -43,21 +43,21 @@ type DAGService interface { ...@@ -43,21 +43,21 @@ type DAGService interface {
NodeGetter NodeGetter
// Add adds a node to this DAG. // Add adds a node to this DAG.
Add(Node) error Add(context.Context, Node) error
// Remove removes a node from this DAG. // Remove removes a node from this DAG.
// //
// Remove returns no error if the requested node is not present in this DAG. // Remove returns no error if the requested node is not present in this DAG.
Remove(*cid.Cid) error Remove(context.Context, *cid.Cid) error
// AddMany adds many nodes to this DAG. // AddMany adds many nodes to this DAG.
// //
// Consider using NewBatch instead of calling this directly if you need // Consider using NewBatch instead of calling this directly if you need
// to add an unbounded number of nodes to avoid buffering too much. // to add an unbounded number of nodes to avoid buffering too much.
AddMany([]Node) error AddMany(context.Context, []Node) error
// RemoveMany removes many nodes from this DAG. // RemoveMany removes many nodes from this DAG.
// //
// It returns success even if the nodes were not present in the DAG. // It returns success even if the nodes were not present in the DAG.
RemoveMany([]*cid.Cid) error RemoveMany(context.Context, []*cid.Cid) error
} }
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