编程与软件工程cstack.github.io编辑精选
让我们构建一个简单的数据库
Let’s Build a Simple Database (2017)
2017 — 2021,它一次次回到首页
黑点是上首页(≥ 50 分)的那次投稿,空心点是没冲上去的投稿,点越大分数越高。
导读
这是一个分步教程系列:用 C 语言从零实现一个简化版 SQLite 克隆。它从 REPL 循环、命令解析开始,逐步引入内存中的行存储、B-tree 磁盘存储、插入与简单查询等环节,每一步都配有可运行的代码和测试。
为什么经久不衰:它把看似高深的数据库内部机制拆成能亲手敲出来的小步骤,十多年来一直是理解 B-tree、存储与解析的经典入门路径。
HN 评论精选
推荐 Sciore 的书,认为它是少有的给出可上手实例的数据库实现教材。
我花了不少时间用 Java 写数据库,这绝不是小工程。我翻遍了能找的书和论文,只有一本真正给出了构建 SQL 处理系统的概览和实例,其余要么纯理论、要么太泛,让人无从下手——那就是 Sciore 的《Database Design and Implementation》,有点过时但不算严重,是被低估的资源。
ccleve查看英文原文
I have spent quite a lot of time building a database in Java. It's not a small undertaking. I've gone through as many books and papers as I can, and there is only one, one, that gives an overview and actual examples of building a system that processes SQL. Everything else is theoretical or so general that you don't get enough direction to get started. It's "Database Design and Implementation" by Sciore. It's a little dated, but not by much. It is an underappreciated resource.
补充两篇经典综述性资料:Anatomy of a Database System 和 Redbook。
对这个话题感兴趣的人可以看看 Stonebraker 等人的《Anatomy of a Database System》,我认为是该主题最简洁的论述;另外可以看 Redbook 了解数据库研究的最新想法集合。
ignoramous查看英文原文
Folks interested in this sort of thing might like Anatomy of a Database System by Stonebreaker et al, which I think is the most concise treatment on the subject: http://db.cs.berkeley.edu/papers/fntdb07-architecture.pdf Also, see Redbook for the latest ensemble of research ideas in databases. http://www.redbook.io
建议通过 SQLite 虚拟表和 VFS 层做实践练习。
另一个好玩的练习是折腾 SQLite 的虚拟表(virtual table)和 VFS 层,文中给了官方文档链接和一个用 VFS 添加实用功能的小例子。
tyingq查看英文原文
Another exercise that's fun is playing with Sqlite virtual tables and/or the Sqlite VFS layer. Virtual tables: https://www.sqlite.org/draft/vtab.html A simple example of using VFS to add a useful feature: https://github.com/Metalnem/sqlite-vfsdemo
现身说法:受约束的专用场景下自研事务数据库可行且有价值,鼓励大家动手写。
写数据库是值得推荐给每个人的练习。通用数据库很难,但如果清楚预期负载和性能、一致性要求,就会简单很多。我十年前为商用项目写过自己的事务数据库:设备内存预算最多 1MB(代码、栈、堆和存储都算在内),存储在不带磨损均衡的闪存上,需要精确控制哪些字节被擦写、多久擦一次;要存几十到几百字节、大小不一的小对象,还要能同时事务性地修改多个对象(比如两个账户),无论应用出错还是断电都必须保持一致。它在固定内存中运行,允许应用注册钩子来计算记录两个版本的差异并应用增量,数据库不关心数据如何编码。它把数据存成事务日志,写入都追加到日志末尾……
lmilcin查看英文原文
Writing a database is a good exercise I could recommend to everybody. Writing a general purpose database is very difficult. On the other hand, if you know what kind of load you can expect and when you have an idea of performance and consistency requirements it may become much simpler. I have rolled out my own transactional database for a commercial project. This was a decade ago, the device had at most 1MB of memory budget for compiled code, stack, heap, and database storage. The database was to be stored on a non-wear-leveled flash requiring precise control of which bytes and how frequently were erased. We required ability to store small objects (tens to few hundreds of bytes at most) of varying size and be able to record changes to multiple objects at the same time, transactionally (think modifying two accounts). The requirement was that the database stayed consistent no matter what happened, regardless of any application error or power cycles. These devices were operated by hundreds of thousands of customers and power cycled liberally whenever anything didn't work. The database run in constant memory. It allowed the application to register hooks to calculate deltas between two versions of records and to apply delta to base version to produce result version. The database didn't care how the data was encoded, this was application detail. The database was stored as transaction log. During operation, all writes were directed to the end of the log. When the log reached half of the available memory, all live records would be coalesced and written to the other half of the space and the original space would be erased. The read algorithmic complexity was horrible with some algorithms being cubic(!) complexity. This was absolutely ok, since there could never be enough records that would make it a problem. Having extremely simple algorithms allowed for the entire code to be very simple and compact. Everything fit about 200 lines or so lines of code. I really enjoyed the project.
历次讨论
同类还值得读
开源应用架构
The Architecture of Open Source Applications
《开源软件架构》(AOSA)是一本在线免费书,邀请知名开源项目的作者亲自剖析其内部设计,涵盖 SQLAlchemy、LLVM、Audacity、Apache、Bash 等系统。
编程与软件工程上首页 9 次 · 2011–2024aosabook.org编辑精选编程语言简史:简短、不完整且多半错误
A Brief, Incomplete, and Mostly Wrong History of Programming Languages
一篇以戏谑、故意不准确的方式回顾编程语言史的博客文章,用夸张轶事和讽刺定义串起 Lisp、C、C++、Java、JavaScript、PHP 等语言。
编程与软件工程上首页 8 次 · 2009–2021james-iry.blogspot.comSQLite 是如何测试的
How SQLite is tested
SQLite 官方文档页,系统介绍 SQLite 的测试体系:多种测试方法(TCL 测试、模糊测试、回归测试、静态分析等)和海量测试用例,展示了这个小型开源项目如何靠极致测试获得…
编程与软件工程上首页 8 次 · 2009–2025sqlite.org编辑精选高级编程语言(2009)
Advanced programming languages (2009)
Matt Might 的经典文章,按用途推荐“高级编程语言”:如果追求就业就选主流语言,如果追求表达力、学术研究或创业,则推荐 Haskell、OCaml、Scala 等函数式语…
编程与软件工程上首页 8 次 · 2009–2022matt.might.net我的 Forth 与栈机器经历
Yosefk - My history with Forth & stack machines
作者以亲身经历回顾了使用 Forth 语言和栈式机器的历程:既赞叹其简洁优雅、代码量小、可自举的优点,也坦率指出栈操作在实际编程中的痛苦,以及 Forth 社区文化中的问题。
编程与软件工程上首页 8 次 · 2010–2024yosefk.com编辑精选