Skip to content

Commit 817d0df

Browse files
committed
Some documentation
1 parent 02a219c commit 817d0df

3 files changed

Lines changed: 40 additions & 0 deletions

File tree

packages/web/src/db/adapters/memory/client.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,29 @@ function createWriteAheadLogBuffers(): WriteAheadBuffers {
1313
}
1414

1515
interface InMemoryOptions {
16+
/**
17+
* The amount of workers to use, must at least be `1`.
18+
*/
1619
numWorkers: number;
1720
}
1821

22+
/**
23+
* An in-memory database backed by a pool of workers.
24+
*
25+
* Multiple workers can execute readonly transactions in parallel, and a single writer is allowed to write to the
26+
* database in parallel to readers. To allow multiple workers to access the same database, this is based on shared array
27+
* buffers and requires the page to be [cross-origin isolated](https://web.dev/articles/cross-origin-isolation-guide?hl=en).
28+
*
29+
* This database uses a concept known as a write-ahead log: Instead of writing changes directly to the database (which
30+
* would cause conflicts with readers), the writer appends modified database pages into an append-only overlay log. When
31+
* readers start a transaction, they consider the main database and items from the log until a specific position,
32+
* meaning that future appends won't discrupt existing readers.
33+
*
34+
* To avoid the write-ahead log from growing indefinitely, it is regularly copied back into the main database file. This
35+
* process is called a checkpoint, and in this implementation it blocks all other database access. However, it only
36+
* needs to copy between memory and is usually fairly fast. This implementation checkpoints after the write-ahead log
37+
* has grown to 512 KiB or larger. We might want to fine-tune that later.
38+
*/
1939
export class InMemoryWriteAheadLogPool extends DBAdapter {
2040
readonly name: string = `in-memory-${crypto.randomUUID()}`;
2141

packages/web/src/db/adapters/memory/shared.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,33 @@ export interface WalIndexChange {
2626
fileSize: number;
2727
walEnd: number;
2828

29+
/**
30+
* New overlay entries to add, encoded as (key, value) pairs of {@link WriteAheadState.overlay}.
31+
*/
2932
added: (number | WalOverlayEntry)[];
33+
34+
/**
35+
* Whether to clear the overlay (meaning that we ran a checkpoint).
36+
*/
3037
cleared: boolean;
3138
}
3239

3340
export interface DatabaseServer {
41+
/**
42+
* Open a database connection from the given database and write-ahead log buffers.
43+
*/
3444
open(buffers: WriteAheadBuffers): Promise<void>;
45+
46+
/**
47+
* Push new wal changes to this worker.
48+
*/
3549
updateWalState(overlay: WalIndexChange): Promise<void>;
3650

3751
executeRaw(query: string, params?: any[] | undefined): Promise<RawQueryResult>;
52+
53+
/**
54+
* Takes WAL changes made by this worker during its active write iteration.
55+
*/
3856
takeWalChanges(): Promise<WalIndexChange>;
3957
}
4058

packages/web/src/db/adapters/memory/vfs.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import { emptyWalState, WalIndexChange, WalOverlayEntry, WriteAheadBuffers, Writ
66
const mainDbSentinel = Symbol();
77

88
export class InMemoryWriteAheadLog extends FacadeVFS {
9+
// The main /database file is the only file for which we create a write-ahead log. All other files (including the
10+
// journal) are local-only.
911
#openedFiles = new Map<number, LocalFile | typeof mainDbSentinel>();
1012
#files = new Map<string, LocalFile | typeof mainDbSentinel>();
1113

0 commit comments

Comments
 (0)