mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-12-05 19:25:00 +03:00
SQLite is now the only clock writer for document updates. Each row takes the event time or 1 ms after the document clock, whichever is later. The session no longer keeps its own copy of the clock and gives SQLite the event time of each update. Each committed row reports its slot: the document clock before the write and the row time. A row that the server holds moves pushed_clock when the row took the slot right after the current pushed_clock. A row from the server holds this proof when SQLite writes it. A local row holds it when the server acknowledged its push and every earlier push of the document. The session keeps the slots of the other committed rows until the server holds them. The flush checks only peer_clocks and clocks in its write transaction, and it reads no row data. Thus a remote update on a pushed document that the session never opened moves pushed_clock in the same transaction. The next start no longer loads and pushes that document again. A row of another writer between two own rows stops pushed_clock. When the server holds the whole in-memory state of that document, a content check runs after the commit in one deferred transaction. The same transaction reads the rows and writes pushed_clock. If another connection writes first, SQLite returns SQLITE_BUSY_SNAPSHOT, the transaction rolls back, and the next flush tries again. A SQLite-backed session drops the staged broadcasts of the oldest unknown document above the 64 document or 1 MiB limit. It now keeps the ID of that document in a queue of up to 1024 IDs. The next flush looks up these IDs in SQLite in one query. A document that another writer added becomes a disk document that the session loads and reconciles with the server, so the dropped changes come back. The flush drops the other IDs. On a page with 10,000 blocks, the flush after 20 acknowledged edits takes 7.5 ms instead of 432 ms. The flush after 50 remote updates takes 12.3 ms instead of 1,024 ms.