node.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. package iradix
  2. import (
  3. "bytes"
  4. "sort"
  5. )
  6. // WalkFn is used when walking the tree. Takes a
  7. // key and value, returning if iteration should
  8. // be terminated.
  9. type WalkFn func(k []byte, v interface{}) bool
  10. // leafNode is used to represent a value
  11. type leafNode struct {
  12. mutateCh chan struct{}
  13. key []byte
  14. val interface{}
  15. }
  16. // edge is used to represent an edge node
  17. type edge struct {
  18. label byte
  19. node *Node
  20. }
  21. // Node is an immutable node in the radix tree
  22. type Node struct {
  23. // mutateCh is closed if this node is modified
  24. mutateCh chan struct{}
  25. // leaf is used to store possible leaf
  26. leaf *leafNode
  27. // prefix is the common prefix we ignore
  28. prefix []byte
  29. // Edges should be stored in-order for iteration.
  30. // We avoid a fully materialized slice to save memory,
  31. // since in most cases we expect to be sparse
  32. edges edges
  33. }
  34. func (n *Node) isLeaf() bool {
  35. return n.leaf != nil
  36. }
  37. func (n *Node) addEdge(e edge) {
  38. num := len(n.edges)
  39. idx := sort.Search(num, func(i int) bool {
  40. return n.edges[i].label >= e.label
  41. })
  42. n.edges = append(n.edges, e)
  43. if idx != num {
  44. copy(n.edges[idx+1:], n.edges[idx:num])
  45. n.edges[idx] = e
  46. }
  47. }
  48. func (n *Node) replaceEdge(e edge) {
  49. num := len(n.edges)
  50. idx := sort.Search(num, func(i int) bool {
  51. return n.edges[i].label >= e.label
  52. })
  53. if idx < num && n.edges[idx].label == e.label {
  54. n.edges[idx].node = e.node
  55. return
  56. }
  57. panic("replacing missing edge")
  58. }
  59. func (n *Node) getEdge(label byte) (int, *Node) {
  60. num := len(n.edges)
  61. idx := sort.Search(num, func(i int) bool {
  62. return n.edges[i].label >= label
  63. })
  64. if idx < num && n.edges[idx].label == label {
  65. return idx, n.edges[idx].node
  66. }
  67. return -1, nil
  68. }
  69. func (n *Node) delEdge(label byte) {
  70. num := len(n.edges)
  71. idx := sort.Search(num, func(i int) bool {
  72. return n.edges[i].label >= label
  73. })
  74. if idx < num && n.edges[idx].label == label {
  75. copy(n.edges[idx:], n.edges[idx+1:])
  76. n.edges[len(n.edges)-1] = edge{}
  77. n.edges = n.edges[:len(n.edges)-1]
  78. }
  79. }
  80. func (n *Node) GetWatch(k []byte) (<-chan struct{}, interface{}, bool) {
  81. search := k
  82. watch := n.mutateCh
  83. for {
  84. // Check for key exhaustion
  85. if len(search) == 0 {
  86. if n.isLeaf() {
  87. return n.leaf.mutateCh, n.leaf.val, true
  88. }
  89. break
  90. }
  91. // Look for an edge
  92. _, n = n.getEdge(search[0])
  93. if n == nil {
  94. break
  95. }
  96. // Update to the finest granularity as the search makes progress
  97. watch = n.mutateCh
  98. // Consume the search prefix
  99. if bytes.HasPrefix(search, n.prefix) {
  100. search = search[len(n.prefix):]
  101. } else {
  102. break
  103. }
  104. }
  105. return watch, nil, false
  106. }
  107. func (n *Node) Get(k []byte) (interface{}, bool) {
  108. _, val, ok := n.GetWatch(k)
  109. return val, ok
  110. }
  111. // LongestPrefix is like Get, but instead of an
  112. // exact match, it will return the longest prefix match.
  113. func (n *Node) LongestPrefix(k []byte) ([]byte, interface{}, bool) {
  114. var last *leafNode
  115. search := k
  116. for {
  117. // Look for a leaf node
  118. if n.isLeaf() {
  119. last = n.leaf
  120. }
  121. // Check for key exhaution
  122. if len(search) == 0 {
  123. break
  124. }
  125. // Look for an edge
  126. _, n = n.getEdge(search[0])
  127. if n == nil {
  128. break
  129. }
  130. // Consume the search prefix
  131. if bytes.HasPrefix(search, n.prefix) {
  132. search = search[len(n.prefix):]
  133. } else {
  134. break
  135. }
  136. }
  137. if last != nil {
  138. return last.key, last.val, true
  139. }
  140. return nil, nil, false
  141. }
  142. // Minimum is used to return the minimum value in the tree
  143. func (n *Node) Minimum() ([]byte, interface{}, bool) {
  144. for {
  145. if n.isLeaf() {
  146. return n.leaf.key, n.leaf.val, true
  147. }
  148. if len(n.edges) > 0 {
  149. n = n.edges[0].node
  150. } else {
  151. break
  152. }
  153. }
  154. return nil, nil, false
  155. }
  156. // Maximum is used to return the maximum value in the tree
  157. func (n *Node) Maximum() ([]byte, interface{}, bool) {
  158. for {
  159. if num := len(n.edges); num > 0 {
  160. n = n.edges[num-1].node
  161. continue
  162. }
  163. if n.isLeaf() {
  164. return n.leaf.key, n.leaf.val, true
  165. } else {
  166. break
  167. }
  168. }
  169. return nil, nil, false
  170. }
  171. // Iterator is used to return an iterator at
  172. // the given node to walk the tree
  173. func (n *Node) Iterator() *Iterator {
  174. return &Iterator{node: n}
  175. }
  176. // rawIterator is used to return a raw iterator at the given node to walk the
  177. // tree.
  178. func (n *Node) rawIterator() *rawIterator {
  179. iter := &rawIterator{node: n}
  180. iter.Next()
  181. return iter
  182. }
  183. // Walk is used to walk the tree
  184. func (n *Node) Walk(fn WalkFn) {
  185. recursiveWalk(n, fn)
  186. }
  187. // WalkPrefix is used to walk the tree under a prefix
  188. func (n *Node) WalkPrefix(prefix []byte, fn WalkFn) {
  189. search := prefix
  190. for {
  191. // Check for key exhaution
  192. if len(search) == 0 {
  193. recursiveWalk(n, fn)
  194. return
  195. }
  196. // Look for an edge
  197. _, n = n.getEdge(search[0])
  198. if n == nil {
  199. break
  200. }
  201. // Consume the search prefix
  202. if bytes.HasPrefix(search, n.prefix) {
  203. search = search[len(n.prefix):]
  204. } else if bytes.HasPrefix(n.prefix, search) {
  205. // Child may be under our search prefix
  206. recursiveWalk(n, fn)
  207. return
  208. } else {
  209. break
  210. }
  211. }
  212. }
  213. // WalkPath is used to walk the tree, but only visiting nodes
  214. // from the root down to a given leaf. Where WalkPrefix walks
  215. // all the entries *under* the given prefix, this walks the
  216. // entries *above* the given prefix.
  217. func (n *Node) WalkPath(path []byte, fn WalkFn) {
  218. search := path
  219. for {
  220. // Visit the leaf values if any
  221. if n.leaf != nil && fn(n.leaf.key, n.leaf.val) {
  222. return
  223. }
  224. // Check for key exhaution
  225. if len(search) == 0 {
  226. return
  227. }
  228. // Look for an edge
  229. _, n = n.getEdge(search[0])
  230. if n == nil {
  231. return
  232. }
  233. // Consume the search prefix
  234. if bytes.HasPrefix(search, n.prefix) {
  235. search = search[len(n.prefix):]
  236. } else {
  237. break
  238. }
  239. }
  240. }
  241. // recursiveWalk is used to do a pre-order walk of a node
  242. // recursively. Returns true if the walk should be aborted
  243. func recursiveWalk(n *Node, fn WalkFn) bool {
  244. // Visit the leaf values if any
  245. if n.leaf != nil && fn(n.leaf.key, n.leaf.val) {
  246. return true
  247. }
  248. // Recurse on the children
  249. for _, e := range n.edges {
  250. if recursiveWalk(e.node, fn) {
  251. return true
  252. }
  253. }
  254. return false
  255. }