@cryptotaxi247 / kubo / commits / 20383472c

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)

Kristoffer Ström committed Apr 5, 2015 at 04:05 UTC 20383472c7f933d49f0617e0fd3bc0ae6190083b
1 file changed +36 -6
merkledag/node.go
+36 -6
@@ -88,18 +88,21 @@ func (l *Link) GetNode(serv DAGService) (*Node, error) {
88 // AddNodeLink adds a link to another node.
89 func (n *Node) AddNodeLink(name string, that *Node) error {
90 n.encoded = nil
91 +
92 lnk, err := MakeLink(that)
93 +
94 + lnk.Name = name
95 + lnk.Node = that
96 if err != nil {
97 return err
98 }
95 - lnk.Name = name
96 - lnk.Node = that
99
98 - n.Links = append(n.Links, lnk)
100 + n.AddRawLink(name, lnk)
101 +
102 return nil
103 }
104
102 -// AddNodeLink adds a link to another node. without keeping a reference to
105 +// AddNodeLinkClean adds a link to another node. without keeping a reference to
106 // the child node
107 func (n *Node) AddNodeLinkClean(name string, that *Node) error {
108 n.encoded = nil
@@ -107,9 +110,21 @@ func (n *Node) AddNodeLinkClean(name string, that *Node) error {
110 if err != nil {
111 return err
112 }
110 - lnk.Name = name
113 + n.AddRawLink(name, lnk)
114 +
115 + return nil
116 +}
117 +
118 +// AddRawLink adds a copy of a link to this node
119 +func (n *Node) AddRawLink(name string, l *Link) error {
120 + n.encoded = nil
121 + n.Links = append(n.Links, &Link{
122 + Name: name,
123 + Size: l.Size,
124 + Hash: l.Hash,
125 + Node: l.Node,
126 + })
127
112 - n.Links = append(n.Links, lnk)
128 return nil
129 }
130
@@ -125,6 +140,21 @@ func (n *Node) RemoveNodeLink(name string) error {
140 return ErrNotFound
141 }
142
143 +// Return a copy of the link with given name
144 +func (n *Node) GetNodeLink(name string) (*Link, error) {
145 + for _, l := range n.Links {
146 + if l.Name == name {
147 + return &Link{
148 + Name: l.Name,
149 + Size: l.Size,
150 + Hash: l.Hash,
151 + Node: l.Node,
152 + }, nil
153 + }
154 + }
155 + return nil, ErrNotFound
156 +}
157 +
158 // Copy returns a copy of the node.
159 // NOTE: does not make copies of Node objects in the links.
160 func (n *Node) Copy() *Node {