{
    "componentChunkName": "component---src-templates-issues-tsx",
    "path": "/issues/46",
    "result": {"data":{"issuesJson":{"id":"8970fb4a-9ca2-530d-8671-8b31427fca15","title":"etcd-io/bbolt: Go 语言开发的嵌入式键/值数据库","number":46,"bodyHTML":"<div align=\"center\" dir=\"auto\">\n<p dir=\"auto\"><a target=\"_blank\" rel=\"noopener noreferrer nofollow\" href=\"https://camo.githubusercontent.com/a15b413168923d02e21292705ceaf86b92675f98e250c5c896a02ba7504a6eae/68747470733a2f2f63646e2e6a7364656c6976722e6e65742f67682f657279616a662f7475406d61696e2f696d672f696d6167655f32303234303432305f3231343430382e676966\"><img src=\"https://camo.githubusercontent.com/a15b413168923d02e21292705ceaf86b92675f98e250c5c896a02ba7504a6eae/68747470733a2f2f63646e2e6a7364656c6976722e6e65742f67682f657279616a662f7475406d61696e2f696d672f696d6167655f32303234303432305f3231343430382e676966\" width=\"100%\" height=\"3\" data-animated-image=\"\" data-canonical-src=\"https://cdn.jsdelivr.net/gh/eryajf/tu@main/img/image_20240420_214408.gif\" style=\"max-width: 100%; height: auto; max-height: 3px;\"></a><br><br></p>\n<markdown-accessiblity-table><table role=\"table\">\n<thead>\n<tr>\n<th align=\"right\">Repos</th>\n<th align=\"left\"><a href=\"https://github.com/etcd-io/bbolt\">etcd-io/bbolt</a></th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td align=\"right\"><strong>Views</strong></td>\n<td align=\"left\"><a href=\"https://github.com/opsre/awesome-ops\"><img src=\"https://camo.githubusercontent.com/b008c6be9d26d6333668d670ebc85094b38640d73657cc8c443db835177a218a/68747470733a2f2f76696577732e77686174696c656172656e65642e746f6461792f76696577732f6769746875622f657463642d696f2f62626f6c742e737667\" alt=\"views\" data-canonical-src=\"https://views.whatilearened.today/views/github/etcd-io/bbolt.svg\" style=\"max-width: 100%;\"></a></td>\n</tr>\n<tr>\n<td align=\"right\"><strong>Stars</strong></td>\n<td align=\"left\"><a href=\"https://github.com/opsre/awesome-ops\"><img src=\"https://camo.githubusercontent.com/625448a6e25c32e1bee8064cfda0c7ce3dc876150d94d4fef85e9dadc67e6cd3/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f73746172732f657463642d696f2f62626f6c743f636f6c6f723d663266303864266c6f676f3d556e64657274616c65266c6f676f436f6c6f723d656234363330\" alt=\"stars\" data-canonical-src=\"https://img.shields.io/github/stars/etcd-io/bbolt?color=f2f08d&amp;logo=Undertale&amp;logoColor=eb4630\" style=\"max-width: 100%;\"></a></td>\n</tr>\n<tr>\n<td align=\"right\"><strong>Forks</strong></td>\n<td align=\"left\"><a href=\"https://github.com/opsre/awesome-ops\"><img src=\"https://camo.githubusercontent.com/d0c41df1d8f0e2d495cd813fcd1cf80f6b13b29b3db2bb2b6bbf2a221295069b/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f666f726b732f657463642d696f2f62626f6c743f636f6c6f723d626138366562266c6f676f3d48616e647368616b65266c6f676f436f6c6f723d656136616136\" alt=\"forks\" data-canonical-src=\"https://img.shields.io/github/forks/etcd-io/bbolt?color=ba86eb&amp;logo=Handshake&amp;logoColor=ea6aa6\" style=\"max-width: 100%;\"></a></td>\n</tr>\n<tr>\n<td align=\"right\"><strong>License</strong></td>\n<td align=\"left\"><a href=\"https://github.com/opsre/awesome-ops\"><img src=\"https://camo.githubusercontent.com/9fa5c56862ecc84a3d8baa944d16dde8c6ba4cb8f216473730f7d208676400e2/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f657463642d696f2f62626f6c743f6c6f676f3d64617461253341696d616765253246706e672533426261736536342532436956424f5277304b47676f414141414e53556845556741414144414141414177434159414141425841766d48414141414358424957584d41414173544141414c457745416d70775941414144336b6c45515652346e4f335953557a5555426748384a726f57567255474a66456d3773584e63534446784d544c327069347348456778654e526f77586770455742445552522532424e414e4f4b4f4b36366f6f4b4a4742586545475263574163455646475152484677516d483525324630774861655748493944554455354c356b6e6568644f62377a66652532423139636e434a47495243516945596c514237594a4979474c38616f73756c565a36695246516a69474b6b7564576735614c6c704f70704e586e665071773555304459524a6d56786a4367464633424c755a476d416f5655694b4541726d583744375553677351706f72674761336748664b34483663754262435644334771683141352532424c6745253242467749656e514d306a6f446f66714c6f48564e7742796d384370546e416d327a51713073676478624964515a556c416b7150415a3664676a303541446f59526f6f66792532466f5869726f376b3551586a4c6f70677a4b69514e6c4c4461716f4967754577426a7a7150355066446a45394479495879493633464d547751464d43567271774e61763451666f52673538514625324266724d485172454b3844546f434d714e4179574e4866786d54526f4c4f726d5352566747744466714345715a4f4851727a6f34706243557341333431365169364551394b476a6345465267484f724f4b6e5536574162396247455459656b4b7843766a546167394534706a653534446b3551503862624d4859762532464333756541654a6b50304f4778422532424a70426e4172595238455951516e6f4e31414e4661426e5050356c385372736559514c303641646b30464a5938484f5761424d6c6541486a683068506f73497a6c6f3876304125324633345a694f6548726130714b52504d5653493774762532423932747725324676687855654252713456454c674d376642714b354770526d6f514c584e7075625475367a6f4e52706754386e5051627138347764253246494375767978694b4871694e41664933774d367549684271476b4c696977414f734b44714f68743749747239567938536a52424557667941627225324668523142667056514664484a42253246423268683778376a36517077435665655951326b72554235436c636a3641326d3067576a25324244306d4c34397a573563517943484c4e37726a6c6d6d6174457958582532464372547a412532466f51785a6b576c39474a5443566f25324279546a3837253242346769504b6376307149486f344156344430565a727251493334706e70524d36357875652532467a417265453425324654253246536f676c664542794d7369517441546c4c3352414a355948725378365a44665337307337754d486842705225324659423573305057366f455270316236253246667252684b3353444c35546965364f515548516c5133735644752532427a50636b397657453973747225324236496a533967486d5750616861444a25324277434b364e494242587348423948796b587378774470686c436d416476706c6162554a4e6e5a504239342532464e7436784f6538587a4562503257684d335741673147756236744825324233513150673337365239713475716c6e6f36676448476a444f5139306562303141484d3672596975554a314f713472344656756a35765239427a4d3957674e764f7977446869495951467667626366774166774d764863615067425041253246447948476a50624e394138616d6562636477416444354e6633373574787165774e5552656f797677424958594c645170576c6877465771745061435044334173467541535636717171496c62304a766b564331464c39576b4c55556c55574b337a585a4c46432532423125324642726f46746f364f7358497545594a5034447a7a5732594656316f4a614141414141456c46546b5375516d4343\" alt=\"license\" data-canonical-src=\"https://img.shields.io/github/license/etcd-io/bbolt?logo=data%3Aimage%2Fpng%3Bbase64%2CiVBORw0KGgoAAAANSUhEUgAAADAAAAAwCAYAAABXAvmHAAAACXBIWXMAAAsTAAALEwEAmpwYAAAD3klEQVR4nO3YSUzUUBgH8JroWVrUGJfEm7sXNcSDFxMTL2pi4sHEgxeNRowXgpEWBDURR%2BNANOKOK66ooKJGBXeEGRcWAcEVFGQRHFwQmH5%2F0wHaeWHI9DUDU5L5knehdOb7zfe%2B19cnCJGIRCQiEYlQB7YJIyGL8aosulVZ6iRFQjiGKkudWg5aLlpOppNXnfPqw5U0DYRJmVxjCgFF3BLuZGmAoVUiKEArmX7D7USgsQporgGa3gHfK4H6cuBbCVD3Gqh1A5%2BLgE%2BFwIenQM0joDofqLoHVNwBym8CpTnAm2zQq0sgdxbIdQZUlAkqPAZ6dgj05ADoYRoofy%2FoXiro7k5QXjLopgzKiQNlLDaqoIguEwBjzqP5PfDjE9DyIXyI63FMTwQFMCVrqwNav4QfoRg58QF%2BfrMHQrEK8DToCMqNAyWNHfxmTRoLOrmSRVgGtDfqCEqZOHQrzo4pbCUsA3416Qi6EQ9KGjcEFRgHOrOKnU6WAb9bGETYekKxCvjTag9E4pje54Dk5QP8bbMHYv%2FC3ueAeJkP0OGxB%2BJpBnArYR8EYQQnoN1ANFaBnPP5l8SrseYQL06Adk0FJY8HOWaBMleAHjh0hPosIzlo8v0A%2F34ZiOeHra0qKRPMVSI7tv%2B92tw%2FvhxUeBRq4VELgM7fBqK5GpRmoQLXNpubTu6zoNRpgT8nPQbq84wd%2FICuvyxiKHqiNAfI3wM6uIhBqGkLiiwAOsKDqOht7Itr9Vy8SjRBEWfyAbr%2FhR1BfpVQFdHJB%2FB2hh7x7j6QpwCVeeYQ2krUB5Clcj6A2m0gWj%2BD0mL49zW5cQyCHLN7rjlmmatEyXX%2FCrTzA%2FoQxZkWl9GJTCVo%2ByTj87%2B4giPKcv0qIHo4AV4D0VZrrQI34pnpRM65xue%2FzAreE4%2FT%2FSoglfEByMsiQtATlL3RAJ5YHrSx6ZDfS70s7uMHhBpR%2FYB5s0PW6oERp1b6%2FfrRhK3SDL5Tie6OQUHQlQ3sVDu%2BzPck9vWE9str%2B6IjS9gHmWPahaDJ%2BwCK6NIBBXsHB9HykXsxwDphlCmAdvplabUJNnZPB94%2FNt6xOe8XzEbP2WhM3WAg1Gub6tH%2B3Q1Pg376R9q4uqlno6gdHGjDOQ90eb01AHM6rYiuUJ1Oq4r4FVuj5vR9BzM9WgNvOywDhiIYQFvgbcfwAfwMvHcaPgBPA%2FDyHGjPbN9A8amebcdwAdD5Nf375txqewNUReoyvwBIXYLdQpWlhwFWqtPaCPD3AsFuASV6qqqIlb0JvkVC1FL9WkLUUlUWK3zXZLFC%2B1%2FBroFto6OsXIuEYJP4DzzW2YFV1oJaAAAAAElFTkSuQmCC\" style=\"max-width: 100%;\"></a></td>\n</tr>\n<tr>\n<td align=\"right\"><strong>UpdatedAt</strong></td>\n<td align=\"left\"><a href=\"https://github.com/opsre/awesome-ops\"><img src=\"https://camo.githubusercontent.com/f58a1f4d96e2ccd9220e39920bdf7ed47b3d3b17a122da555b97fd3f94578999/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6173742d636f6d6d69742f657463642d696f2f62626f6c743f646973706c61795f74696d657374616d703d636f6d6d6974746572266c6f676f3d64617461253341696d616765253246706e672533426261736536342532436956424f5277304b47676f414141414e5355684555674141414234414141416543415941414141374d4b3669414141414358424957584d41414173544141414c457745416d7077594141414648556c45515652346e4c5658375539615678776d3236646c582532466468627825324232253246516e626b7655627264554151674a4a496655444e574454546d4f76314651554661504a346b7573725630314a7431734e70637453327533626f745a746d786439744a5346536f714b71494635625944464c436f76436c656673747a4a387758524c445a4c7a6e6b634f343535376e3364332532466e655a3472454f515a74315771462532423858467234253242664f4c4565326a6f59307a776638546f795a50766a45676b4c592532426b306d6d72584f36644b696e787a476b30666a5430782532425279723055716e6359637a4d317263347451253242497270324c4758646f344e4334567657715453373277716c632532427231334f527269364b6458646e624f484c6c776c7a4a70524b6e3657342532424b36706f4f434e6e494166486a253246253242776168495a4234764b6e715a6630714a704e49716c2532467638545530634e6c3572627963337730516e54703379324c54614a374d314e617844723266526e315370504736476961323174664533345463614f617931694d55564f5146624659714957536f31573854694156616e43306576584f4542375770313448466232327a453566714c4f433545524c515769644e614e49347545636339693768634a6c644868324e57725135674464624f6c7061756d49754c62326174415144506e7a3062384e6257636d785656524a33486d7870536377777a4f4a6d4b44524765384c6c435a444c4739673754496e5631516c376462586256316533675430574b697243466f6d6b5079767758466c5a474a4f6a563625324253743634757564445a4f55354555636f782532464d455144567674364d62634e3237593341797a687633737061557242365a3971724a534f3139657a6a387032704c426b4a792532426343484a7857493567573574636652527a31645531394766476b6f7539505a4f34736d52396a4746777065783444793362766e6d4b79706f74625531335669646a71595a68726a343972764d457425324625324659714c7a396430304e6a57253246637a67796f394f35317a7336614e6c6f354644747530445a675146686c47584a30644241446f4e6864367576703657686f617967374e394c564748386d443462253246486e667455516f4e4734764c51306769784d716c5725324234714f6a744e48446733723166365969525347785279375576714c62395534716b716e78504f4e7661356c447048723125324279794b524e4b57425a793565644d554842326d3572792532422532424551772532427a6766347a6f39253246306f634e3357527a4c4277344a2532424a7950574372716d4967475974554f735744346f785a46516f50556a4770564871493425324661666b51504336665a5165654d31253242764c7549516e6a754257514444437363726d485039656f4e5041744271664f6e5875614b25324268364a45624772732532427076764d6d78654962683836336162565065597953456f39464a48704e4d464a59253242503563575a6b6667374f3174537759436553514c54593345385130392532464256504a556c78516a7368543142736343593032723955445542667643484236367063665041336c794165336e6772332532463437584467614a7a736c7937787741364e5a746c555550437541487161536a5549502532427375653842525741432532464e6651374a5a505a35397330476a3756747049537a34685125324243706658474d4b685a63764c70554b7866574d386f68766637702532464f446a4842666356463849736b383267314648796b63564645253242555a414158343761452532464d6c34504f3530506e75683025324678346e6d637957507364774468427836436d6b4c562532466746446a594b784f4a4f4674623077517949684c39527943774b33414f664947703159464542686e4d4a634c525745624b644b516f55366e63545a6b494544696341792532463831645675454d3552775066657934784f7830496b25324649324e6e466b692532425761664f6f464959466367595a43797864376543556a626334416d46363566742532466b4d686b336f25324235686337734d4a79716a4a45477659466151464972375930775077384246415925324236253242766d6b3377367a7a5275444d6d5a5652736668385274413075455453443775434254364459514e36697665553066706b594c6a453671725672744f78654e4b553954484c5a4a396b4255324a427342685635416976422532466f4b61514e4b675043427744596149665a4334616454684f71463457454e56694c4a77566f586f62664c424b56773637414f6154734c6336355461564b32317651344c613939654b6359673576627873624f627a5451394e37554b4467554f3177446a6944494942736868357a746733396e514d4c535a4248344f7a424f55444551586d323036643366634c67737762585141345068634b336e6873775531427a3877765130395248472532466f597933656a6677424e6d746f59714c4130586741414141424a52553545726b4a676767253344253344266c6162656c3d557064617465644174\" alt=\"last-commit\" data-canonical-src=\"https://img.shields.io/github/last-commit/etcd-io/bbolt?display_timestamp=committer&amp;logo=data%3Aimage%2Fpng%3Bbase64%2CiVBORw0KGgoAAAANSUhEUgAAAB4AAAAeCAYAAAA7MK6iAAAACXBIWXMAAAsTAAALEwEAmpwYAAAFHUlEQVR4nLVX7U9aVxwm26dlX%2Fdhbx%2B2%2FQnbkvUbrdUAQgJJIfUDNWDTTmOv1FQUFaPJ4kusrV01Jt1sNpctS2u3botZtmxd9tJSFSoqKqIF5bYDFLCovClefstzJ8wXRLDZLznkcO4557n3d3%2FneZ4rEOQZt1WqF%2B8XFr4%2BfOLEe2joY0zwf8ToyZPvjEgkLY%2Bk0mmrXO6dKinxzGk0fjT0x%2BRyr0UqncYczM1rc4tQ%2BIrp2LGXdo4NC4VvWqTS72wqlc%2Br13ORri6KdXdnbOHLlwlzJpRKn6W4%2BK6poOCNnIAfHj%2F%2BwahIZB4vKnqZf0qJpNIql%2Fv8TU0cNl5rbyc3w0QnTp3y2LTaJ7M1NaxDr2fRn1SpPG6Gia21tfE34TcaOay1iMUVOQFbFYqIWSo1W8TiAVanC0evXOEB7Wp14HFb22zE5fqLOC5ERLQWidNaNI4uEcc9i7hcJldHh2NWrQ5gDdbOlpaumIuLb2atAQDPnz0b8NbWcmxVVRJ3HmxpScwwzOJmKDRGe8LlCZDLG9g7TInV1Ql7dbXbV1e3gT0WKirCFomkPyvwXFlZGJOjV6%2BSt64uudDZOU5EUcox%2FMEQDVvt6MbcN27Y3Ayzhv3spaUrB6Z9qrJSO19ezj8p2pLBkJy%2BcCHJxWI5gW5tcfRRz1dU19GfGkou9PZO4smR9jGFwpex4Dy3bvnmKypotbU13VidjqYZhrj49rvMEt%2F%2FYqLz9d00NjW%2Fczgyo9O51zs6aNlo5FDtu0DZgQFhlGXJ0dBADoNhd6uvp6Whoayg7N9LVGH8mD4b%2FHnftUQoNG4vLQ0gixMqlW%2B4qOjtNHDg3r1f6YiRSGxRy7UvqLb9U4qkqnxPONva5lDpHr1%2ByyKRNKWBZy5edMUHB2m5ry%2B%2BEQw%2Bzgf4zo9%2F0ocN3WRzLBw4J%2BJyPWCrqmIgGYtUOsWD4oxZFQoPUjGpVHqI4%2FafkQPC6fZQeeM1%2BvLuIQnjuBWQDDCscrmHP9eoNPAtBqfOnXuaK%2Bh6JEbGrs%2BpvvMmxeIbh863abVPeYySEo9FJHpNMFJY%2BP5cWZkfg7O1tSwYCeSQLTY3E8Q09%2FBVPJUlxQjshT1BscCY02r9UDUBfvCHB66pcfPA3lyAe3ngr3%2F47XDgaJzsly7xwA6NZtlUUPCuAHqaSjUIP%2Bsue8BRWAC%2FNfQ7JZPZ59s0Gj7VtpISz4hQ%2BCpfXGMKhZcvLpUKxfWM8ohvf7p%2FODjHBfcVF8Isk82g1FHykcVFE%2BUZAAX47aE%2FMl4PO50Pnuh0%2Fx4nmcyWPsdwDhBx6CmkLV%2FgFDjYKxOJOFtb0wQyIhL9RyCwK3AOfIGp1YFEBhnMJcLRWEbKdKQoU6ncTZkIEDicAy%2F81dVuEM5RwPfey4xOx0Ik%2FI2NnFki%2BWafOoFIYFcgYZCyxd7eCUjbc4AmF65ft%2FkMhk3o%2B5hc7sMJyqjJEGvYFaQFIr7Y0wPw8BFAY%2B6%2Bvmk3w6zzRuDMmZVRsfh8RtA0uETSD7uCBT6DYQN6iveU0fpkYLjE6qrVrtOxeNKU9THLZJ9kBU2JBsBhV5AivB%2FoKaQNKgPCBwDYaIfZC4adThOqF4WENViLJwVoXobfLBKVw67AOaTsLc65TaVK21vQ4La99eKcYg5vbxsbObzTQ9N7UKDgUO1wDjiDIIBshh5ztg39nQMLSZBH4OzBOUDEQXm206d3fcLgswbXQA4PhcK3nhswU1Bz8wvQ09RHG%2FoYy3ejfwBNmtoYqLA0XgAAAABJRU5ErkJggg%3D%3D&amp;label=UpdatedAt\" style=\"max-width: 100%;\"></a></td>\n</tr>\n<tr>\n<td align=\"right\"><strong>CreatedAt</strong></td>\n<td align=\"left\"><a href=\"https://github.com/opsre/awesome-ops\"><img src=\"https://camo.githubusercontent.com/baac47b1e1180e2cda1e7c850fefcec6f3cf1c62c3dc55fb4b3fbb878583e7d9/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f637265617465642d61742f657463642d696f2f62626f6c743f6c6f676f3d64617461253341696d616765253246706e672533426261736536342532436956424f5277304b47676f414141414e5355684555674141414234414141416543415941414141374d4b3669414141414358424957584d41414173544141414c457745416d7077594141414648556c45515652346e4c5658375539615678776d3236646c582532466468627825324232253246516e626b7655627264554151674a4a496655444e574454546d4f76314651554661504a346b7573725630314a7431734e70637453327533626f745a746d786439744a5346536f714b71494635625944464c436f76436c656673747a4a387758524c445a4c7a6e6b634f343535376e3364332532466e655a3472454f515a74315771462532423858467234253242664f4c4565326a6f59307a776638546f795a50766a45676b4c592532426b306d6d72584f36644b696e787a476b30666a5430782532425279723055716e6359637a4d317263347451253242497270324c4758646f344e4334567657715453373277716c632532427231334f527269364b6458646e624f484c6c776c7a4a70524b6e3657342532424b36706f4f434e6e494166486a253246253242776168495a4234764b6e715a6630714a704e49716c2532467638545530634e6c3572627963337730516e54703379324c54614a374d314e617844723266526e315370504736476961323174664533345463614f617931694d55564f5146624659714957536f31573854694156616e43306576584f4542375770313448466232327a453566714c4f433545524c515769644e614e49347545636339693768634a6c644868324e57725135674464624f6c7061756d49754c62326174415144506e7a3062384e6257636d785656524a33486d7870536377777a4f4a6d4b44524765384c6c435a444c4739673754496e5631516c376462586256316533675430574b697243466f6d6b5079767758466c5a474a4f6a563625324253743634757564445a4f55354555636f782532464d455144567674364d62634e3237593341797a687633737061557242365a3971724a534f3139657a6a387032704c426b4a792532426343484a7857493567573574636652527a31645531394766476b6f7539505a4f34736d52396a4746777065783444793362766e6d4b79706f74625531335669646a71595a68726a343972764d457425324625324659714c7a396430304e6a57253246637a67796f394f35317a7336614e6c6f354644747530445a675146686c47584a30644241446f4e6864367576703657686f617967374e394c564748386d443462253246486e667455516f4e4734764c51306769784d716c5725324234714f6a744e48446733723166365969525347785279375576714c62395534716b716e78504f4e7661356c447048723125324279794b524e4b57425a793565644d554842326d3572792532422532424551772532427a6766347a6f39253246306f634e3357527a4c4277344a2532424a7950574372716d4967475974554f735744346f785a46516f50556a4770564871493425324661666b51504336665a5165654d31253242764c7549516e6a754257514444437363726d485039656f4e5041744271664f6e5875614b25324268364a45624772732532427076764d6d78654962683836336162565065597953456f39464a48704e4d464a59253242503563575a6b6667374f3174537759436553514c54593345385130392532464256504a556c78516a7368543142736343593032723955445542667643484236367063665041336c794165336e6772332532463437584467614a7a736c7937787741364e5a746c555550437541487161536a5549502532427375653842525741432532464e6651374a5a505a35397330476a3756747049537a34685125324243706658474d4b685a63764c70554b7866574d386f68766637702532464f446a4842666356463849736b383267314648796b63564645253242555a414158343761452532464d6c34504f3530506e75683025324678346e6d637957507364774468427836436d6b4c562532466746446a594b784f4a4f4674623077517949684c39527943774b33414f664947703159464542686e4d4a634c525745624b644b516f55366e63545a6b494544696341792532463831645675454d3552775066657934784f7830496b25324649324e6e466b692532425761664f6f464959466367595a43797864376543556a626334416d46363566742532466b4d686b336f25324235686337734d4a79716a4a45477659466151464972375930775077384246415925324236253242766d6b3377367a7a5275444d6d5a5652736668385274413075455453443775434254364459514e36697665553066706b594c6a453671725672744f78654e4b553954484c5a4a396b4255324a427342685635416976422532466f4b61514e4b675043427744596149665a4334616454684f71463457454e56694c4a77566f586f62664c424b56773637414f6154734c6336355461564b32317651344c613939654b6359673576627873624f627a5451394e37554b4467554f3177446a6944494942736868357a746733396e514d4c535a4248344f7a424f55444551586d323036643366634c67737762585141345068634b336e6873775531427a3877765130395248472532466f597933656a6677424e6d746f59714c4130586741414141424a52553545726b4a676767253344253344266c6162656c3d437265617465644174\" alt=\"create-at\" data-canonical-src=\"https://img.shields.io/github/created-at/etcd-io/bbolt?logo=data%3Aimage%2Fpng%3Bbase64%2CiVBORw0KGgoAAAANSUhEUgAAAB4AAAAeCAYAAAA7MK6iAAAACXBIWXMAAAsTAAALEwEAmpwYAAAFHUlEQVR4nLVX7U9aVxwm26dlX%2Fdhbx%2B2%2FQnbkvUbrdUAQgJJIfUDNWDTTmOv1FQUFaPJ4kusrV01Jt1sNpctS2u3botZtmxd9tJSFSoqKqIF5bYDFLCovClefstzJ8wXRLDZLznkcO4557n3d3%2FneZ4rEOQZt1WqF%2B8XFr4%2BfOLEe2joY0zwf8ToyZPvjEgkLY%2Bk0mmrXO6dKinxzGk0fjT0x%2BRyr0UqncYczM1rc4tQ%2BIrp2LGXdo4NC4VvWqTS72wqlc%2Br13ORri6KdXdnbOHLlwlzJpRKn6W4%2BK6poOCNnIAfHj%2F%2BwahIZB4vKnqZf0qJpNIql%2Fv8TU0cNl5rbyc3w0QnTp3y2LTaJ7M1NaxDr2fRn1SpPG6Gia21tfE34TcaOay1iMUVOQFbFYqIWSo1W8TiAVanC0evXOEB7Wp14HFb22zE5fqLOC5ERLQWidNaNI4uEcc9i7hcJldHh2NWrQ5gDdbOlpaumIuLb2atAQDPnz0b8NbWcmxVVRJ3HmxpScwwzOJmKDRGe8LlCZDLG9g7TInV1Ql7dbXbV1e3gT0WKirCFomkPyvwXFlZGJOjV6%2BSt64uudDZOU5EUcox%2FMEQDVvt6MbcN27Y3Ayzhv3spaUrB6Z9qrJSO19ezj8p2pLBkJy%2BcCHJxWI5gW5tcfRRz1dU19GfGkou9PZO4smR9jGFwpex4Dy3bvnmKypotbU13VidjqYZhrj49rvMEt%2F%2FYqLz9d00NjW%2Fczgyo9O51zs6aNlo5FDtu0DZgQFhlGXJ0dBADoNhd6uvp6Whoayg7N9LVGH8mD4b%2FHnftUQoNG4vLQ0gixMqlW%2B4qOjtNHDg3r1f6YiRSGxRy7UvqLb9U4qkqnxPONva5lDpHr1%2ByyKRNKWBZy5edMUHB2m5ry%2B%2BEQw%2Bzgf4zo9%2F0ocN3WRzLBw4J%2BJyPWCrqmIgGYtUOsWD4oxZFQoPUjGpVHqI4%2FafkQPC6fZQeeM1%2BvLuIQnjuBWQDDCscrmHP9eoNPAtBqfOnXuaK%2Bh6JEbGrs%2BpvvMmxeIbh863abVPeYySEo9FJHpNMFJY%2BP5cWZkfg7O1tSwYCeSQLTY3E8Q09%2FBVPJUlxQjshT1BscCY02r9UDUBfvCHB66pcfPA3lyAe3ngr3%2F47XDgaJzsly7xwA6NZtlUUPCuAHqaSjUIP%2Bsue8BRWAC%2FNfQ7JZPZ59s0Gj7VtpISz4hQ%2BCpfXGMKhZcvLpUKxfWM8ohvf7p%2FODjHBfcVF8Isk82g1FHykcVFE%2BUZAAX47aE%2FMl4PO50Pnuh0%2Fx4nmcyWPsdwDhBx6CmkLV%2FgFDjYKxOJOFtb0wQyIhL9RyCwK3AOfIGp1YFEBhnMJcLRWEbKdKQoU6ncTZkIEDicAy%2F81dVuEM5RwPfey4xOx0Ik%2FI2NnFki%2BWafOoFIYFcgYZCyxd7eCUjbc4AmF65ft%2FkMhk3o%2B5hc7sMJyqjJEGvYFaQFIr7Y0wPw8BFAY%2B6%2Bvmk3w6zzRuDMmZVRsfh8RtA0uETSD7uCBT6DYQN6iveU0fpkYLjE6qrVrtOxeNKU9THLZJ9kBU2JBsBhV5AivB%2FoKaQNKgPCBwDYaIfZC4adThOqF4WENViLJwVoXobfLBKVw67AOaTsLc65TaVK21vQ4La99eKcYg5vbxsbObzTQ9N7UKDgUO1wDjiDIIBshh5ztg39nQMLSZBH4OzBOUDEQXm206d3fcLgswbXQA4PhcK3nhswU1Bz8wvQ09RHG%2FoYy3ejfwBNmtoYqLA0XgAAAABJRU5ErkJggg%3D%3D&amp;label=CreatedAt\" style=\"max-width: 100%;\"></a></td>\n</tr>\n</tbody>\n</table></markdown-accessiblity-table>\n<a href=\"https://github.com/opsre/awesome-ops\">\n</a><p dir=\"auto\"><a href=\"https://github.com/opsre/awesome-ops\"><img src=\"https://camo.githubusercontent.com/b964a36ea4078c39f603d46a29436371c7541a2f26e0228d7b21b9d9805b43d3/68747470733a2f2f736f6369616c6966792e6769742e63692f6f707372652f617765736f6d652d6f70732f696d6167653f6465736372697074696f6e3d3126666f6e743d42697474657226666f726b733d31266973737565733d31266c616e67756167653d31266c6f676f3d6874747073253341253246253246617661746172732e67697468756275736572636f6e74656e742e636f6d25324675253246313838353638303230266e616d653d31266f776e65723d31267061747465726e3d436972637569742b426f6172642670756c6c733d31267374617267617a6572733d31267468656d653d4c69676874\" alt=\"\" data-canonical-src=\"https://socialify.git.ci/opsre/awesome-ops/image?description=1&amp;font=Bitter&amp;forks=1&amp;issues=1&amp;language=1&amp;logo=https%3A%2F%2Favatars.githubusercontent.com%2Fu%2F188568020&amp;name=1&amp;owner=1&amp;pattern=Circuit+Board&amp;pulls=1&amp;stargazers=1&amp;theme=Light\" style=\"max-width: 100%;\"></a></p>\n<a target=\"_blank\" rel=\"noopener noreferrer nofollow\" href=\"https://camo.githubusercontent.com/a15b413168923d02e21292705ceaf86b92675f98e250c5c896a02ba7504a6eae/68747470733a2f2f63646e2e6a7364656c6976722e6e65742f67682f657279616a662f7475406d61696e2f696d672f696d6167655f32303234303432305f3231343430382e676966\"><img src=\"https://camo.githubusercontent.com/a15b413168923d02e21292705ceaf86b92675f98e250c5c896a02ba7504a6eae/68747470733a2f2f63646e2e6a7364656c6976722e6e65742f67682f657279616a662f7475406d61696e2f696d672f696d6167655f32303234303432305f3231343430382e676966\" width=\"100%\" height=\"3\" data-animated-image=\"\" data-canonical-src=\"https://cdn.jsdelivr.net/gh/eryajf/tu@main/img/image_20240420_214408.gif\" style=\"max-width: 100%; height: auto; max-height: 3px;\"></a>\n</div>\n<h1 dir=\"auto\">bbolt</h1>\n<p dir=\"auto\"><a href=\"https://goreportcard.com/report/go.etcd.io/bbolt\" rel=\"nofollow\"><img src=\"https://camo.githubusercontent.com/56ec3cf11f77f2202c823f1a564eb2e6b1893da37aceebd6fce040d0060ff4e1/68747470733a2f2f676f7265706f7274636172642e636f6d2f62616467652f676f2e657463642e696f2f62626f6c743f7374796c653d666c61742d737175617265\" alt=\"Go Report Card\" data-canonical-src=\"https://goreportcard.com/badge/go.etcd.io/bbolt?style=flat-square\" style=\"max-width: 100%;\"></a><br>\n<a href=\"https://pkg.go.dev/go.etcd.io/bbolt\" rel=\"nofollow\"><img src=\"https://camo.githubusercontent.com/69d1c8cc36708993d29b937e7e4e9befbbda4de592061f4f4fd302919330b352/68747470733a2f2f706b672e676f2e6465762f62616467652f676f2e657463642e696f2f62626f6c742e737667\" alt=\"Go Reference\" data-canonical-src=\"https://pkg.go.dev/badge/go.etcd.io/bbolt.svg\" style=\"max-width: 100%;\"></a><br>\n<a href=\"https://github.com/etcd-io/bbolt/releases\"><img src=\"https://camo.githubusercontent.com/7f59c95ab8a0cc2230fe134a94946aef4a9ce233aed5c1ff7827e269a0ab76f5/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f72656c656173652f657463642d696f2f62626f6c742f616c6c2e7376673f7374796c653d666c61742d737175617265\" alt=\"Releases\" data-canonical-src=\"https://img.shields.io/github/release/etcd-io/bbolt/all.svg?style=flat-square\" style=\"max-width: 100%;\"></a><br>\n<a href=\"https://github.com/etcd-io/bbolt/blob/master/LICENSE\"><img src=\"https://camo.githubusercontent.com/156bde1434f5f63014a950791ed6b727f30bc2a3c0253b7ffe0e02759cf132a8/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f657463642d696f2f62626f6c742e7376673f7374796c653d666c61742d737175617265\" alt=\"LICENSE\" data-canonical-src=\"https://img.shields.io/github/license/etcd-io/bbolt.svg?style=flat-square\" style=\"max-width: 100%;\"></a></p>\n<p dir=\"auto\">bbolt is a fork of <a href=\"https://github.com/benbjohnson\">Ben Johnson's</a> <a href=\"https://github.com/boltdb/bolt\">Bolt</a> key/value<br>\nstore. The purpose of this fork is to provide the Go community with an active<br>\nmaintenance and development target for Bolt; the goal is improved reliability<br>\nand stability. bbolt includes bug fixes, performance enhancements, and features<br>\nnot found in Bolt while preserving backwards compatibility with the Bolt API.</p>\n<p dir=\"auto\">Bolt is a pure Go key/value store inspired by <a href=\"https://twitter.com/hyc_symas\" rel=\"nofollow\">Howard Chu's</a><br>\n<a href=\"https://www.symas.com/symas-embedded-database-lmdb\" rel=\"nofollow\">LMDB project</a>. The goal of the project is to provide a simple,<br>\nfast, and reliable database for projects that don't require a full database<br>\nserver such as Postgres or MySQL.</p>\n<p dir=\"auto\">Since Bolt is meant to be used as such a low-level piece of functionality,<br>\nsimplicity is key. The API will be small and only focus on getting values<br>\nand setting values. That's it.</p>\n<h2 dir=\"auto\">Project Status</h2>\n<p dir=\"auto\">Bolt is stable, the API is fixed, and the file format is fixed. Full unit<br>\ntest coverage and randomized black box testing are used to ensure database<br>\nconsistency and thread safety. Bolt is currently used in high-load production<br>\nenvironments serving databases as large as 1TB. Many companies such as<br>\nShopify and Heroku use Bolt-backed services every day.</p>\n<h2 dir=\"auto\">Project versioning</h2>\n<p dir=\"auto\">bbolt uses <a href=\"http://semver.org\" rel=\"nofollow\">semantic versioning</a>.<br>\nAPI should not change between patch and minor releases.<br>\nNew minor versions may add additional features to the API.</p>\n<h2 dir=\"auto\">Table of Contents</h2>\n<ul dir=\"auto\">\n<li><a href=\"#getting-started\">Getting Started</a>\n<ul dir=\"auto\">\n<li><a href=\"#installing\">Installing</a></li>\n<li><a href=\"#opening-a-database\">Opening a database</a></li>\n<li><a href=\"#transactions\">Transactions</a>\n<ul dir=\"auto\">\n<li><a href=\"#read-write-transactions\">Read-write transactions</a></li>\n<li><a href=\"#read-only-transactions\">Read-only transactions</a></li>\n<li><a href=\"#batch-read-write-transactions\">Batch read-write transactions</a></li>\n<li><a href=\"#managing-transactions-manually\">Managing transactions manually</a></li>\n</ul>\n</li>\n<li><a href=\"#using-buckets\">Using buckets</a></li>\n<li><a href=\"#using-keyvalue-pairs\">Using key/value pairs</a></li>\n<li><a href=\"#autoincrementing-integer-for-the-bucket\">Autoincrementing integer for the bucket</a></li>\n<li><a href=\"#iterating-over-keys\">Iterating over keys</a>\n<ul dir=\"auto\">\n<li><a href=\"#prefix-scans\">Prefix scans</a></li>\n<li><a href=\"#range-scans\">Range scans</a></li>\n<li><a href=\"#foreach\">ForEach()</a></li>\n</ul>\n</li>\n<li><a href=\"#nested-buckets\">Nested buckets</a></li>\n<li><a href=\"#database-backups\">Database backups</a></li>\n<li><a href=\"#statistics\">Statistics</a></li>\n<li><a href=\"#read-only-mode\">Read-Only Mode</a></li>\n<li><a href=\"#mobile-use-iosandroid\">Mobile Use (iOS/Android)</a></li>\n</ul>\n</li>\n<li><a href=\"#resources\">Resources</a></li>\n<li><a href=\"#comparison-with-other-databases\">Comparison with other databases</a>\n<ul dir=\"auto\">\n<li><a href=\"#postgres-mysql--other-relational-databases\">Postgres, MySQL, &amp; other relational databases</a></li>\n<li><a href=\"#leveldb-rocksdb\">LevelDB, RocksDB</a></li>\n<li><a href=\"#lmdb\">LMDB</a></li>\n</ul>\n</li>\n<li><a href=\"#caveats--limitations\">Caveats &amp; Limitations</a></li>\n<li><a href=\"#reading-the-source\">Reading the Source</a></li>\n<li><a href=\"#known-issues\">Known Issues</a></li>\n<li><a href=\"#other-projects-using-bolt\">Other Projects Using Bolt</a></li>\n</ul>\n<h2 dir=\"auto\">Getting Started</h2>\n<h3 dir=\"auto\">Installing</h3>\n<p dir=\"auto\">To start using <code class=\"notranslate\">bbolt</code>, install Go and run <code class=\"notranslate\">go get</code>:</p>\n<div class=\"highlight highlight-source-shell notranslate position-relative overflow-auto\" dir=\"auto\" data-snippet-clipboard-copy-content=\"$ go get go.etcd.io/bbolt@latest\"><pre class=\"notranslate\">$ go get go.etcd.io/bbolt@latest</pre></div>\n<p dir=\"auto\">This will retrieve the library and update your <code class=\"notranslate\">go.mod</code> and <code class=\"notranslate\">go.sum</code> files.</p>\n<p dir=\"auto\">To run the command line utility, execute:</p>\n<div class=\"highlight highlight-source-shell notranslate position-relative overflow-auto\" dir=\"auto\" data-snippet-clipboard-copy-content=\"$ go run go.etcd.io/bbolt/cmd/bbolt@latest\"><pre class=\"notranslate\">$ go run go.etcd.io/bbolt/cmd/bbolt@latest</pre></div>\n<p dir=\"auto\">Run <code class=\"notranslate\">go install</code> to install the <code class=\"notranslate\">bbolt</code> command line utility into<br>\nyour <code class=\"notranslate\">$GOBIN</code> path, which defaults to <code class=\"notranslate\">$GOPATH/bin</code> or <code class=\"notranslate\">$HOME/go/bin</code> if the<br>\n<code class=\"notranslate\">GOPATH</code> environment variable is not set.</p>\n<div class=\"highlight highlight-source-shell notranslate position-relative overflow-auto\" dir=\"auto\" data-snippet-clipboard-copy-content=\"$ go install go.etcd.io/bbolt/cmd/bbolt@latest\"><pre class=\"notranslate\">$ go install go.etcd.io/bbolt/cmd/bbolt@latest</pre></div>\n<h3 dir=\"auto\">Importing bbolt</h3>\n<p dir=\"auto\">To use bbolt as an embedded key-value store, import as:</p>\n<div class=\"highlight highlight-source-go notranslate position-relative overflow-auto\" dir=\"auto\" data-snippet-clipboard-copy-content=\"import bolt &quot;go.etcd.io/bbolt&quot;\n\ndb, err := bolt.Open(path, 0600, nil)\nif err != nil {\n  return err\n}\ndefer db.Close()\"><pre class=\"notranslate\"><span class=\"pl-k\">import</span> bolt <span class=\"pl-s\">\"go.etcd.io/bbolt\"</span>\n\n<span class=\"pl-s1\">db</span>, <span class=\"pl-s1\">err</span> <span class=\"pl-c1\">:=</span> <span class=\"pl-s1\">bolt</span>.<span class=\"pl-c1\">Open</span>(<span class=\"pl-s1\">path</span>, <span class=\"pl-c1\">0600</span>, <span class=\"pl-c1\">nil</span>)\n<span class=\"pl-k\">if</span> <span class=\"pl-s1\">err</span> <span class=\"pl-c1\">!=</span> <span class=\"pl-c1\">nil</span> {\n  <span class=\"pl-k\">return</span> <span class=\"pl-s1\">err</span>\n}\n<span class=\"pl-k\">defer</span> <span class=\"pl-s1\">db</span>.<span class=\"pl-c1\">Close</span>()</pre></div>\n<h3 dir=\"auto\">Opening a database</h3>\n<p dir=\"auto\">The top-level object in Bolt is a <code class=\"notranslate\">DB</code>. It is represented as a single file on<br>\nyour disk and represents a consistent snapshot of your data.</p>\n<p dir=\"auto\">To open your database, simply use the <code class=\"notranslate\">bolt.Open()</code> function:</p>\n<div class=\"highlight highlight-source-go notranslate position-relative overflow-auto\" dir=\"auto\" data-snippet-clipboard-copy-content=\"package main\n\nimport (\n\t&quot;log&quot;\n\n\tbolt &quot;go.etcd.io/bbolt&quot;\n)\n\nfunc main() {\n\t// Open the my.db data file in your current directory.\n\t// It will be created if it doesn't exist.\n\tdb, err := bolt.Open(&quot;my.db&quot;, 0600, nil)\n\tif err != nil {\n\t\tlog.Fatal(err)\n\t}\n\tdefer db.Close()\n\n\t...\n}\"><pre class=\"notranslate\"><span class=\"pl-k\">package</span> main\n\n<span class=\"pl-k\">import</span> (\n\t<span class=\"pl-s\">\"log\"</span>\n\n\tbolt <span class=\"pl-s\">\"go.etcd.io/bbolt\"</span>\n)\n\n<span class=\"pl-k\">func</span> <span class=\"pl-s1\">main</span>() {\n\t<span class=\"pl-c\">// Open the my.db data file in your current directory.</span>\n\t<span class=\"pl-c\">// It will be created if it doesn't exist.</span>\n\t<span class=\"pl-s1\">db</span>, <span class=\"pl-s1\">err</span> <span class=\"pl-c1\">:=</span> <span class=\"pl-s1\">bolt</span>.<span class=\"pl-c1\">Open</span>(<span class=\"pl-s\">\"my.db\"</span>, <span class=\"pl-c1\">0600</span>, <span class=\"pl-c1\">nil</span>)\n\t<span class=\"pl-k\">if</span> <span class=\"pl-s1\">err</span> <span class=\"pl-c1\">!=</span> <span class=\"pl-c1\">nil</span> {\n\t\t<span class=\"pl-s1\">log</span>.<span class=\"pl-c1\">Fatal</span>(<span class=\"pl-s1\">err</span>)\n\t}\n\t<span class=\"pl-k\">defer</span> <span class=\"pl-s1\">db</span>.<span class=\"pl-c1\">Close</span>()\n\n\t<span class=\"pl-c1\">...</span>\n}</pre></div>\n<p dir=\"auto\">Please note that Bolt obtains a file lock on the data file so multiple processes<br>\ncannot open the same database at the same time. Opening an already open Bolt<br>\ndatabase will cause it to hang until the other process closes it. To prevent<br>\nan indefinite wait you can pass a timeout option to the <code class=\"notranslate\">Open()</code> function:</p>\n<div class=\"highlight highlight-source-go notranslate position-relative overflow-auto\" dir=\"auto\" data-snippet-clipboard-copy-content=\"db, err := bolt.Open(&quot;my.db&quot;, 0600, &amp;bolt.Options{Timeout: 1 * time.Second})\"><pre class=\"notranslate\"><span class=\"pl-s1\">db</span>, <span class=\"pl-s1\">err</span> <span class=\"pl-c1\">:=</span> <span class=\"pl-s1\">bolt</span>.<span class=\"pl-c1\">Open</span>(<span class=\"pl-s\">\"my.db\"</span>, <span class=\"pl-c1\">0600</span>, <span class=\"pl-c1\">&amp;</span>bolt.<span class=\"pl-smi\">Options</span>{<span class=\"pl-s1\">Timeout</span>: <span class=\"pl-c1\">1</span> <span class=\"pl-c1\">*</span> <span class=\"pl-s1\">time</span>.<span class=\"pl-c1\">Second</span>})</pre></div>\n<h3 dir=\"auto\">Transactions</h3>\n<p dir=\"auto\">Bolt allows only one read-write transaction at a time but allows as many<br>\nread-only transactions as you want at a time. Each transaction has a consistent<br>\nview of the data as it existed when the transaction started.</p>\n<p dir=\"auto\">Individual transactions and all objects created from them (e.g. buckets, keys)<br>\nare not thread safe. To work with data in multiple goroutines you must start<br>\na transaction for each one or use locking to ensure only one goroutine accesses<br>\na transaction at a time. Creating transaction from the <code class=\"notranslate\">DB</code> is thread safe.</p>\n<p dir=\"auto\">Transactions should not depend on one another and generally shouldn't be opened<br>\nsimultaneously in the same goroutine. This can cause a deadlock as the read-write<br>\ntransaction needs to periodically re-map the data file but it cannot do so while<br>\nany read-only transaction is open. Even a nested read-only transaction can cause<br>\na deadlock, as the child transaction can block the parent transaction from releasing<br>\nits resources.</p>\n<h4 dir=\"auto\">Read-write transactions</h4>\n<p dir=\"auto\">To start a read-write transaction, you can use the <code class=\"notranslate\">DB.Update()</code> function:</p>\n<div class=\"highlight highlight-source-go notranslate position-relative overflow-auto\" dir=\"auto\" data-snippet-clipboard-copy-content=\"err := db.Update(func(tx *bolt.Tx) error {\n\t...\n\treturn nil\n})\"><pre class=\"notranslate\"><span class=\"pl-s1\">err</span> <span class=\"pl-c1\">:=</span> <span class=\"pl-s1\">db</span>.<span class=\"pl-c1\">Update</span>(<span class=\"pl-k\">func</span>(<span class=\"pl-s1\">tx</span> <span class=\"pl-c1\">*</span>bolt.<span class=\"pl-smi\">Tx</span>) <span class=\"pl-smi\">error</span> {\n\t<span class=\"pl-c1\">...</span>\n\t<span class=\"pl-k\">return</span> <span class=\"pl-c1\">nil</span>\n})</pre></div>\n<p dir=\"auto\">Inside the closure, you have a consistent view of the database. You commit the<br>\ntransaction by returning <code class=\"notranslate\">nil</code> at the end. You can also rollback the transaction<br>\nat any point by returning an error. All database operations are allowed inside<br>\na read-write transaction.</p>\n<p dir=\"auto\">Always check the return error as it will report any disk failures that can cause<br>\nyour transaction to not complete. If you return an error within your closure<br>\nit will be passed through.</p>\n<h4 dir=\"auto\">Read-only transactions</h4>\n<p dir=\"auto\">To start a read-only transaction, you can use the <code class=\"notranslate\">DB.View()</code> function:</p>\n<div class=\"highlight highlight-source-go notranslate position-relative overflow-auto\" dir=\"auto\" data-snippet-clipboard-copy-content=\"err := db.View(func(tx *bolt.Tx) error {\n\t...\n\treturn nil\n})\"><pre class=\"notranslate\"><span class=\"pl-s1\">err</span> <span class=\"pl-c1\">:=</span> <span class=\"pl-s1\">db</span>.<span class=\"pl-c1\">View</span>(<span class=\"pl-k\">func</span>(<span class=\"pl-s1\">tx</span> <span class=\"pl-c1\">*</span>bolt.<span class=\"pl-smi\">Tx</span>) <span class=\"pl-smi\">error</span> {\n\t<span class=\"pl-c1\">...</span>\n\t<span class=\"pl-k\">return</span> <span class=\"pl-c1\">nil</span>\n})</pre></div>\n<p dir=\"auto\">You also get a consistent view of the database within this closure, however,<br>\nno mutating operations are allowed within a read-only transaction. You can only<br>\nretrieve buckets, retrieve values, and copy the database within a read-only<br>\ntransaction.</p>\n<h4 dir=\"auto\">Batch read-write transactions</h4>\n<p dir=\"auto\">Each <code class=\"notranslate\">DB.Update()</code> waits for disk to commit the writes. This overhead<br>\ncan be minimized by combining multiple updates with the <code class=\"notranslate\">DB.Batch()</code><br>\nfunction:</p>\n<div class=\"highlight highlight-source-go notranslate position-relative overflow-auto\" dir=\"auto\" data-snippet-clipboard-copy-content=\"err := db.Batch(func(tx *bolt.Tx) error {\n\t...\n\treturn nil\n})\"><pre class=\"notranslate\"><span class=\"pl-s1\">err</span> <span class=\"pl-c1\">:=</span> <span class=\"pl-s1\">db</span>.<span class=\"pl-c1\">Batch</span>(<span class=\"pl-k\">func</span>(<span class=\"pl-s1\">tx</span> <span class=\"pl-c1\">*</span>bolt.<span class=\"pl-smi\">Tx</span>) <span class=\"pl-smi\">error</span> {\n\t<span class=\"pl-c1\">...</span>\n\t<span class=\"pl-k\">return</span> <span class=\"pl-c1\">nil</span>\n})</pre></div>\n<p dir=\"auto\">Concurrent Batch calls are opportunistically combined into larger<br>\ntransactions. Batch is only useful when there are multiple goroutines<br>\ncalling it.</p>\n<p dir=\"auto\">The trade-off is that <code class=\"notranslate\">Batch</code> can call the given<br>\nfunction multiple times, if parts of the transaction fail. The<br>\nfunction must be idempotent and side effects must take effect only<br>\nafter a successful return from <code class=\"notranslate\">DB.Batch()</code>.</p>\n<p dir=\"auto\">For example: don't display messages from inside the function, instead<br>\nset variables in the enclosing scope:</p>\n<div class=\"highlight highlight-source-go notranslate position-relative overflow-auto\" dir=\"auto\" data-snippet-clipboard-copy-content=\"var id uint64\nerr := db.Batch(func(tx *bolt.Tx) error {\n\t// Find last key in bucket, decode as bigendian uint64, increment\n\t// by one, encode back to []byte, and add new key.\n\t...\n\tid = newValue\n\treturn nil\n})\nif err != nil {\n\treturn ...\n}\nfmt.Println(&quot;Allocated ID %d&quot;, id)\"><pre class=\"notranslate\"><span class=\"pl-k\">var</span> <span class=\"pl-s1\">id</span> <span class=\"pl-smi\">uint64</span>\n<span class=\"pl-s1\">err</span> <span class=\"pl-c1\">:=</span> <span class=\"pl-s1\">db</span>.<span class=\"pl-c1\">Batch</span>(<span class=\"pl-k\">func</span>(<span class=\"pl-s1\">tx</span> <span class=\"pl-c1\">*</span>bolt.<span class=\"pl-smi\">Tx</span>) <span class=\"pl-smi\">error</span> {\n\t<span class=\"pl-c\">// Find last key in bucket, decode as bigendian uint64, increment</span>\n\t<span class=\"pl-c\">// by one, encode back to []byte, and add new key.</span>\n\t<span class=\"pl-c1\">...</span>\n\t<span class=\"pl-s1\">id</span> <span class=\"pl-c1\">=</span> <span class=\"pl-s1\">newValue</span>\n\t<span class=\"pl-k\">return</span> <span class=\"pl-c1\">nil</span>\n})\n<span class=\"pl-k\">if</span> <span class=\"pl-s1\">err</span> <span class=\"pl-c1\">!=</span> <span class=\"pl-c1\">nil</span> {\n\t<span class=\"pl-k\">return</span> <span class=\"pl-c1\">...</span>\n}\n<span class=\"pl-s1\">fmt</span>.<span class=\"pl-c1\">Println</span>(<span class=\"pl-s\">\"Allocated ID %d\"</span>, <span class=\"pl-s1\">id</span>)</pre></div>\n<h4 dir=\"auto\">Managing transactions manually</h4>\n<p dir=\"auto\">The <code class=\"notranslate\">DB.View()</code> and <code class=\"notranslate\">DB.Update()</code> functions are wrappers around the <code class=\"notranslate\">DB.Begin()</code><br>\nfunction. These helper functions will start the transaction, execute a function,<br>\nand then safely close your transaction if an error is returned. This is the<br>\nrecommended way to use Bolt transactions.</p>\n<p dir=\"auto\">However, sometimes you may want to manually start and end your transactions.<br>\nYou can use the <code class=\"notranslate\">DB.Begin()</code> function directly but <strong>please</strong> be sure to close<br>\nthe transaction.</p>\n<div class=\"highlight highlight-source-go notranslate position-relative overflow-auto\" dir=\"auto\" data-snippet-clipboard-copy-content=\"// Start a writable transaction.\ntx, err := db.Begin(true)\nif err != nil {\n    return err\n}\ndefer tx.Rollback()\n\n// Use the transaction...\n_, err := tx.CreateBucket([]byte(&quot;MyBucket&quot;))\nif err != nil {\n    return err\n}\n\n// Commit the transaction and check for error.\nif err := tx.Commit(); err != nil {\n    return err\n}\"><pre class=\"notranslate\"><span class=\"pl-c\">// Start a writable transaction.</span>\n<span class=\"pl-s1\">tx</span>, <span class=\"pl-s1\">err</span> <span class=\"pl-c1\">:=</span> <span class=\"pl-s1\">db</span>.<span class=\"pl-c1\">Begin</span>(<span class=\"pl-c1\">true</span>)\n<span class=\"pl-k\">if</span> <span class=\"pl-s1\">err</span> <span class=\"pl-c1\">!=</span> <span class=\"pl-c1\">nil</span> {\n    <span class=\"pl-k\">return</span> <span class=\"pl-s1\">err</span>\n}\n<span class=\"pl-k\">defer</span> <span class=\"pl-s1\">tx</span>.<span class=\"pl-c1\">Rollback</span>()\n\n<span class=\"pl-c\">// Use the transaction...</span>\n<span class=\"pl-s1\">_</span>, <span class=\"pl-s1\">err</span> <span class=\"pl-c1\">:=</span> <span class=\"pl-s1\">tx</span>.<span class=\"pl-c1\">CreateBucket</span>([]<span class=\"pl-smi\">byte</span>(<span class=\"pl-s\">\"MyBucket\"</span>))\n<span class=\"pl-k\">if</span> <span class=\"pl-s1\">err</span> <span class=\"pl-c1\">!=</span> <span class=\"pl-c1\">nil</span> {\n    <span class=\"pl-k\">return</span> <span class=\"pl-s1\">err</span>\n}\n\n<span class=\"pl-c\">// Commit the transaction and check for error.</span>\n<span class=\"pl-k\">if</span> <span class=\"pl-s1\">err</span> <span class=\"pl-c1\">:=</span> <span class=\"pl-s1\">tx</span>.<span class=\"pl-c1\">Commit</span>(); <span class=\"pl-s1\">err</span> <span class=\"pl-c1\">!=</span> <span class=\"pl-c1\">nil</span> {\n    <span class=\"pl-k\">return</span> <span class=\"pl-s1\">err</span>\n}</pre></div>\n<p dir=\"auto\">The first argument to <code class=\"notranslate\">DB.Begin()</code> is a boolean stating if the transaction<br>\nshould be writable.</p>\n<h3 dir=\"auto\">Using buckets</h3>\n<p dir=\"auto\">Buckets are collections of key/value pairs within the database. All keys in a<br>\nbucket must be unique. You can create a bucket using the <code class=\"notranslate\">Tx.CreateBucket()</code><br>\nfunction:</p>\n<div class=\"highlight highlight-source-go notranslate position-relative overflow-auto\" dir=\"auto\" data-snippet-clipboard-copy-content=\"db.Update(func(tx *bolt.Tx) error {\n\tb, err := tx.CreateBucket([]byte(&quot;MyBucket&quot;))\n\tif err != nil {\n\t\treturn fmt.Errorf(&quot;create bucket: %s&quot;, err)\n\t}\n\treturn nil\n})\"><pre class=\"notranslate\"><span class=\"pl-s1\">db</span>.<span class=\"pl-c1\">Update</span>(<span class=\"pl-k\">func</span>(<span class=\"pl-s1\">tx</span> <span class=\"pl-c1\">*</span>bolt.<span class=\"pl-smi\">Tx</span>) <span class=\"pl-smi\">error</span> {\n\t<span class=\"pl-s1\">b</span>, <span class=\"pl-s1\">err</span> <span class=\"pl-c1\">:=</span> <span class=\"pl-s1\">tx</span>.<span class=\"pl-c1\">CreateBucket</span>([]<span class=\"pl-smi\">byte</span>(<span class=\"pl-s\">\"MyBucket\"</span>))\n\t<span class=\"pl-k\">if</span> <span class=\"pl-s1\">err</span> <span class=\"pl-c1\">!=</span> <span class=\"pl-c1\">nil</span> {\n\t\t<span class=\"pl-k\">return</span> <span class=\"pl-s1\">fmt</span>.<span class=\"pl-c1\">Errorf</span>(<span class=\"pl-s\">\"create bucket: %s\"</span>, <span class=\"pl-s1\">err</span>)\n\t}\n\t<span class=\"pl-k\">return</span> <span class=\"pl-c1\">nil</span>\n})</pre></div>\n<p dir=\"auto\">You can retrieve an existing bucket using the <code class=\"notranslate\">Tx.Bucket()</code> function:</p>\n<div class=\"highlight highlight-source-go notranslate position-relative overflow-auto\" dir=\"auto\" data-snippet-clipboard-copy-content=\"db.Update(func(tx *bolt.Tx) error {\n\tb := tx.Bucket([]byte(&quot;MyBucket&quot;))\n\tif b == nil {\n\t\treturn errors.New(&quot;bucket does not exist&quot;)\n\t}\n\treturn nil\n})\"><pre class=\"notranslate\"><span class=\"pl-s1\">db</span>.<span class=\"pl-c1\">Update</span>(<span class=\"pl-k\">func</span>(<span class=\"pl-s1\">tx</span> <span class=\"pl-c1\">*</span>bolt.<span class=\"pl-smi\">Tx</span>) <span class=\"pl-smi\">error</span> {\n\t<span class=\"pl-s1\">b</span> <span class=\"pl-c1\">:=</span> <span class=\"pl-s1\">tx</span>.<span class=\"pl-c1\">Bucket</span>([]<span class=\"pl-smi\">byte</span>(<span class=\"pl-s\">\"MyBucket\"</span>))\n\t<span class=\"pl-k\">if</span> <span class=\"pl-s1\">b</span> <span class=\"pl-c1\">==</span> <span class=\"pl-c1\">nil</span> {\n\t\t<span class=\"pl-k\">return</span> <span class=\"pl-s1\">errors</span>.<span class=\"pl-c1\">New</span>(<span class=\"pl-s\">\"bucket does not exist\"</span>)\n\t}\n\t<span class=\"pl-k\">return</span> <span class=\"pl-c1\">nil</span>\n})</pre></div>\n<p dir=\"auto\">You can also create a bucket only if it doesn't exist by using the<br>\n<code class=\"notranslate\">Tx.CreateBucketIfNotExists()</code> function. It's a common pattern to call this<br>\nfunction for all your top-level buckets after you open your database so you can<br>\nguarantee that they exist for future transactions.</p>\n<p dir=\"auto\">To delete a bucket, simply call the <code class=\"notranslate\">Tx.DeleteBucket()</code> function.</p>\n<p dir=\"auto\">You can also iterate over all existing top-level buckets with <code class=\"notranslate\">Tx.ForEach()</code>:</p>\n<div class=\"highlight highlight-source-go notranslate position-relative overflow-auto\" dir=\"auto\" data-snippet-clipboard-copy-content=\"db.View(func(tx *bolt.Tx) error {\n\ttx.ForEach(func(name []byte, b *bolt.Bucket) error {\n\t\tfmt.Println(string(name))\n\t\treturn nil\n\t})\n\treturn nil\n})\"><pre class=\"notranslate\"><span class=\"pl-s1\">db</span>.<span class=\"pl-c1\">View</span>(<span class=\"pl-k\">func</span>(<span class=\"pl-s1\">tx</span> <span class=\"pl-c1\">*</span>bolt.<span class=\"pl-smi\">Tx</span>) <span class=\"pl-smi\">error</span> {\n\t<span class=\"pl-s1\">tx</span>.<span class=\"pl-c1\">ForEach</span>(<span class=\"pl-k\">func</span>(<span class=\"pl-s1\">name</span> []<span class=\"pl-smi\">byte</span>, <span class=\"pl-s1\">b</span> <span class=\"pl-c1\">*</span>bolt.<span class=\"pl-smi\">Bucket</span>) <span class=\"pl-smi\">error</span> {\n\t\t<span class=\"pl-s1\">fmt</span>.<span class=\"pl-c1\">Println</span>(<span class=\"pl-s1\">string</span>(<span class=\"pl-s1\">name</span>))\n\t\t<span class=\"pl-k\">return</span> <span class=\"pl-c1\">nil</span>\n\t})\n\t<span class=\"pl-k\">return</span> <span class=\"pl-c1\">nil</span>\n})</pre></div>\n<h3 dir=\"auto\">Using key/value pairs</h3>\n<p dir=\"auto\">To save a key/value pair to a bucket, use the <code class=\"notranslate\">Bucket.Put()</code> function:</p>\n<div class=\"highlight highlight-source-go notranslate position-relative overflow-auto\" dir=\"auto\" data-snippet-clipboard-copy-content=\"db.Update(func(tx *bolt.Tx) error {\n\tb := tx.Bucket([]byte(&quot;MyBucket&quot;))\n\terr := b.Put([]byte(&quot;answer&quot;), []byte(&quot;42&quot;))\n\treturn err\n})\"><pre class=\"notranslate\"><span class=\"pl-s1\">db</span>.<span class=\"pl-c1\">Update</span>(<span class=\"pl-k\">func</span>(<span class=\"pl-s1\">tx</span> <span class=\"pl-c1\">*</span>bolt.<span class=\"pl-smi\">Tx</span>) <span class=\"pl-smi\">error</span> {\n\t<span class=\"pl-s1\">b</span> <span class=\"pl-c1\">:=</span> <span class=\"pl-s1\">tx</span>.<span class=\"pl-c1\">Bucket</span>([]<span class=\"pl-smi\">byte</span>(<span class=\"pl-s\">\"MyBucket\"</span>))\n\t<span class=\"pl-s1\">err</span> <span class=\"pl-c1\">:=</span> <span class=\"pl-s1\">b</span>.<span class=\"pl-c1\">Put</span>([]<span class=\"pl-smi\">byte</span>(<span class=\"pl-s\">\"answer\"</span>), []<span class=\"pl-smi\">byte</span>(<span class=\"pl-s\">\"42\"</span>))\n\t<span class=\"pl-k\">return</span> <span class=\"pl-s1\">err</span>\n})</pre></div>\n<p dir=\"auto\">This will set the value of the <code class=\"notranslate\">\"answer\"</code> key to <code class=\"notranslate\">\"42\"</code> in the <code class=\"notranslate\">MyBucket</code><br>\nbucket. To retrieve this value, we can use the <code class=\"notranslate\">Bucket.Get()</code> function:</p>\n<div class=\"highlight highlight-source-go notranslate position-relative overflow-auto\" dir=\"auto\" data-snippet-clipboard-copy-content=\"db.View(func(tx *bolt.Tx) error {\n\tb := tx.Bucket([]byte(&quot;MyBucket&quot;))\n\tv := b.Get([]byte(&quot;answer&quot;))\n\tfmt.Printf(&quot;The answer is: %s\\n&quot;, v)\n\treturn nil\n})\"><pre class=\"notranslate\"><span class=\"pl-s1\">db</span>.<span class=\"pl-c1\">View</span>(<span class=\"pl-k\">func</span>(<span class=\"pl-s1\">tx</span> <span class=\"pl-c1\">*</span>bolt.<span class=\"pl-smi\">Tx</span>) <span class=\"pl-smi\">error</span> {\n\t<span class=\"pl-s1\">b</span> <span class=\"pl-c1\">:=</span> <span class=\"pl-s1\">tx</span>.<span class=\"pl-c1\">Bucket</span>([]<span class=\"pl-smi\">byte</span>(<span class=\"pl-s\">\"MyBucket\"</span>))\n\t<span class=\"pl-s1\">v</span> <span class=\"pl-c1\">:=</span> <span class=\"pl-s1\">b</span>.<span class=\"pl-c1\">Get</span>([]<span class=\"pl-smi\">byte</span>(<span class=\"pl-s\">\"answer\"</span>))\n\t<span class=\"pl-s1\">fmt</span>.<span class=\"pl-c1\">Printf</span>(<span class=\"pl-s\">\"The answer is: %s<span class=\"pl-cce\">\\n</span>\"</span>, <span class=\"pl-s1\">v</span>)\n\t<span class=\"pl-k\">return</span> <span class=\"pl-c1\">nil</span>\n})</pre></div>\n<p dir=\"auto\">The <code class=\"notranslate\">Get()</code> function does not return an error because its operation is<br>\nguaranteed to work (unless there is some kind of system failure). If the key<br>\nexists then it will return its byte slice value. If it doesn't exist then it<br>\nwill return <code class=\"notranslate\">nil</code>. It's important to note that you can have a zero-length value<br>\nset to a key which is different than the key not existing.</p>\n<p dir=\"auto\">Use the <code class=\"notranslate\">Bucket.Delete()</code> function to delete a key from the bucket:</p>\n<div class=\"highlight highlight-source-go notranslate position-relative overflow-auto\" dir=\"auto\" data-snippet-clipboard-copy-content=\"db.Update(func (tx *bolt.Tx) error {\n    b := tx.Bucket([]byte(&quot;MyBucket&quot;))\n    err := b.Delete([]byte(&quot;answer&quot;))\n    return err\n})\"><pre class=\"notranslate\"><span class=\"pl-s1\">db</span>.<span class=\"pl-c1\">Update</span>(<span class=\"pl-k\">func</span> (<span class=\"pl-s1\">tx</span> <span class=\"pl-c1\">*</span>bolt.<span class=\"pl-smi\">Tx</span>) <span class=\"pl-smi\">error</span> {\n    <span class=\"pl-s1\">b</span> <span class=\"pl-c1\">:=</span> <span class=\"pl-s1\">tx</span>.<span class=\"pl-c1\">Bucket</span>([]<span class=\"pl-smi\">byte</span>(<span class=\"pl-s\">\"MyBucket\"</span>))\n    <span class=\"pl-s1\">err</span> <span class=\"pl-c1\">:=</span> <span class=\"pl-s1\">b</span>.<span class=\"pl-c1\">Delete</span>([]<span class=\"pl-smi\">byte</span>(<span class=\"pl-s\">\"answer\"</span>))\n    <span class=\"pl-k\">return</span> <span class=\"pl-s1\">err</span>\n})</pre></div>\n<p dir=\"auto\">This will delete the key <code class=\"notranslate\">answers</code> from the bucket <code class=\"notranslate\">MyBucket</code>.</p>\n<p dir=\"auto\">Please note that values returned from <code class=\"notranslate\">Get()</code> are only valid while the<br>\ntransaction is open. If you need to use a value outside of the transaction<br>\nthen you must use <code class=\"notranslate\">copy()</code> to copy it to another byte slice.</p>\n<h3 dir=\"auto\">Autoincrementing integer for the bucket</h3>\n<p dir=\"auto\">By using the <code class=\"notranslate\">NextSequence()</code> function, you can let Bolt determine a sequence<br>\nwhich can be used as the unique identifier for your key/value pairs. See the<br>\nexample below.</p>\n<div class=\"highlight highlight-source-go notranslate position-relative overflow-auto\" dir=\"auto\" data-snippet-clipboard-copy-content=\"// CreateUser saves u to the store. The new user ID is set on u once the data is persisted.\nfunc (s *Store) CreateUser(u *User) error {\n    return s.db.Update(func(tx *bolt.Tx) error {\n        // Retrieve the users bucket.\n        // This should be created when the DB is first opened.\n        b := tx.Bucket([]byte(&quot;users&quot;))\n\n        // Generate ID for the user.\n        // This returns an error only if the Tx is closed or not writeable.\n        // That can't happen in an Update() call so I ignore the error check.\n        id, _ := b.NextSequence()\n        u.ID = int(id)\n\n        // Marshal user data into bytes.\n        buf, err := json.Marshal(u)\n        if err != nil {\n            return err\n        }\n\n        // Persist bytes to users bucket.\n        return b.Put(itob(u.ID), buf)\n    })\n}\n\n// itob returns an 8-byte big endian representation of v.\nfunc itob(v int) []byte {\n    b := make([]byte, 8)\n    binary.BigEndian.PutUint64(b, uint64(v))\n    return b\n}\n\ntype User struct {\n    ID int\n    ...\n}\"><pre class=\"notranslate\"><span class=\"pl-c\">// CreateUser saves u to the store. The new user ID is set on u once the data is persisted.</span>\n<span class=\"pl-k\">func</span> (<span class=\"pl-s1\">s</span> <span class=\"pl-c1\">*</span><span class=\"pl-smi\">Store</span>) <span class=\"pl-c1\">CreateUser</span>(<span class=\"pl-s1\">u</span> <span class=\"pl-c1\">*</span><span class=\"pl-smi\">User</span>) <span class=\"pl-smi\">error</span> {\n    <span class=\"pl-k\">return</span> <span class=\"pl-s1\">s</span>.<span class=\"pl-c1\">db</span>.<span class=\"pl-c1\">Update</span>(<span class=\"pl-k\">func</span>(<span class=\"pl-s1\">tx</span> <span class=\"pl-c1\">*</span>bolt.<span class=\"pl-smi\">Tx</span>) <span class=\"pl-smi\">error</span> {\n        <span class=\"pl-c\">// Retrieve the users bucket.</span>\n        <span class=\"pl-c\">// This should be created when the DB is first opened.</span>\n        <span class=\"pl-s1\">b</span> <span class=\"pl-c1\">:=</span> <span class=\"pl-s1\">tx</span>.<span class=\"pl-c1\">Bucket</span>([]<span class=\"pl-smi\">byte</span>(<span class=\"pl-s\">\"users\"</span>))\n\n        <span class=\"pl-c\">// Generate ID for the user.</span>\n        <span class=\"pl-c\">// This returns an error only if the Tx is closed or not writeable.</span>\n        <span class=\"pl-c\">// That can't happen in an Update() call so I ignore the error check.</span>\n        <span class=\"pl-s1\">id</span>, <span class=\"pl-s1\">_</span> <span class=\"pl-c1\">:=</span> <span class=\"pl-s1\">b</span>.<span class=\"pl-c1\">NextSequence</span>()\n        <span class=\"pl-s1\">u</span>.<span class=\"pl-c1\">ID</span> <span class=\"pl-c1\">=</span> <span class=\"pl-s1\">int</span>(<span class=\"pl-s1\">id</span>)\n\n        <span class=\"pl-c\">// Marshal user data into bytes.</span>\n        <span class=\"pl-s1\">buf</span>, <span class=\"pl-s1\">err</span> <span class=\"pl-c1\">:=</span> <span class=\"pl-s1\">json</span>.<span class=\"pl-c1\">Marshal</span>(<span class=\"pl-s1\">u</span>)\n        <span class=\"pl-k\">if</span> <span class=\"pl-s1\">err</span> <span class=\"pl-c1\">!=</span> <span class=\"pl-c1\">nil</span> {\n            <span class=\"pl-k\">return</span> <span class=\"pl-s1\">err</span>\n        }\n\n        <span class=\"pl-c\">// Persist bytes to users bucket.</span>\n        <span class=\"pl-k\">return</span> <span class=\"pl-s1\">b</span>.<span class=\"pl-c1\">Put</span>(<span class=\"pl-s1\">itob</span>(<span class=\"pl-s1\">u</span>.<span class=\"pl-c1\">ID</span>), <span class=\"pl-s1\">buf</span>)\n    })\n}\n\n<span class=\"pl-c\">// itob returns an 8-byte big endian representation of v.</span>\n<span class=\"pl-k\">func</span> <span class=\"pl-s1\">itob</span>(<span class=\"pl-s1\">v</span> <span class=\"pl-smi\">int</span>) []<span class=\"pl-smi\">byte</span> {\n    <span class=\"pl-s1\">b</span> <span class=\"pl-c1\">:=</span> <span class=\"pl-s1\">make</span>([]<span class=\"pl-smi\">byte</span>, <span class=\"pl-c1\">8</span>)\n    <span class=\"pl-s1\">binary</span>.<span class=\"pl-c1\">BigEndian</span>.<span class=\"pl-c1\">PutUint64</span>(<span class=\"pl-s1\">b</span>, <span class=\"pl-s1\">uint64</span>(<span class=\"pl-s1\">v</span>))\n    <span class=\"pl-k\">return</span> <span class=\"pl-s1\">b</span>\n}\n\n<span class=\"pl-k\">type</span> <span class=\"pl-smi\">User</span> <span class=\"pl-k\">struct</span> {\n    <span class=\"pl-c1\">ID</span> <span class=\"pl-smi\">int</span>\n    <span class=\"pl-c1\">...</span>\n}</pre></div>\n<h3 dir=\"auto\">Iterating over keys</h3>\n<p dir=\"auto\">Bolt stores its keys in byte-sorted order within a bucket. This makes sequential<br>\niteration over these keys extremely fast. To iterate over keys we'll use a<br>\n<code class=\"notranslate\">Cursor</code>:</p>\n<div class=\"highlight highlight-source-go notranslate position-relative overflow-auto\" dir=\"auto\" data-snippet-clipboard-copy-content=\"db.View(func(tx *bolt.Tx) error {\n\t// Assume bucket exists and has keys\n\tb := tx.Bucket([]byte(&quot;MyBucket&quot;))\n\n\tc := b.Cursor()\n\n\tfor k, v := c.First(); k != nil; k, v = c.Next() {\n\t\tfmt.Printf(&quot;key=%s, value=%s\\n&quot;, k, v)\n\t}\n\n\treturn nil\n})\"><pre class=\"notranslate\"><span class=\"pl-s1\">db</span>.<span class=\"pl-c1\">View</span>(<span class=\"pl-k\">func</span>(<span class=\"pl-s1\">tx</span> <span class=\"pl-c1\">*</span>bolt.<span class=\"pl-smi\">Tx</span>) <span class=\"pl-smi\">error</span> {\n\t<span class=\"pl-c\">// Assume bucket exists and has keys</span>\n\t<span class=\"pl-s1\">b</span> <span class=\"pl-c1\">:=</span> <span class=\"pl-s1\">tx</span>.<span class=\"pl-c1\">Bucket</span>([]<span class=\"pl-smi\">byte</span>(<span class=\"pl-s\">\"MyBucket\"</span>))\n\n\t<span class=\"pl-s1\">c</span> <span class=\"pl-c1\">:=</span> <span class=\"pl-s1\">b</span>.<span class=\"pl-c1\">Cursor</span>()\n\n\t<span class=\"pl-k\">for</span> <span class=\"pl-s1\">k</span>, <span class=\"pl-s1\">v</span> <span class=\"pl-c1\">:=</span> <span class=\"pl-s1\">c</span>.<span class=\"pl-c1\">First</span>(); <span class=\"pl-s1\">k</span> <span class=\"pl-c1\">!=</span> <span class=\"pl-c1\">nil</span>; <span class=\"pl-s1\">k</span>, <span class=\"pl-s1\">v</span> <span class=\"pl-c1\">=</span> <span class=\"pl-s1\">c</span>.<span class=\"pl-c1\">Next</span>() {\n\t\t<span class=\"pl-s1\">fmt</span>.<span class=\"pl-c1\">Printf</span>(<span class=\"pl-s\">\"key=%s, value=%s<span class=\"pl-cce\">\\n</span>\"</span>, <span class=\"pl-s1\">k</span>, <span class=\"pl-s1\">v</span>)\n\t}\n\n\t<span class=\"pl-k\">return</span> <span class=\"pl-c1\">nil</span>\n})</pre></div>\n<p dir=\"auto\">The cursor allows you to move to a specific point in the list of keys and move<br>\nforward or backward through the keys one at a time.</p>\n<p dir=\"auto\">The following functions are available on the cursor:</p>\n<div class=\"snippet-clipboard-content notranslate position-relative overflow-auto\" data-snippet-clipboard-copy-content=\"First()  Move to the first key.\nLast()   Move to the last key.\nSeek()   Move to a specific key.\nNext()   Move to the next key.\nPrev()   Move to the previous key.\"><pre class=\"notranslate\"><code class=\"notranslate\">First()  Move to the first key.\nLast()   Move to the last key.\nSeek()   Move to a specific key.\nNext()   Move to the next key.\nPrev()   Move to the previous key.\n</code></pre></div>\n<p dir=\"auto\">Each of those functions has a return signature of <code class=\"notranslate\">(key []byte, value []byte)</code>.<br>\nYou must seek to a position using <code class=\"notranslate\">First()</code>, <code class=\"notranslate\">Last()</code>, or <code class=\"notranslate\">Seek()</code> before calling<br>\n<code class=\"notranslate\">Next()</code> or <code class=\"notranslate\">Prev()</code>. If you do not seek to a position then these functions will<br>\nreturn a <code class=\"notranslate\">nil</code> key.</p>\n<p dir=\"auto\">When you have iterated to the end of the cursor, then <code class=\"notranslate\">Next()</code> will return a<br>\n<code class=\"notranslate\">nil</code> key and the cursor still points to the last element if present. When you<br>\nhave iterated to the beginning of the cursor, then <code class=\"notranslate\">Prev()</code> will return a <code class=\"notranslate\">nil</code><br>\nkey and the cursor still points to the first element if present.</p>\n<p dir=\"auto\">If you remove key/value pairs during iteration, the cursor may automatically<br>\nmove to the next position if present in current node each time removing a key.<br>\nWhen you call <code class=\"notranslate\">c.Next()</code> after removing a key, it may skip one key/value pair.<br>\nRefer to <a href=\"https://github.com/etcd-io/bbolt/pull/611\" data-hovercard-type=\"pull_request\" data-hovercard-url=\"/etcd-io/bbolt/pull/611/hovercard\">pull/611</a> to get more detailed info.</p>\n<p dir=\"auto\">During iteration, if the key is non-<code class=\"notranslate\">nil</code> but the value is <code class=\"notranslate\">nil</code>, that means<br>\nthe key refers to a bucket rather than a value.  Use <code class=\"notranslate\">Bucket.Bucket()</code> to<br>\naccess the sub-bucket.</p>\n<h4 dir=\"auto\">Prefix scans</h4>\n<p dir=\"auto\">To iterate over a key prefix, you can combine <code class=\"notranslate\">Seek()</code> and <code class=\"notranslate\">bytes.HasPrefix()</code>:</p>\n<div class=\"highlight highlight-source-go notranslate position-relative overflow-auto\" dir=\"auto\" data-snippet-clipboard-copy-content=\"db.View(func(tx *bolt.Tx) error {\n\t// Assume bucket exists and has keys\n\tc := tx.Bucket([]byte(&quot;MyBucket&quot;)).Cursor()\n\n\tprefix := []byte(&quot;1234&quot;)\n\tfor k, v := c.Seek(prefix); k != nil &amp;&amp; bytes.HasPrefix(k, prefix); k, v = c.Next() {\n\t\tfmt.Printf(&quot;key=%s, value=%s\\n&quot;, k, v)\n\t}\n\n\treturn nil\n})\"><pre class=\"notranslate\"><span class=\"pl-s1\">db</span>.<span class=\"pl-c1\">View</span>(<span class=\"pl-k\">func</span>(<span class=\"pl-s1\">tx</span> <span class=\"pl-c1\">*</span>bolt.<span class=\"pl-smi\">Tx</span>) <span class=\"pl-smi\">error</span> {\n\t<span class=\"pl-c\">// Assume bucket exists and has keys</span>\n\t<span class=\"pl-s1\">c</span> <span class=\"pl-c1\">:=</span> <span class=\"pl-s1\">tx</span>.<span class=\"pl-c1\">Bucket</span>([]<span class=\"pl-smi\">byte</span>(<span class=\"pl-s\">\"MyBucket\"</span>)).<span class=\"pl-c1\">Cursor</span>()\n\n\t<span class=\"pl-s1\">prefix</span> <span class=\"pl-c1\">:=</span> []<span class=\"pl-smi\">byte</span>(<span class=\"pl-s\">\"1234\"</span>)\n\t<span class=\"pl-k\">for</span> <span class=\"pl-s1\">k</span>, <span class=\"pl-s1\">v</span> <span class=\"pl-c1\">:=</span> <span class=\"pl-s1\">c</span>.<span class=\"pl-c1\">Seek</span>(<span class=\"pl-s1\">prefix</span>); <span class=\"pl-s1\">k</span> <span class=\"pl-c1\">!=</span> <span class=\"pl-c1\">nil</span> <span class=\"pl-c1\">&amp;&amp;</span> <span class=\"pl-s1\">bytes</span>.<span class=\"pl-c1\">HasPrefix</span>(<span class=\"pl-s1\">k</span>, <span class=\"pl-s1\">prefix</span>); <span class=\"pl-s1\">k</span>, <span class=\"pl-s1\">v</span> <span class=\"pl-c1\">=</span> <span class=\"pl-s1\">c</span>.<span class=\"pl-c1\">Next</span>() {\n\t\t<span class=\"pl-s1\">fmt</span>.<span class=\"pl-c1\">Printf</span>(<span class=\"pl-s\">\"key=%s, value=%s<span class=\"pl-cce\">\\n</span>\"</span>, <span class=\"pl-s1\">k</span>, <span class=\"pl-s1\">v</span>)\n\t}\n\n\t<span class=\"pl-k\">return</span> <span class=\"pl-c1\">nil</span>\n})</pre></div>\n<h4 dir=\"auto\">Range scans</h4>\n<p dir=\"auto\">Another common use case is scanning over a range such as a time range. If you<br>\nuse a sortable time encoding such as RFC3339 then you can query a specific<br>\ndate range like this:</p>\n<div class=\"highlight highlight-source-go notranslate position-relative overflow-auto\" dir=\"auto\" data-snippet-clipboard-copy-content=\"db.View(func(tx *bolt.Tx) error {\n\t// Assume our events bucket exists and has RFC3339 encoded time keys.\n\tc := tx.Bucket([]byte(&quot;Events&quot;)).Cursor()\n\n\t// Our time range spans the 90's decade.\n\tmin := []byte(&quot;1990-01-01T00:00:00Z&quot;)\n\tmax := []byte(&quot;2000-01-01T00:00:00Z&quot;)\n\n\t// Iterate over the 90's.\n\tfor k, v := c.Seek(min); k != nil &amp;&amp; bytes.Compare(k, max) &lt;= 0; k, v = c.Next() {\n\t\tfmt.Printf(&quot;%s: %s\\n&quot;, k, v)\n\t}\n\n\treturn nil\n})\"><pre class=\"notranslate\"><span class=\"pl-s1\">db</span>.<span class=\"pl-c1\">View</span>(<span class=\"pl-k\">func</span>(<span class=\"pl-s1\">tx</span> <span class=\"pl-c1\">*</span>bolt.<span class=\"pl-smi\">Tx</span>) <span class=\"pl-smi\">error</span> {\n\t<span class=\"pl-c\">// Assume our events bucket exists and has RFC3339 encoded time keys.</span>\n\t<span class=\"pl-s1\">c</span> <span class=\"pl-c1\">:=</span> <span class=\"pl-s1\">tx</span>.<span class=\"pl-c1\">Bucket</span>([]<span class=\"pl-smi\">byte</span>(<span class=\"pl-s\">\"Events\"</span>)).<span class=\"pl-c1\">Cursor</span>()\n\n\t<span class=\"pl-c\">// Our time range spans the 90's decade.</span>\n\t<span class=\"pl-s1\">min</span> <span class=\"pl-c1\">:=</span> []<span class=\"pl-smi\">byte</span>(<span class=\"pl-s\">\"1990-01-01T00:00:00Z\"</span>)\n\t<span class=\"pl-s1\">max</span> <span class=\"pl-c1\">:=</span> []<span class=\"pl-smi\">byte</span>(<span class=\"pl-s\">\"2000-01-01T00:00:00Z\"</span>)\n\n\t<span class=\"pl-c\">// Iterate over the 90's.</span>\n\t<span class=\"pl-k\">for</span> <span class=\"pl-s1\">k</span>, <span class=\"pl-s1\">v</span> <span class=\"pl-c1\">:=</span> <span class=\"pl-s1\">c</span>.<span class=\"pl-c1\">Seek</span>(<span class=\"pl-s1\">min</span>); <span class=\"pl-s1\">k</span> <span class=\"pl-c1\">!=</span> <span class=\"pl-c1\">nil</span> <span class=\"pl-c1\">&amp;&amp;</span> <span class=\"pl-s1\">bytes</span>.<span class=\"pl-c1\">Compare</span>(<span class=\"pl-s1\">k</span>, <span class=\"pl-s1\">max</span>) <span class=\"pl-c1\">&lt;=</span> <span class=\"pl-c1\">0</span>; <span class=\"pl-s1\">k</span>, <span class=\"pl-s1\">v</span> <span class=\"pl-c1\">=</span> <span class=\"pl-s1\">c</span>.<span class=\"pl-c1\">Next</span>() {\n\t\t<span class=\"pl-s1\">fmt</span>.<span class=\"pl-c1\">Printf</span>(<span class=\"pl-s\">\"%s: %s<span class=\"pl-cce\">\\n</span>\"</span>, <span class=\"pl-s1\">k</span>, <span class=\"pl-s1\">v</span>)\n\t}\n\n\t<span class=\"pl-k\">return</span> <span class=\"pl-c1\">nil</span>\n})</pre></div>\n<p dir=\"auto\">Note that, while RFC3339 is sortable, the Golang implementation of RFC3339Nano does not use a fixed number of digits after the decimal point and is therefore not sortable.</p>\n<h4 dir=\"auto\">ForEach()</h4>\n<p dir=\"auto\">You can also use the function <code class=\"notranslate\">ForEach()</code> if you know you'll be iterating over<br>\nall the keys in a bucket:</p>\n<div class=\"highlight highlight-source-go notranslate position-relative overflow-auto\" dir=\"auto\" data-snippet-clipboard-copy-content=\"db.View(func(tx *bolt.Tx) error {\n\t// Assume bucket exists and has keys\n\tb := tx.Bucket([]byte(&quot;MyBucket&quot;))\n\n\tb.ForEach(func(k, v []byte) error {\n\t\tfmt.Printf(&quot;key=%s, value=%s\\n&quot;, k, v)\n\t\treturn nil\n\t})\n\treturn nil\n})\"><pre class=\"notranslate\"><span class=\"pl-s1\">db</span>.<span class=\"pl-c1\">View</span>(<span class=\"pl-k\">func</span>(<span class=\"pl-s1\">tx</span> <span class=\"pl-c1\">*</span>bolt.<span class=\"pl-smi\">Tx</span>) <span class=\"pl-smi\">error</span> {\n\t<span class=\"pl-c\">// Assume bucket exists and has keys</span>\n\t<span class=\"pl-s1\">b</span> <span class=\"pl-c1\">:=</span> <span class=\"pl-s1\">tx</span>.<span class=\"pl-c1\">Bucket</span>([]<span class=\"pl-smi\">byte</span>(<span class=\"pl-s\">\"MyBucket\"</span>))\n\n\t<span class=\"pl-s1\">b</span>.<span class=\"pl-c1\">ForEach</span>(<span class=\"pl-k\">func</span>(<span class=\"pl-s1\">k</span>, <span class=\"pl-s1\">v</span> []<span class=\"pl-smi\">byte</span>) <span class=\"pl-smi\">error</span> {\n\t\t<span class=\"pl-s1\">fmt</span>.<span class=\"pl-c1\">Printf</span>(<span class=\"pl-s\">\"key=%s, value=%s<span class=\"pl-cce\">\\n</span>\"</span>, <span class=\"pl-s1\">k</span>, <span class=\"pl-s1\">v</span>)\n\t\t<span class=\"pl-k\">return</span> <span class=\"pl-c1\">nil</span>\n\t})\n\t<span class=\"pl-k\">return</span> <span class=\"pl-c1\">nil</span>\n})</pre></div>\n<p dir=\"auto\">Please note that keys and values in <code class=\"notranslate\">ForEach()</code> are only valid while<br>\nthe transaction is open. If you need to use a key or value outside of<br>\nthe transaction, you must use <code class=\"notranslate\">copy()</code> to copy it to another byte<br>\nslice.</p>\n<h3 dir=\"auto\">Nested buckets</h3>\n<p dir=\"auto\">You can also store a bucket in a key to create nested buckets. The API is the<br>\nsame as the bucket management API on the <code class=\"notranslate\">DB</code> object:</p>\n<div class=\"highlight highlight-source-go notranslate position-relative overflow-auto\" dir=\"auto\" data-snippet-clipboard-copy-content=\"func (*Bucket) CreateBucket(key []byte) (*Bucket, error)\nfunc (*Bucket) CreateBucketIfNotExists(key []byte) (*Bucket, error)\nfunc (*Bucket) DeleteBucket(key []byte) error\"><pre class=\"notranslate\"><span class=\"pl-k\">func</span> (<span class=\"pl-c1\">*</span><span class=\"pl-smi\">Bucket</span>) <span class=\"pl-c1\">CreateBucket</span>(<span class=\"pl-s1\">key</span> []<span class=\"pl-smi\">byte</span>) (<span class=\"pl-c1\">*</span><span class=\"pl-smi\">Bucket</span>, <span class=\"pl-smi\">error</span>)\n<span class=\"pl-k\">func</span> (<span class=\"pl-c1\">*</span><span class=\"pl-smi\">Bucket</span>) <span class=\"pl-c1\">CreateBucketIfNotExists</span>(<span class=\"pl-s1\">key</span> []<span class=\"pl-smi\">byte</span>) (<span class=\"pl-c1\">*</span><span class=\"pl-smi\">Bucket</span>, <span class=\"pl-smi\">error</span>)\n<span class=\"pl-k\">func</span> (<span class=\"pl-c1\">*</span><span class=\"pl-smi\">Bucket</span>) <span class=\"pl-c1\">DeleteBucket</span>(<span class=\"pl-s1\">key</span> []<span class=\"pl-smi\">byte</span>) <span class=\"pl-smi\">error</span></pre></div>\n<p dir=\"auto\">Say you had a multi-tenant application where the root level bucket was the account bucket. Inside of this bucket was a sequence of accounts which themselves are buckets. And inside the sequence bucket you could have many buckets pertaining to the Account itself (Users, Notes, etc) isolating the information into logical groupings.</p>\n<div class=\"highlight highlight-source-go notranslate position-relative overflow-auto\" dir=\"auto\" data-snippet-clipboard-copy-content=\"\n// createUser creates a new user in the given account.\nfunc createUser(accountID int, u *User) error {\n    // Start the transaction.\n    tx, err := db.Begin(true)\n    if err != nil {\n        return err\n    }\n    defer tx.Rollback()\n\n    // Retrieve the root bucket for the account.\n    // Assume this has already been created when the account was set up.\n    root := tx.Bucket([]byte(strconv.FormatUint(accountID, 10)))\n\n    // Setup the users bucket.\n    bkt, err := root.CreateBucketIfNotExists([]byte(&quot;USERS&quot;))\n    if err != nil {\n        return err\n    }\n\n    // Generate an ID for the new user.\n    userID, err := bkt.NextSequence()\n    if err != nil {\n        return err\n    }\n    u.ID = userID\n\n    // Marshal and save the encoded user.\n    if buf, err := json.Marshal(u); err != nil {\n        return err\n    } else if err := bkt.Put([]byte(strconv.FormatUint(u.ID, 10)), buf); err != nil {\n        return err\n    }\n\n    // Commit the transaction.\n    if err := tx.Commit(); err != nil {\n        return err\n    }\n\n    return nil\n}\n\"><pre class=\"notranslate\"><span class=\"pl-c\">// createUser creates a new user in the given account.</span>\n<span class=\"pl-k\">func</span> <span class=\"pl-s1\">createUser</span>(<span class=\"pl-s1\">accountID</span> <span class=\"pl-smi\">int</span>, <span class=\"pl-s1\">u</span> <span class=\"pl-c1\">*</span><span class=\"pl-smi\">User</span>) <span class=\"pl-smi\">error</span> {\n    <span class=\"pl-c\">// Start the transaction.</span>\n    <span class=\"pl-s1\">tx</span>, <span class=\"pl-s1\">err</span> <span class=\"pl-c1\">:=</span> <span class=\"pl-s1\">db</span>.<span class=\"pl-c1\">Begin</span>(<span class=\"pl-c1\">true</span>)\n    <span class=\"pl-k\">if</span> <span class=\"pl-s1\">err</span> <span class=\"pl-c1\">!=</span> <span class=\"pl-c1\">nil</span> {\n        <span class=\"pl-k\">return</span> <span class=\"pl-s1\">err</span>\n    }\n    <span class=\"pl-k\">defer</span> <span class=\"pl-s1\">tx</span>.<span class=\"pl-c1\">Rollback</span>()\n\n    <span class=\"pl-c\">// Retrieve the root bucket for the account.</span>\n    <span class=\"pl-c\">// Assume this has already been created when the account was set up.</span>\n    <span class=\"pl-s1\">root</span> <span class=\"pl-c1\">:=</span> <span class=\"pl-s1\">tx</span>.<span class=\"pl-c1\">Bucket</span>([]<span class=\"pl-smi\">byte</span>(<span class=\"pl-s1\">strconv</span>.<span class=\"pl-c1\">FormatUint</span>(<span class=\"pl-s1\">accountID</span>, <span class=\"pl-c1\">10</span>)))\n\n    <span class=\"pl-c\">// Setup the users bucket.</span>\n    <span class=\"pl-s1\">bkt</span>, <span class=\"pl-s1\">err</span> <span class=\"pl-c1\">:=</span> <span class=\"pl-s1\">root</span>.<span class=\"pl-c1\">CreateBucketIfNotExists</span>([]<span class=\"pl-smi\">byte</span>(<span class=\"pl-s\">\"USERS\"</span>))\n    <span class=\"pl-k\">if</span> <span class=\"pl-s1\">err</span> <span class=\"pl-c1\">!=</span> <span class=\"pl-c1\">nil</span> {\n        <span class=\"pl-k\">return</span> <span class=\"pl-s1\">err</span>\n    }\n\n    <span class=\"pl-c\">// Generate an ID for the new user.</span>\n    <span class=\"pl-s1\">userID</span>, <span class=\"pl-s1\">err</span> <span class=\"pl-c1\">:=</span> <span class=\"pl-s1\">bkt</span>.<span class=\"pl-c1\">NextSequence</span>()\n    <span class=\"pl-k\">if</span> <span class=\"pl-s1\">err</span> <span class=\"pl-c1\">!=</span> <span class=\"pl-c1\">nil</span> {\n        <span class=\"pl-k\">return</span> <span class=\"pl-s1\">err</span>\n    }\n    <span class=\"pl-s1\">u</span>.<span class=\"pl-c1\">ID</span> <span class=\"pl-c1\">=</span> <span class=\"pl-s1\">userID</span>\n\n    <span class=\"pl-c\">// Marshal and save the encoded user.</span>\n    <span class=\"pl-k\">if</span> <span class=\"pl-s1\">buf</span>, <span class=\"pl-s1\">err</span> <span class=\"pl-c1\">:=</span> <span class=\"pl-s1\">json</span>.<span class=\"pl-c1\">Marshal</span>(<span class=\"pl-s1\">u</span>); <span class=\"pl-s1\">err</span> <span class=\"pl-c1\">!=</span> <span class=\"pl-c1\">nil</span> {\n        <span class=\"pl-k\">return</span> <span class=\"pl-s1\">err</span>\n    } <span class=\"pl-k\">else</span> <span class=\"pl-k\">if</span> <span class=\"pl-s1\">err</span> <span class=\"pl-c1\">:=</span> <span class=\"pl-s1\">bkt</span>.<span class=\"pl-c1\">Put</span>([]<span class=\"pl-smi\">byte</span>(<span class=\"pl-s1\">strconv</span>.<span class=\"pl-c1\">FormatUint</span>(<span class=\"pl-s1\">u</span>.<span class=\"pl-c1\">ID</span>, <span class=\"pl-c1\">10</span>)), <span class=\"pl-s1\">buf</span>); <span class=\"pl-s1\">err</span> <span class=\"pl-c1\">!=</span> <span class=\"pl-c1\">nil</span> {\n        <span class=\"pl-k\">return</span> <span class=\"pl-s1\">err</span>\n    }\n\n    <span class=\"pl-c\">// Commit the transaction.</span>\n    <span class=\"pl-k\">if</span> <span class=\"pl-s1\">err</span> <span class=\"pl-c1\">:=</span> <span class=\"pl-s1\">tx</span>.<span class=\"pl-c1\">Commit</span>(); <span class=\"pl-s1\">err</span> <span class=\"pl-c1\">!=</span> <span class=\"pl-c1\">nil</span> {\n        <span class=\"pl-k\">return</span> <span class=\"pl-s1\">err</span>\n    }\n\n    <span class=\"pl-k\">return</span> <span class=\"pl-c1\">nil</span>\n}</pre></div>\n<h3 dir=\"auto\">Database backups</h3>\n<p dir=\"auto\">Bolt is a single file so it's easy to backup. You can use the <code class=\"notranslate\">Tx.WriteTo()</code><br>\nfunction to write a consistent view of the database to a writer. If you call<br>\nthis from a read-only transaction, it will perform a hot backup and not block<br>\nyour other database reads and writes.</p>\n<p dir=\"auto\">By default, it will use a regular file handle which will utilize the operating<br>\nsystem's page cache. See the <a href=\"https://godoc.org/go.etcd.io/bbolt#Tx\" rel=\"nofollow\"><code class=\"notranslate\">Tx</code></a><br>\ndocumentation for information about optimizing for larger-than-RAM datasets.</p>\n<p dir=\"auto\">One common use case is to backup over HTTP so you can use tools like <code class=\"notranslate\">cURL</code> to<br>\ndo database backups:</p>\n<div class=\"highlight highlight-source-go notranslate position-relative overflow-auto\" dir=\"auto\" data-snippet-clipboard-copy-content=\"func BackupHandleFunc(w http.ResponseWriter, req *http.Request) {\n\terr := db.View(func(tx *bolt.Tx) error {\n\t\tw.Header().Set(&quot;Content-Type&quot;, &quot;application/octet-stream&quot;)\n\t\tw.Header().Set(&quot;Content-Disposition&quot;, `attachment; filename=&quot;my.db&quot;`)\n\t\tw.Header().Set(&quot;Content-Length&quot;, strconv.Itoa(int(tx.Size())))\n\t\t_, err := tx.WriteTo(w)\n\t\treturn err\n\t})\n\tif err != nil {\n\t\thttp.Error(w, err.Error(), http.StatusInternalServerError)\n\t}\n}\"><pre class=\"notranslate\"><span class=\"pl-k\">func</span> <span class=\"pl-s1\">BackupHandleFunc</span>(<span class=\"pl-s1\">w</span> http.<span class=\"pl-smi\">ResponseWriter</span>, <span class=\"pl-s1\">req</span> <span class=\"pl-c1\">*</span>http.<span class=\"pl-smi\">Request</span>) {\n\t<span class=\"pl-s1\">err</span> <span class=\"pl-c1\">:=</span> <span class=\"pl-s1\">db</span>.<span class=\"pl-c1\">View</span>(<span class=\"pl-k\">func</span>(<span class=\"pl-s1\">tx</span> <span class=\"pl-c1\">*</span>bolt.<span class=\"pl-smi\">Tx</span>) <span class=\"pl-smi\">error</span> {\n\t\t<span class=\"pl-s1\">w</span>.<span class=\"pl-c1\">Header</span>().<span class=\"pl-c1\">Set</span>(<span class=\"pl-s\">\"Content-Type\"</span>, <span class=\"pl-s\">\"application/octet-stream\"</span>)\n\t\t<span class=\"pl-s1\">w</span>.<span class=\"pl-c1\">Header</span>().<span class=\"pl-c1\">Set</span>(<span class=\"pl-s\">\"Content-Disposition\"</span>, <span class=\"pl-s\">`attachment; filename=\"my.db\"`</span>)\n\t\t<span class=\"pl-s1\">w</span>.<span class=\"pl-c1\">Header</span>().<span class=\"pl-c1\">Set</span>(<span class=\"pl-s\">\"Content-Length\"</span>, <span class=\"pl-s1\">strconv</span>.<span class=\"pl-c1\">Itoa</span>(<span class=\"pl-s1\">int</span>(<span class=\"pl-s1\">tx</span>.<span class=\"pl-c1\">Size</span>())))\n\t\t<span class=\"pl-s1\">_</span>, <span class=\"pl-s1\">err</span> <span class=\"pl-c1\">:=</span> <span class=\"pl-s1\">tx</span>.<span class=\"pl-c1\">WriteTo</span>(<span class=\"pl-s1\">w</span>)\n\t\t<span class=\"pl-k\">return</span> <span class=\"pl-s1\">err</span>\n\t})\n\t<span class=\"pl-k\">if</span> <span class=\"pl-s1\">err</span> <span class=\"pl-c1\">!=</span> <span class=\"pl-c1\">nil</span> {\n\t\t<span class=\"pl-s1\">http</span>.<span class=\"pl-c1\">Error</span>(<span class=\"pl-s1\">w</span>, <span class=\"pl-s1\">err</span>.<span class=\"pl-c1\">Error</span>(), <span class=\"pl-s1\">http</span>.<span class=\"pl-c1\">StatusInternalServerError</span>)\n\t}\n}</pre></div>\n<p dir=\"auto\">Then you can backup using this command:</p>\n<div class=\"highlight highlight-source-shell notranslate position-relative overflow-auto\" dir=\"auto\" data-snippet-clipboard-copy-content=\"$ curl http://localhost/backup &gt; my.db\"><pre class=\"notranslate\">$ curl http://localhost/backup <span class=\"pl-k\">&gt;</span> my.db</pre></div>\n<p dir=\"auto\">Or you can open your browser to <code class=\"notranslate\">http://localhost/backup</code> and it will download<br>\nautomatically.</p>\n<p dir=\"auto\">If you want to backup to another file you can use the <code class=\"notranslate\">Tx.CopyFile()</code> helper<br>\nfunction.</p>\n<h3 dir=\"auto\">Statistics</h3>\n<p dir=\"auto\">The database keeps a running count of many of the internal operations it<br>\nperforms so you can better understand what's going on. By grabbing a snapshot<br>\nof these stats at two points in time we can see what operations were performed<br>\nin that time range.</p>\n<p dir=\"auto\">For example, we could start a goroutine to log stats every 10 seconds:</p>\n<div class=\"highlight highlight-source-go notranslate position-relative overflow-auto\" dir=\"auto\" data-snippet-clipboard-copy-content=\"go func() {\n\t// Grab the initial stats.\n\tprev := db.Stats()\n\n\tfor {\n\t\t// Wait for 10s.\n\t\ttime.Sleep(10 * time.Second)\n\n\t\t// Grab the current stats and diff them.\n\t\tstats := db.Stats()\n\t\tdiff := stats.Sub(&amp;prev)\n\n\t\t// Encode stats to JSON and print to STDERR.\n\t\tjson.NewEncoder(os.Stderr).Encode(diff)\n\n\t\t// Save stats for the next loop.\n\t\tprev = stats\n\t}\n}()\"><pre class=\"notranslate\"><span class=\"pl-k\">go</span> <span class=\"pl-k\">func</span>() {\n\t<span class=\"pl-c\">// Grab the initial stats.</span>\n\t<span class=\"pl-s1\">prev</span> <span class=\"pl-c1\">:=</span> <span class=\"pl-s1\">db</span>.<span class=\"pl-c1\">Stats</span>()\n\n\t<span class=\"pl-k\">for</span> {\n\t\t<span class=\"pl-c\">// Wait for 10s.</span>\n\t\t<span class=\"pl-s1\">time</span>.<span class=\"pl-c1\">Sleep</span>(<span class=\"pl-c1\">10</span> <span class=\"pl-c1\">*</span> <span class=\"pl-s1\">time</span>.<span class=\"pl-c1\">Second</span>)\n\n\t\t<span class=\"pl-c\">// Grab the current stats and diff them.</span>\n\t\t<span class=\"pl-s1\">stats</span> <span class=\"pl-c1\">:=</span> <span class=\"pl-s1\">db</span>.<span class=\"pl-c1\">Stats</span>()\n\t\t<span class=\"pl-s1\">diff</span> <span class=\"pl-c1\">:=</span> <span class=\"pl-s1\">stats</span>.<span class=\"pl-c1\">Sub</span>(<span class=\"pl-c1\">&amp;</span><span class=\"pl-s1\">prev</span>)\n\n\t\t<span class=\"pl-c\">// Encode stats to JSON and print to STDERR.</span>\n\t\t<span class=\"pl-s1\">json</span>.<span class=\"pl-c1\">NewEncoder</span>(<span class=\"pl-s1\">os</span>.<span class=\"pl-c1\">Stderr</span>).<span class=\"pl-c1\">Encode</span>(<span class=\"pl-s1\">diff</span>)\n\n\t\t<span class=\"pl-c\">// Save stats for the next loop.</span>\n\t\t<span class=\"pl-s1\">prev</span> <span class=\"pl-c1\">=</span> <span class=\"pl-s1\">stats</span>\n\t}\n}()</pre></div>\n<p dir=\"auto\">It's also useful to pipe these stats to a service such as statsd for monitoring<br>\nor to provide an HTTP endpoint that will perform a fixed-length sample.</p>\n<h3 dir=\"auto\">Read-Only Mode</h3>\n<p dir=\"auto\">Sometimes it is useful to create a shared, read-only Bolt database. To this,<br>\nset the <code class=\"notranslate\">Options.ReadOnly</code> flag when opening your database. Read-only mode<br>\nuses a shared lock to allow multiple processes to read from the database but<br>\nit will block any processes from opening the database in read-write mode.</p>\n<div class=\"highlight highlight-source-go notranslate position-relative overflow-auto\" dir=\"auto\" data-snippet-clipboard-copy-content=\"db, err := bolt.Open(&quot;my.db&quot;, 0600, &amp;bolt.Options{ReadOnly: true})\nif err != nil {\n\tlog.Fatal(err)\n}\"><pre class=\"notranslate\"><span class=\"pl-s1\">db</span>, <span class=\"pl-s1\">err</span> <span class=\"pl-c1\">:=</span> <span class=\"pl-s1\">bolt</span>.<span class=\"pl-c1\">Open</span>(<span class=\"pl-s\">\"my.db\"</span>, <span class=\"pl-c1\">0600</span>, <span class=\"pl-c1\">&amp;</span>bolt.<span class=\"pl-smi\">Options</span>{<span class=\"pl-s1\">ReadOnly</span>: <span class=\"pl-c1\">true</span>})\n<span class=\"pl-k\">if</span> <span class=\"pl-s1\">err</span> <span class=\"pl-c1\">!=</span> <span class=\"pl-c1\">nil</span> {\n\t<span class=\"pl-s1\">log</span>.<span class=\"pl-c1\">Fatal</span>(<span class=\"pl-s1\">err</span>)\n}</pre></div>\n<h3 dir=\"auto\">Mobile Use (iOS/Android)</h3>\n<p dir=\"auto\">Bolt is able to run on mobile devices by leveraging the binding feature of the<br>\n<a href=\"https://github.com/golang/mobile\">gomobile</a> tool. Create a struct that will<br>\ncontain your database logic and a reference to a <code class=\"notranslate\">*bolt.DB</code> with a initializing<br>\nconstructor that takes in a filepath where the database file will be stored.<br>\nNeither Android nor iOS require extra permissions or cleanup from using this method.</p>\n<div class=\"highlight highlight-source-go notranslate position-relative overflow-auto\" dir=\"auto\" data-snippet-clipboard-copy-content=\"func NewBoltDB(filepath string) *BoltDB {\n\tdb, err := bolt.Open(filepath+&quot;/demo.db&quot;, 0600, nil)\n\tif err != nil {\n\t\tlog.Fatal(err)\n\t}\n\n\treturn &amp;BoltDB{db}\n}\n\ntype BoltDB struct {\n\tdb *bolt.DB\n\t...\n}\n\nfunc (b *BoltDB) Path() string {\n\treturn b.db.Path()\n}\n\nfunc (b *BoltDB) Close() {\n\tb.db.Close()\n}\"><pre class=\"notranslate\"><span class=\"pl-k\">func</span> <span class=\"pl-s1\">NewBoltDB</span>(<span class=\"pl-s1\">filepath</span> <span class=\"pl-smi\">string</span>) <span class=\"pl-c1\">*</span><span class=\"pl-smi\">BoltDB</span> {\n\t<span class=\"pl-s1\">db</span>, <span class=\"pl-s1\">err</span> <span class=\"pl-c1\">:=</span> <span class=\"pl-s1\">bolt</span>.<span class=\"pl-c1\">Open</span>(<span class=\"pl-s1\">filepath</span><span class=\"pl-c1\">+</span><span class=\"pl-s\">\"/demo.db\"</span>, <span class=\"pl-c1\">0600</span>, <span class=\"pl-c1\">nil</span>)\n\t<span class=\"pl-k\">if</span> <span class=\"pl-s1\">err</span> <span class=\"pl-c1\">!=</span> <span class=\"pl-c1\">nil</span> {\n\t\t<span class=\"pl-s1\">log</span>.<span class=\"pl-c1\">Fatal</span>(<span class=\"pl-s1\">err</span>)\n\t}\n\n\t<span class=\"pl-k\">return</span> <span class=\"pl-c1\">&amp;</span><span class=\"pl-smi\">BoltDB</span>{<span class=\"pl-s1\">db</span>}\n}\n\n<span class=\"pl-k\">type</span> <span class=\"pl-smi\">BoltDB</span> <span class=\"pl-k\">struct</span> {\n\t<span class=\"pl-c1\">db</span> <span class=\"pl-c1\">*</span>bolt.<span class=\"pl-smi\">DB</span>\n\t<span class=\"pl-c1\">...</span>\n}\n\n<span class=\"pl-k\">func</span> (<span class=\"pl-s1\">b</span> <span class=\"pl-c1\">*</span><span class=\"pl-smi\">BoltDB</span>) <span class=\"pl-c1\">Path</span>() <span class=\"pl-smi\">string</span> {\n\t<span class=\"pl-k\">return</span> <span class=\"pl-s1\">b</span>.<span class=\"pl-c1\">db</span>.<span class=\"pl-c1\">Path</span>()\n}\n\n<span class=\"pl-k\">func</span> (<span class=\"pl-s1\">b</span> <span class=\"pl-c1\">*</span><span class=\"pl-smi\">BoltDB</span>) <span class=\"pl-c1\">Close</span>() {\n\t<span class=\"pl-s1\">b</span>.<span class=\"pl-c1\">db</span>.<span class=\"pl-c1\">Close</span>()\n}</pre></div>\n<p dir=\"auto\">Database logic should be defined as methods on this wrapper struct.</p>\n<p dir=\"auto\">To initialize this struct from the native language (both platforms now sync<br>\ntheir local storage to the cloud. These snippets disable that functionality for the<br>\ndatabase file):</p>\n<h4 dir=\"auto\">Android</h4>\n<div class=\"highlight highlight-source-java notranslate position-relative overflow-auto\" dir=\"auto\" data-snippet-clipboard-copy-content=\"String path;\nif (android.os.Build.VERSION.SDK_INT &gt;=android.os.Build.VERSION_CODES.LOLLIPOP){\n    path = getNoBackupFilesDir().getAbsolutePath();\n} else{\n    path = getFilesDir().getAbsolutePath();\n}\nBoltmobiledemo.BoltDB boltDB = Boltmobiledemo.NewBoltDB(path)\"><pre class=\"notranslate\"><span class=\"pl-smi\">String</span> <span class=\"pl-s1\">path</span>;\n<span class=\"pl-k\">if</span> (<span class=\"pl-s1\">android</span>.<span class=\"pl-s1\">os</span>.<span class=\"pl-s1\">Build</span>.<span class=\"pl-c1\">VERSION</span>.<span class=\"pl-c1\">SDK_INT</span> &gt;=<span class=\"pl-s1\">android</span>.<span class=\"pl-s1\">os</span>.<span class=\"pl-s1\">Build</span>.<span class=\"pl-c1\">VERSION_CODES</span>.<span class=\"pl-c1\">LOLLIPOP</span>){\n    <span class=\"pl-s1\">path</span> = <span class=\"pl-en\">getNoBackupFilesDir</span>().<span class=\"pl-en\">getAbsolutePath</span>();\n} <span class=\"pl-k\">else</span>{\n    <span class=\"pl-s1\">path</span> = <span class=\"pl-en\">getFilesDir</span>().<span class=\"pl-en\">getAbsolutePath</span>();\n}\n<span class=\"pl-smi\">Boltmobiledemo</span>.<span class=\"pl-smi\">BoltDB</span> <span class=\"pl-s1\">boltDB</span> = <span class=\"pl-smi\">Boltmobiledemo</span>.<span class=\"pl-en\">NewBoltDB</span>(<span class=\"pl-s1\">path</span>)</pre></div>\n<h4 dir=\"auto\">iOS</h4>\n<div class=\"highlight highlight-source-objc notranslate position-relative overflow-auto\" dir=\"auto\" data-snippet-clipboard-copy-content=\"- (void)demo {\n    NSString* path = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory,\n                                                          NSUserDomainMask,\n                                                          YES) objectAtIndex:0];\n\tGoBoltmobiledemoBoltDB * demo = GoBoltmobiledemoNewBoltDB(path);\n\t[self addSkipBackupAttributeToItemAtPath:demo.path];\n\t//Some DB Logic would go here\n\t[demo close];\n}\n\n- (BOOL)addSkipBackupAttributeToItemAtPath:(NSString *) filePathString\n{\n    NSURL* URL= [NSURL fileURLWithPath: filePathString];\n    assert([[NSFileManager defaultManager] fileExistsAtPath: [URL path]]);\n\n    NSError *error = nil;\n    BOOL success = [URL setResourceValue: [NSNumber numberWithBool: YES]\n                                  forKey: NSURLIsExcludedFromBackupKey error: &amp;error];\n    if(!success){\n        NSLog(@&quot;Error excluding %@ from backup %@&quot;, [URL lastPathComponent], error);\n    }\n    return success;\n}\n\"><pre class=\"notranslate\">- (<span class=\"pl-k\">void</span>)demo {\n    <span class=\"pl-c1\">NSString</span>* path = [<span class=\"pl-c1\">NSSearchPathForDirectoriesInDomains</span>(NSLibraryDirectory,\n                                                          NSUserDomainMask,\n                                                          <span class=\"pl-c1\">YES</span>) <span class=\"pl-c1\">objectAtIndex:</span><span class=\"pl-c1\">0</span>];\n\tGoBoltmobiledemoBoltDB * demo = <span class=\"pl-c1\">GoBoltmobiledemoNewBoltDB</span>(path);\n\t[<span class=\"pl-c1\">self</span> <span class=\"pl-c1\">addSkipBackupAttributeToItemAtPath:</span>demo.path];\n\t<span class=\"pl-c\"><span class=\"pl-c\">//</span>Some DB Logic would go here</span>\n\t[demo <span class=\"pl-c1\">close</span>];\n}\n\n- (<span class=\"pl-c1\">BOOL</span>)addSkipBackupAttributeToItemAtPath:(<span class=\"pl-c1\">NSString</span> *) filePathString\n{\n    <span class=\"pl-c1\">NSURL</span>* URL= [<span class=\"pl-c1\">NSURL</span> <span class=\"pl-c1\">fileURLWithPath:</span> filePathString];\n    <span class=\"pl-c1\">assert</span>([[<span class=\"pl-c1\">NSFileManager</span> <span class=\"pl-c1\">defaultManager</span>] <span class=\"pl-c1\">fileExistsAtPath:</span> [URL <span class=\"pl-c1\">path</span>]]);\n\n    <span class=\"pl-c1\">NSError</span> *error = <span class=\"pl-c1\">nil</span>;\n    <span class=\"pl-c1\">BOOL</span> success = [URL <span class=\"pl-c1\">setResourceValue:</span> [<span class=\"pl-c1\">NSNumber</span> <span class=\"pl-c1\">numberWithBool:</span> <span class=\"pl-c1\">YES</span>]\n                                  <span class=\"pl-c1\">forKey:</span> <span class=\"pl-c1\">NSURLIsExcludedFromBackupKey</span> <span class=\"pl-c1\">error:</span> &amp;error];\n    <span class=\"pl-k\">if</span>(!success){\n        <span class=\"pl-c1\">NSLog</span>(<span class=\"pl-s\"><span class=\"pl-pds\">@\"</span>Error excluding <span class=\"pl-c1\">%@</span> from backup <span class=\"pl-c1\">%@</span><span class=\"pl-pds\">\"</span></span>, [URL <span class=\"pl-c1\">lastPathComponent</span>], error);\n    }\n    <span class=\"pl-k\">return</span> success;\n}\n</pre></div>\n<h2 dir=\"auto\">Resources</h2>\n<p dir=\"auto\">For more information on getting started with Bolt, check out the following articles:</p>\n<ul dir=\"auto\">\n<li><a href=\"http://npf.io/2014/07/intro-to-boltdb-painless-performant-persistence/\" rel=\"nofollow\">Intro to BoltDB: Painless Performant Persistence</a> by <a href=\"https://github.com/natefinch\">Nate Finch</a>.</li>\n<li><a href=\"https://www.progville.com/go/bolt-embedded-db-golang/\" rel=\"nofollow\">Bolt -- an embedded key/value database for Go</a> by Progville</li>\n</ul>\n<h2 dir=\"auto\">Comparison with other databases</h2>\n<h3 dir=\"auto\">Postgres, MySQL, &amp; other relational databases</h3>\n<p dir=\"auto\">Relational databases structure data into rows and are only accessible through<br>\nthe use of SQL. This approach provides flexibility in how you store and query<br>\nyour data but also incurs overhead in parsing and planning SQL statements. Bolt<br>\naccesses all data by a byte slice key. This makes Bolt fast to read and write<br>\ndata by key but provides no built-in support for joining values together.</p>\n<p dir=\"auto\">Most relational databases (with the exception of SQLite) are standalone servers<br>\nthat run separately from your application. This gives your systems<br>\nflexibility to connect multiple application servers to a single database<br>\nserver but also adds overhead in serializing and transporting data over the<br>\nnetwork. Bolt runs as a library included in your application so all data access<br>\nhas to go through your application's process. This brings data closer to your<br>\napplication but limits multi-process access to the data.</p>\n<h3 dir=\"auto\">LevelDB, RocksDB</h3>\n<p dir=\"auto\">LevelDB and its derivatives (RocksDB, HyperLevelDB) are similar to Bolt in that<br>\nthey are libraries bundled into the application, however, their underlying<br>\nstructure is a log-structured merge-tree (LSM tree). An LSM tree optimizes<br>\nrandom writes by using a write ahead log and multi-tiered, sorted files called<br>\nSSTables. Bolt uses a B+tree internally and only a single file. Both approaches<br>\nhave trade-offs.</p>\n<p dir=\"auto\">If you require a high random write throughput (&gt;10,000 w/sec) or you need to use<br>\nspinning disks then LevelDB could be a good choice. If your application is<br>\nread-heavy or does a lot of range scans then Bolt could be a good choice.</p>\n<p dir=\"auto\">One other important consideration is that LevelDB does not have transactions.<br>\nIt supports batch writing of key/values pairs and it supports read snapshots<br>\nbut it will not give you the ability to do a compare-and-swap operation safely.<br>\nBolt supports fully serializable ACID transactions.</p>\n<h3 dir=\"auto\">LMDB</h3>\n<p dir=\"auto\">Bolt was originally a port of LMDB so it is architecturally similar. Both use<br>\na B+tree, have ACID semantics with fully serializable transactions, and support<br>\nlock-free MVCC using a single writer and multiple readers.</p>\n<p dir=\"auto\">The two projects have somewhat diverged. LMDB heavily focuses on raw performance<br>\nwhile Bolt has focused on simplicity and ease of use. For example, LMDB allows<br>\nseveral unsafe actions such as direct writes for the sake of performance. Bolt<br>\nopts to disallow actions which can leave the database in a corrupted state. The<br>\nonly exception to this in Bolt is <code class=\"notranslate\">DB.NoSync</code>.</p>\n<p dir=\"auto\">There are also a few differences in API. LMDB requires a maximum mmap size when<br>\nopening an <code class=\"notranslate\">mdb_env</code> whereas Bolt will handle incremental mmap resizing<br>\nautomatically. LMDB overloads the getter and setter functions with multiple<br>\nflags whereas Bolt splits these specialized cases into their own functions.</p>\n<h2 dir=\"auto\">Caveats &amp; Limitations</h2>\n<p dir=\"auto\">It's important to pick the right tool for the job and Bolt is no exception.<br>\nHere are a few things to note when evaluating and using Bolt:</p>\n<ul dir=\"auto\">\n<li>\n<p dir=\"auto\">Bolt is good for read intensive workloads. Sequential write performance is<br>\nalso fast but random writes can be slow. You can use <code class=\"notranslate\">DB.Batch()</code> or add a<br>\nwrite-ahead log to help mitigate this issue.</p>\n</li>\n<li>\n<p dir=\"auto\">Bolt uses a B+tree internally so there can be a lot of random page access.<br>\nSSDs provide a significant performance boost over spinning disks.</p>\n</li>\n<li>\n<p dir=\"auto\">Try to avoid long running read transactions. Bolt uses copy-on-write so<br>\nold pages cannot be reclaimed while an old transaction is using them.</p>\n</li>\n<li>\n<p dir=\"auto\">Byte slices returned from Bolt are only valid during a transaction. Once the<br>\ntransaction has been committed or rolled back then the memory they point to<br>\ncan be reused by a new page or can be unmapped from virtual memory and you'll<br>\nsee an <code class=\"notranslate\">unexpected fault address</code> panic when accessing it.</p>\n</li>\n<li>\n<p dir=\"auto\">Bolt uses an exclusive write lock on the database file so it cannot be<br>\nshared by multiple processes.</p>\n</li>\n<li>\n<p dir=\"auto\">Be careful when using <code class=\"notranslate\">Bucket.FillPercent</code>. Setting a high fill percent for<br>\nbuckets that have random inserts will cause your database to have very poor<br>\npage utilization.</p>\n</li>\n<li>\n<p dir=\"auto\">Use larger buckets in general. Smaller buckets causes poor page utilization<br>\nonce they become larger than the page size (typically 4KB).</p>\n</li>\n<li>\n<p dir=\"auto\">Bulk loading a lot of random writes into a new bucket can be slow as the<br>\npage will not split until the transaction is committed. Randomly inserting<br>\nmore than 100,000 key/value pairs into a single new bucket in a single<br>\ntransaction is not advised.</p>\n</li>\n<li>\n<p dir=\"auto\">Bolt uses a memory-mapped file so the underlying operating system handles the<br>\ncaching of the data. Typically, the OS will cache as much of the file as it<br>\ncan in memory and will release memory as needed to other processes. This means<br>\nthat Bolt can show very high memory usage when working with large databases.<br>\nHowever, this is expected and the OS will release memory as needed. Bolt can<br>\nhandle databases much larger than the available physical RAM, provided its<br>\nmemory-map fits in the process virtual address space. It may be problematic<br>\non 32-bits systems.</p>\n</li>\n<li>\n<p dir=\"auto\">The data structures in the Bolt database are memory mapped so the data file<br>\nwill be endian specific. This means that you cannot copy a Bolt file from a<br>\nlittle endian machine to a big endian machine and have it work. For most<br>\nusers this is not a concern since most modern CPUs are little endian.</p>\n</li>\n<li>\n<p dir=\"auto\">Because of the way pages are laid out on disk, Bolt cannot truncate data files<br>\nand return free pages back to the disk. Instead, Bolt maintains a free list<br>\nof unused pages within its data file. These free pages can be reused by later<br>\ntransactions. This works well for many use cases as databases generally tend<br>\nto grow. However, it's important to note that deleting large chunks of data<br>\nwill not allow you to reclaim that space on disk.</p>\n</li>\n<li>\n<p dir=\"auto\">Removing key/values pairs in a bucket during iteration on the bucket using<br>\ncursor may not work properly. Each time when removing a key/value pair, the<br>\ncursor may automatically move to the next position if present. When users<br>\ncall <code class=\"notranslate\">c.Next()</code> after removing a key, it may skip one key/value pair.<br>\nRefer to <a class=\"issue-link js-issue-link\" data-error-text=\"Failed to load title\" data-id=\"1995192020\" data-permission-text=\"Title is private\" data-url=\"https://github.com/etcd-io/bbolt/issues/611\" data-hovercard-type=\"pull_request\" data-hovercard-url=\"/etcd-io/bbolt/pull/611/hovercard\" href=\"https://github.com/etcd-io/bbolt/pull/611\">etcd-io/bbolt#611</a> for more detailed info.</p>\n<p dir=\"auto\">For more information on page allocation, <a href=\"https://github.com/boltdb/bolt/issues/308#issuecomment-74811638\" data-hovercard-type=\"issue\" data-hovercard-url=\"/boltdb/bolt/issues/308/hovercard\">see this comment</a>.</p>\n</li>\n</ul>\n<h2 dir=\"auto\">Reading the Source</h2>\n<p dir=\"auto\">Bolt is a relatively small code base (&lt;5KLOC) for an embedded, serializable,<br>\ntransactional key/value database so it can be a good starting point for people<br>\ninterested in how databases work.</p>\n<p dir=\"auto\">The best places to start are the main entry points into Bolt:</p>\n<ul dir=\"auto\">\n<li>\n<p dir=\"auto\"><code class=\"notranslate\">Open()</code> - Initializes the reference to the database. It's responsible for<br>\ncreating the database if it doesn't exist, obtaining an exclusive lock on the<br>\nfile, reading the meta pages, &amp; memory-mapping the file.</p>\n</li>\n<li>\n<p dir=\"auto\"><code class=\"notranslate\">DB.Begin()</code> - Starts a read-only or read-write transaction depending on the<br>\nvalue of the <code class=\"notranslate\">writable</code> argument. This requires briefly obtaining the \"meta\"<br>\nlock to keep track of open transactions. Only one read-write transaction can<br>\nexist at a time so the \"rwlock\" is acquired during the life of a read-write<br>\ntransaction.</p>\n</li>\n<li>\n<p dir=\"auto\"><code class=\"notranslate\">Bucket.Put()</code> - Writes a key/value pair into a bucket. After validating the<br>\narguments, a cursor is used to traverse the B+tree to the page and position<br>\nwhere the key &amp; value will be written. Once the position is found, the bucket<br>\nmaterializes the underlying page and the page's parent pages into memory as<br>\n\"nodes\". These nodes are where mutations occur during read-write transactions.<br>\nThese changes get flushed to disk during commit.</p>\n</li>\n<li>\n<p dir=\"auto\"><code class=\"notranslate\">Bucket.Get()</code> - Retrieves a key/value pair from a bucket. This uses a cursor<br>\nto move to the page &amp; position of a key/value pair. During a read-only<br>\ntransaction, the key and value data is returned as a direct reference to the<br>\nunderlying mmap file so there's no allocation overhead. For read-write<br>\ntransactions, this data may reference the mmap file or one of the in-memory<br>\nnode values.</p>\n</li>\n<li>\n<p dir=\"auto\"><code class=\"notranslate\">Cursor</code> - This object is simply for traversing the B+tree of on-disk pages<br>\nor in-memory nodes. It can seek to a specific key, move to the first or last<br>\nvalue, or it can move forward or backward. The cursor handles the movement up<br>\nand down the B+tree transparently to the end user.</p>\n</li>\n<li>\n<p dir=\"auto\"><code class=\"notranslate\">Tx.Commit()</code> - Converts the in-memory dirty nodes and the list of free pages<br>\ninto pages to be written to disk. Writing to disk then occurs in two phases.<br>\nFirst, the dirty pages are written to disk and an <code class=\"notranslate\">fsync()</code> occurs. Second, a<br>\nnew meta page with an incremented transaction ID is written and another<br>\n<code class=\"notranslate\">fsync()</code> occurs. This two phase write ensures that partially written data<br>\npages are ignored in the event of a crash since the meta page pointing to them<br>\nis never written. Partially written meta pages are invalidated because they<br>\nare written with a checksum.</p>\n</li>\n</ul>\n<p dir=\"auto\">If you have additional notes that could be helpful for others, please submit<br>\nthem via pull request.</p>\n<h2 dir=\"auto\">Known Issues</h2>\n<ul dir=\"auto\">\n<li>\n<p dir=\"auto\">bbolt might run into data corruption issue on Linux when the feature<br>\n<a href=\"https://lwn.net/Articles/842385/\" rel=\"nofollow\">ext4: fast commit</a>, which was introduced in<br>\nlinux kernel version v5.10, is enabled. The fixes to the issue were included in<br>\nlinux kernel version v5.17, please refer to links below,</p>\n<ul dir=\"auto\">\n<li><a href=\"https://lore.kernel.org/linux-ext4/20211223032337.5198-3-yinxin.x@bytedance.com/\" rel=\"nofollow\">ext4: fast commit may miss tracking unwritten range during ftruncate</a></li>\n<li><a href=\"https://lore.kernel.org/lkml/202201091544.W5HHEXAp-lkp@intel.com/T/#ma0768815e4b5f671e9e451d578256ef9a76fe30e\" rel=\"nofollow\">ext4: fast commit may not fallback for ineligible commit</a></li>\n<li><a href=\"https://lore.kernel.org/lkml/YdyxjTFaLWif6BCM@mit.edu/\" rel=\"nofollow\">ext4 updates for 5.17</a></li>\n</ul>\n<p dir=\"auto\">Please also refer to the discussion in <a class=\"issue-link js-issue-link\" data-error-text=\"Failed to load title\" data-id=\"1883513047\" data-permission-text=\"Title is private\" data-url=\"https://github.com/etcd-io/bbolt/issues/562\" data-hovercard-type=\"issue\" data-hovercard-url=\"/etcd-io/bbolt/issues/562/hovercard\" href=\"https://github.com/etcd-io/bbolt/issues/562\">etcd-io/bbolt#562</a>.</p>\n</li>\n<li>\n<p dir=\"auto\">Writing a value with a length of 0 will always result in reading back an empty <code class=\"notranslate\">[]byte{}</code> value.<br>\nPlease refer to <a href=\"https://github.com/etcd-io/bbolt/issues/726#issuecomment-2061694802\" data-hovercard-type=\"issue\" data-hovercard-url=\"/etcd-io/bbolt/issues/726/hovercard\">issues/726#issuecomment-2061694802</a>.</p>\n</li>\n</ul>\n<h2 dir=\"auto\">Other Projects Using Bolt</h2>\n<p dir=\"auto\">Below is a list of public, open source projects that use Bolt:</p>\n<ul dir=\"auto\">\n<li><a href=\"https://github.com/xyproto/algernon\">Algernon</a> - A HTTP/2 web server with built-in support for Lua. Uses BoltDB as the default database backend.</li>\n<li><a href=\"https://bazil.org/\" rel=\"nofollow\">Bazil</a> - A file system that lets your data reside where it is most convenient for it to reside.</li>\n<li><a href=\"https://github.com/hasit/bolter\">bolter</a> - Command-line app for viewing BoltDB file in your terminal.</li>\n<li><a href=\"https://github.com/spacewander/boltcli\">boltcli</a> - the redis-cli for boltdb with Lua script support.</li>\n<li><a href=\"https://github.com/timshannon/bolthold\">BoltHold</a> - An embeddable NoSQL store for Go types built on BoltDB</li>\n<li><a href=\"https://github.com/yosssi/boltstore\">BoltStore</a> - Session store using Bolt.</li>\n<li><a href=\"https://github.com/bobintornado/boltdb-boilerplate\">Boltdb Boilerplate</a> - Boilerplate wrapper around bolt aiming to make simple calls one-liners.</li>\n<li><a href=\"https://github.com/evnix/boltdbweb\">BoltDbWeb</a> - A web based GUI for BoltDB files.</li>\n<li><a href=\"https://github.com/zc310/rich_boltdb\">BoltDB Viewer</a> - A BoltDB Viewer Can run on Windows、Linux、Android system.</li>\n<li><a href=\"http://www.blevesearch.com/\" rel=\"nofollow\">bleve</a> - A pure Go search engine similar to ElasticSearch that uses Bolt as the default storage backend.</li>\n<li><a href=\"https://github.com/mjl-/bstore\">bstore</a> - Database library storing Go values, with referential/unique/nonzero constraints, indices, automatic schema management with struct tags, and a query API.</li>\n<li><a href=\"https://github.com/btcsuite/btcwallet\">btcwallet</a> - A bitcoin wallet.</li>\n<li><a href=\"https://github.com/joyrexus/buckets\">buckets</a> - a bolt wrapper streamlining<br>\nsimple tx and key scans.</li>\n<li><a href=\"https://github.com/moby/buildkit\">Buildkit</a> - concurrent, cache-efficient, and Dockerfile-agnostic builder toolkit</li>\n<li><a href=\"https://github.com/google/cayley\">cayley</a> - Cayley is an open-source graph database using Bolt as optional backend.</li>\n<li><a href=\"https://github.com/pressly/chainstore\">ChainStore</a> - Simple key-value interface to a variety of storage engines organized as a chain of operations.</li>\n<li><a href=\"https://github.com/jrapoport/chestnut\">🌰 Chestnut</a> - Chestnut is encrypted storage for Go.</li>\n<li><a href=\"https://github.com/hashicorp/consul\">Consul</a> - Consul is service discovery and configuration made easy. Distributed, highly available, and datacenter-aware.</li>\n<li><a href=\"https://github.com/containerd/containerd\">Containerd</a> - An open and reliable container runtime</li>\n<li><a href=\"https://github.com/janelia-flyem/dvid\">DVID</a> - Added Bolt as optional storage engine and testing it against Basho-tuned leveldb.</li>\n<li><a href=\"https://github.com/decred/dcrwallet\">dcrwallet</a> - A wallet for the Decred cryptocurrency.</li>\n<li><a href=\"https://github.com/odeke-em/drive\">drive</a> - drive is an unofficial Google Drive command line client for *NIX operating systems.</li>\n<li><a href=\"https://github.com/sclasen/event-shuttle\">event-shuttle</a> - A Unix system service to collect and reliably deliver messages to Kafka.</li>\n<li><a href=\"http://tshannon.bitbucket.org/freehold/\" rel=\"nofollow\">Freehold</a> - An open, secure, and lightweight platform for your files and data.</li>\n<li><a href=\"https://goreportcard.com/\" rel=\"nofollow\">Go Report Card</a> - Go code quality report cards as a (free and open source) service.</li>\n<li><a href=\"https://github.com/josephspurrier/gowebapp\">GoWebApp</a> - A basic MVC web application in Go using BoltDB.</li>\n<li><a href=\"https://github.com/pankajkhairnar/goShort\">GoShort</a> - GoShort is a URL shortener written in Golang and BoltDB for persistent key/value storage and for routing it's using high performent HTTPRouter.</li>\n<li><a href=\"https://github.com/gopherpit/gopherpit\">gopherpit</a> - A web service to manage Go remote import paths with custom domains</li>\n<li><a href=\"https://github.com/philippgille/gokv\">gokv</a> - Simple key-value store abstraction and implementations for Go (Redis, Consul, etcd, bbolt, BadgerDB, LevelDB, Memcached, DynamoDB, S3, PostgreSQL, MongoDB, CockroachDB and many more)</li>\n<li><a href=\"https://github.com/gitchain/gitchain\">Gitchain</a> - Decentralized, peer-to-peer Git repositories aka \"Git meets Bitcoin\".</li>\n<li><a href=\"https://influxdata.com\" rel=\"nofollow\">InfluxDB</a> - Scalable datastore for metrics, events, and real-time analytics.</li>\n<li><a href=\"https://github.com/AndreasBriese/ipLocator\">ipLocator</a> - A fast ip-geo-location-server using bolt with bloom filters.</li>\n<li><a href=\"https://github.com/kelseyhightower/ipxed\">ipxed</a> - Web interface and api for ipxed.</li>\n<li><a href=\"https://github.com/timshannon/ironsmith\">Ironsmith</a> - A simple, script-driven continuous integration (build - &gt; test -&gt; release) tool, with no external dependencies</li>\n<li><a href=\"https://github.com/ajvb/kala\">Kala</a> - Kala is a modern job scheduler optimized to run on a single node. It is persistent, JSON over HTTP API, ISO 8601 duration notation, and dependent jobs.</li>\n<li><a href=\"https://github.com/kval-access-language\">Key Value Access Language (KVAL)</a> - A proposed grammar for key-value datastores offering a bbolt binding.</li>\n<li><a href=\"https://github.com/siddontang/ledisdb\">LedisDB</a> - A high performance NoSQL, using Bolt as optional storage.</li>\n<li><a href=\"https://github.com/crowdriff/lru\">lru</a> - Easy to use Bolt-backed Least-Recently-Used (LRU) read-through cache with chainable remote stores.</li>\n<li><a href=\"https://github.com/abhigupta912/mbuckets\">mbuckets</a> - A Bolt wrapper that allows easy operations on multi level (nested) buckets.</li>\n<li><a href=\"https://github.com/msiebuhr/MetricBase\">MetricBase</a> - Single-binary version of Graphite.</li>\n<li><a href=\"https://github.com/dankomiocevic/mulifs\">MuLiFS</a> - Music Library Filesystem creates a filesystem to organise your music files.</li>\n<li><a href=\"https://github.com/nats-io/nats-streaming-server\">NATS</a> - NATS Streaming uses bbolt for message and metadata storage.</li>\n<li><a href=\"https://github.com/portainer/portainer\">Portainer</a> - A lightweight service delivery platform for containerized applications that can be used to manage Docker, Swarm, Kubernetes and ACI environments.</li>\n<li><a href=\"https://github.com/oliver006/prom_annotation_server\">Prometheus Annotation Server</a> - Annotation server for PromDash &amp; Prometheus service monitoring system.</li>\n<li><a href=\"https://github.com/cenkalti/rain\">Rain</a> - BitTorrent client and library.</li>\n<li><a href=\"https://github.com/reef-pi/reef-pi\">reef-pi</a> - reef-pi is an award winning, modular, DIY reef tank controller using easy to learn electronics based on a Raspberry Pi.</li>\n<li><a href=\"https://github.com/darklynx/request-baskets\">Request Baskets</a> - A web service to collect arbitrary HTTP requests and inspect them via REST API or simple web UI, similar to <a href=\"http://requestb.in/\" rel=\"nofollow\">RequestBin</a> service</li>\n<li><a href=\"https://github.com/chrislusf/seaweedfs\">Seaweed File System</a> - Highly scalable distributed key~file system with O(1) disk read.</li>\n<li><a href=\"https://github.com/djherbis/stow\">stow</a> -  a persistence manager for objects<br>\nbacked by boltdb.</li>\n<li><a href=\"https://github.com/asdine/storm\">Storm</a> - Simple and powerful ORM for BoltDB.</li>\n<li><a href=\"https://github.com/xyproto/simplebolt\">SimpleBolt</a> - A simple way to use BoltDB. Deals mainly with strings.</li>\n<li><a href=\"https://github.com/skybox/skybox\">Skybox Analytics</a> - A standalone funnel analysis tool for web analytics.</li>\n<li><a href=\"https://github.com/benbjohnson/scuttlebutt\">Scuttlebutt</a> - Uses Bolt to store and process all Twitter mentions of GitHub projects.</li>\n<li><a href=\"https://github.com/optiflows/tentacool\">tentacool</a> - REST api server to manage system stuff (IP, DNS, Gateway...) on a linux server.</li>\n<li><a href=\"https://github.com/anacrolix/torrent\">torrent</a> - Full-featured BitTorrent client package and utilities in Go. BoltDB is a storage backend in development.</li>\n<li><a href=\"https://github.com/peterhellberg/wiki\">Wiki</a> - A tiny wiki using Goji, BoltDB and Blackfriday.</li>\n</ul>\n<p dir=\"auto\">If you are using Bolt in a project please send a pull request to add it to the list.</p>","updatedAt":"2025-05-21T15:49:57Z","upvoteCount":null,"author":{"login":"eryajf","avatarUrl":"https://avatars.githubusercontent.com/u/33259379?u=e4a4090a38ac2473aaed4ef9945233636776c6c3&v=4","url":"https://github.com/eryajf"},"category":null,"labels":{"edges":[{"node":{"name":"更多","color":"25B472"}},{"node":{"name":"Database","color":"3fbc9d"}},{"node":{"name":"etcd-io","color":"5a1fed"}}]},"comments":{"edges":[]}}},"pageContext":{"number":46,"previous":{"title":"sharkdp/bat: 类似 cat 命令，但带有 git 集成和语法高亮.","number":45},"next":{"title":"elastic/beats: Elasticsearch 和 Logstash 的轻量级传输器","number":47}}},
    "staticQueryHashes": ["151096407","2861350382"]}