cursor_test.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817
  1. package bbolt_test
  2. import (
  3. "bytes"
  4. "encoding/binary"
  5. "fmt"
  6. "log"
  7. "os"
  8. "reflect"
  9. "sort"
  10. "testing"
  11. "testing/quick"
  12. bolt "go.etcd.io/bbolt"
  13. )
  14. // Ensure that a cursor can return a reference to the bucket that created it.
  15. func TestCursor_Bucket(t *testing.T) {
  16. db := MustOpenDB()
  17. defer db.MustClose()
  18. if err := db.Update(func(tx *bolt.Tx) error {
  19. b, err := tx.CreateBucket([]byte("widgets"))
  20. if err != nil {
  21. t.Fatal(err)
  22. }
  23. if cb := b.Cursor().Bucket(); !reflect.DeepEqual(cb, b) {
  24. t.Fatal("cursor bucket mismatch")
  25. }
  26. return nil
  27. }); err != nil {
  28. t.Fatal(err)
  29. }
  30. }
  31. // Ensure that a Tx cursor can seek to the appropriate keys.
  32. func TestCursor_Seek(t *testing.T) {
  33. db := MustOpenDB()
  34. defer db.MustClose()
  35. if err := db.Update(func(tx *bolt.Tx) error {
  36. b, err := tx.CreateBucket([]byte("widgets"))
  37. if err != nil {
  38. t.Fatal(err)
  39. }
  40. if err := b.Put([]byte("foo"), []byte("0001")); err != nil {
  41. t.Fatal(err)
  42. }
  43. if err := b.Put([]byte("bar"), []byte("0002")); err != nil {
  44. t.Fatal(err)
  45. }
  46. if err := b.Put([]byte("baz"), []byte("0003")); err != nil {
  47. t.Fatal(err)
  48. }
  49. if _, err := b.CreateBucket([]byte("bkt")); err != nil {
  50. t.Fatal(err)
  51. }
  52. return nil
  53. }); err != nil {
  54. t.Fatal(err)
  55. }
  56. if err := db.View(func(tx *bolt.Tx) error {
  57. c := tx.Bucket([]byte("widgets")).Cursor()
  58. // Exact match should go to the key.
  59. if k, v := c.Seek([]byte("bar")); !bytes.Equal(k, []byte("bar")) {
  60. t.Fatalf("unexpected key: %v", k)
  61. } else if !bytes.Equal(v, []byte("0002")) {
  62. t.Fatalf("unexpected value: %v", v)
  63. }
  64. // Inexact match should go to the next key.
  65. if k, v := c.Seek([]byte("bas")); !bytes.Equal(k, []byte("baz")) {
  66. t.Fatalf("unexpected key: %v", k)
  67. } else if !bytes.Equal(v, []byte("0003")) {
  68. t.Fatalf("unexpected value: %v", v)
  69. }
  70. // Low key should go to the first key.
  71. if k, v := c.Seek([]byte("")); !bytes.Equal(k, []byte("bar")) {
  72. t.Fatalf("unexpected key: %v", k)
  73. } else if !bytes.Equal(v, []byte("0002")) {
  74. t.Fatalf("unexpected value: %v", v)
  75. }
  76. // High key should return no key.
  77. if k, v := c.Seek([]byte("zzz")); k != nil {
  78. t.Fatalf("expected nil key: %v", k)
  79. } else if v != nil {
  80. t.Fatalf("expected nil value: %v", v)
  81. }
  82. // Buckets should return their key but no value.
  83. if k, v := c.Seek([]byte("bkt")); !bytes.Equal(k, []byte("bkt")) {
  84. t.Fatalf("unexpected key: %v", k)
  85. } else if v != nil {
  86. t.Fatalf("expected nil value: %v", v)
  87. }
  88. return nil
  89. }); err != nil {
  90. t.Fatal(err)
  91. }
  92. }
  93. func TestCursor_Delete(t *testing.T) {
  94. db := MustOpenDB()
  95. defer db.MustClose()
  96. const count = 1000
  97. // Insert every other key between 0 and $count.
  98. if err := db.Update(func(tx *bolt.Tx) error {
  99. b, err := tx.CreateBucket([]byte("widgets"))
  100. if err != nil {
  101. t.Fatal(err)
  102. }
  103. for i := 0; i < count; i += 1 {
  104. k := make([]byte, 8)
  105. binary.BigEndian.PutUint64(k, uint64(i))
  106. if err := b.Put(k, make([]byte, 100)); err != nil {
  107. t.Fatal(err)
  108. }
  109. }
  110. if _, err := b.CreateBucket([]byte("sub")); err != nil {
  111. t.Fatal(err)
  112. }
  113. return nil
  114. }); err != nil {
  115. t.Fatal(err)
  116. }
  117. if err := db.Update(func(tx *bolt.Tx) error {
  118. c := tx.Bucket([]byte("widgets")).Cursor()
  119. bound := make([]byte, 8)
  120. binary.BigEndian.PutUint64(bound, uint64(count/2))
  121. for key, _ := c.First(); bytes.Compare(key, bound) < 0; key, _ = c.Next() {
  122. if err := c.Delete(); err != nil {
  123. t.Fatal(err)
  124. }
  125. }
  126. c.Seek([]byte("sub"))
  127. if err := c.Delete(); err != bolt.ErrIncompatibleValue {
  128. t.Fatalf("unexpected error: %s", err)
  129. }
  130. return nil
  131. }); err != nil {
  132. t.Fatal(err)
  133. }
  134. if err := db.View(func(tx *bolt.Tx) error {
  135. stats := tx.Bucket([]byte("widgets")).Stats()
  136. if stats.KeyN != count/2+1 {
  137. t.Fatalf("unexpected KeyN: %d", stats.KeyN)
  138. }
  139. return nil
  140. }); err != nil {
  141. t.Fatal(err)
  142. }
  143. }
  144. // Ensure that a Tx cursor can seek to the appropriate keys when there are a
  145. // large number of keys. This test also checks that seek will always move
  146. // forward to the next key.
  147. //
  148. // Related: https://github.com/boltdb/bolt/pull/187
  149. func TestCursor_Seek_Large(t *testing.T) {
  150. db := MustOpenDB()
  151. defer db.MustClose()
  152. var count = 10000
  153. // Insert every other key between 0 and $count.
  154. if err := db.Update(func(tx *bolt.Tx) error {
  155. b, err := tx.CreateBucket([]byte("widgets"))
  156. if err != nil {
  157. t.Fatal(err)
  158. }
  159. for i := 0; i < count; i += 100 {
  160. for j := i; j < i+100; j += 2 {
  161. k := make([]byte, 8)
  162. binary.BigEndian.PutUint64(k, uint64(j))
  163. if err := b.Put(k, make([]byte, 100)); err != nil {
  164. t.Fatal(err)
  165. }
  166. }
  167. }
  168. return nil
  169. }); err != nil {
  170. t.Fatal(err)
  171. }
  172. if err := db.View(func(tx *bolt.Tx) error {
  173. c := tx.Bucket([]byte("widgets")).Cursor()
  174. for i := 0; i < count; i++ {
  175. seek := make([]byte, 8)
  176. binary.BigEndian.PutUint64(seek, uint64(i))
  177. k, _ := c.Seek(seek)
  178. // The last seek is beyond the end of the the range so
  179. // it should return nil.
  180. if i == count-1 {
  181. if k != nil {
  182. t.Fatal("expected nil key")
  183. }
  184. continue
  185. }
  186. // Otherwise we should seek to the exact key or the next key.
  187. num := binary.BigEndian.Uint64(k)
  188. if i%2 == 0 {
  189. if num != uint64(i) {
  190. t.Fatalf("unexpected num: %d", num)
  191. }
  192. } else {
  193. if num != uint64(i+1) {
  194. t.Fatalf("unexpected num: %d", num)
  195. }
  196. }
  197. }
  198. return nil
  199. }); err != nil {
  200. t.Fatal(err)
  201. }
  202. }
  203. // Ensure that a cursor can iterate over an empty bucket without error.
  204. func TestCursor_EmptyBucket(t *testing.T) {
  205. db := MustOpenDB()
  206. defer db.MustClose()
  207. if err := db.Update(func(tx *bolt.Tx) error {
  208. _, err := tx.CreateBucket([]byte("widgets"))
  209. return err
  210. }); err != nil {
  211. t.Fatal(err)
  212. }
  213. if err := db.View(func(tx *bolt.Tx) error {
  214. c := tx.Bucket([]byte("widgets")).Cursor()
  215. k, v := c.First()
  216. if k != nil {
  217. t.Fatalf("unexpected key: %v", k)
  218. } else if v != nil {
  219. t.Fatalf("unexpected value: %v", v)
  220. }
  221. return nil
  222. }); err != nil {
  223. t.Fatal(err)
  224. }
  225. }
  226. // Ensure that a Tx cursor can reverse iterate over an empty bucket without error.
  227. func TestCursor_EmptyBucketReverse(t *testing.T) {
  228. db := MustOpenDB()
  229. defer db.MustClose()
  230. if err := db.Update(func(tx *bolt.Tx) error {
  231. _, err := tx.CreateBucket([]byte("widgets"))
  232. return err
  233. }); err != nil {
  234. t.Fatal(err)
  235. }
  236. if err := db.View(func(tx *bolt.Tx) error {
  237. c := tx.Bucket([]byte("widgets")).Cursor()
  238. k, v := c.Last()
  239. if k != nil {
  240. t.Fatalf("unexpected key: %v", k)
  241. } else if v != nil {
  242. t.Fatalf("unexpected value: %v", v)
  243. }
  244. return nil
  245. }); err != nil {
  246. t.Fatal(err)
  247. }
  248. }
  249. // Ensure that a Tx cursor can iterate over a single root with a couple elements.
  250. func TestCursor_Iterate_Leaf(t *testing.T) {
  251. db := MustOpenDB()
  252. defer db.MustClose()
  253. if err := db.Update(func(tx *bolt.Tx) error {
  254. b, err := tx.CreateBucket([]byte("widgets"))
  255. if err != nil {
  256. t.Fatal(err)
  257. }
  258. if err := b.Put([]byte("baz"), []byte{}); err != nil {
  259. t.Fatal(err)
  260. }
  261. if err := b.Put([]byte("foo"), []byte{0}); err != nil {
  262. t.Fatal(err)
  263. }
  264. if err := b.Put([]byte("bar"), []byte{1}); err != nil {
  265. t.Fatal(err)
  266. }
  267. return nil
  268. }); err != nil {
  269. t.Fatal(err)
  270. }
  271. tx, err := db.Begin(false)
  272. if err != nil {
  273. t.Fatal(err)
  274. }
  275. defer func() { _ = tx.Rollback() }()
  276. c := tx.Bucket([]byte("widgets")).Cursor()
  277. k, v := c.First()
  278. if !bytes.Equal(k, []byte("bar")) {
  279. t.Fatalf("unexpected key: %v", k)
  280. } else if !bytes.Equal(v, []byte{1}) {
  281. t.Fatalf("unexpected value: %v", v)
  282. }
  283. k, v = c.Next()
  284. if !bytes.Equal(k, []byte("baz")) {
  285. t.Fatalf("unexpected key: %v", k)
  286. } else if !bytes.Equal(v, []byte{}) {
  287. t.Fatalf("unexpected value: %v", v)
  288. }
  289. k, v = c.Next()
  290. if !bytes.Equal(k, []byte("foo")) {
  291. t.Fatalf("unexpected key: %v", k)
  292. } else if !bytes.Equal(v, []byte{0}) {
  293. t.Fatalf("unexpected value: %v", v)
  294. }
  295. k, v = c.Next()
  296. if k != nil {
  297. t.Fatalf("expected nil key: %v", k)
  298. } else if v != nil {
  299. t.Fatalf("expected nil value: %v", v)
  300. }
  301. k, v = c.Next()
  302. if k != nil {
  303. t.Fatalf("expected nil key: %v", k)
  304. } else if v != nil {
  305. t.Fatalf("expected nil value: %v", v)
  306. }
  307. if err := tx.Rollback(); err != nil {
  308. t.Fatal(err)
  309. }
  310. }
  311. // Ensure that a Tx cursor can iterate in reverse over a single root with a couple elements.
  312. func TestCursor_LeafRootReverse(t *testing.T) {
  313. db := MustOpenDB()
  314. defer db.MustClose()
  315. if err := db.Update(func(tx *bolt.Tx) error {
  316. b, err := tx.CreateBucket([]byte("widgets"))
  317. if err != nil {
  318. t.Fatal(err)
  319. }
  320. if err := b.Put([]byte("baz"), []byte{}); err != nil {
  321. t.Fatal(err)
  322. }
  323. if err := b.Put([]byte("foo"), []byte{0}); err != nil {
  324. t.Fatal(err)
  325. }
  326. if err := b.Put([]byte("bar"), []byte{1}); err != nil {
  327. t.Fatal(err)
  328. }
  329. return nil
  330. }); err != nil {
  331. t.Fatal(err)
  332. }
  333. tx, err := db.Begin(false)
  334. if err != nil {
  335. t.Fatal(err)
  336. }
  337. c := tx.Bucket([]byte("widgets")).Cursor()
  338. if k, v := c.Last(); !bytes.Equal(k, []byte("foo")) {
  339. t.Fatalf("unexpected key: %v", k)
  340. } else if !bytes.Equal(v, []byte{0}) {
  341. t.Fatalf("unexpected value: %v", v)
  342. }
  343. if k, v := c.Prev(); !bytes.Equal(k, []byte("baz")) {
  344. t.Fatalf("unexpected key: %v", k)
  345. } else if !bytes.Equal(v, []byte{}) {
  346. t.Fatalf("unexpected value: %v", v)
  347. }
  348. if k, v := c.Prev(); !bytes.Equal(k, []byte("bar")) {
  349. t.Fatalf("unexpected key: %v", k)
  350. } else if !bytes.Equal(v, []byte{1}) {
  351. t.Fatalf("unexpected value: %v", v)
  352. }
  353. if k, v := c.Prev(); k != nil {
  354. t.Fatalf("expected nil key: %v", k)
  355. } else if v != nil {
  356. t.Fatalf("expected nil value: %v", v)
  357. }
  358. if k, v := c.Prev(); k != nil {
  359. t.Fatalf("expected nil key: %v", k)
  360. } else if v != nil {
  361. t.Fatalf("expected nil value: %v", v)
  362. }
  363. if err := tx.Rollback(); err != nil {
  364. t.Fatal(err)
  365. }
  366. }
  367. // Ensure that a Tx cursor can restart from the beginning.
  368. func TestCursor_Restart(t *testing.T) {
  369. db := MustOpenDB()
  370. defer db.MustClose()
  371. if err := db.Update(func(tx *bolt.Tx) error {
  372. b, err := tx.CreateBucket([]byte("widgets"))
  373. if err != nil {
  374. t.Fatal(err)
  375. }
  376. if err := b.Put([]byte("bar"), []byte{}); err != nil {
  377. t.Fatal(err)
  378. }
  379. if err := b.Put([]byte("foo"), []byte{}); err != nil {
  380. t.Fatal(err)
  381. }
  382. return nil
  383. }); err != nil {
  384. t.Fatal(err)
  385. }
  386. tx, err := db.Begin(false)
  387. if err != nil {
  388. t.Fatal(err)
  389. }
  390. c := tx.Bucket([]byte("widgets")).Cursor()
  391. if k, _ := c.First(); !bytes.Equal(k, []byte("bar")) {
  392. t.Fatalf("unexpected key: %v", k)
  393. }
  394. if k, _ := c.Next(); !bytes.Equal(k, []byte("foo")) {
  395. t.Fatalf("unexpected key: %v", k)
  396. }
  397. if k, _ := c.First(); !bytes.Equal(k, []byte("bar")) {
  398. t.Fatalf("unexpected key: %v", k)
  399. }
  400. if k, _ := c.Next(); !bytes.Equal(k, []byte("foo")) {
  401. t.Fatalf("unexpected key: %v", k)
  402. }
  403. if err := tx.Rollback(); err != nil {
  404. t.Fatal(err)
  405. }
  406. }
  407. // Ensure that a cursor can skip over empty pages that have been deleted.
  408. func TestCursor_First_EmptyPages(t *testing.T) {
  409. db := MustOpenDB()
  410. defer db.MustClose()
  411. // Create 1000 keys in the "widgets" bucket.
  412. if err := db.Update(func(tx *bolt.Tx) error {
  413. b, err := tx.CreateBucket([]byte("widgets"))
  414. if err != nil {
  415. t.Fatal(err)
  416. }
  417. for i := 0; i < 1000; i++ {
  418. if err := b.Put(u64tob(uint64(i)), []byte{}); err != nil {
  419. t.Fatal(err)
  420. }
  421. }
  422. return nil
  423. }); err != nil {
  424. t.Fatal(err)
  425. }
  426. // Delete half the keys and then try to iterate.
  427. if err := db.Update(func(tx *bolt.Tx) error {
  428. b := tx.Bucket([]byte("widgets"))
  429. for i := 0; i < 600; i++ {
  430. if err := b.Delete(u64tob(uint64(i))); err != nil {
  431. t.Fatal(err)
  432. }
  433. }
  434. c := b.Cursor()
  435. var n int
  436. for k, _ := c.First(); k != nil; k, _ = c.Next() {
  437. n++
  438. }
  439. if n != 400 {
  440. t.Fatalf("unexpected key count: %d", n)
  441. }
  442. return nil
  443. }); err != nil {
  444. t.Fatal(err)
  445. }
  446. }
  447. // Ensure that a Tx can iterate over all elements in a bucket.
  448. func TestCursor_QuickCheck(t *testing.T) {
  449. f := func(items testdata) bool {
  450. db := MustOpenDB()
  451. defer db.MustClose()
  452. // Bulk insert all values.
  453. tx, err := db.Begin(true)
  454. if err != nil {
  455. t.Fatal(err)
  456. }
  457. b, err := tx.CreateBucket([]byte("widgets"))
  458. if err != nil {
  459. t.Fatal(err)
  460. }
  461. for _, item := range items {
  462. if err := b.Put(item.Key, item.Value); err != nil {
  463. t.Fatal(err)
  464. }
  465. }
  466. if err := tx.Commit(); err != nil {
  467. t.Fatal(err)
  468. }
  469. // Sort test data.
  470. sort.Sort(items)
  471. // Iterate over all items and check consistency.
  472. var index = 0
  473. tx, err = db.Begin(false)
  474. if err != nil {
  475. t.Fatal(err)
  476. }
  477. c := tx.Bucket([]byte("widgets")).Cursor()
  478. for k, v := c.First(); k != nil && index < len(items); k, v = c.Next() {
  479. if !bytes.Equal(k, items[index].Key) {
  480. t.Fatalf("unexpected key: %v", k)
  481. } else if !bytes.Equal(v, items[index].Value) {
  482. t.Fatalf("unexpected value: %v", v)
  483. }
  484. index++
  485. }
  486. if len(items) != index {
  487. t.Fatalf("unexpected item count: %v, expected %v", len(items), index)
  488. }
  489. if err := tx.Rollback(); err != nil {
  490. t.Fatal(err)
  491. }
  492. return true
  493. }
  494. if err := quick.Check(f, qconfig()); err != nil {
  495. t.Error(err)
  496. }
  497. }
  498. // Ensure that a transaction can iterate over all elements in a bucket in reverse.
  499. func TestCursor_QuickCheck_Reverse(t *testing.T) {
  500. f := func(items testdata) bool {
  501. db := MustOpenDB()
  502. defer db.MustClose()
  503. // Bulk insert all values.
  504. tx, err := db.Begin(true)
  505. if err != nil {
  506. t.Fatal(err)
  507. }
  508. b, err := tx.CreateBucket([]byte("widgets"))
  509. if err != nil {
  510. t.Fatal(err)
  511. }
  512. for _, item := range items {
  513. if err := b.Put(item.Key, item.Value); err != nil {
  514. t.Fatal(err)
  515. }
  516. }
  517. if err := tx.Commit(); err != nil {
  518. t.Fatal(err)
  519. }
  520. // Sort test data.
  521. sort.Sort(revtestdata(items))
  522. // Iterate over all items and check consistency.
  523. var index = 0
  524. tx, err = db.Begin(false)
  525. if err != nil {
  526. t.Fatal(err)
  527. }
  528. c := tx.Bucket([]byte("widgets")).Cursor()
  529. for k, v := c.Last(); k != nil && index < len(items); k, v = c.Prev() {
  530. if !bytes.Equal(k, items[index].Key) {
  531. t.Fatalf("unexpected key: %v", k)
  532. } else if !bytes.Equal(v, items[index].Value) {
  533. t.Fatalf("unexpected value: %v", v)
  534. }
  535. index++
  536. }
  537. if len(items) != index {
  538. t.Fatalf("unexpected item count: %v, expected %v", len(items), index)
  539. }
  540. if err := tx.Rollback(); err != nil {
  541. t.Fatal(err)
  542. }
  543. return true
  544. }
  545. if err := quick.Check(f, qconfig()); err != nil {
  546. t.Error(err)
  547. }
  548. }
  549. // Ensure that a Tx cursor can iterate over subbuckets.
  550. func TestCursor_QuickCheck_BucketsOnly(t *testing.T) {
  551. db := MustOpenDB()
  552. defer db.MustClose()
  553. if err := db.Update(func(tx *bolt.Tx) error {
  554. b, err := tx.CreateBucket([]byte("widgets"))
  555. if err != nil {
  556. t.Fatal(err)
  557. }
  558. if _, err := b.CreateBucket([]byte("foo")); err != nil {
  559. t.Fatal(err)
  560. }
  561. if _, err := b.CreateBucket([]byte("bar")); err != nil {
  562. t.Fatal(err)
  563. }
  564. if _, err := b.CreateBucket([]byte("baz")); err != nil {
  565. t.Fatal(err)
  566. }
  567. return nil
  568. }); err != nil {
  569. t.Fatal(err)
  570. }
  571. if err := db.View(func(tx *bolt.Tx) error {
  572. var names []string
  573. c := tx.Bucket([]byte("widgets")).Cursor()
  574. for k, v := c.First(); k != nil; k, v = c.Next() {
  575. names = append(names, string(k))
  576. if v != nil {
  577. t.Fatalf("unexpected value: %v", v)
  578. }
  579. }
  580. if !reflect.DeepEqual(names, []string{"bar", "baz", "foo"}) {
  581. t.Fatalf("unexpected names: %+v", names)
  582. }
  583. return nil
  584. }); err != nil {
  585. t.Fatal(err)
  586. }
  587. }
  588. // Ensure that a Tx cursor can reverse iterate over subbuckets.
  589. func TestCursor_QuickCheck_BucketsOnly_Reverse(t *testing.T) {
  590. db := MustOpenDB()
  591. defer db.MustClose()
  592. if err := db.Update(func(tx *bolt.Tx) error {
  593. b, err := tx.CreateBucket([]byte("widgets"))
  594. if err != nil {
  595. t.Fatal(err)
  596. }
  597. if _, err := b.CreateBucket([]byte("foo")); err != nil {
  598. t.Fatal(err)
  599. }
  600. if _, err := b.CreateBucket([]byte("bar")); err != nil {
  601. t.Fatal(err)
  602. }
  603. if _, err := b.CreateBucket([]byte("baz")); err != nil {
  604. t.Fatal(err)
  605. }
  606. return nil
  607. }); err != nil {
  608. t.Fatal(err)
  609. }
  610. if err := db.View(func(tx *bolt.Tx) error {
  611. var names []string
  612. c := tx.Bucket([]byte("widgets")).Cursor()
  613. for k, v := c.Last(); k != nil; k, v = c.Prev() {
  614. names = append(names, string(k))
  615. if v != nil {
  616. t.Fatalf("unexpected value: %v", v)
  617. }
  618. }
  619. if !reflect.DeepEqual(names, []string{"foo", "baz", "bar"}) {
  620. t.Fatalf("unexpected names: %+v", names)
  621. }
  622. return nil
  623. }); err != nil {
  624. t.Fatal(err)
  625. }
  626. }
  627. func ExampleCursor() {
  628. // Open the database.
  629. db, err := bolt.Open(tempfile(), 0666, nil)
  630. if err != nil {
  631. log.Fatal(err)
  632. }
  633. defer os.Remove(db.Path())
  634. // Start a read-write transaction.
  635. if err := db.Update(func(tx *bolt.Tx) error {
  636. // Create a new bucket.
  637. b, err := tx.CreateBucket([]byte("animals"))
  638. if err != nil {
  639. return err
  640. }
  641. // Insert data into a bucket.
  642. if err := b.Put([]byte("dog"), []byte("fun")); err != nil {
  643. log.Fatal(err)
  644. }
  645. if err := b.Put([]byte("cat"), []byte("lame")); err != nil {
  646. log.Fatal(err)
  647. }
  648. if err := b.Put([]byte("liger"), []byte("awesome")); err != nil {
  649. log.Fatal(err)
  650. }
  651. // Create a cursor for iteration.
  652. c := b.Cursor()
  653. // Iterate over items in sorted key order. This starts from the
  654. // first key/value pair and updates the k/v variables to the
  655. // next key/value on each iteration.
  656. //
  657. // The loop finishes at the end of the cursor when a nil key is returned.
  658. for k, v := c.First(); k != nil; k, v = c.Next() {
  659. fmt.Printf("A %s is %s.\n", k, v)
  660. }
  661. return nil
  662. }); err != nil {
  663. log.Fatal(err)
  664. }
  665. if err := db.Close(); err != nil {
  666. log.Fatal(err)
  667. }
  668. // Output:
  669. // A cat is lame.
  670. // A dog is fun.
  671. // A liger is awesome.
  672. }
  673. func ExampleCursor_reverse() {
  674. // Open the database.
  675. db, err := bolt.Open(tempfile(), 0666, nil)
  676. if err != nil {
  677. log.Fatal(err)
  678. }
  679. defer os.Remove(db.Path())
  680. // Start a read-write transaction.
  681. if err := db.Update(func(tx *bolt.Tx) error {
  682. // Create a new bucket.
  683. b, err := tx.CreateBucket([]byte("animals"))
  684. if err != nil {
  685. return err
  686. }
  687. // Insert data into a bucket.
  688. if err := b.Put([]byte("dog"), []byte("fun")); err != nil {
  689. log.Fatal(err)
  690. }
  691. if err := b.Put([]byte("cat"), []byte("lame")); err != nil {
  692. log.Fatal(err)
  693. }
  694. if err := b.Put([]byte("liger"), []byte("awesome")); err != nil {
  695. log.Fatal(err)
  696. }
  697. // Create a cursor for iteration.
  698. c := b.Cursor()
  699. // Iterate over items in reverse sorted key order. This starts
  700. // from the last key/value pair and updates the k/v variables to
  701. // the previous key/value on each iteration.
  702. //
  703. // The loop finishes at the beginning of the cursor when a nil key
  704. // is returned.
  705. for k, v := c.Last(); k != nil; k, v = c.Prev() {
  706. fmt.Printf("A %s is %s.\n", k, v)
  707. }
  708. return nil
  709. }); err != nil {
  710. log.Fatal(err)
  711. }
  712. // Close the database to release the file lock.
  713. if err := db.Close(); err != nil {
  714. log.Fatal(err)
  715. }
  716. // Output:
  717. // A liger is awesome.
  718. // A dog is fun.
  719. // A cat is lame.
  720. }