Commit b5b61991 authored by Kristoffer Ström's avatar Kristoffer Ström

Add additional link manipulation functions

// AddRawLink adds a link to this node
AddRawLink(name string, lnk *Link) error

// Return a copy of the link with given name
GetNodeLink(name string) (*Link, error)
parent c31a3536
...@@ -88,18 +88,21 @@ func (l *Link) GetNode(serv DAGService) (*Node, error) { ...@@ -88,18 +88,21 @@ func (l *Link) GetNode(serv DAGService) (*Node, error) {
// AddNodeLink adds a link to another node. // AddNodeLink adds a link to another node.
func (n *Node) AddNodeLink(name string, that *Node) error { func (n *Node) AddNodeLink(name string, that *Node) error {
n.encoded = nil n.encoded = nil
lnk, err := MakeLink(that) lnk, err := MakeLink(that)
lnk.Name = name
lnk.Node = that
if err != nil { if err != nil {
return err return err
} }
lnk.Name = name
lnk.Node = that
n.Links = append(n.Links, lnk) n.AddRawLink(name, lnk)
return nil return nil
} }
// AddNodeLink adds a link to another node. without keeping a reference to // AddNodeLinkClean adds a link to another node. without keeping a reference to
// the child node // the child node
func (n *Node) AddNodeLinkClean(name string, that *Node) error { func (n *Node) AddNodeLinkClean(name string, that *Node) error {
n.encoded = nil n.encoded = nil
...@@ -107,9 +110,21 @@ func (n *Node) AddNodeLinkClean(name string, that *Node) error { ...@@ -107,9 +110,21 @@ func (n *Node) AddNodeLinkClean(name string, that *Node) error {
if err != nil { if err != nil {
return err return err
} }
lnk.Name = name n.AddRawLink(name, lnk)
return nil
}
// AddRawLink adds a copy of a link to this node
func (n *Node) AddRawLink(name string, l *Link) error {
n.encoded = nil
n.Links = append(n.Links, &Link{
Name: name,
Size: l.Size,
Hash: l.Hash,
Node: l.Node,
})
n.Links = append(n.Links, lnk)
return nil return nil
} }
...@@ -125,6 +140,21 @@ func (n *Node) RemoveNodeLink(name string) error { ...@@ -125,6 +140,21 @@ func (n *Node) RemoveNodeLink(name string) error {
return ErrNotFound return ErrNotFound
} }
// Return a copy of the link with given name
func (n *Node) GetNodeLink(name string) (*Link, error) {
for _, l := range n.Links {
if l.Name == name {
return &Link{
Name: l.Name,
Size: l.Size,
Hash: l.Hash,
Node: l.Node,
}, nil
}
}
return nil, ErrNotFound
}
// Copy returns a copy of the node. // Copy returns a copy of the node.
// NOTE: does not make copies of Node objects in the links. // NOTE: does not make copies of Node objects in the links.
func (n *Node) Copy() *Node { func (n *Node) Copy() *Node {
......
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