@cryptotaxi247 / kubo / commits / 5b0be85b6

change FindLink to FindLinks

cc @maybebtc

Jeromy committed Dec 15, 2014 at 01:03 UTC 5b0be85b6b068b8dfcf6259971d12dabf38b9699
1 file changed +9 -18
merkledag/merkledag.go
+9 -18
@@ -292,13 +292,14 @@ func FetchGraph(ctx context.Context, root *Node, serv DAGService) chan struct{}
292
293 // Searches this nodes links for one to the given key,
294 // returns the index of said link
295 -func FindLink(n *Node, k u.Key, found []*Node) (int, error) {
295 +func FindLinks(n *Node, k u.Key) []int {
296 + var out []int
297 for i, lnk := range n.Links {
297 - if u.Key(lnk.Hash) == k && found[i] == nil {
298 - return i, nil
298 + if u.Key(lnk.Hash) == k {
299 + out = append(out, i)
300 }
301 }
301 - return -1, u.ErrNotFound
302 + return out
303 }
304
305 // GetDAG will fill out all of the links of the given Node.
@@ -318,12 +319,6 @@ func (ds *dagService) GetDAG(ctx context.Context, root *Node) <-chan *Node {
319 nodes := make([]*Node, len(root.Links))
320 next := 0
321 for blk := range blkchan {
321 - i, err := FindLink(root, blk.Key(), nodes)
322 - if err != nil {
323 - // NB: can only occur as a result of programmer error
324 - panic("Received block that wasnt in this nodes links!")
325 - }
326 -
322 nd, err := Decoded(blk.Data)
323 if err != nil {
324 // NB: can occur in normal situations, with improperly formatted
@@ -331,16 +326,12 @@ func (ds *dagService) GetDAG(ctx context.Context, root *Node) <-chan *Node {
326 log.Error("Got back bad block!")
327 break
328 }
334 - nodes[i] = nd
335 - for { //Check for duplicate links
336 - ni, err := FindLink(root, blk.Key(), nodes)
337 - if err != nil {
338 - break
339 - }
340 - nodes[ni] = nd
329 + is := FindLinks(root, blk.Key())
330 + for _, i := range is {
331 + nodes[i] = nd
332 }
333
343 - if next == i {
334 + if next == is[0] {
335 sig <- nd
336 next++
337 for ; next < len(nodes) && nodes[next] != nil; next++ {