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 { ...@@ -20,6 +20,7 @@ type DAGService interface {
AddRecursive(*Node) error AddRecursive(*Node) error
Get(context.Context, key.Key) (*Node, error) Get(context.Context, key.Key) (*Node, error)
Remove(*Node) error Remove(*Node) error
RemoveRecursive(*Node) error
// GetDAG returns, in order, all the single leve child // GetDAG returns, in order, all the single leve child
// nodes of the passed in node. // nodes of the passed in node.
...@@ -107,12 +108,20 @@ func (n *dagService) Get(ctx context.Context, k key.Key) (*Node, error) { ...@@ -107,12 +108,20 @@ 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 // 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 { for _, l := range nd.Links {
if l.Node != nil { if l.Node != nil {
n.Remove(l.Node) n.RemoveRecursive(l.Node)
}
} }
k, err := nd.Key()
if err != nil {
return err
} }
return n.Blocks.DeleteBlock(k)
}
func (n *dagService) Remove(nd *Node) error {
k, err := nd.Key() k, err := nd.Key()
if err != nil { if err != nil {
return err return err
......
...@@ -40,6 +40,8 @@ func addLink(ctx context.Context, ds dag.DAGService, root *dag.Node, childname s ...@@ -40,6 +40,8 @@ func addLink(ctx context.Context, ds dag.DAGService, root *dag.Node, childname s
return nil, err return nil, err
} }
_ = ds.Remove(root)
// ensure no link with that name already exists // ensure no link with that name already exists
_ = root.RemoveNodeLink(childname) // ignore error, only option is ErrNotFound _ = 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 ...@@ -83,6 +85,8 @@ func insertNodeAtPath(ctx context.Context, ds dag.DAGService, root *dag.Node, pa
return nil, err return nil, err
} }
_ = ds.Remove(root)
_ = root.RemoveNodeLink(path[0]) _ = root.RemoveNodeLink(path[0])
err = root.AddNodeLinkClean(path[0], ndprime) err = root.AddNodeLinkClean(path[0], ndprime)
if err != nil { if err != nil {
...@@ -133,6 +137,8 @@ func rmLink(ctx context.Context, ds dag.DAGService, root *dag.Node, path []strin ...@@ -133,6 +137,8 @@ func rmLink(ctx context.Context, ds dag.DAGService, root *dag.Node, path []strin
return nil, err return nil, err
} }
_ = ds.Remove(root)
_ = root.RemoveNodeLink(path[0]) _ = root.RemoveNodeLink(path[0])
err = root.AddNodeLinkClean(path[0], nnode) err = root.AddNodeLinkClean(path[0], nnode)
if err != nil { 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