traverse.go 4.63 KB
Newer Older
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
1 2 3 4
// Package traverse provides merkledag traversal functions
package traverse

import (
5
	"context"
Jeromy's avatar
Jeromy committed
6
	"errors"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
7

Steven Allen's avatar
Steven Allen committed
8
	ipld "gx/ipfs/QmZtNq8dArGfnpCZfx2pUNY7UcjGhVp5qqwQ4hH6mpTMRQ/go-ipld-format"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
9 10 11 12 13
)

// Order is an identifier for traversal algorithm orders
type Order int

14
// These constants define different traversing methods
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
15
const (
16 17 18 19 20 21
	// DFSPre defines depth-first pre-order
	DFSPre Order = iota
	// DFSPost defines depth-first post-order
	DFSPost
	// BFS defines breadth-first order
	BFS
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
22 23 24 25
)

// Options specifies a series of traversal options
type Options struct {
26
	DAG     ipld.NodeGetter // the dagservice to fetch nodes
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
27 28 29 30 31 32 33 34 35
	Order   Order           // what order to traverse in
	Func    Func            // the function to perform at each step
	ErrFunc ErrFunc         // see ErrFunc. Optional

	SkipDuplicates bool // whether to skip duplicate nodes
}

// State is a current traversal state
type State struct {
36
	Node  ipld.Node
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
37 38 39 40 41 42 43 44
	Depth int
}

type traversal struct {
	opts Options
	seen map[string]struct{}
}

45
func (t *traversal) shouldSkip(n ipld.Node) (bool, error) {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
46
	if t.opts.SkipDuplicates {
47 48
		k := n.Cid()
		if _, found := t.seen[k.KeyString()]; found {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
49 50
			return true, nil
		}
51
		t.seen[k.KeyString()] = struct{}{}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
52 53 54 55 56 57 58 59 60 61 62 63 64
	}

	return false, nil
}

func (t *traversal) callFunc(next State) error {
	return t.opts.Func(next)
}

// getNode returns the node for link. If it return an error,
// stop processing. if it returns a nil node, just skip it.
//
// the error handling is a little complicated.
65
func (t *traversal) getNode(link *ipld.Link) (ipld.Node, error) {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
66

67
	getNode := func(l *ipld.Link) (ipld.Node, error) {
68
		next, err := l.GetNode(context.TODO(), t.opts.DAG)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
		if err != nil {
			return nil, err
		}

		skip, err := t.shouldSkip(next)
		if skip {
			next = nil
		}
		return next, err
	}

	next, err := getNode(link)
	if err != nil && t.opts.ErrFunc != nil { // attempt recovery.
		err = t.opts.ErrFunc(err)
		next = nil // skip regardless
	}
	return next, err
}

// Func is the type of the function called for each dag.Node visited by Traverse.
// The traversal argument contains the current traversal state.
// If an error is returned, processing stops.
type Func func(current State) error

93 94 95
// ErrFunc is provided to handle problems when walking to the Node. Traverse
// will call ErrFunc with the error encountered. ErrFunc can decide how to
// handle that error, and return an error back to Traversal with how to proceed:
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
96 97 98 99 100 101 102 103 104
//   * nil - skip the Node and its children, but continue processing
//   * all other errors halt processing immediately.
//
// If ErrFunc is nil, Traversal will stop, as if:
//
//   opts.ErrFunc = func(err error) { return err }
//
type ErrFunc func(err error) error

105 106
// Traverse initiates a DAG traversal with the given options starting at
// the given root.
107
func Traverse(root ipld.Node, o Options) error {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
	t := traversal{
		opts: o,
		seen: map[string]struct{}{},
	}

	state := State{
		Node:  root,
		Depth: 0,
	}

	switch o.Order {
	default:
		return dfsPreTraverse(state, &t)
	case DFSPre:
		return dfsPreTraverse(state, &t)
	case DFSPost:
		return dfsPostTraverse(state, &t)
	case BFS:
		return bfsTraverse(state, &t)
	}
}

type dfsFunc func(state State, t *traversal) error

func dfsPreTraverse(state State, t *traversal) error {
	if err := t.callFunc(state); err != nil {
		return err
	}
136
	return dfsDescend(dfsPreTraverse, state, t)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
137 138 139 140 141 142
}

func dfsPostTraverse(state State, t *traversal) error {
	if err := dfsDescend(dfsPostTraverse, state, t); err != nil {
		return err
	}
143
	return t.callFunc(state)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
144 145 146
}

func dfsDescend(df dfsFunc, curr State, t *traversal) error {
147
	for _, l := range curr.Node.Links() {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
		node, err := t.getNode(l)
		if err != nil {
			return err
		}
		if node == nil { // skip
			continue
		}

		next := State{
			Node:  node,
			Depth: curr.Depth + 1,
		}
		if err := df(next, t); err != nil {
			return err
		}
	}
	return nil
}

func bfsTraverse(root State, t *traversal) error {

	if skip, err := t.shouldSkip(root.Node); skip || err != nil {
		return err
	}

	var q queue
	q.enq(root)
	for q.len() > 0 {
		curr := q.deq()
		if curr.Node == nil {
			return errors.New("failed to dequeue though queue not empty")
		}

		// call user's func
		if err := t.callFunc(curr); err != nil {
			return err
		}

186
		for _, l := range curr.Node.Links() {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
			node, err := t.getNode(l)
			if err != nil {
				return err
			}
			if node == nil { // skip
				continue
			}

			q.enq(State{
				Node:  node,
				Depth: curr.Depth + 1,
			})
		}
	}
	return nil
}

type queue struct {
	s []State
}

func (q *queue) enq(n State) {
	q.s = append(q.s, n)
}

func (q *queue) deq() State {
	if len(q.s) < 1 {
		return State{}
	}
	n := q.s[0]
	q.s = q.s[1:]
	return n
}

func (q *queue) len() int {
	return len(q.s)
}