iterator.go 2.88 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
// Copyright 2014 The go-ethereum Authors
// This file is part of go-ethereum.
//
// go-ethereum is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// go-ethereum is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with go-ethereum.  If not, see <http://www.gnu.org/licenses/>.

obscuren's avatar
obscuren committed
17
package trie
obscuren's avatar
obscuren committed
18

19 20 21
import (
	"bytes"
)
obscuren's avatar
obscuren committed
22 23 24 25 26

type Iterator struct {
	trie *Trie

	Key   []byte
27
	Value []byte
obscuren's avatar
obscuren committed
28 29 30
}

func NewIterator(trie *Trie) *Iterator {
31
	return &Iterator{trie: trie, Key: nil}
obscuren's avatar
obscuren committed
32 33
}

34 35 36
func (self *Iterator) Next() bool {
	self.trie.mu.Lock()
	defer self.trie.mu.Unlock()
obscuren's avatar
obscuren committed
37

38 39 40 41 42 43
	isIterStart := false
	if self.Key == nil {
		isIterStart = true
		self.Key = make([]byte, 32)
	}

44
	key := RemTerm(CompactHexDecode(string(self.Key)))
45
	k := self.next(self.trie.root, key, isIterStart)
obscuren's avatar
obscuren committed
46

47
	self.Key = []byte(DecodeCompact(k))
obscuren's avatar
obscuren committed
48

49
	return len(k) > 0
obscuren's avatar
obscuren committed
50 51
}

52
func (self *Iterator) next(node Node, key []byte, isIterStart bool) []byte {
53
	if node == nil {
obscuren's avatar
obscuren committed
54
		return nil
55
	}
obscuren's avatar
obscuren committed
56

57 58 59
	switch node := node.(type) {
	case *FullNode:
		if len(key) > 0 {
60
			k := self.next(node.branch(key[0]), key[1:], isIterStart)
61 62
			if k != nil {
				return append([]byte{key[0]}, k...)
obscuren's avatar
obscuren committed
63 64 65
			}
		}

66
		var r byte
obscuren's avatar
obscuren committed
67 68 69 70 71
		if len(key) > 0 {
			r = key[0] + 1
		}

		for i := r; i < 16; i++ {
72 73 74
			k := self.key(node.branch(byte(i)))
			if k != nil {
				return append([]byte{i}, k...)
obscuren's avatar
obscuren committed
75 76 77
			}
		}

78 79 80
	case *ShortNode:
		k := RemTerm(node.Key())
		if vnode, ok := node.Value().(*ValueNode); ok {
81 82 83 84 85 86 87
			switch bytes.Compare([]byte(k), key) {
			case 0:
				if isIterStart {
					self.Value = vnode.Val()
					return k
				}
			case 1:
88
				self.Value = vnode.Val()
obscuren's avatar
obscuren committed
89 90 91
				return k
			}
		} else {
92 93
			cnode := node.Value()

obscuren's avatar
obscuren committed
94
			var ret []byte
95
			skey := key[len(k):]
obscuren's avatar
obscuren committed
96
			if BeginsWith(key, k) {
97
				ret = self.next(cnode, skey, isIterStart)
obscuren's avatar
obscuren committed
98
			} else if bytes.Compare(k, key[:len(k)]) > 0 {
obscuren's avatar
obscuren committed
99
				return self.key(node)
obscuren's avatar
obscuren committed
100 101 102 103 104 105 106 107 108 109 110
			}

			if ret != nil {
				return append(k, ret...)
			}
		}
	}

	return nil
}

111 112 113 114 115 116 117
func (self *Iterator) key(node Node) []byte {
	switch node := node.(type) {
	case *ShortNode:
		// Leaf node
		if vnode, ok := node.Value().(*ValueNode); ok {
			k := RemTerm(node.Key())
			self.Value = vnode.Val()
obscuren's avatar
obscuren committed
118

119 120 121 122 123 124 125 126
			return k
		} else {
			k := RemTerm(node.Key())
			return append(k, self.key(node.Value())...)
		}
	case *FullNode:
		if node.Value() != nil {
			self.Value = node.Value().(*ValueNode).Val()
obscuren's avatar
obscuren committed
127

128 129
			return []byte{16}
		}
obscuren's avatar
obscuren committed
130

131 132 133 134 135 136 137 138 139
		for i := 0; i < 16; i++ {
			k := self.key(node.branch(byte(i)))
			if k != nil {
				return append([]byte{byte(i)}, k...)
			}
		}
	}

	return nil
obscuren's avatar
obscuren committed
140
}