reverse_iter.go 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. package iradix
  2. import (
  3. "bytes"
  4. )
  5. // ReverseIterator is used to iterate over a set of nodes
  6. // in reverse in-order
  7. type ReverseIterator struct {
  8. i *Iterator
  9. // expandedParents stores the set of parent nodes whose relevant children have
  10. // already been pushed into the stack. This can happen during seek or during
  11. // iteration.
  12. //
  13. // Unlike forward iteration we need to recurse into children before we can
  14. // output the value stored in an internal leaf since all children are greater.
  15. // We use this to track whether we have already ensured all the children are
  16. // in the stack.
  17. expandedParents map[*Node]struct{}
  18. }
  19. // NewReverseIterator returns a new ReverseIterator at a node
  20. func NewReverseIterator(n *Node) *ReverseIterator {
  21. return &ReverseIterator{
  22. i: &Iterator{node: n},
  23. }
  24. }
  25. // SeekPrefixWatch is used to seek the iterator to a given prefix
  26. // and returns the watch channel of the finest granularity
  27. func (ri *ReverseIterator) SeekPrefixWatch(prefix []byte) (watch <-chan struct{}) {
  28. return ri.i.SeekPrefixWatch(prefix)
  29. }
  30. // SeekPrefix is used to seek the iterator to a given prefix
  31. func (ri *ReverseIterator) SeekPrefix(prefix []byte) {
  32. ri.i.SeekPrefixWatch(prefix)
  33. }
  34. // SeekReverseLowerBound is used to seek the iterator to the largest key that is
  35. // lower or equal to the given key. There is no watch variant as it's hard to
  36. // predict based on the radix structure which node(s) changes might affect the
  37. // result.
  38. func (ri *ReverseIterator) SeekReverseLowerBound(key []byte) {
  39. // Wipe the stack. Unlike Prefix iteration, we need to build the stack as we
  40. // go because we need only a subset of edges of many nodes in the path to the
  41. // leaf with the lower bound. Note that the iterator will still recurse into
  42. // children that we don't traverse on the way to the reverse lower bound as it
  43. // walks the stack.
  44. ri.i.stack = []edges{}
  45. // ri.i.node starts off in the common case as pointing to the root node of the
  46. // tree. By the time we return we have either found a lower bound and setup
  47. // the stack to traverse all larger keys, or we have not and the stack and
  48. // node should both be nil to prevent the iterator from assuming it is just
  49. // iterating the whole tree from the root node. Either way this needs to end
  50. // up as nil so just set it here.
  51. n := ri.i.node
  52. ri.i.node = nil
  53. search := key
  54. if ri.expandedParents == nil {
  55. ri.expandedParents = make(map[*Node]struct{})
  56. }
  57. found := func(n *Node) {
  58. ri.i.stack = append(ri.i.stack, edges{edge{node: n}})
  59. // We need to mark this node as expanded in advance too otherwise the
  60. // iterator will attempt to walk all of its children even though they are
  61. // greater than the lower bound we have found. We've expanded it in the
  62. // sense that all of its children that we want to walk are already in the
  63. // stack (i.e. none of them).
  64. ri.expandedParents[n] = struct{}{}
  65. }
  66. for {
  67. // Compare current prefix with the search key's same-length prefix.
  68. var prefixCmp int
  69. if len(n.prefix) < len(search) {
  70. prefixCmp = bytes.Compare(n.prefix, search[0:len(n.prefix)])
  71. } else {
  72. prefixCmp = bytes.Compare(n.prefix, search)
  73. }
  74. if prefixCmp < 0 {
  75. // Prefix is smaller than search prefix, that means there is no exact
  76. // match for the search key. But we are looking in reverse, so the reverse
  77. // lower bound will be the largest leaf under this subtree, since it is
  78. // the value that would come right before the current search key if it
  79. // were in the tree. So we need to follow the maximum path in this subtree
  80. // to find it. Note that this is exactly what the iterator will already do
  81. // if it finds a node in the stack that has _not_ been marked as expanded
  82. // so in this one case we don't call `found` and instead let the iterator
  83. // do the expansion and recursion through all the children.
  84. ri.i.stack = append(ri.i.stack, edges{edge{node: n}})
  85. return
  86. }
  87. if prefixCmp > 0 {
  88. // Prefix is larger than search prefix, or there is no prefix but we've
  89. // also exhausted the search key. Either way, that means there is no
  90. // reverse lower bound since nothing comes before our current search
  91. // prefix.
  92. return
  93. }
  94. // If this is a leaf, something needs to happen! Note that if it's a leaf
  95. // and prefixCmp was zero (which it must be to get here) then the leaf value
  96. // is either an exact match for the search, or it's lower. It can't be
  97. // greater.
  98. if n.isLeaf() {
  99. // Firstly, if it's an exact match, we're done!
  100. if bytes.Equal(n.leaf.key, key) {
  101. found(n)
  102. return
  103. }
  104. // It's not so this node's leaf value must be lower and could still be a
  105. // valid contender for reverse lower bound.
  106. // If it has no children then we are also done.
  107. if len(n.edges) == 0 {
  108. // This leaf is the lower bound.
  109. found(n)
  110. return
  111. }
  112. // Finally, this leaf is internal (has children) so we'll keep searching,
  113. // but we need to add it to the iterator's stack since it has a leaf value
  114. // that needs to be iterated over. It needs to be added to the stack
  115. // before its children below as it comes first.
  116. ri.i.stack = append(ri.i.stack, edges{edge{node: n}})
  117. // We also need to mark it as expanded since we'll be adding any of its
  118. // relevant children below and so don't want the iterator to re-add them
  119. // on its way back up the stack.
  120. ri.expandedParents[n] = struct{}{}
  121. }
  122. // Consume the search prefix. Note that this is safe because if n.prefix is
  123. // longer than the search slice prefixCmp would have been > 0 above and the
  124. // method would have already returned.
  125. search = search[len(n.prefix):]
  126. if len(search) == 0 {
  127. // We've exhausted the search key but we are not at a leaf. That means all
  128. // children are greater than the search key so a reverse lower bound
  129. // doesn't exist in this subtree. Note that there might still be one in
  130. // the whole radix tree by following a different path somewhere further
  131. // up. If that's the case then the iterator's stack will contain all the
  132. // smaller nodes already and Previous will walk through them correctly.
  133. return
  134. }
  135. // Otherwise, take the lower bound next edge.
  136. idx, lbNode := n.getLowerBoundEdge(search[0])
  137. // From here, we need to update the stack with all values lower than
  138. // the lower bound edge. Since getLowerBoundEdge() returns -1 when the
  139. // search prefix is larger than all edges, we need to place idx at the
  140. // last edge index so they can all be place in the stack, since they
  141. // come before our search prefix.
  142. if idx == -1 {
  143. idx = len(n.edges)
  144. }
  145. // Create stack edges for the all strictly lower edges in this node.
  146. if len(n.edges[:idx]) > 0 {
  147. ri.i.stack = append(ri.i.stack, n.edges[:idx])
  148. }
  149. // Exit if there's no lower bound edge. The stack will have the previous
  150. // nodes already.
  151. if lbNode == nil {
  152. return
  153. }
  154. // Recurse
  155. n = lbNode
  156. }
  157. }
  158. // Previous returns the previous node in reverse order
  159. func (ri *ReverseIterator) Previous() ([]byte, interface{}, bool) {
  160. // Initialize our stack if needed
  161. if ri.i.stack == nil && ri.i.node != nil {
  162. ri.i.stack = []edges{
  163. {
  164. edge{node: ri.i.node},
  165. },
  166. }
  167. }
  168. if ri.expandedParents == nil {
  169. ri.expandedParents = make(map[*Node]struct{})
  170. }
  171. for len(ri.i.stack) > 0 {
  172. // Inspect the last element of the stack
  173. n := len(ri.i.stack)
  174. last := ri.i.stack[n-1]
  175. m := len(last)
  176. elem := last[m-1].node
  177. _, alreadyExpanded := ri.expandedParents[elem]
  178. // If this is an internal node and we've not seen it already, we need to
  179. // leave it in the stack so we can return its possible leaf value _after_
  180. // we've recursed through all its children.
  181. if len(elem.edges) > 0 && !alreadyExpanded {
  182. // record that we've seen this node!
  183. ri.expandedParents[elem] = struct{}{}
  184. // push child edges onto stack and skip the rest of the loop to recurse
  185. // into the largest one.
  186. ri.i.stack = append(ri.i.stack, elem.edges)
  187. continue
  188. }
  189. // Remove the node from the stack
  190. if m > 1 {
  191. ri.i.stack[n-1] = last[:m-1]
  192. } else {
  193. ri.i.stack = ri.i.stack[:n-1]
  194. }
  195. // We don't need this state any more as it's no longer in the stack so we
  196. // won't visit it again
  197. if alreadyExpanded {
  198. delete(ri.expandedParents, elem)
  199. }
  200. // If this is a leaf, return it
  201. if elem.leaf != nil {
  202. return elem.leaf.key, elem.leaf.val, true
  203. }
  204. // it's not a leaf so keep walking the stack to find the previous leaf
  205. }
  206. return nil, nil, false
  207. }