btree.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908
  1. // Copyright 2014 The b Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package b
  5. import (
  6. "fmt"
  7. "io"
  8. "sync"
  9. )
  10. const (
  11. kx = 32 //TODO benchmark tune this number if using custom key/value type(s).
  12. kd = 32 //TODO benchmark tune this number if using custom key/value type(s).
  13. )
  14. func init() {
  15. if kd < 1 {
  16. panic(fmt.Errorf("kd %d: out of range", kd))
  17. }
  18. if kx < 2 {
  19. panic(fmt.Errorf("kx %d: out of range", kx))
  20. }
  21. }
  22. var (
  23. btDPool = sync.Pool{New: func() interface{} { return &d{} }}
  24. btEPool = btEpool{sync.Pool{New: func() interface{} { return &Enumerator{} }}}
  25. btTPool = btTpool{sync.Pool{New: func() interface{} { return &Tree{} }}}
  26. btXPool = sync.Pool{New: func() interface{} { return &x{} }}
  27. )
  28. type btTpool struct{ sync.Pool }
  29. func (p *btTpool) get(cmp Cmp) *Tree {
  30. x := p.Get().(*Tree)
  31. x.cmp = cmp
  32. return x
  33. }
  34. type btEpool struct{ sync.Pool }
  35. func (p *btEpool) get(err error, hit bool, i int, k interface{} /*K*/, q *d, t *Tree, ver int64) *Enumerator {
  36. x := p.Get().(*Enumerator)
  37. x.err, x.hit, x.i, x.k, x.q, x.t, x.ver = err, hit, i, k, q, t, ver
  38. return x
  39. }
  40. type (
  41. // Cmp compares a and b. Return value is:
  42. //
  43. // < 0 if a < b
  44. // 0 if a == b
  45. // > 0 if a > b
  46. //
  47. Cmp func(a, b interface{} /*K*/) int
  48. d struct { // data page
  49. c int
  50. d [2*kd + 1]de
  51. n *d
  52. p *d
  53. }
  54. de struct { // d element
  55. k interface{} /*K*/
  56. v interface{} /*V*/
  57. }
  58. // Enumerator captures the state of enumerating a tree. It is returned
  59. // from the Seek* methods. The enumerator is aware of any mutations
  60. // made to the tree in the process of enumerating it and automatically
  61. // resumes the enumeration at the proper key, if possible.
  62. //
  63. // However, once an Enumerator returns io.EOF to signal "no more
  64. // items", it does no more attempt to "resync" on tree mutation(s). In
  65. // other words, io.EOF from an Enumerator is "sticky" (idempotent).
  66. Enumerator struct {
  67. err error
  68. hit bool
  69. i int
  70. k interface{} /*K*/
  71. q *d
  72. t *Tree
  73. ver int64
  74. }
  75. // Tree is a B+tree.
  76. Tree struct {
  77. c int
  78. cmp Cmp
  79. first *d
  80. last *d
  81. r interface{}
  82. ver int64
  83. }
  84. xe struct { // x element
  85. ch interface{}
  86. k interface{} /*K*/
  87. }
  88. x struct { // index page
  89. c int
  90. x [2*kx + 2]xe
  91. }
  92. )
  93. var ( // R/O zero values
  94. zd d
  95. zde de
  96. ze Enumerator
  97. zk interface{} /*K*/
  98. zt Tree
  99. zx x
  100. zxe xe
  101. )
  102. func clr(q interface{}) {
  103. switch x := q.(type) {
  104. case *x:
  105. for i := 0; i <= x.c; i++ { // Ch0 Sep0 ... Chn-1 Sepn-1 Chn
  106. clr(x.x[i].ch)
  107. }
  108. *x = zx
  109. btXPool.Put(x)
  110. case *d:
  111. *x = zd
  112. btDPool.Put(x)
  113. }
  114. }
  115. // -------------------------------------------------------------------------- x
  116. func newX(ch0 interface{}) *x {
  117. r := btXPool.Get().(*x)
  118. r.x[0].ch = ch0
  119. return r
  120. }
  121. func (q *x) extract(i int) {
  122. q.c--
  123. if i < q.c {
  124. copy(q.x[i:], q.x[i+1:q.c+1])
  125. q.x[q.c].ch = q.x[q.c+1].ch
  126. q.x[q.c].k = zk // GC
  127. q.x[q.c+1] = zxe // GC
  128. }
  129. }
  130. func (q *x) insert(i int, k interface{} /*K*/, ch interface{}) *x {
  131. c := q.c
  132. if i < c {
  133. q.x[c+1].ch = q.x[c].ch
  134. copy(q.x[i+2:], q.x[i+1:c])
  135. q.x[i+1].k = q.x[i].k
  136. }
  137. c++
  138. q.c = c
  139. q.x[i].k = k
  140. q.x[i+1].ch = ch
  141. return q
  142. }
  143. func (q *x) siblings(i int) (l, r *d) {
  144. if i >= 0 {
  145. if i > 0 {
  146. l = q.x[i-1].ch.(*d)
  147. }
  148. if i < q.c {
  149. r = q.x[i+1].ch.(*d)
  150. }
  151. }
  152. return
  153. }
  154. // -------------------------------------------------------------------------- d
  155. func (l *d) mvL(r *d, c int) {
  156. copy(l.d[l.c:], r.d[:c])
  157. copy(r.d[:], r.d[c:r.c])
  158. l.c += c
  159. r.c -= c
  160. }
  161. func (l *d) mvR(r *d, c int) {
  162. copy(r.d[c:], r.d[:r.c])
  163. copy(r.d[:c], l.d[l.c-c:])
  164. r.c += c
  165. l.c -= c
  166. }
  167. // ----------------------------------------------------------------------- Tree
  168. // TreeNew returns a newly created, empty Tree. The compare function is used
  169. // for key collation.
  170. func TreeNew(cmp Cmp) *Tree {
  171. return btTPool.get(cmp)
  172. }
  173. // Clear removes all K/V pairs from the tree.
  174. func (t *Tree) Clear() {
  175. if t.r == nil {
  176. return
  177. }
  178. clr(t.r)
  179. t.c, t.first, t.last, t.r = 0, nil, nil, nil
  180. t.ver++
  181. }
  182. // Close performs Clear and recycles t to a pool for possible later reuse. No
  183. // references to t should exist or such references must not be used afterwards.
  184. func (t *Tree) Close() {
  185. t.Clear()
  186. *t = zt
  187. btTPool.Put(t)
  188. }
  189. func (t *Tree) cat(p *x, q, r *d, pi int) {
  190. t.ver++
  191. q.mvL(r, r.c)
  192. if r.n != nil {
  193. r.n.p = q
  194. } else {
  195. t.last = q
  196. }
  197. q.n = r.n
  198. *r = zd
  199. btDPool.Put(r)
  200. if p.c > 1 {
  201. p.extract(pi)
  202. p.x[pi].ch = q
  203. return
  204. }
  205. switch x := t.r.(type) {
  206. case *x:
  207. *x = zx
  208. btXPool.Put(x)
  209. case *d:
  210. *x = zd
  211. btDPool.Put(x)
  212. }
  213. t.r = q
  214. }
  215. func (t *Tree) catX(p, q, r *x, pi int) {
  216. t.ver++
  217. q.x[q.c].k = p.x[pi].k
  218. copy(q.x[q.c+1:], r.x[:r.c])
  219. q.c += r.c + 1
  220. q.x[q.c].ch = r.x[r.c].ch
  221. *r = zx
  222. btXPool.Put(r)
  223. if p.c > 1 {
  224. p.c--
  225. pc := p.c
  226. if pi < pc {
  227. p.x[pi].k = p.x[pi+1].k
  228. copy(p.x[pi+1:], p.x[pi+2:pc+1])
  229. p.x[pc].ch = p.x[pc+1].ch
  230. p.x[pc].k = zk // GC
  231. p.x[pc+1].ch = nil // GC
  232. }
  233. return
  234. }
  235. switch x := t.r.(type) {
  236. case *x:
  237. *x = zx
  238. btXPool.Put(x)
  239. case *d:
  240. *x = zd
  241. btDPool.Put(x)
  242. }
  243. t.r = q
  244. }
  245. // Delete removes the k's KV pair, if it exists, in which case Delete returns
  246. // true.
  247. func (t *Tree) Delete(k interface{} /*K*/) (ok bool) {
  248. pi := -1
  249. var p *x
  250. q := t.r
  251. if q == nil {
  252. return false
  253. }
  254. for {
  255. var i int
  256. i, ok = t.find(q, k)
  257. if ok {
  258. switch x := q.(type) {
  259. case *x:
  260. if x.c < kx && q != t.r {
  261. x, i = t.underflowX(p, x, pi, i)
  262. }
  263. pi = i + 1
  264. p = x
  265. q = x.x[pi].ch
  266. continue
  267. case *d:
  268. t.extract(x, i)
  269. if x.c >= kd {
  270. return true
  271. }
  272. if q != t.r {
  273. t.underflow(p, x, pi)
  274. } else if t.c == 0 {
  275. t.Clear()
  276. }
  277. return true
  278. }
  279. }
  280. switch x := q.(type) {
  281. case *x:
  282. if x.c < kx && q != t.r {
  283. x, i = t.underflowX(p, x, pi, i)
  284. }
  285. pi = i
  286. p = x
  287. q = x.x[i].ch
  288. case *d:
  289. return false
  290. }
  291. }
  292. }
  293. func (t *Tree) extract(q *d, i int) { // (r interface{} /*V*/) {
  294. t.ver++
  295. //r = q.d[i].v // prepared for Extract
  296. q.c--
  297. if i < q.c {
  298. copy(q.d[i:], q.d[i+1:q.c+1])
  299. }
  300. q.d[q.c] = zde // GC
  301. t.c--
  302. }
  303. func (t *Tree) find(q interface{}, k interface{} /*K*/) (i int, ok bool) {
  304. var mk interface{} /*K*/
  305. l := 0
  306. switch x := q.(type) {
  307. case *x:
  308. h := x.c - 1
  309. for l <= h {
  310. m := (l + h) >> 1
  311. mk = x.x[m].k
  312. switch cmp := t.cmp(k, mk); {
  313. case cmp > 0:
  314. l = m + 1
  315. case cmp == 0:
  316. return m, true
  317. default:
  318. h = m - 1
  319. }
  320. }
  321. case *d:
  322. h := x.c - 1
  323. for l <= h {
  324. m := (l + h) >> 1
  325. mk = x.d[m].k
  326. switch cmp := t.cmp(k, mk); {
  327. case cmp > 0:
  328. l = m + 1
  329. case cmp == 0:
  330. return m, true
  331. default:
  332. h = m - 1
  333. }
  334. }
  335. }
  336. return l, false
  337. }
  338. // First returns the first item of the tree in the key collating order, or
  339. // (zero-value, zero-value) if the tree is empty.
  340. func (t *Tree) First() (k interface{} /*K*/, v interface{} /*V*/) {
  341. if q := t.first; q != nil {
  342. q := &q.d[0]
  343. k, v = q.k, q.v
  344. }
  345. return
  346. }
  347. // Get returns the value associated with k and true if it exists. Otherwise Get
  348. // returns (zero-value, false).
  349. func (t *Tree) Get(k interface{} /*K*/) (v interface{} /*V*/, ok bool) {
  350. q := t.r
  351. if q == nil {
  352. return
  353. }
  354. for {
  355. var i int
  356. if i, ok = t.find(q, k); ok {
  357. switch x := q.(type) {
  358. case *x:
  359. q = x.x[i+1].ch
  360. continue
  361. case *d:
  362. return x.d[i].v, true
  363. }
  364. }
  365. switch x := q.(type) {
  366. case *x:
  367. q = x.x[i].ch
  368. default:
  369. return
  370. }
  371. }
  372. }
  373. func (t *Tree) insert(q *d, i int, k interface{} /*K*/, v interface{} /*V*/) *d {
  374. t.ver++
  375. c := q.c
  376. if i < c {
  377. copy(q.d[i+1:], q.d[i:c])
  378. }
  379. c++
  380. q.c = c
  381. q.d[i].k, q.d[i].v = k, v
  382. t.c++
  383. return q
  384. }
  385. // Last returns the last item of the tree in the key collating order, or
  386. // (zero-value, zero-value) if the tree is empty.
  387. func (t *Tree) Last() (k interface{} /*K*/, v interface{} /*V*/) {
  388. if q := t.last; q != nil {
  389. q := &q.d[q.c-1]
  390. k, v = q.k, q.v
  391. }
  392. return
  393. }
  394. // Len returns the number of items in the tree.
  395. func (t *Tree) Len() int {
  396. return t.c
  397. }
  398. func (t *Tree) overflow(p *x, q *d, pi, i int, k interface{} /*K*/, v interface{} /*V*/) {
  399. t.ver++
  400. l, r := p.siblings(pi)
  401. if l != nil && l.c < 2*kd && i != 0 {
  402. l.mvL(q, 1)
  403. t.insert(q, i-1, k, v)
  404. p.x[pi-1].k = q.d[0].k
  405. return
  406. }
  407. if r != nil && r.c < 2*kd {
  408. if i < 2*kd {
  409. q.mvR(r, 1)
  410. t.insert(q, i, k, v)
  411. p.x[pi].k = r.d[0].k
  412. return
  413. }
  414. t.insert(r, 0, k, v)
  415. p.x[pi].k = k
  416. return
  417. }
  418. t.split(p, q, pi, i, k, v)
  419. }
  420. // Seek returns an Enumerator positioned on an item such that k >= item's key.
  421. // ok reports if k == item.key The Enumerator's position is possibly after the
  422. // last item in the tree.
  423. func (t *Tree) Seek(k interface{} /*K*/) (e *Enumerator, ok bool) {
  424. q := t.r
  425. if q == nil {
  426. e = btEPool.get(nil, false, 0, k, nil, t, t.ver)
  427. return
  428. }
  429. for {
  430. var i int
  431. if i, ok = t.find(q, k); ok {
  432. switch x := q.(type) {
  433. case *x:
  434. q = x.x[i+1].ch
  435. continue
  436. case *d:
  437. return btEPool.get(nil, ok, i, k, x, t, t.ver), true
  438. }
  439. }
  440. switch x := q.(type) {
  441. case *x:
  442. q = x.x[i].ch
  443. case *d:
  444. return btEPool.get(nil, ok, i, k, x, t, t.ver), false
  445. }
  446. }
  447. }
  448. // SeekFirst returns an enumerator positioned on the first KV pair in the tree,
  449. // if any. For an empty tree, err == io.EOF is returned and e will be nil.
  450. func (t *Tree) SeekFirst() (e *Enumerator, err error) {
  451. q := t.first
  452. if q == nil {
  453. return nil, io.EOF
  454. }
  455. return btEPool.get(nil, true, 0, q.d[0].k, q, t, t.ver), nil
  456. }
  457. // SeekLast returns an enumerator positioned on the last KV pair in the tree,
  458. // if any. For an empty tree, err == io.EOF is returned and e will be nil.
  459. func (t *Tree) SeekLast() (e *Enumerator, err error) {
  460. q := t.last
  461. if q == nil {
  462. return nil, io.EOF
  463. }
  464. return btEPool.get(nil, true, q.c-1, q.d[q.c-1].k, q, t, t.ver), nil
  465. }
  466. // Set sets the value associated with k.
  467. func (t *Tree) Set(k interface{} /*K*/, v interface{} /*V*/) {
  468. //dbg("--- PRE Set(%v, %v)\n%s", k, v, t.dump())
  469. //defer func() {
  470. // dbg("--- POST\n%s\n====\n", t.dump())
  471. //}()
  472. pi := -1
  473. var p *x
  474. q := t.r
  475. if q == nil {
  476. z := t.insert(btDPool.Get().(*d), 0, k, v)
  477. t.r, t.first, t.last = z, z, z
  478. return
  479. }
  480. for {
  481. i, ok := t.find(q, k)
  482. if ok {
  483. switch x := q.(type) {
  484. case *x:
  485. i++
  486. if x.c > 2*kx {
  487. x, i = t.splitX(p, x, pi, i)
  488. }
  489. pi = i
  490. p = x
  491. q = x.x[i].ch
  492. continue
  493. case *d:
  494. x.d[i].v = v
  495. }
  496. return
  497. }
  498. switch x := q.(type) {
  499. case *x:
  500. if x.c > 2*kx {
  501. x, i = t.splitX(p, x, pi, i)
  502. }
  503. pi = i
  504. p = x
  505. q = x.x[i].ch
  506. case *d:
  507. switch {
  508. case x.c < 2*kd:
  509. t.insert(x, i, k, v)
  510. default:
  511. t.overflow(p, x, pi, i, k, v)
  512. }
  513. return
  514. }
  515. }
  516. }
  517. // Put combines Get and Set in a more efficient way where the tree is walked
  518. // only once. The upd(ater) receives (old-value, true) if a KV pair for k
  519. // exists or (zero-value, false) otherwise. It can then return a (new-value,
  520. // true) to create or overwrite the existing value in the KV pair, or
  521. // (whatever, false) if it decides not to create or not to update the value of
  522. // the KV pair.
  523. //
  524. // tree.Set(k, v) call conceptually equals calling
  525. //
  526. // tree.Put(k, func(interface{} /*K*/, bool){ return v, true })
  527. //
  528. // modulo the differing return values.
  529. func (t *Tree) Put(k interface{} /*K*/, upd func(oldV interface{} /*V*/, exists bool) (newV interface{} /*V*/, write bool)) (oldV interface{} /*V*/, written bool) {
  530. pi := -1
  531. var p *x
  532. q := t.r
  533. var newV interface{} /*V*/
  534. if q == nil {
  535. // new KV pair in empty tree
  536. newV, written = upd(newV, false)
  537. if !written {
  538. return
  539. }
  540. z := t.insert(btDPool.Get().(*d), 0, k, newV)
  541. t.r, t.first, t.last = z, z, z
  542. return
  543. }
  544. for {
  545. i, ok := t.find(q, k)
  546. if ok {
  547. switch x := q.(type) {
  548. case *x:
  549. i++
  550. if x.c > 2*kx {
  551. x, i = t.splitX(p, x, pi, i)
  552. }
  553. pi = i
  554. p = x
  555. q = x.x[i].ch
  556. continue
  557. case *d:
  558. oldV = x.d[i].v
  559. newV, written = upd(oldV, true)
  560. if !written {
  561. return
  562. }
  563. x.d[i].v = newV
  564. }
  565. return
  566. }
  567. switch x := q.(type) {
  568. case *x:
  569. if x.c > 2*kx {
  570. x, i = t.splitX(p, x, pi, i)
  571. }
  572. pi = i
  573. p = x
  574. q = x.x[i].ch
  575. case *d: // new KV pair
  576. newV, written = upd(newV, false)
  577. if !written {
  578. return
  579. }
  580. switch {
  581. case x.c < 2*kd:
  582. t.insert(x, i, k, newV)
  583. default:
  584. t.overflow(p, x, pi, i, k, newV)
  585. }
  586. return
  587. }
  588. }
  589. }
  590. func (t *Tree) split(p *x, q *d, pi, i int, k interface{} /*K*/, v interface{} /*V*/) {
  591. t.ver++
  592. r := btDPool.Get().(*d)
  593. if q.n != nil {
  594. r.n = q.n
  595. r.n.p = r
  596. } else {
  597. t.last = r
  598. }
  599. q.n = r
  600. r.p = q
  601. copy(r.d[:], q.d[kd:2*kd])
  602. for i := range q.d[kd:] {
  603. q.d[kd+i] = zde
  604. }
  605. q.c = kd
  606. r.c = kd
  607. var done bool
  608. if i > kd {
  609. done = true
  610. t.insert(r, i-kd, k, v)
  611. }
  612. if pi >= 0 {
  613. p.insert(pi, r.d[0].k, r)
  614. } else {
  615. t.r = newX(q).insert(0, r.d[0].k, r)
  616. }
  617. if done {
  618. return
  619. }
  620. t.insert(q, i, k, v)
  621. }
  622. func (t *Tree) splitX(p *x, q *x, pi int, i int) (*x, int) {
  623. t.ver++
  624. r := btXPool.Get().(*x)
  625. copy(r.x[:], q.x[kx+1:])
  626. q.c = kx
  627. r.c = kx
  628. if pi >= 0 {
  629. p.insert(pi, q.x[kx].k, r)
  630. } else {
  631. t.r = newX(q).insert(0, q.x[kx].k, r)
  632. }
  633. q.x[kx].k = zk
  634. for i := range q.x[kx+1:] {
  635. q.x[kx+i+1] = zxe
  636. }
  637. if i > kx {
  638. q = r
  639. i -= kx + 1
  640. }
  641. return q, i
  642. }
  643. func (t *Tree) underflow(p *x, q *d, pi int) {
  644. t.ver++
  645. l, r := p.siblings(pi)
  646. if l != nil && l.c+q.c >= 2*kd {
  647. l.mvR(q, 1)
  648. p.x[pi-1].k = q.d[0].k
  649. return
  650. }
  651. if r != nil && q.c+r.c >= 2*kd {
  652. q.mvL(r, 1)
  653. p.x[pi].k = r.d[0].k
  654. r.d[r.c] = zde // GC
  655. return
  656. }
  657. if l != nil {
  658. t.cat(p, l, q, pi-1)
  659. return
  660. }
  661. t.cat(p, q, r, pi)
  662. }
  663. func (t *Tree) underflowX(p *x, q *x, pi int, i int) (*x, int) {
  664. t.ver++
  665. var l, r *x
  666. if pi >= 0 {
  667. if pi > 0 {
  668. l = p.x[pi-1].ch.(*x)
  669. }
  670. if pi < p.c {
  671. r = p.x[pi+1].ch.(*x)
  672. }
  673. }
  674. if l != nil && l.c > kx {
  675. q.x[q.c+1].ch = q.x[q.c].ch
  676. copy(q.x[1:], q.x[:q.c])
  677. q.x[0].ch = l.x[l.c].ch
  678. q.x[0].k = p.x[pi-1].k
  679. q.c++
  680. i++
  681. l.c--
  682. p.x[pi-1].k = l.x[l.c].k
  683. return q, i
  684. }
  685. if r != nil && r.c > kx {
  686. q.x[q.c].k = p.x[pi].k
  687. q.c++
  688. q.x[q.c].ch = r.x[0].ch
  689. p.x[pi].k = r.x[0].k
  690. copy(r.x[:], r.x[1:r.c])
  691. r.c--
  692. rc := r.c
  693. r.x[rc].ch = r.x[rc+1].ch
  694. r.x[rc].k = zk
  695. r.x[rc+1].ch = nil
  696. return q, i
  697. }
  698. if l != nil {
  699. i += l.c + 1
  700. t.catX(p, l, q, pi-1)
  701. q = l
  702. return q, i
  703. }
  704. t.catX(p, q, r, pi)
  705. return q, i
  706. }
  707. // ----------------------------------------------------------------- Enumerator
  708. // Close recycles e to a pool for possible later reuse. No references to e
  709. // should exist or such references must not be used afterwards.
  710. func (e *Enumerator) Close() {
  711. *e = ze
  712. btEPool.Put(e)
  713. }
  714. // Next returns the currently enumerated item, if it exists and moves to the
  715. // next item in the key collation order. If there is no item to return, err ==
  716. // io.EOF is returned.
  717. func (e *Enumerator) Next() (k interface{} /*K*/, v interface{} /*V*/, err error) {
  718. if err = e.err; err != nil {
  719. return
  720. }
  721. if e.ver != e.t.ver {
  722. f, _ := e.t.Seek(e.k)
  723. *e = *f
  724. f.Close()
  725. }
  726. if e.q == nil {
  727. e.err, err = io.EOF, io.EOF
  728. return
  729. }
  730. if e.i >= e.q.c {
  731. if err = e.next(); err != nil {
  732. return
  733. }
  734. }
  735. i := e.q.d[e.i]
  736. k, v = i.k, i.v
  737. e.k, e.hit = k, true
  738. e.next()
  739. return
  740. }
  741. func (e *Enumerator) next() error {
  742. if e.q == nil {
  743. e.err = io.EOF
  744. return io.EOF
  745. }
  746. switch {
  747. case e.i < e.q.c-1:
  748. e.i++
  749. default:
  750. if e.q, e.i = e.q.n, 0; e.q == nil {
  751. e.err = io.EOF
  752. }
  753. }
  754. return e.err
  755. }
  756. // Prev returns the currently enumerated item, if it exists and moves to the
  757. // previous item in the key collation order. If there is no item to return, err
  758. // == io.EOF is returned.
  759. func (e *Enumerator) Prev() (k interface{} /*K*/, v interface{} /*V*/, err error) {
  760. if err = e.err; err != nil {
  761. return
  762. }
  763. if e.ver != e.t.ver {
  764. f, _ := e.t.Seek(e.k)
  765. *e = *f
  766. f.Close()
  767. }
  768. if e.q == nil {
  769. e.err, err = io.EOF, io.EOF
  770. return
  771. }
  772. if !e.hit {
  773. // move to previous because Seek overshoots if there's no hit
  774. if err = e.prev(); err != nil {
  775. return
  776. }
  777. }
  778. if e.i >= e.q.c {
  779. if err = e.prev(); err != nil {
  780. return
  781. }
  782. }
  783. i := e.q.d[e.i]
  784. k, v = i.k, i.v
  785. e.k, e.hit = k, true
  786. e.prev()
  787. return
  788. }
  789. func (e *Enumerator) prev() error {
  790. if e.q == nil {
  791. e.err = io.EOF
  792. return io.EOF
  793. }
  794. switch {
  795. case e.i > 0:
  796. e.i--
  797. default:
  798. if e.q = e.q.p; e.q == nil {
  799. e.err = io.EOF
  800. break
  801. }
  802. e.i = e.q.c - 1
  803. }
  804. return e.err
  805. }