Commit b54c4fa0 authored by Jeromy's avatar Jeromy

improves memory usage of add

License: MIT
Signed-off-by: default avatarJeromy <jeromyj@gmail.com>
parent 64ec8e46
......@@ -20,6 +20,7 @@ type DAGService interface {
AddRecursive(*Node) error
Get(context.Context, key.Key) (*Node, error)
Remove(*Node) error
RemoveRecursive(*Node) error
// GetDAG returns, in order, all the single leve child
// nodes of the passed in node.
......@@ -107,10 +108,10 @@ func (n *dagService) Get(ctx context.Context, k key.Key) (*Node, error) {
}
// Remove deletes the given node and all of its children from the BlockService
func (n *dagService) Remove(nd *Node) error {
func (n *dagService) RemoveRecursive(nd *Node) error {
for _, l := range nd.Links {
if l.Node != nil {
n.Remove(l.Node)
n.RemoveRecursive(l.Node)
}
}
k, err := nd.Key()
......@@ -120,6 +121,14 @@ func (n *dagService) Remove(nd *Node) error {
return n.Blocks.DeleteBlock(k)
}
func (n *dagService) Remove(nd *Node) error {
k, err := nd.Key()
if err != nil {
return err
}
return n.Blocks.DeleteBlock(k)
}
// FetchGraph fetches all nodes that are children of the given node
func FetchGraph(ctx context.Context, root *Node, serv DAGService) error {
return EnumerateChildrenAsync(ctx, serv, root, key.NewKeySet())
......
......@@ -40,6 +40,8 @@ func addLink(ctx context.Context, ds dag.DAGService, root *dag.Node, childname s
return nil, err
}
_ = ds.Remove(root)
// ensure no link with that name already exists
_ = root.RemoveNodeLink(childname) // ignore error, only option is ErrNotFound
......@@ -83,6 +85,8 @@ func insertNodeAtPath(ctx context.Context, ds dag.DAGService, root *dag.Node, pa
return nil, err
}
_ = ds.Remove(root)
_ = root.RemoveNodeLink(path[0])
err = root.AddNodeLinkClean(path[0], ndprime)
if err != nil {
......@@ -133,6 +137,8 @@ func rmLink(ctx context.Context, ds dag.DAGService, root *dag.Node, path []strin
return nil, err
}
_ = ds.Remove(root)
_ = root.RemoveNodeLink(path[0])
err = root.AddNodeLinkClean(path[0], nnode)
if err != nil {
......
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