bucket_test.go 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959
  1. package bbolt_test
  2. import (
  3. "bytes"
  4. "encoding/binary"
  5. "errors"
  6. "fmt"
  7. "log"
  8. "math/rand"
  9. "os"
  10. "strconv"
  11. "strings"
  12. "testing"
  13. "testing/quick"
  14. bolt "go.etcd.io/bbolt"
  15. )
  16. // Ensure that a bucket that gets a non-existent key returns nil.
  17. func TestBucket_Get_NonExistent(t *testing.T) {
  18. db := MustOpenDB()
  19. defer db.MustClose()
  20. if err := db.Update(func(tx *bolt.Tx) error {
  21. b, err := tx.CreateBucket([]byte("widgets"))
  22. if err != nil {
  23. t.Fatal(err)
  24. }
  25. if v := b.Get([]byte("foo")); v != nil {
  26. t.Fatal("expected nil value")
  27. }
  28. return nil
  29. }); err != nil {
  30. t.Fatal(err)
  31. }
  32. }
  33. // Ensure that a bucket can read a value that is not flushed yet.
  34. func TestBucket_Get_FromNode(t *testing.T) {
  35. db := MustOpenDB()
  36. defer db.MustClose()
  37. if err := db.Update(func(tx *bolt.Tx) error {
  38. b, err := tx.CreateBucket([]byte("widgets"))
  39. if err != nil {
  40. t.Fatal(err)
  41. }
  42. if err := b.Put([]byte("foo"), []byte("bar")); err != nil {
  43. t.Fatal(err)
  44. }
  45. if v := b.Get([]byte("foo")); !bytes.Equal(v, []byte("bar")) {
  46. t.Fatalf("unexpected value: %v", v)
  47. }
  48. return nil
  49. }); err != nil {
  50. t.Fatal(err)
  51. }
  52. }
  53. // Ensure that a bucket retrieved via Get() returns a nil.
  54. func TestBucket_Get_IncompatibleValue(t *testing.T) {
  55. db := MustOpenDB()
  56. defer db.MustClose()
  57. if err := db.Update(func(tx *bolt.Tx) error {
  58. _, err := tx.CreateBucket([]byte("widgets"))
  59. if err != nil {
  60. t.Fatal(err)
  61. }
  62. if _, err := tx.Bucket([]byte("widgets")).CreateBucket([]byte("foo")); err != nil {
  63. t.Fatal(err)
  64. }
  65. if tx.Bucket([]byte("widgets")).Get([]byte("foo")) != nil {
  66. t.Fatal("expected nil value")
  67. }
  68. return nil
  69. }); err != nil {
  70. t.Fatal(err)
  71. }
  72. }
  73. // Ensure that a slice returned from a bucket has a capacity equal to its length.
  74. // This also allows slices to be appended to since it will require a realloc by Go.
  75. //
  76. // https://github.com/boltdb/bolt/issues/544
  77. func TestBucket_Get_Capacity(t *testing.T) {
  78. db := MustOpenDB()
  79. defer db.MustClose()
  80. // Write key to a bucket.
  81. if err := db.Update(func(tx *bolt.Tx) error {
  82. b, err := tx.CreateBucket([]byte("bucket"))
  83. if err != nil {
  84. return err
  85. }
  86. return b.Put([]byte("key"), []byte("val"))
  87. }); err != nil {
  88. t.Fatal(err)
  89. }
  90. // Retrieve value and attempt to append to it.
  91. if err := db.Update(func(tx *bolt.Tx) error {
  92. k, v := tx.Bucket([]byte("bucket")).Cursor().First()
  93. // Verify capacity.
  94. if len(k) != cap(k) {
  95. t.Fatalf("unexpected key slice capacity: %d", cap(k))
  96. } else if len(v) != cap(v) {
  97. t.Fatalf("unexpected value slice capacity: %d", cap(v))
  98. }
  99. // Ensure slice can be appended to without a segfault.
  100. k = append(k, []byte("123")...)
  101. v = append(v, []byte("123")...)
  102. _, _ = k, v // to pass ineffassign
  103. return nil
  104. }); err != nil {
  105. t.Fatal(err)
  106. }
  107. }
  108. // Ensure that a bucket can write a key/value.
  109. func TestBucket_Put(t *testing.T) {
  110. db := MustOpenDB()
  111. defer db.MustClose()
  112. if err := db.Update(func(tx *bolt.Tx) error {
  113. b, err := tx.CreateBucket([]byte("widgets"))
  114. if err != nil {
  115. t.Fatal(err)
  116. }
  117. if err := b.Put([]byte("foo"), []byte("bar")); err != nil {
  118. t.Fatal(err)
  119. }
  120. v := tx.Bucket([]byte("widgets")).Get([]byte("foo"))
  121. if !bytes.Equal([]byte("bar"), v) {
  122. t.Fatalf("unexpected value: %v", v)
  123. }
  124. return nil
  125. }); err != nil {
  126. t.Fatal(err)
  127. }
  128. }
  129. // Ensure that a bucket can rewrite a key in the same transaction.
  130. func TestBucket_Put_Repeat(t *testing.T) {
  131. db := MustOpenDB()
  132. defer db.MustClose()
  133. if err := db.Update(func(tx *bolt.Tx) error {
  134. b, err := tx.CreateBucket([]byte("widgets"))
  135. if err != nil {
  136. t.Fatal(err)
  137. }
  138. if err := b.Put([]byte("foo"), []byte("bar")); err != nil {
  139. t.Fatal(err)
  140. }
  141. if err := b.Put([]byte("foo"), []byte("baz")); err != nil {
  142. t.Fatal(err)
  143. }
  144. value := tx.Bucket([]byte("widgets")).Get([]byte("foo"))
  145. if !bytes.Equal([]byte("baz"), value) {
  146. t.Fatalf("unexpected value: %v", value)
  147. }
  148. return nil
  149. }); err != nil {
  150. t.Fatal(err)
  151. }
  152. }
  153. // Ensure that a bucket can write a bunch of large values.
  154. func TestBucket_Put_Large(t *testing.T) {
  155. db := MustOpenDB()
  156. defer db.MustClose()
  157. count, factor := 100, 200
  158. if err := db.Update(func(tx *bolt.Tx) error {
  159. b, err := tx.CreateBucket([]byte("widgets"))
  160. if err != nil {
  161. t.Fatal(err)
  162. }
  163. for i := 1; i < count; i++ {
  164. if err := b.Put([]byte(strings.Repeat("0", i*factor)), []byte(strings.Repeat("X", (count-i)*factor))); err != nil {
  165. t.Fatal(err)
  166. }
  167. }
  168. return nil
  169. }); err != nil {
  170. t.Fatal(err)
  171. }
  172. if err := db.View(func(tx *bolt.Tx) error {
  173. b := tx.Bucket([]byte("widgets"))
  174. for i := 1; i < count; i++ {
  175. value := b.Get([]byte(strings.Repeat("0", i*factor)))
  176. if !bytes.Equal(value, []byte(strings.Repeat("X", (count-i)*factor))) {
  177. t.Fatalf("unexpected value: %v", value)
  178. }
  179. }
  180. return nil
  181. }); err != nil {
  182. t.Fatal(err)
  183. }
  184. }
  185. // Ensure that a database can perform multiple large appends safely.
  186. func TestDB_Put_VeryLarge(t *testing.T) {
  187. if testing.Short() {
  188. t.Skip("skipping test in short mode.")
  189. }
  190. n, batchN := 400000, 200000
  191. ksize, vsize := 8, 500
  192. db := MustOpenDB()
  193. defer db.MustClose()
  194. for i := 0; i < n; i += batchN {
  195. if err := db.Update(func(tx *bolt.Tx) error {
  196. b, err := tx.CreateBucketIfNotExists([]byte("widgets"))
  197. if err != nil {
  198. t.Fatal(err)
  199. }
  200. for j := 0; j < batchN; j++ {
  201. k, v := make([]byte, ksize), make([]byte, vsize)
  202. binary.BigEndian.PutUint32(k, uint32(i+j))
  203. if err := b.Put(k, v); err != nil {
  204. t.Fatal(err)
  205. }
  206. }
  207. return nil
  208. }); err != nil {
  209. t.Fatal(err)
  210. }
  211. }
  212. }
  213. // Ensure that a setting a value on a key with a bucket value returns an error.
  214. func TestBucket_Put_IncompatibleValue(t *testing.T) {
  215. db := MustOpenDB()
  216. defer db.MustClose()
  217. if err := db.Update(func(tx *bolt.Tx) error {
  218. b0, err := tx.CreateBucket([]byte("widgets"))
  219. if err != nil {
  220. t.Fatal(err)
  221. }
  222. if _, err := tx.Bucket([]byte("widgets")).CreateBucket([]byte("foo")); err != nil {
  223. t.Fatal(err)
  224. }
  225. if err := b0.Put([]byte("foo"), []byte("bar")); err != bolt.ErrIncompatibleValue {
  226. t.Fatalf("unexpected error: %s", err)
  227. }
  228. return nil
  229. }); err != nil {
  230. t.Fatal(err)
  231. }
  232. }
  233. // Ensure that a setting a value while the transaction is closed returns an error.
  234. func TestBucket_Put_Closed(t *testing.T) {
  235. db := MustOpenDB()
  236. defer db.MustClose()
  237. tx, err := db.Begin(true)
  238. if err != nil {
  239. t.Fatal(err)
  240. }
  241. b, err := tx.CreateBucket([]byte("widgets"))
  242. if err != nil {
  243. t.Fatal(err)
  244. }
  245. if err := tx.Rollback(); err != nil {
  246. t.Fatal(err)
  247. }
  248. if err := b.Put([]byte("foo"), []byte("bar")); err != bolt.ErrTxClosed {
  249. t.Fatalf("unexpected error: %s", err)
  250. }
  251. }
  252. // Ensure that setting a value on a read-only bucket returns an error.
  253. func TestBucket_Put_ReadOnly(t *testing.T) {
  254. db := MustOpenDB()
  255. defer db.MustClose()
  256. if err := db.Update(func(tx *bolt.Tx) error {
  257. if _, err := tx.CreateBucket([]byte("widgets")); err != nil {
  258. t.Fatal(err)
  259. }
  260. return nil
  261. }); err != nil {
  262. t.Fatal(err)
  263. }
  264. if err := db.View(func(tx *bolt.Tx) error {
  265. b := tx.Bucket([]byte("widgets"))
  266. if err := b.Put([]byte("foo"), []byte("bar")); err != bolt.ErrTxNotWritable {
  267. t.Fatalf("unexpected error: %s", err)
  268. }
  269. return nil
  270. }); err != nil {
  271. t.Fatal(err)
  272. }
  273. }
  274. // Ensure that a bucket can delete an existing key.
  275. func TestBucket_Delete(t *testing.T) {
  276. db := MustOpenDB()
  277. defer db.MustClose()
  278. if err := db.Update(func(tx *bolt.Tx) error {
  279. b, err := tx.CreateBucket([]byte("widgets"))
  280. if err != nil {
  281. t.Fatal(err)
  282. }
  283. if err := b.Put([]byte("foo"), []byte("bar")); err != nil {
  284. t.Fatal(err)
  285. }
  286. if err := b.Delete([]byte("foo")); err != nil {
  287. t.Fatal(err)
  288. }
  289. if v := b.Get([]byte("foo")); v != nil {
  290. t.Fatalf("unexpected value: %v", v)
  291. }
  292. return nil
  293. }); err != nil {
  294. t.Fatal(err)
  295. }
  296. }
  297. // Ensure that deleting a large set of keys will work correctly.
  298. func TestBucket_Delete_Large(t *testing.T) {
  299. db := MustOpenDB()
  300. defer db.MustClose()
  301. if err := db.Update(func(tx *bolt.Tx) error {
  302. b, err := tx.CreateBucket([]byte("widgets"))
  303. if err != nil {
  304. t.Fatal(err)
  305. }
  306. for i := 0; i < 100; i++ {
  307. if err := b.Put([]byte(strconv.Itoa(i)), []byte(strings.Repeat("*", 1024))); err != nil {
  308. t.Fatal(err)
  309. }
  310. }
  311. return nil
  312. }); err != nil {
  313. t.Fatal(err)
  314. }
  315. if err := db.Update(func(tx *bolt.Tx) error {
  316. b := tx.Bucket([]byte("widgets"))
  317. for i := 0; i < 100; i++ {
  318. if err := b.Delete([]byte(strconv.Itoa(i))); err != nil {
  319. t.Fatal(err)
  320. }
  321. }
  322. return nil
  323. }); err != nil {
  324. t.Fatal(err)
  325. }
  326. if err := db.View(func(tx *bolt.Tx) error {
  327. b := tx.Bucket([]byte("widgets"))
  328. for i := 0; i < 100; i++ {
  329. if v := b.Get([]byte(strconv.Itoa(i))); v != nil {
  330. t.Fatalf("unexpected value: %v, i=%d", v, i)
  331. }
  332. }
  333. return nil
  334. }); err != nil {
  335. t.Fatal(err)
  336. }
  337. }
  338. // Deleting a very large list of keys will cause the freelist to use overflow.
  339. func TestBucket_Delete_FreelistOverflow(t *testing.T) {
  340. if testing.Short() {
  341. t.Skip("skipping test in short mode.")
  342. }
  343. db := MustOpenDB()
  344. defer db.MustClose()
  345. k := make([]byte, 16)
  346. for i := uint64(0); i < 10000; i++ {
  347. if err := db.Update(func(tx *bolt.Tx) error {
  348. b, err := tx.CreateBucketIfNotExists([]byte("0"))
  349. if err != nil {
  350. t.Fatalf("bucket error: %s", err)
  351. }
  352. for j := uint64(0); j < 1000; j++ {
  353. binary.BigEndian.PutUint64(k[:8], i)
  354. binary.BigEndian.PutUint64(k[8:], j)
  355. if err := b.Put(k, nil); err != nil {
  356. t.Fatalf("put error: %s", err)
  357. }
  358. }
  359. return nil
  360. }); err != nil {
  361. t.Fatal(err)
  362. }
  363. }
  364. // Delete all of them in one large transaction
  365. if err := db.Update(func(tx *bolt.Tx) error {
  366. b := tx.Bucket([]byte("0"))
  367. c := b.Cursor()
  368. for k, _ := c.First(); k != nil; k, _ = c.Next() {
  369. if err := c.Delete(); err != nil {
  370. t.Fatal(err)
  371. }
  372. }
  373. return nil
  374. }); err != nil {
  375. t.Fatal(err)
  376. }
  377. // Check more than an overflow's worth of pages are freed.
  378. stats := db.Stats()
  379. freePages := stats.FreePageN + stats.PendingPageN
  380. if freePages <= 0xFFFF {
  381. t.Fatalf("expected more than 0xFFFF free pages, got %v", freePages)
  382. }
  383. // Free page count should be preserved on reopen.
  384. if err := db.DB.Close(); err != nil {
  385. t.Fatal(err)
  386. }
  387. db.MustReopen()
  388. if reopenFreePages := db.Stats().FreePageN; freePages != reopenFreePages {
  389. t.Fatalf("expected %d free pages, got %+v", freePages, db.Stats())
  390. }
  391. }
  392. // Ensure that deleting of non-existing key is a no-op.
  393. func TestBucket_Delete_NonExisting(t *testing.T) {
  394. db := MustOpenDB()
  395. defer db.MustClose()
  396. if err := db.Update(func(tx *bolt.Tx) error {
  397. b, err := tx.CreateBucket([]byte("widgets"))
  398. if err != nil {
  399. t.Fatal(err)
  400. }
  401. if _, err = b.CreateBucket([]byte("nested")); err != nil {
  402. t.Fatal(err)
  403. }
  404. return nil
  405. }); err != nil {
  406. t.Fatal(err)
  407. }
  408. if err := db.Update(func(tx *bolt.Tx) error {
  409. b := tx.Bucket([]byte("widgets"))
  410. if err := b.Delete([]byte("foo")); err != nil {
  411. t.Fatal(err)
  412. }
  413. if b.Bucket([]byte("nested")) == nil {
  414. t.Fatal("nested bucket has been deleted")
  415. }
  416. return nil
  417. }); err != nil {
  418. t.Fatal(err)
  419. }
  420. }
  421. // Ensure that accessing and updating nested buckets is ok across transactions.
  422. func TestBucket_Nested(t *testing.T) {
  423. db := MustOpenDB()
  424. defer db.MustClose()
  425. if err := db.Update(func(tx *bolt.Tx) error {
  426. // Create a widgets bucket.
  427. b, err := tx.CreateBucket([]byte("widgets"))
  428. if err != nil {
  429. t.Fatal(err)
  430. }
  431. // Create a widgets/foo bucket.
  432. _, err = b.CreateBucket([]byte("foo"))
  433. if err != nil {
  434. t.Fatal(err)
  435. }
  436. // Create a widgets/bar key.
  437. if err := b.Put([]byte("bar"), []byte("0000")); err != nil {
  438. t.Fatal(err)
  439. }
  440. return nil
  441. }); err != nil {
  442. t.Fatal(err)
  443. }
  444. db.MustCheck()
  445. // Update widgets/bar.
  446. if err := db.Update(func(tx *bolt.Tx) error {
  447. b := tx.Bucket([]byte("widgets"))
  448. if err := b.Put([]byte("bar"), []byte("xxxx")); err != nil {
  449. t.Fatal(err)
  450. }
  451. return nil
  452. }); err != nil {
  453. t.Fatal(err)
  454. }
  455. db.MustCheck()
  456. // Cause a split.
  457. if err := db.Update(func(tx *bolt.Tx) error {
  458. var b = tx.Bucket([]byte("widgets"))
  459. for i := 0; i < 10000; i++ {
  460. if err := b.Put([]byte(strconv.Itoa(i)), []byte(strconv.Itoa(i))); err != nil {
  461. t.Fatal(err)
  462. }
  463. }
  464. return nil
  465. }); err != nil {
  466. t.Fatal(err)
  467. }
  468. db.MustCheck()
  469. // Insert into widgets/foo/baz.
  470. if err := db.Update(func(tx *bolt.Tx) error {
  471. var b = tx.Bucket([]byte("widgets"))
  472. if err := b.Bucket([]byte("foo")).Put([]byte("baz"), []byte("yyyy")); err != nil {
  473. t.Fatal(err)
  474. }
  475. return nil
  476. }); err != nil {
  477. t.Fatal(err)
  478. }
  479. db.MustCheck()
  480. // Verify.
  481. if err := db.View(func(tx *bolt.Tx) error {
  482. var b = tx.Bucket([]byte("widgets"))
  483. if v := b.Bucket([]byte("foo")).Get([]byte("baz")); !bytes.Equal(v, []byte("yyyy")) {
  484. t.Fatalf("unexpected value: %v", v)
  485. }
  486. if v := b.Get([]byte("bar")); !bytes.Equal(v, []byte("xxxx")) {
  487. t.Fatalf("unexpected value: %v", v)
  488. }
  489. for i := 0; i < 10000; i++ {
  490. if v := b.Get([]byte(strconv.Itoa(i))); !bytes.Equal(v, []byte(strconv.Itoa(i))) {
  491. t.Fatalf("unexpected value: %v", v)
  492. }
  493. }
  494. return nil
  495. }); err != nil {
  496. t.Fatal(err)
  497. }
  498. }
  499. // Ensure that deleting a bucket using Delete() returns an error.
  500. func TestBucket_Delete_Bucket(t *testing.T) {
  501. db := MustOpenDB()
  502. defer db.MustClose()
  503. if err := db.Update(func(tx *bolt.Tx) error {
  504. b, err := tx.CreateBucket([]byte("widgets"))
  505. if err != nil {
  506. t.Fatal(err)
  507. }
  508. if _, err := b.CreateBucket([]byte("foo")); err != nil {
  509. t.Fatal(err)
  510. }
  511. if err := b.Delete([]byte("foo")); err != bolt.ErrIncompatibleValue {
  512. t.Fatalf("unexpected error: %s", err)
  513. }
  514. return nil
  515. }); err != nil {
  516. t.Fatal(err)
  517. }
  518. }
  519. // Ensure that deleting a key on a read-only bucket returns an error.
  520. func TestBucket_Delete_ReadOnly(t *testing.T) {
  521. db := MustOpenDB()
  522. defer db.MustClose()
  523. if err := db.Update(func(tx *bolt.Tx) error {
  524. if _, err := tx.CreateBucket([]byte("widgets")); err != nil {
  525. t.Fatal(err)
  526. }
  527. return nil
  528. }); err != nil {
  529. t.Fatal(err)
  530. }
  531. if err := db.View(func(tx *bolt.Tx) error {
  532. if err := tx.Bucket([]byte("widgets")).Delete([]byte("foo")); err != bolt.ErrTxNotWritable {
  533. t.Fatalf("unexpected error: %s", err)
  534. }
  535. return nil
  536. }); err != nil {
  537. t.Fatal(err)
  538. }
  539. }
  540. // Ensure that a deleting value while the transaction is closed returns an error.
  541. func TestBucket_Delete_Closed(t *testing.T) {
  542. db := MustOpenDB()
  543. defer db.MustClose()
  544. tx, err := db.Begin(true)
  545. if err != nil {
  546. t.Fatal(err)
  547. }
  548. b, err := tx.CreateBucket([]byte("widgets"))
  549. if err != nil {
  550. t.Fatal(err)
  551. }
  552. if err := tx.Rollback(); err != nil {
  553. t.Fatal(err)
  554. }
  555. if err := b.Delete([]byte("foo")); err != bolt.ErrTxClosed {
  556. t.Fatalf("unexpected error: %s", err)
  557. }
  558. }
  559. // Ensure that deleting a bucket causes nested buckets to be deleted.
  560. func TestBucket_DeleteBucket_Nested(t *testing.T) {
  561. db := MustOpenDB()
  562. defer db.MustClose()
  563. if err := db.Update(func(tx *bolt.Tx) error {
  564. widgets, err := tx.CreateBucket([]byte("widgets"))
  565. if err != nil {
  566. t.Fatal(err)
  567. }
  568. foo, err := widgets.CreateBucket([]byte("foo"))
  569. if err != nil {
  570. t.Fatal(err)
  571. }
  572. bar, err := foo.CreateBucket([]byte("bar"))
  573. if err != nil {
  574. t.Fatal(err)
  575. }
  576. if err := bar.Put([]byte("baz"), []byte("bat")); err != nil {
  577. t.Fatal(err)
  578. }
  579. if err := tx.Bucket([]byte("widgets")).DeleteBucket([]byte("foo")); err != nil {
  580. t.Fatal(err)
  581. }
  582. return nil
  583. }); err != nil {
  584. t.Fatal(err)
  585. }
  586. }
  587. // Ensure that deleting a bucket causes nested buckets to be deleted after they have been committed.
  588. func TestBucket_DeleteBucket_Nested2(t *testing.T) {
  589. db := MustOpenDB()
  590. defer db.MustClose()
  591. if err := db.Update(func(tx *bolt.Tx) error {
  592. widgets, err := tx.CreateBucket([]byte("widgets"))
  593. if err != nil {
  594. t.Fatal(err)
  595. }
  596. foo, err := widgets.CreateBucket([]byte("foo"))
  597. if err != nil {
  598. t.Fatal(err)
  599. }
  600. bar, err := foo.CreateBucket([]byte("bar"))
  601. if err != nil {
  602. t.Fatal(err)
  603. }
  604. if err := bar.Put([]byte("baz"), []byte("bat")); err != nil {
  605. t.Fatal(err)
  606. }
  607. return nil
  608. }); err != nil {
  609. t.Fatal(err)
  610. }
  611. if err := db.Update(func(tx *bolt.Tx) error {
  612. widgets := tx.Bucket([]byte("widgets"))
  613. if widgets == nil {
  614. t.Fatal("expected widgets bucket")
  615. }
  616. foo := widgets.Bucket([]byte("foo"))
  617. if foo == nil {
  618. t.Fatal("expected foo bucket")
  619. }
  620. bar := foo.Bucket([]byte("bar"))
  621. if bar == nil {
  622. t.Fatal("expected bar bucket")
  623. }
  624. if v := bar.Get([]byte("baz")); !bytes.Equal(v, []byte("bat")) {
  625. t.Fatalf("unexpected value: %v", v)
  626. }
  627. if err := tx.DeleteBucket([]byte("widgets")); err != nil {
  628. t.Fatal(err)
  629. }
  630. return nil
  631. }); err != nil {
  632. t.Fatal(err)
  633. }
  634. if err := db.View(func(tx *bolt.Tx) error {
  635. if tx.Bucket([]byte("widgets")) != nil {
  636. t.Fatal("expected bucket to be deleted")
  637. }
  638. return nil
  639. }); err != nil {
  640. t.Fatal(err)
  641. }
  642. }
  643. // Ensure that deleting a child bucket with multiple pages causes all pages to get collected.
  644. // NOTE: Consistency check in bolt_test.DB.Close() will panic if pages not freed properly.
  645. func TestBucket_DeleteBucket_Large(t *testing.T) {
  646. db := MustOpenDB()
  647. defer db.MustClose()
  648. if err := db.Update(func(tx *bolt.Tx) error {
  649. widgets, err := tx.CreateBucket([]byte("widgets"))
  650. if err != nil {
  651. t.Fatal(err)
  652. }
  653. foo, err := widgets.CreateBucket([]byte("foo"))
  654. if err != nil {
  655. t.Fatal(err)
  656. }
  657. for i := 0; i < 1000; i++ {
  658. if err := foo.Put([]byte(fmt.Sprintf("%d", i)), []byte(fmt.Sprintf("%0100d", i))); err != nil {
  659. t.Fatal(err)
  660. }
  661. }
  662. return nil
  663. }); err != nil {
  664. t.Fatal(err)
  665. }
  666. if err := db.Update(func(tx *bolt.Tx) error {
  667. if err := tx.DeleteBucket([]byte("widgets")); err != nil {
  668. t.Fatal(err)
  669. }
  670. return nil
  671. }); err != nil {
  672. t.Fatal(err)
  673. }
  674. }
  675. // Ensure that a simple value retrieved via Bucket() returns a nil.
  676. func TestBucket_Bucket_IncompatibleValue(t *testing.T) {
  677. db := MustOpenDB()
  678. defer db.MustClose()
  679. if err := db.Update(func(tx *bolt.Tx) error {
  680. widgets, err := tx.CreateBucket([]byte("widgets"))
  681. if err != nil {
  682. t.Fatal(err)
  683. }
  684. if err := widgets.Put([]byte("foo"), []byte("bar")); err != nil {
  685. t.Fatal(err)
  686. }
  687. if b := tx.Bucket([]byte("widgets")).Bucket([]byte("foo")); b != nil {
  688. t.Fatal("expected nil bucket")
  689. }
  690. return nil
  691. }); err != nil {
  692. t.Fatal(err)
  693. }
  694. }
  695. // Ensure that creating a bucket on an existing non-bucket key returns an error.
  696. func TestBucket_CreateBucket_IncompatibleValue(t *testing.T) {
  697. db := MustOpenDB()
  698. defer db.MustClose()
  699. if err := db.Update(func(tx *bolt.Tx) error {
  700. widgets, err := tx.CreateBucket([]byte("widgets"))
  701. if err != nil {
  702. t.Fatal(err)
  703. }
  704. if err := widgets.Put([]byte("foo"), []byte("bar")); err != nil {
  705. t.Fatal(err)
  706. }
  707. if _, err := widgets.CreateBucket([]byte("foo")); err != bolt.ErrIncompatibleValue {
  708. t.Fatalf("unexpected error: %s", err)
  709. }
  710. return nil
  711. }); err != nil {
  712. t.Fatal(err)
  713. }
  714. }
  715. // Ensure that deleting a bucket on an existing non-bucket key returns an error.
  716. func TestBucket_DeleteBucket_IncompatibleValue(t *testing.T) {
  717. db := MustOpenDB()
  718. defer db.MustClose()
  719. if err := db.Update(func(tx *bolt.Tx) error {
  720. widgets, err := tx.CreateBucket([]byte("widgets"))
  721. if err != nil {
  722. t.Fatal(err)
  723. }
  724. if err := widgets.Put([]byte("foo"), []byte("bar")); err != nil {
  725. t.Fatal(err)
  726. }
  727. if err := tx.Bucket([]byte("widgets")).DeleteBucket([]byte("foo")); err != bolt.ErrIncompatibleValue {
  728. t.Fatalf("unexpected error: %s", err)
  729. }
  730. return nil
  731. }); err != nil {
  732. t.Fatal(err)
  733. }
  734. }
  735. // Ensure bucket can set and update its sequence number.
  736. func TestBucket_Sequence(t *testing.T) {
  737. db := MustOpenDB()
  738. defer db.MustClose()
  739. if err := db.Update(func(tx *bolt.Tx) error {
  740. bkt, err := tx.CreateBucket([]byte("0"))
  741. if err != nil {
  742. t.Fatal(err)
  743. }
  744. // Retrieve sequence.
  745. if v := bkt.Sequence(); v != 0 {
  746. t.Fatalf("unexpected sequence: %d", v)
  747. }
  748. // Update sequence.
  749. if err := bkt.SetSequence(1000); err != nil {
  750. t.Fatal(err)
  751. }
  752. // Read sequence again.
  753. if v := bkt.Sequence(); v != 1000 {
  754. t.Fatalf("unexpected sequence: %d", v)
  755. }
  756. return nil
  757. }); err != nil {
  758. t.Fatal(err)
  759. }
  760. // Verify sequence in separate transaction.
  761. if err := db.View(func(tx *bolt.Tx) error {
  762. if v := tx.Bucket([]byte("0")).Sequence(); v != 1000 {
  763. t.Fatalf("unexpected sequence: %d", v)
  764. }
  765. return nil
  766. }); err != nil {
  767. t.Fatal(err)
  768. }
  769. }
  770. // Ensure that a bucket can return an autoincrementing sequence.
  771. func TestBucket_NextSequence(t *testing.T) {
  772. db := MustOpenDB()
  773. defer db.MustClose()
  774. if err := db.Update(func(tx *bolt.Tx) error {
  775. widgets, err := tx.CreateBucket([]byte("widgets"))
  776. if err != nil {
  777. t.Fatal(err)
  778. }
  779. woojits, err := tx.CreateBucket([]byte("woojits"))
  780. if err != nil {
  781. t.Fatal(err)
  782. }
  783. // Make sure sequence increments.
  784. if seq, err := widgets.NextSequence(); err != nil {
  785. t.Fatal(err)
  786. } else if seq != 1 {
  787. t.Fatalf("unexpecte sequence: %d", seq)
  788. }
  789. if seq, err := widgets.NextSequence(); err != nil {
  790. t.Fatal(err)
  791. } else if seq != 2 {
  792. t.Fatalf("unexpected sequence: %d", seq)
  793. }
  794. // Buckets should be separate.
  795. if seq, err := woojits.NextSequence(); err != nil {
  796. t.Fatal(err)
  797. } else if seq != 1 {
  798. t.Fatalf("unexpected sequence: %d", 1)
  799. }
  800. return nil
  801. }); err != nil {
  802. t.Fatal(err)
  803. }
  804. }
  805. // Ensure that a bucket will persist an autoincrementing sequence even if its
  806. // the only thing updated on the bucket.
  807. // https://github.com/boltdb/bolt/issues/296
  808. func TestBucket_NextSequence_Persist(t *testing.T) {
  809. db := MustOpenDB()
  810. defer db.MustClose()
  811. if err := db.Update(func(tx *bolt.Tx) error {
  812. if _, err := tx.CreateBucket([]byte("widgets")); err != nil {
  813. t.Fatal(err)
  814. }
  815. return nil
  816. }); err != nil {
  817. t.Fatal(err)
  818. }
  819. if err := db.Update(func(tx *bolt.Tx) error {
  820. if _, err := tx.Bucket([]byte("widgets")).NextSequence(); err != nil {
  821. t.Fatal(err)
  822. }
  823. return nil
  824. }); err != nil {
  825. t.Fatal(err)
  826. }
  827. if err := db.Update(func(tx *bolt.Tx) error {
  828. seq, err := tx.Bucket([]byte("widgets")).NextSequence()
  829. if err != nil {
  830. t.Fatalf("unexpected error: %s", err)
  831. } else if seq != 2 {
  832. t.Fatalf("unexpected sequence: %d", seq)
  833. }
  834. return nil
  835. }); err != nil {
  836. t.Fatal(err)
  837. }
  838. }
  839. // Ensure that retrieving the next sequence on a read-only bucket returns an error.
  840. func TestBucket_NextSequence_ReadOnly(t *testing.T) {
  841. db := MustOpenDB()
  842. defer db.MustClose()
  843. if err := db.Update(func(tx *bolt.Tx) error {
  844. if _, err := tx.CreateBucket([]byte("widgets")); err != nil {
  845. t.Fatal(err)
  846. }
  847. return nil
  848. }); err != nil {
  849. t.Fatal(err)
  850. }
  851. if err := db.View(func(tx *bolt.Tx) error {
  852. _, err := tx.Bucket([]byte("widgets")).NextSequence()
  853. if err != bolt.ErrTxNotWritable {
  854. t.Fatalf("unexpected error: %s", err)
  855. }
  856. return nil
  857. }); err != nil {
  858. t.Fatal(err)
  859. }
  860. }
  861. // Ensure that retrieving the next sequence for a bucket on a closed database return an error.
  862. func TestBucket_NextSequence_Closed(t *testing.T) {
  863. db := MustOpenDB()
  864. defer db.MustClose()
  865. tx, err := db.Begin(true)
  866. if err != nil {
  867. t.Fatal(err)
  868. }
  869. b, err := tx.CreateBucket([]byte("widgets"))
  870. if err != nil {
  871. t.Fatal(err)
  872. }
  873. if err := tx.Rollback(); err != nil {
  874. t.Fatal(err)
  875. }
  876. if _, err := b.NextSequence(); err != bolt.ErrTxClosed {
  877. t.Fatal(err)
  878. }
  879. }
  880. // Ensure a user can loop over all key/value pairs in a bucket.
  881. func TestBucket_ForEach(t *testing.T) {
  882. db := MustOpenDB()
  883. defer db.MustClose()
  884. if err := db.Update(func(tx *bolt.Tx) error {
  885. b, err := tx.CreateBucket([]byte("widgets"))
  886. if err != nil {
  887. t.Fatal(err)
  888. }
  889. if err := b.Put([]byte("foo"), []byte("0000")); err != nil {
  890. t.Fatal(err)
  891. }
  892. if err := b.Put([]byte("baz"), []byte("0001")); err != nil {
  893. t.Fatal(err)
  894. }
  895. if err := b.Put([]byte("bar"), []byte("0002")); err != nil {
  896. t.Fatal(err)
  897. }
  898. var index int
  899. if err := b.ForEach(func(k, v []byte) error {
  900. switch index {
  901. case 0:
  902. if !bytes.Equal(k, []byte("bar")) {
  903. t.Fatalf("unexpected key: %v", k)
  904. } else if !bytes.Equal(v, []byte("0002")) {
  905. t.Fatalf("unexpected value: %v", v)
  906. }
  907. case 1:
  908. if !bytes.Equal(k, []byte("baz")) {
  909. t.Fatalf("unexpected key: %v", k)
  910. } else if !bytes.Equal(v, []byte("0001")) {
  911. t.Fatalf("unexpected value: %v", v)
  912. }
  913. case 2:
  914. if !bytes.Equal(k, []byte("foo")) {
  915. t.Fatalf("unexpected key: %v", k)
  916. } else if !bytes.Equal(v, []byte("0000")) {
  917. t.Fatalf("unexpected value: %v", v)
  918. }
  919. }
  920. index++
  921. return nil
  922. }); err != nil {
  923. t.Fatal(err)
  924. }
  925. if index != 3 {
  926. t.Fatalf("unexpected index: %d", index)
  927. }
  928. return nil
  929. }); err != nil {
  930. t.Fatal(err)
  931. }
  932. }
  933. // Ensure a database can stop iteration early.
  934. func TestBucket_ForEach_ShortCircuit(t *testing.T) {
  935. db := MustOpenDB()
  936. defer db.MustClose()
  937. if err := db.Update(func(tx *bolt.Tx) error {
  938. b, err := tx.CreateBucket([]byte("widgets"))
  939. if err != nil {
  940. t.Fatal(err)
  941. }
  942. if err := b.Put([]byte("bar"), []byte("0000")); err != nil {
  943. t.Fatal(err)
  944. }
  945. if err := b.Put([]byte("baz"), []byte("0000")); err != nil {
  946. t.Fatal(err)
  947. }
  948. if err := b.Put([]byte("foo"), []byte("0000")); err != nil {
  949. t.Fatal(err)
  950. }
  951. var index int
  952. if err := tx.Bucket([]byte("widgets")).ForEach(func(k, v []byte) error {
  953. index++
  954. if bytes.Equal(k, []byte("baz")) {
  955. return errors.New("marker")
  956. }
  957. return nil
  958. }); err == nil || err.Error() != "marker" {
  959. t.Fatalf("unexpected error: %s", err)
  960. }
  961. if index != 2 {
  962. t.Fatalf("unexpected index: %d", index)
  963. }
  964. return nil
  965. }); err != nil {
  966. t.Fatal(err)
  967. }
  968. }
  969. // Ensure that looping over a bucket on a closed database returns an error.
  970. func TestBucket_ForEach_Closed(t *testing.T) {
  971. db := MustOpenDB()
  972. defer db.MustClose()
  973. tx, err := db.Begin(true)
  974. if err != nil {
  975. t.Fatal(err)
  976. }
  977. b, err := tx.CreateBucket([]byte("widgets"))
  978. if err != nil {
  979. t.Fatal(err)
  980. }
  981. if err := tx.Rollback(); err != nil {
  982. t.Fatal(err)
  983. }
  984. if err := b.ForEach(func(k, v []byte) error { return nil }); err != bolt.ErrTxClosed {
  985. t.Fatalf("unexpected error: %s", err)
  986. }
  987. }
  988. // Ensure that an error is returned when inserting with an empty key.
  989. func TestBucket_Put_EmptyKey(t *testing.T) {
  990. db := MustOpenDB()
  991. defer db.MustClose()
  992. if err := db.Update(func(tx *bolt.Tx) error {
  993. b, err := tx.CreateBucket([]byte("widgets"))
  994. if err != nil {
  995. t.Fatal(err)
  996. }
  997. if err := b.Put([]byte(""), []byte("bar")); err != bolt.ErrKeyRequired {
  998. t.Fatalf("unexpected error: %s", err)
  999. }
  1000. if err := b.Put(nil, []byte("bar")); err != bolt.ErrKeyRequired {
  1001. t.Fatalf("unexpected error: %s", err)
  1002. }
  1003. return nil
  1004. }); err != nil {
  1005. t.Fatal(err)
  1006. }
  1007. }
  1008. // Ensure that an error is returned when inserting with a key that's too large.
  1009. func TestBucket_Put_KeyTooLarge(t *testing.T) {
  1010. db := MustOpenDB()
  1011. defer db.MustClose()
  1012. if err := db.Update(func(tx *bolt.Tx) error {
  1013. b, err := tx.CreateBucket([]byte("widgets"))
  1014. if err != nil {
  1015. t.Fatal(err)
  1016. }
  1017. if err := b.Put(make([]byte, 32769), []byte("bar")); err != bolt.ErrKeyTooLarge {
  1018. t.Fatalf("unexpected error: %s", err)
  1019. }
  1020. return nil
  1021. }); err != nil {
  1022. t.Fatal(err)
  1023. }
  1024. }
  1025. // Ensure that an error is returned when inserting a value that's too large.
  1026. func TestBucket_Put_ValueTooLarge(t *testing.T) {
  1027. // Skip this test on DroneCI because the machine is resource constrained.
  1028. if os.Getenv("DRONE") == "true" {
  1029. t.Skip("not enough RAM for test")
  1030. }
  1031. db := MustOpenDB()
  1032. defer db.MustClose()
  1033. if err := db.Update(func(tx *bolt.Tx) error {
  1034. b, err := tx.CreateBucket([]byte("widgets"))
  1035. if err != nil {
  1036. t.Fatal(err)
  1037. }
  1038. if err := b.Put([]byte("foo"), make([]byte, bolt.MaxValueSize+1)); err != bolt.ErrValueTooLarge {
  1039. t.Fatalf("unexpected error: %s", err)
  1040. }
  1041. return nil
  1042. }); err != nil {
  1043. t.Fatal(err)
  1044. }
  1045. }
  1046. // Ensure a bucket can calculate stats.
  1047. func TestBucket_Stats(t *testing.T) {
  1048. db := MustOpenDB()
  1049. defer db.MustClose()
  1050. // Add bucket with fewer keys but one big value.
  1051. bigKey := []byte("really-big-value")
  1052. for i := 0; i < 500; i++ {
  1053. if err := db.Update(func(tx *bolt.Tx) error {
  1054. b, err := tx.CreateBucketIfNotExists([]byte("woojits"))
  1055. if err != nil {
  1056. t.Fatal(err)
  1057. }
  1058. if err := b.Put([]byte(fmt.Sprintf("%03d", i)), []byte(strconv.Itoa(i))); err != nil {
  1059. t.Fatal(err)
  1060. }
  1061. return nil
  1062. }); err != nil {
  1063. t.Fatal(err)
  1064. }
  1065. }
  1066. if err := db.Update(func(tx *bolt.Tx) error {
  1067. if err := tx.Bucket([]byte("woojits")).Put(bigKey, []byte(strings.Repeat("*", 10000))); err != nil {
  1068. t.Fatal(err)
  1069. }
  1070. return nil
  1071. }); err != nil {
  1072. t.Fatal(err)
  1073. }
  1074. db.MustCheck()
  1075. if err := db.View(func(tx *bolt.Tx) error {
  1076. stats := tx.Bucket([]byte("woojits")).Stats()
  1077. if stats.BranchPageN != 1 {
  1078. t.Fatalf("unexpected BranchPageN: %d", stats.BranchPageN)
  1079. } else if stats.BranchOverflowN != 0 {
  1080. t.Fatalf("unexpected BranchOverflowN: %d", stats.BranchOverflowN)
  1081. } else if stats.LeafPageN != 7 {
  1082. t.Fatalf("unexpected LeafPageN: %d", stats.LeafPageN)
  1083. } else if stats.LeafOverflowN != 2 {
  1084. t.Fatalf("unexpected LeafOverflowN: %d", stats.LeafOverflowN)
  1085. } else if stats.KeyN != 501 {
  1086. t.Fatalf("unexpected KeyN: %d", stats.KeyN)
  1087. } else if stats.Depth != 2 {
  1088. t.Fatalf("unexpected Depth: %d", stats.Depth)
  1089. }
  1090. branchInuse := 16 // branch page header
  1091. branchInuse += 7 * 16 // branch elements
  1092. branchInuse += 7 * 3 // branch keys (6 3-byte keys)
  1093. if stats.BranchInuse != branchInuse {
  1094. t.Fatalf("unexpected BranchInuse: %d", stats.BranchInuse)
  1095. }
  1096. leafInuse := 7 * 16 // leaf page header
  1097. leafInuse += 501 * 16 // leaf elements
  1098. leafInuse += 500*3 + len(bigKey) // leaf keys
  1099. leafInuse += 1*10 + 2*90 + 3*400 + 10000 // leaf values
  1100. if stats.LeafInuse != leafInuse {
  1101. t.Fatalf("unexpected LeafInuse: %d", stats.LeafInuse)
  1102. }
  1103. // Only check allocations for 4KB pages.
  1104. if db.Info().PageSize == 4096 {
  1105. if stats.BranchAlloc != 4096 {
  1106. t.Fatalf("unexpected BranchAlloc: %d", stats.BranchAlloc)
  1107. } else if stats.LeafAlloc != 36864 {
  1108. t.Fatalf("unexpected LeafAlloc: %d", stats.LeafAlloc)
  1109. }
  1110. }
  1111. if stats.BucketN != 1 {
  1112. t.Fatalf("unexpected BucketN: %d", stats.BucketN)
  1113. } else if stats.InlineBucketN != 0 {
  1114. t.Fatalf("unexpected InlineBucketN: %d", stats.InlineBucketN)
  1115. } else if stats.InlineBucketInuse != 0 {
  1116. t.Fatalf("unexpected InlineBucketInuse: %d", stats.InlineBucketInuse)
  1117. }
  1118. return nil
  1119. }); err != nil {
  1120. t.Fatal(err)
  1121. }
  1122. }
  1123. // Ensure a bucket with random insertion utilizes fill percentage correctly.
  1124. func TestBucket_Stats_RandomFill(t *testing.T) {
  1125. if testing.Short() {
  1126. t.Skip("skipping test in short mode.")
  1127. } else if os.Getpagesize() != 4096 {
  1128. t.Skip("invalid page size for test")
  1129. }
  1130. db := MustOpenDB()
  1131. defer db.MustClose()
  1132. // Add a set of values in random order. It will be the same random
  1133. // order so we can maintain consistency between test runs.
  1134. var count int
  1135. rand := rand.New(rand.NewSource(42))
  1136. for _, i := range rand.Perm(1000) {
  1137. if err := db.Update(func(tx *bolt.Tx) error {
  1138. b, err := tx.CreateBucketIfNotExists([]byte("woojits"))
  1139. if err != nil {
  1140. t.Fatal(err)
  1141. }
  1142. b.FillPercent = 0.9
  1143. for _, j := range rand.Perm(100) {
  1144. index := (j * 10000) + i
  1145. if err := b.Put([]byte(fmt.Sprintf("%d000000000000000", index)), []byte("0000000000")); err != nil {
  1146. t.Fatal(err)
  1147. }
  1148. count++
  1149. }
  1150. return nil
  1151. }); err != nil {
  1152. t.Fatal(err)
  1153. }
  1154. }
  1155. db.MustCheck()
  1156. if err := db.View(func(tx *bolt.Tx) error {
  1157. stats := tx.Bucket([]byte("woojits")).Stats()
  1158. if stats.KeyN != 100000 {
  1159. t.Fatalf("unexpected KeyN: %d", stats.KeyN)
  1160. }
  1161. if stats.BranchPageN != 98 {
  1162. t.Fatalf("unexpected BranchPageN: %d", stats.BranchPageN)
  1163. } else if stats.BranchOverflowN != 0 {
  1164. t.Fatalf("unexpected BranchOverflowN: %d", stats.BranchOverflowN)
  1165. } else if stats.BranchInuse != 130984 {
  1166. t.Fatalf("unexpected BranchInuse: %d", stats.BranchInuse)
  1167. } else if stats.BranchAlloc != 401408 {
  1168. t.Fatalf("unexpected BranchAlloc: %d", stats.BranchAlloc)
  1169. }
  1170. if stats.LeafPageN != 3412 {
  1171. t.Fatalf("unexpected LeafPageN: %d", stats.LeafPageN)
  1172. } else if stats.LeafOverflowN != 0 {
  1173. t.Fatalf("unexpected LeafOverflowN: %d", stats.LeafOverflowN)
  1174. } else if stats.LeafInuse != 4742482 {
  1175. t.Fatalf("unexpected LeafInuse: %d", stats.LeafInuse)
  1176. } else if stats.LeafAlloc != 13975552 {
  1177. t.Fatalf("unexpected LeafAlloc: %d", stats.LeafAlloc)
  1178. }
  1179. return nil
  1180. }); err != nil {
  1181. t.Fatal(err)
  1182. }
  1183. }
  1184. // Ensure a bucket can calculate stats.
  1185. func TestBucket_Stats_Small(t *testing.T) {
  1186. db := MustOpenDB()
  1187. defer db.MustClose()
  1188. if err := db.Update(func(tx *bolt.Tx) error {
  1189. // Add a bucket that fits on a single root leaf.
  1190. b, err := tx.CreateBucket([]byte("whozawhats"))
  1191. if err != nil {
  1192. t.Fatal(err)
  1193. }
  1194. if err := b.Put([]byte("foo"), []byte("bar")); err != nil {
  1195. t.Fatal(err)
  1196. }
  1197. return nil
  1198. }); err != nil {
  1199. t.Fatal(err)
  1200. }
  1201. db.MustCheck()
  1202. if err := db.View(func(tx *bolt.Tx) error {
  1203. b := tx.Bucket([]byte("whozawhats"))
  1204. stats := b.Stats()
  1205. if stats.BranchPageN != 0 {
  1206. t.Fatalf("unexpected BranchPageN: %d", stats.BranchPageN)
  1207. } else if stats.BranchOverflowN != 0 {
  1208. t.Fatalf("unexpected BranchOverflowN: %d", stats.BranchOverflowN)
  1209. } else if stats.LeafPageN != 0 {
  1210. t.Fatalf("unexpected LeafPageN: %d", stats.LeafPageN)
  1211. } else if stats.LeafOverflowN != 0 {
  1212. t.Fatalf("unexpected LeafOverflowN: %d", stats.LeafOverflowN)
  1213. } else if stats.KeyN != 1 {
  1214. t.Fatalf("unexpected KeyN: %d", stats.KeyN)
  1215. } else if stats.Depth != 1 {
  1216. t.Fatalf("unexpected Depth: %d", stats.Depth)
  1217. } else if stats.BranchInuse != 0 {
  1218. t.Fatalf("unexpected BranchInuse: %d", stats.BranchInuse)
  1219. } else if stats.LeafInuse != 0 {
  1220. t.Fatalf("unexpected LeafInuse: %d", stats.LeafInuse)
  1221. }
  1222. if db.Info().PageSize == 4096 {
  1223. if stats.BranchAlloc != 0 {
  1224. t.Fatalf("unexpected BranchAlloc: %d", stats.BranchAlloc)
  1225. } else if stats.LeafAlloc != 0 {
  1226. t.Fatalf("unexpected LeafAlloc: %d", stats.LeafAlloc)
  1227. }
  1228. }
  1229. if stats.BucketN != 1 {
  1230. t.Fatalf("unexpected BucketN: %d", stats.BucketN)
  1231. } else if stats.InlineBucketN != 1 {
  1232. t.Fatalf("unexpected InlineBucketN: %d", stats.InlineBucketN)
  1233. } else if stats.InlineBucketInuse != 16+16+6 {
  1234. t.Fatalf("unexpected InlineBucketInuse: %d", stats.InlineBucketInuse)
  1235. }
  1236. return nil
  1237. }); err != nil {
  1238. t.Fatal(err)
  1239. }
  1240. }
  1241. func TestBucket_Stats_EmptyBucket(t *testing.T) {
  1242. db := MustOpenDB()
  1243. defer db.MustClose()
  1244. if err := db.Update(func(tx *bolt.Tx) error {
  1245. // Add a bucket that fits on a single root leaf.
  1246. if _, err := tx.CreateBucket([]byte("whozawhats")); err != nil {
  1247. t.Fatal(err)
  1248. }
  1249. return nil
  1250. }); err != nil {
  1251. t.Fatal(err)
  1252. }
  1253. db.MustCheck()
  1254. if err := db.View(func(tx *bolt.Tx) error {
  1255. b := tx.Bucket([]byte("whozawhats"))
  1256. stats := b.Stats()
  1257. if stats.BranchPageN != 0 {
  1258. t.Fatalf("unexpected BranchPageN: %d", stats.BranchPageN)
  1259. } else if stats.BranchOverflowN != 0 {
  1260. t.Fatalf("unexpected BranchOverflowN: %d", stats.BranchOverflowN)
  1261. } else if stats.LeafPageN != 0 {
  1262. t.Fatalf("unexpected LeafPageN: %d", stats.LeafPageN)
  1263. } else if stats.LeafOverflowN != 0 {
  1264. t.Fatalf("unexpected LeafOverflowN: %d", stats.LeafOverflowN)
  1265. } else if stats.KeyN != 0 {
  1266. t.Fatalf("unexpected KeyN: %d", stats.KeyN)
  1267. } else if stats.Depth != 1 {
  1268. t.Fatalf("unexpected Depth: %d", stats.Depth)
  1269. } else if stats.BranchInuse != 0 {
  1270. t.Fatalf("unexpected BranchInuse: %d", stats.BranchInuse)
  1271. } else if stats.LeafInuse != 0 {
  1272. t.Fatalf("unexpected LeafInuse: %d", stats.LeafInuse)
  1273. }
  1274. if db.Info().PageSize == 4096 {
  1275. if stats.BranchAlloc != 0 {
  1276. t.Fatalf("unexpected BranchAlloc: %d", stats.BranchAlloc)
  1277. } else if stats.LeafAlloc != 0 {
  1278. t.Fatalf("unexpected LeafAlloc: %d", stats.LeafAlloc)
  1279. }
  1280. }
  1281. if stats.BucketN != 1 {
  1282. t.Fatalf("unexpected BucketN: %d", stats.BucketN)
  1283. } else if stats.InlineBucketN != 1 {
  1284. t.Fatalf("unexpected InlineBucketN: %d", stats.InlineBucketN)
  1285. } else if stats.InlineBucketInuse != 16 {
  1286. t.Fatalf("unexpected InlineBucketInuse: %d", stats.InlineBucketInuse)
  1287. }
  1288. return nil
  1289. }); err != nil {
  1290. t.Fatal(err)
  1291. }
  1292. }
  1293. // Ensure a bucket can calculate stats.
  1294. func TestBucket_Stats_Nested(t *testing.T) {
  1295. db := MustOpenDB()
  1296. defer db.MustClose()
  1297. if err := db.Update(func(tx *bolt.Tx) error {
  1298. b, err := tx.CreateBucket([]byte("foo"))
  1299. if err != nil {
  1300. t.Fatal(err)
  1301. }
  1302. for i := 0; i < 100; i++ {
  1303. if err := b.Put([]byte(fmt.Sprintf("%02d", i)), []byte(fmt.Sprintf("%02d", i))); err != nil {
  1304. t.Fatal(err)
  1305. }
  1306. }
  1307. bar, err := b.CreateBucket([]byte("bar"))
  1308. if err != nil {
  1309. t.Fatal(err)
  1310. }
  1311. for i := 0; i < 10; i++ {
  1312. if err := bar.Put([]byte(strconv.Itoa(i)), []byte(strconv.Itoa(i))); err != nil {
  1313. t.Fatal(err)
  1314. }
  1315. }
  1316. baz, err := bar.CreateBucket([]byte("baz"))
  1317. if err != nil {
  1318. t.Fatal(err)
  1319. }
  1320. for i := 0; i < 10; i++ {
  1321. if err := baz.Put([]byte(strconv.Itoa(i)), []byte(strconv.Itoa(i))); err != nil {
  1322. t.Fatal(err)
  1323. }
  1324. }
  1325. return nil
  1326. }); err != nil {
  1327. t.Fatal(err)
  1328. }
  1329. db.MustCheck()
  1330. if err := db.View(func(tx *bolt.Tx) error {
  1331. b := tx.Bucket([]byte("foo"))
  1332. stats := b.Stats()
  1333. if stats.BranchPageN != 0 {
  1334. t.Fatalf("unexpected BranchPageN: %d", stats.BranchPageN)
  1335. } else if stats.BranchOverflowN != 0 {
  1336. t.Fatalf("unexpected BranchOverflowN: %d", stats.BranchOverflowN)
  1337. } else if stats.LeafPageN != 2 {
  1338. t.Fatalf("unexpected LeafPageN: %d", stats.LeafPageN)
  1339. } else if stats.LeafOverflowN != 0 {
  1340. t.Fatalf("unexpected LeafOverflowN: %d", stats.LeafOverflowN)
  1341. } else if stats.KeyN != 122 {
  1342. t.Fatalf("unexpected KeyN: %d", stats.KeyN)
  1343. } else if stats.Depth != 3 {
  1344. t.Fatalf("unexpected Depth: %d", stats.Depth)
  1345. } else if stats.BranchInuse != 0 {
  1346. t.Fatalf("unexpected BranchInuse: %d", stats.BranchInuse)
  1347. }
  1348. foo := 16 // foo (pghdr)
  1349. foo += 101 * 16 // foo leaf elements
  1350. foo += 100*2 + 100*2 // foo leaf key/values
  1351. foo += 3 + 16 // foo -> bar key/value
  1352. bar := 16 // bar (pghdr)
  1353. bar += 11 * 16 // bar leaf elements
  1354. bar += 10 + 10 // bar leaf key/values
  1355. bar += 3 + 16 // bar -> baz key/value
  1356. baz := 16 // baz (inline) (pghdr)
  1357. baz += 10 * 16 // baz leaf elements
  1358. baz += 10 + 10 // baz leaf key/values
  1359. if stats.LeafInuse != foo+bar+baz {
  1360. t.Fatalf("unexpected LeafInuse: %d", stats.LeafInuse)
  1361. }
  1362. if db.Info().PageSize == 4096 {
  1363. if stats.BranchAlloc != 0 {
  1364. t.Fatalf("unexpected BranchAlloc: %d", stats.BranchAlloc)
  1365. } else if stats.LeafAlloc != 8192 {
  1366. t.Fatalf("unexpected LeafAlloc: %d", stats.LeafAlloc)
  1367. }
  1368. }
  1369. if stats.BucketN != 3 {
  1370. t.Fatalf("unexpected BucketN: %d", stats.BucketN)
  1371. } else if stats.InlineBucketN != 1 {
  1372. t.Fatalf("unexpected InlineBucketN: %d", stats.InlineBucketN)
  1373. } else if stats.InlineBucketInuse != baz {
  1374. t.Fatalf("unexpected InlineBucketInuse: %d", stats.InlineBucketInuse)
  1375. }
  1376. return nil
  1377. }); err != nil {
  1378. t.Fatal(err)
  1379. }
  1380. }
  1381. // Ensure a large bucket can calculate stats.
  1382. func TestBucket_Stats_Large(t *testing.T) {
  1383. if testing.Short() {
  1384. t.Skip("skipping test in short mode.")
  1385. }
  1386. db := MustOpenDB()
  1387. defer db.MustClose()
  1388. var index int
  1389. for i := 0; i < 100; i++ {
  1390. // Add bucket with lots of keys.
  1391. if err := db.Update(func(tx *bolt.Tx) error {
  1392. b, err := tx.CreateBucketIfNotExists([]byte("widgets"))
  1393. if err != nil {
  1394. t.Fatal(err)
  1395. }
  1396. for i := 0; i < 1000; i++ {
  1397. if err := b.Put([]byte(strconv.Itoa(index)), []byte(strconv.Itoa(index))); err != nil {
  1398. t.Fatal(err)
  1399. }
  1400. index++
  1401. }
  1402. return nil
  1403. }); err != nil {
  1404. t.Fatal(err)
  1405. }
  1406. }
  1407. db.MustCheck()
  1408. if err := db.View(func(tx *bolt.Tx) error {
  1409. stats := tx.Bucket([]byte("widgets")).Stats()
  1410. if stats.BranchPageN != 13 {
  1411. t.Fatalf("unexpected BranchPageN: %d", stats.BranchPageN)
  1412. } else if stats.BranchOverflowN != 0 {
  1413. t.Fatalf("unexpected BranchOverflowN: %d", stats.BranchOverflowN)
  1414. } else if stats.LeafPageN != 1196 {
  1415. t.Fatalf("unexpected LeafPageN: %d", stats.LeafPageN)
  1416. } else if stats.LeafOverflowN != 0 {
  1417. t.Fatalf("unexpected LeafOverflowN: %d", stats.LeafOverflowN)
  1418. } else if stats.KeyN != 100000 {
  1419. t.Fatalf("unexpected KeyN: %d", stats.KeyN)
  1420. } else if stats.Depth != 3 {
  1421. t.Fatalf("unexpected Depth: %d", stats.Depth)
  1422. } else if stats.BranchInuse != 25257 {
  1423. t.Fatalf("unexpected BranchInuse: %d", stats.BranchInuse)
  1424. } else if stats.LeafInuse != 2596916 {
  1425. t.Fatalf("unexpected LeafInuse: %d", stats.LeafInuse)
  1426. }
  1427. if db.Info().PageSize == 4096 {
  1428. if stats.BranchAlloc != 53248 {
  1429. t.Fatalf("unexpected BranchAlloc: %d", stats.BranchAlloc)
  1430. } else if stats.LeafAlloc != 4898816 {
  1431. t.Fatalf("unexpected LeafAlloc: %d", stats.LeafAlloc)
  1432. }
  1433. }
  1434. if stats.BucketN != 1 {
  1435. t.Fatalf("unexpected BucketN: %d", stats.BucketN)
  1436. } else if stats.InlineBucketN != 0 {
  1437. t.Fatalf("unexpected InlineBucketN: %d", stats.InlineBucketN)
  1438. } else if stats.InlineBucketInuse != 0 {
  1439. t.Fatalf("unexpected InlineBucketInuse: %d", stats.InlineBucketInuse)
  1440. }
  1441. return nil
  1442. }); err != nil {
  1443. t.Fatal(err)
  1444. }
  1445. }
  1446. // Ensure that a bucket can write random keys and values across multiple transactions.
  1447. func TestBucket_Put_Single(t *testing.T) {
  1448. if testing.Short() {
  1449. t.Skip("skipping test in short mode.")
  1450. }
  1451. index := 0
  1452. if err := quick.Check(func(items testdata) bool {
  1453. db := MustOpenDB()
  1454. defer db.MustClose()
  1455. m := make(map[string][]byte)
  1456. if err := db.Update(func(tx *bolt.Tx) error {
  1457. if _, err := tx.CreateBucket([]byte("widgets")); err != nil {
  1458. t.Fatal(err)
  1459. }
  1460. return nil
  1461. }); err != nil {
  1462. t.Fatal(err)
  1463. }
  1464. for _, item := range items {
  1465. if err := db.Update(func(tx *bolt.Tx) error {
  1466. if err := tx.Bucket([]byte("widgets")).Put(item.Key, item.Value); err != nil {
  1467. panic("put error: " + err.Error())
  1468. }
  1469. m[string(item.Key)] = item.Value
  1470. return nil
  1471. }); err != nil {
  1472. t.Fatal(err)
  1473. }
  1474. // Verify all key/values so far.
  1475. if err := db.View(func(tx *bolt.Tx) error {
  1476. i := 0
  1477. for k, v := range m {
  1478. value := tx.Bucket([]byte("widgets")).Get([]byte(k))
  1479. if !bytes.Equal(value, v) {
  1480. t.Logf("value mismatch [run %d] (%d of %d):\nkey: %x\ngot: %x\nexp: %x", index, i, len(m), []byte(k), value, v)
  1481. db.CopyTempFile()
  1482. t.FailNow()
  1483. }
  1484. i++
  1485. }
  1486. return nil
  1487. }); err != nil {
  1488. t.Fatal(err)
  1489. }
  1490. }
  1491. index++
  1492. return true
  1493. }, qconfig()); err != nil {
  1494. t.Error(err)
  1495. }
  1496. }
  1497. // Ensure that a transaction can insert multiple key/value pairs at once.
  1498. func TestBucket_Put_Multiple(t *testing.T) {
  1499. if testing.Short() {
  1500. t.Skip("skipping test in short mode.")
  1501. }
  1502. if err := quick.Check(func(items testdata) bool {
  1503. db := MustOpenDB()
  1504. defer db.MustClose()
  1505. // Bulk insert all values.
  1506. if err := db.Update(func(tx *bolt.Tx) error {
  1507. if _, err := tx.CreateBucket([]byte("widgets")); err != nil {
  1508. t.Fatal(err)
  1509. }
  1510. return nil
  1511. }); err != nil {
  1512. t.Fatal(err)
  1513. }
  1514. if err := db.Update(func(tx *bolt.Tx) error {
  1515. b := tx.Bucket([]byte("widgets"))
  1516. for _, item := range items {
  1517. if err := b.Put(item.Key, item.Value); err != nil {
  1518. t.Fatal(err)
  1519. }
  1520. }
  1521. return nil
  1522. }); err != nil {
  1523. t.Fatal(err)
  1524. }
  1525. // Verify all items exist.
  1526. if err := db.View(func(tx *bolt.Tx) error {
  1527. b := tx.Bucket([]byte("widgets"))
  1528. for _, item := range items {
  1529. value := b.Get(item.Key)
  1530. if !bytes.Equal(item.Value, value) {
  1531. db.CopyTempFile()
  1532. t.Fatalf("exp=%x; got=%x", item.Value, value)
  1533. }
  1534. }
  1535. return nil
  1536. }); err != nil {
  1537. t.Fatal(err)
  1538. }
  1539. return true
  1540. }, qconfig()); err != nil {
  1541. t.Error(err)
  1542. }
  1543. }
  1544. // Ensure that a transaction can delete all key/value pairs and return to a single leaf page.
  1545. func TestBucket_Delete_Quick(t *testing.T) {
  1546. if testing.Short() {
  1547. t.Skip("skipping test in short mode.")
  1548. }
  1549. if err := quick.Check(func(items testdata) bool {
  1550. db := MustOpenDB()
  1551. defer db.MustClose()
  1552. // Bulk insert all values.
  1553. if err := db.Update(func(tx *bolt.Tx) error {
  1554. if _, err := tx.CreateBucket([]byte("widgets")); err != nil {
  1555. t.Fatal(err)
  1556. }
  1557. return nil
  1558. }); err != nil {
  1559. t.Fatal(err)
  1560. }
  1561. if err := db.Update(func(tx *bolt.Tx) error {
  1562. b := tx.Bucket([]byte("widgets"))
  1563. for _, item := range items {
  1564. if err := b.Put(item.Key, item.Value); err != nil {
  1565. t.Fatal(err)
  1566. }
  1567. }
  1568. return nil
  1569. }); err != nil {
  1570. t.Fatal(err)
  1571. }
  1572. // Remove items one at a time and check consistency.
  1573. for _, item := range items {
  1574. if err := db.Update(func(tx *bolt.Tx) error {
  1575. return tx.Bucket([]byte("widgets")).Delete(item.Key)
  1576. }); err != nil {
  1577. t.Fatal(err)
  1578. }
  1579. }
  1580. // Anything before our deletion index should be nil.
  1581. if err := db.View(func(tx *bolt.Tx) error {
  1582. if err := tx.Bucket([]byte("widgets")).ForEach(func(k, v []byte) error {
  1583. t.Fatalf("bucket should be empty; found: %06x", trunc(k, 3))
  1584. return nil
  1585. }); err != nil {
  1586. t.Fatal(err)
  1587. }
  1588. return nil
  1589. }); err != nil {
  1590. t.Fatal(err)
  1591. }
  1592. return true
  1593. }, qconfig()); err != nil {
  1594. t.Error(err)
  1595. }
  1596. }
  1597. func ExampleBucket_Put() {
  1598. // Open the database.
  1599. db, err := bolt.Open(tempfile(), 0666, nil)
  1600. if err != nil {
  1601. log.Fatal(err)
  1602. }
  1603. defer os.Remove(db.Path())
  1604. // Start a write transaction.
  1605. if err := db.Update(func(tx *bolt.Tx) error {
  1606. // Create a bucket.
  1607. b, err := tx.CreateBucket([]byte("widgets"))
  1608. if err != nil {
  1609. return err
  1610. }
  1611. // Set the value "bar" for the key "foo".
  1612. if err := b.Put([]byte("foo"), []byte("bar")); err != nil {
  1613. return err
  1614. }
  1615. return nil
  1616. }); err != nil {
  1617. log.Fatal(err)
  1618. }
  1619. // Read value back in a different read-only transaction.
  1620. if err := db.View(func(tx *bolt.Tx) error {
  1621. value := tx.Bucket([]byte("widgets")).Get([]byte("foo"))
  1622. fmt.Printf("The value of 'foo' is: %s\n", value)
  1623. return nil
  1624. }); err != nil {
  1625. log.Fatal(err)
  1626. }
  1627. // Close database to release file lock.
  1628. if err := db.Close(); err != nil {
  1629. log.Fatal(err)
  1630. }
  1631. // Output:
  1632. // The value of 'foo' is: bar
  1633. }
  1634. func ExampleBucket_Delete() {
  1635. // Open the database.
  1636. db, err := bolt.Open(tempfile(), 0666, nil)
  1637. if err != nil {
  1638. log.Fatal(err)
  1639. }
  1640. defer os.Remove(db.Path())
  1641. // Start a write transaction.
  1642. if err := db.Update(func(tx *bolt.Tx) error {
  1643. // Create a bucket.
  1644. b, err := tx.CreateBucket([]byte("widgets"))
  1645. if err != nil {
  1646. return err
  1647. }
  1648. // Set the value "bar" for the key "foo".
  1649. if err := b.Put([]byte("foo"), []byte("bar")); err != nil {
  1650. return err
  1651. }
  1652. // Retrieve the key back from the database and verify it.
  1653. value := b.Get([]byte("foo"))
  1654. fmt.Printf("The value of 'foo' was: %s\n", value)
  1655. return nil
  1656. }); err != nil {
  1657. log.Fatal(err)
  1658. }
  1659. // Delete the key in a different write transaction.
  1660. if err := db.Update(func(tx *bolt.Tx) error {
  1661. return tx.Bucket([]byte("widgets")).Delete([]byte("foo"))
  1662. }); err != nil {
  1663. log.Fatal(err)
  1664. }
  1665. // Retrieve the key again.
  1666. if err := db.View(func(tx *bolt.Tx) error {
  1667. value := tx.Bucket([]byte("widgets")).Get([]byte("foo"))
  1668. if value == nil {
  1669. fmt.Printf("The value of 'foo' is now: nil\n")
  1670. }
  1671. return nil
  1672. }); err != nil {
  1673. log.Fatal(err)
  1674. }
  1675. // Close database to release file lock.
  1676. if err := db.Close(); err != nil {
  1677. log.Fatal(err)
  1678. }
  1679. // Output:
  1680. // The value of 'foo' was: bar
  1681. // The value of 'foo' is now: nil
  1682. }
  1683. func ExampleBucket_ForEach() {
  1684. // Open the database.
  1685. db, err := bolt.Open(tempfile(), 0666, nil)
  1686. if err != nil {
  1687. log.Fatal(err)
  1688. }
  1689. defer os.Remove(db.Path())
  1690. // Insert data into a bucket.
  1691. if err := db.Update(func(tx *bolt.Tx) error {
  1692. b, err := tx.CreateBucket([]byte("animals"))
  1693. if err != nil {
  1694. return err
  1695. }
  1696. if err := b.Put([]byte("dog"), []byte("fun")); err != nil {
  1697. return err
  1698. }
  1699. if err := b.Put([]byte("cat"), []byte("lame")); err != nil {
  1700. return err
  1701. }
  1702. if err := b.Put([]byte("liger"), []byte("awesome")); err != nil {
  1703. return err
  1704. }
  1705. // Iterate over items in sorted key order.
  1706. if err := b.ForEach(func(k, v []byte) error {
  1707. fmt.Printf("A %s is %s.\n", k, v)
  1708. return nil
  1709. }); err != nil {
  1710. return err
  1711. }
  1712. return nil
  1713. }); err != nil {
  1714. log.Fatal(err)
  1715. }
  1716. // Close database to release file lock.
  1717. if err := db.Close(); err != nil {
  1718. log.Fatal(err)
  1719. }
  1720. // Output:
  1721. // A cat is lame.
  1722. // A dog is fun.
  1723. // A liger is awesome.
  1724. }