{"id":13603893,"url":"https://github.com/ZhengHe-MD/learn-bolt","last_synced_at":"2025-04-11T22:31:56.550Z","repository":{"id":39922046,"uuid":"196485212","full_name":"ZhengHe-MD/learn-bolt","owner":"ZhengHe-MD","description":"阅读 boltDB 源码后的小结","archived":false,"fork":false,"pushed_at":"2022-10-09T00:41:42.000Z","size":1357,"stargazers_count":254,"open_issues_count":0,"forks_count":43,"subscribers_count":7,"default_branch":"master","last_synced_at":"2024-11-07T08:41:43.055Z","etag":null,"topics":["boltdb"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"gpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ZhengHe-MD.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-07-12T01:08:35.000Z","updated_at":"2024-11-01T07:22:03.000Z","dependencies_parsed_at":"2022-08-09T15:36:31.617Z","dependency_job_id":null,"html_url":"https://github.com/ZhengHe-MD/learn-bolt","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ZhengHe-MD%2Flearn-bolt","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ZhengHe-MD%2Flearn-bolt/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ZhengHe-MD%2Flearn-bolt/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ZhengHe-MD%2Flearn-bolt/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ZhengHe-MD","download_url":"https://codeload.github.com/ZhengHe-MD/learn-bolt/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248489736,"owners_count":21112629,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["boltdb"],"created_at":"2024-08-01T19:00:35.898Z","updated_at":"2025-04-11T22:31:55.793Z","avatar_url":"https://github.com/ZhengHe-MD.png","language":"Go","funding_links":[],"categories":["Go"],"sub_categories":[],"readme":"# learn-boltdb\n\n在最近的闲暇时间 (2019 年初)，我开始弥补自己数据库知识的盲点。这里也推荐一下自己最喜欢的课程和书籍：\n\n* CMU 15-445/645 Intro to Database Systems\n* Designing Data-Intensive Applications (DDIA)\n\n尽管站在巨人的肩膀上，可以体会到数据库的博大精深，但这些努力始终只是在外部远观，并未真正进入其中。\n\n\u003e What I cannot create, I do not understand. --- Richard Feynman\n\n我一直将 Richard Feynman 的这句话记在心中，时刻提醒自己作为软件工程师，不能只满足于看懂，还要能做出来。在动手写一个数据库之前，我想先认真阅读某个数据库的源码，将那些常被提及的概念与实现关联。\n\n在 DDIA 中，Martin Kleppmann 曾多次提及 Bolt，印象颇深的一点是它通过单线程执行读写事务的方式简单粗暴地实现最苛刻的可序列化事务隔离级别。后来的某一天，我心血来潮地读起 Bolt 项目的源码，发现它极简的设计理念正好满足我的学习需求。在阅读过程中，为梳理源码中的概念和逻辑，我开始将自己的理解转化成短文，逐渐形成了本项目。目前，Bolt 项目已经归档，不再更新，这也保证了本文内容不会过时。\n\n## 为什么选择 Bolt\n\n\u003e The original goal of Bolt was to provide a simple pure Go key/value store and to not bloat the code with extraneous features.  --- Ben Johnson\n\nBolt 可能是最适合 Go 语言工程师阅读的第一个数据库项目，原因在于它功能简单：\n\n* 单机部署：没有像 Raft、Paxos 这样的共识协议\n* 无服务端：没有通信协议，直接读写数据库文件\n* 存储键值：没有关系代数，没有数据字典，只有键和值\n* 无跨语言：仅支持 Go SDK 访问，没有 DSL\n\n实现也简单：\n\n* 数据结构：所有数据存储在同一个树形结构中\n* 索引结构：键值数据天然地只有主键索引，使用经典的 B+ 树\n* 事务隔离：仅允许多个只读事务和最多一个读写事务同时运行\n* 缓存管理：仅管理写缓存，利用 mmap 管理读缓存\n\n## 阅读建议\n\n每篇短文覆盖一个话题，描述对应模块的实现。本系列文章将自底向上地介绍 Bolt，各个模块相对独立，顺序阅读和单篇阅读皆可。\n\n| 主题                              | 源码                             |\n| ------------------------------- | --------------------------------- |\n| [存储与缓存](./STORAGE_AND_CACHE.md) | page.go, freelist.go, db.go    |\n| [数据与索引](./DATA_AND_INDEX.md)    | node.go                        |\n| [桶](./BUCKET.md)                   | bucket.go, cursor.go           |\n| [事务](./TX.md)                     | tx.go                          |\n\n## 名词解释\n\n### 中英对照\n\n为避免专业术语的名称歧义，除已经有稳定中文翻译的词语外，其它词语将保留原始英文形式。下面是一份核心专业词语的中英对照表，供读者参考：\n\n| 中文           | 英文                                   |\n| ------------ | ------------------------------------ |\n| B+ 树         | B+Tree                               |\n| 事务           | transaction/tx                       |\n| 读写事务         | read-write transaction/tx            |\n| 只读事务         | read-only transaction/tx             |\n| 隐式事务         | managed/implicit transaction/tx      |\n| 显式事务         | explicit transaction/tx              |\n| 提交           | commit                               |\n| 回滚           | rollback                             |\n| 桶            | bucket                               |\n| 游标           | cursor                               |\n| 键/值/键值对/键值数据 | key/value/key-value(kv) pair/kv data |\n| 分裂           | split                                |\n| 合并           | merge                                |\n\n### Page\n\n块存储中的数据通常会被分割为等长的数据块，作为读写的最小单元。教科书称之为 frame 或 page frame，这是**物理概念**。当这些数据块被读入内存，在程序中被引用、读写时，教科书称之为 page，那是**逻辑概念**。在 Bolt 中，二者都被称为 page，如:\n\n* 常数 pageSize 中的 page 是物理概念\n* page.go 中提到的 page 多是逻辑概念\n\n在阅读源码的过程中要注意区分。\n\n### Bolt 与 Bolt 实例\n\nBolt 指这个[项目](https://github.com/boltdb/bolt)本身，而 Bolt 实例指利用 api 创建的一个新的数据库，也指文件系统中存储对应数据的二进制文件。\n\n## 参考\n\n* [Github: bolt/boltdb](https://github.com/boltdb/bolt)\n* [CMU 15-445/645 Intro to Database Systems](https://www.youtube.com/playlist?list=PLSE8ODhjZXja3hgmuwhf89qboV1kOxMx7)\n* [Designing Data-Intensive Applications](https://dataintensive.net/)\n\n转载请注明出处为本项目: https://github.com/ZhengHe-MD/learn-bolt\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FZhengHe-MD%2Flearn-bolt","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FZhengHe-MD%2Flearn-bolt","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FZhengHe-MD%2Flearn-bolt/lists"}