返回资讯中心
外部精选
软件工程
#开发者实时流

Birth and death of a row in Turso's MVCC

There are three parts to the code: the preamble, the main code, and the end. In the preamble we usually see two things: Init, which jumps to the preamble code, and Transaction, which most of the time is the only thing t…

Hacker News 最新argoeris2026/10/20 · 26 天前6 分钟阅读

以下正文同步自 Hacker News 最新,版权归原站所有,已转换为易读排版。

There are three parts to the code: the preamble, the main code, and the end. In the preamble we usually see two things: Init, which jumps to the preamble code, and Transaction, which most of the time is the only thing there. Transaction is the instruction responsible for starting the transaction. We will see more about it once we add MVCC to the picture.

The main code opens “cursors” with OpenWrite, loads the data of the row into registers with String8 and NewRowid, packs it into a record with MakeRecord, and writes the row itself to the backing storage with Insert. A cursor points to a single table, and it works like an iterator.

Finally, we call Halt. Halt takes care of either committing or rolling back the transaction.

If you like Robert C. Martin, you might prefer reading it as:

let tx = Transaction(); let cursor = tx.OpenWrite(2); let record = MakeRecord(NewRowid(), 'deadbeef'); cursor.insert(record); tx.commit();

Now that we know what the bytecode looks like, let’s dive deeper into Insert.

Insert writes a row into the backing storage. In this case, that is the MVCC in-memory store, which is not durable yet. Rows are stored in a SkipMap. Skip maps are quite relevant here, because they are a lock-free data structure with the same interface as a BTreeMap: an ordered map. It looks something like SkipMap<RowId, RwLock<Vec<RowVersion>>>, where a row id maps to a chain of versions of that row, and each chain has its own lock.

A row version is simply a struct that holds three important pieces of information: the begin timestamp, the end timestamp, and the payload. Timestamps are monotonically increasing numbers that a logical clock hands out to transactions, to control which versions come before or after others. While a transaction is active and hasn’t committed yet, we store a TxId instead of a timestamp. Once it has committed, we store the timestamp it acquired at commit time.

Going back to the Insert instruction: when it is executed, we create a new RowVersion:

RowVersion { begin: TxId(tx.id), end: None, row: record }

A begin with a TxId means we have added the row to the MVCC store, but other transactions cannot read it yet. An end of None means this version hasn’t been deleted yet.

正文由 探How 从来源站点 RSS 同步,内容未经改写;遇到排版缺失或需要图片、视频时请以原文为准。

打开原文
去社区讨论 →