@kedem/okdb 3.0.0 → 3.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +114 -3
- package/README.md +72 -36
- package/bin/okdb +1 -1
- package/docs/auth-and-sync.md +30 -24
- package/docs/auth-permissions.md +5 -4
- package/docs/c-bridge.md +94 -0
- package/docs/change-log.md +79 -36
- package/docs/data-model.md +35 -9
- package/docs/deployment.md +42 -38
- package/docs/diagnostics.md +61 -125
- package/docs/embeddings.md +143 -51
- package/docs/encryption.md +132 -0
- package/docs/files.md +64 -27
- package/docs/fts.md +48 -23
- package/docs/functions.md +109 -56
- package/docs/getting-started.md +121 -9
- package/docs/http-api.md +66 -11
- package/docs/http-cluster.md +80 -88
- package/docs/index.md +33 -15
- package/docs/indexes.md +53 -17
- package/docs/licensing.md +19 -5
- package/docs/logging.md +11 -22
- package/docs/manifest.json +6 -3
- package/docs/migration.md +41 -16
- package/docs/pipelines.md +111 -46
- package/docs/plugins.md +47 -15
- package/docs/process-registry.md +23 -24
- package/docs/processors.md +26 -23
- package/docs/querying.md +102 -29
- package/docs/queue.md +165 -51
- package/docs/resolved-fields.md +24 -1
- package/docs/subscriptions.md +4 -4
- package/docs/sync.md +9 -6
- package/docs/time-machine.md +52 -14
- package/docs/transactions.md +63 -15
- package/docs/ttl.md +34 -13
- package/docs/upgrade-2.0.md +32 -15
- package/docs/upgrade-2.1.md +10 -12
- package/docs/upgrade-3.0.md +144 -0
- package/docs/views.md +165 -61
- package/index.js +1 -1
- package/native/linux-x64-gnu/okdb +0 -0
- package/native/linux-x64-gnu/okdb-node.linux-x64-gnu.node +0 -0
- package/native-loader.js +1 -1
- package/okdb-native.js +1 -1
- package/package.json +1 -1
- package/public/features/files/parts/files-blob-status.ok.js +1 -1
- package/types/environment.d.ts +32 -11
- package/types/errors.d.ts +20 -0
- package/types/features/admin.d.ts +3 -5
- package/types/features/api.d.ts +5 -7
- package/types/features/embeddings.d.ts +8 -7
- package/types/features/engines.d.ts +9 -1
- package/types/features/files.d.ts +2 -1
- package/types/features/fts.d.ts +3 -3
- package/types/features/functions.d.ts +14 -8
- package/types/features/pipelines.d.ts +10 -10
- package/types/features/queue.d.ts +15 -10
- package/types/features/sync.d.ts +7 -0
- package/types/features/time-machine.d.ts +3 -0
- package/types/index.d.ts +3 -0
- package/types/okdb.d.ts +70 -7
- package/types/options.d.ts +12 -8
- package/docs/OKDB-ARCHITECTURE.md +0 -1061
package/docs/http-cluster.md
CHANGED
|
@@ -1,21 +1,20 @@
|
|
|
1
1
|
# HTTP Clustering
|
|
2
2
|
|
|
3
3
|
By default, the `okdb` CLI runs across **N identical capable worker processes** that share
|
|
4
|
-
the listen
|
|
5
|
-
privileged processor** — the 1-of-N
|
|
4
|
+
the listen port: each one serves HTTP + SSE **and** participates in processing. There is **no
|
|
5
|
+
privileged processor** — the 1-of-N processor lease distributes claims across the workers
|
|
6
6
|
(cooperative hold-window rotation load-shares under sustained backlog), and each worker drains
|
|
7
|
-
its claims
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
`bin/okdb`). See [Embedded clustering](#embedded-clustering).
|
|
7
|
+
its claims in bounded quanta, so a heavy drain on one worker never blocks the others. A dead
|
|
8
|
+
worker's claims fail over to the survivors. The supervisor is a thin **passive** process (it
|
|
9
|
+
launches/relaunches the workers and seeds the shared token secret; it does not serve or process).
|
|
10
|
+
|
|
11
|
+
This is a **CLI feature** (the `okdb` command). The embedded library
|
|
12
|
+
(`new OKDB(...).http.listen(port)`) always serves from the calling process —
|
|
13
|
+
`http.listen(port, { workers })` throws `HTTP_CLUSTER_REMOVED`. For multi-process embedding, run
|
|
14
|
+
**N independent full nodes** (each its own `new OKDB(path).http.listen(port)`) on the same data
|
|
15
|
+
path behind your own load balancer, or use Node's `cluster` module: they coordinate through the
|
|
16
|
+
shared LMDB files + bus + the 1-of-N lease exactly like the CLI's workers. See
|
|
17
|
+
[Embedded clustering](#embedded-clustering).
|
|
19
18
|
|
|
20
19
|
---
|
|
21
20
|
|
|
@@ -25,34 +24,36 @@ workers (this is the same "N capable nodes" shape, just supervised by your balan
|
|
|
25
24
|
┌──────────────────────────────────────────────┐
|
|
26
25
|
│ supervisor (PASSIVE — processors:false, │
|
|
27
26
|
│ engines:false, compaction:false) │
|
|
28
|
-
│ •
|
|
27
|
+
│ • binds one SO_REUSEPORT socket per worker │
|
|
28
|
+
│ • launches/relaunches/decommissions workers │
|
|
29
29
|
│ • owns the ~processes registry (publishes a │
|
|
30
30
|
│ row per worker; workers are registry-passive)│
|
|
31
|
-
│ • opens its
|
|
32
|
-
│ shared __tokenSecret so workers agree)
|
|
31
|
+
│ • opens its instance BEFORE launching (seeds │
|
|
32
|
+
│ the shared __tokenSecret so workers agree) │
|
|
33
33
|
│ • no HTTP server; does NOT process │
|
|
34
34
|
└───────────────┬──────────────────────────────┘
|
|
35
|
-
│
|
|
35
|
+
│ launches N (same okdb binary)
|
|
36
36
|
┌───────────────┬───────┴───────┬───────────────┐
|
|
37
37
|
▼ ▼ ▼ ▼
|
|
38
38
|
┌─────────┐ ┌─────────┐ ┌─────────┐ ┌─────────┐
|
|
39
39
|
│ worker │ │ worker │ ... │ worker │ │ worker │ CAPABLE role:
|
|
40
40
|
│HTTP+proc│ │HTTP+proc│ │HTTP+proc│ │HTTP+proc│ processors:true
|
|
41
41
|
└─────────┘ └─────────┘ └─────────┘ └─────────┘ (serve + process +
|
|
42
|
-
|
|
42
|
+
└──────────── one port, kernel-balanced ────┴───────────┘ compact)
|
|
43
43
|
```
|
|
44
44
|
|
|
45
|
-
- **Workers = N identical capable nodes.**
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
45
|
+
- **Workers = N identical capable nodes.** The supervisor re-executes the same native `okdb`
|
|
46
|
+
launcher once per worker (same arguments, plus a worker marker in the environment), handing it
|
|
47
|
+
its listening socket and an IPC channel. Each worker opens a **full-role** instance
|
|
48
|
+
(`processors:true`) and both serves HTTP + SSE **and** processes — it claims the 1-of-N
|
|
49
|
+
processor lease, builds/indexes FTS + views, and drains its claims in bounded quanta. Claims **distribute** across the workers (no
|
|
49
50
|
privileged processor); a dead worker's leases lapse and the survivors re-claim. Workers are
|
|
50
|
-
**registry-passive** (the supervisor publishes their `~processes` rows;
|
|
51
|
-
reports its processing facet to the supervisor over IPC so cluster processing stays
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
the
|
|
51
|
+
**registry-passive** (the supervisor publishes their `~processes` rows; each worker
|
|
52
|
+
reports its stats and processing facet to the supervisor over IPC so cluster processing stays
|
|
53
|
+
visible).
|
|
54
|
+
- **Supervisor = passive.** It opens a passive instance (`processors:false`, `engines:false`,
|
|
55
|
+
`compaction:false`) only to seed the shared `__tokenSecret` before launching workers and to own
|
|
56
|
+
the registry. It runs no HTTP server, never processes, and is **not** in the connection path.
|
|
56
57
|
- **`workers:1`** (or `--no-cluster`) is byte-identical to the single-process path: one full
|
|
57
58
|
capable process serves + processes.
|
|
58
59
|
|
|
@@ -61,24 +62,12 @@ shared changelog); bounded quanta keep that indexing from monopolizing the holde
|
|
|
61
62
|
|
|
62
63
|
### Connection distribution
|
|
63
64
|
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
- Workers are **not** isolated from the supervisor for **new** connections: if the supervisor's
|
|
71
|
-
loop is blocked, every new connection waits until it frees up (measured: a 1.5 s busy-loop in
|
|
72
|
-
the supervisor delayed a new connection by ~1.4 s with 4 idle workers). The supervisor is kept
|
|
73
|
-
thin for this reason — it only forks/respawns workers, relays IPC, and publishes registry rows.
|
|
74
|
-
- Distribution is even (strict rotation).
|
|
75
|
-
|
|
76
|
-
Operators who prefer the OS to distribute connections can set Node's own
|
|
77
|
-
`NODE_CLUSTER_SCHED_POLICY=none` (`SCHED_NONE`) in the `okdb` process environment: each worker
|
|
78
|
-
`accept()`s directly, so a busy supervisor no longer delays new connections — at the cost of
|
|
79
|
-
uneven distribution (measured on Linux with 4 idle workers: 42–209 of 400 serial connections per
|
|
80
|
-
worker; under load the kernel favours whichever workers are waiting in `accept()`, which can
|
|
81
|
-
skew further). Windows always uses `SCHED_NONE`.
|
|
65
|
+
The supervisor binds one listening socket per worker slot on the same address and port, all with
|
|
66
|
+
`SO_REUSEPORT`, and each worker inherits its own. The **kernel** distributes new connections
|
|
67
|
+
across those sockets (by connection hash), and each worker `accept()`s on its own socket — so a
|
|
68
|
+
busy supervisor never delays a new connection, and established connections go straight to their
|
|
69
|
+
worker. A slot's socket is kept across a worker relaunch. Distribution is even on average, not
|
|
70
|
+
strict rotation.
|
|
82
71
|
|
|
83
72
|
---
|
|
84
73
|
|
|
@@ -86,22 +75,22 @@ skew further). Windows always uses `SCHED_NONE`.
|
|
|
86
75
|
|
|
87
76
|
The CLI resolves the worker count from the first source present, highest priority first:
|
|
88
77
|
|
|
89
|
-
| Priority | Source | Notes
|
|
90
|
-
| -------- | ------------------- |
|
|
91
|
-
| 1 | `--no-cluster` | Forces `workers:1` (single-process escape).
|
|
92
|
-
| 2 | `--workers N` | Explicit flag.
|
|
93
|
-
| 3 | `OKDB_HTTP_WORKERS` | Environment variable.
|
|
94
|
-
| 4 | `http.workers` | `.kdbconfig` config file.
|
|
95
|
-
| 5 | _default_ | `max(1, cores − 1)`
|
|
78
|
+
| Priority | Source | Notes |
|
|
79
|
+
| -------- | ------------------- | ------------------------------------------- |
|
|
80
|
+
| 1 | `--no-cluster` | Forces `workers:1` (single-process escape). |
|
|
81
|
+
| 2 | `--workers N` | Explicit flag. |
|
|
82
|
+
| 3 | `OKDB_HTTP_WORKERS` | Environment variable. |
|
|
83
|
+
| 4 | `http.workers` | `.kdbconfig` config file. |
|
|
84
|
+
| 5 | _default_ | `max(1, cores − 1)` (available CPUs). |
|
|
96
85
|
|
|
97
|
-
A non-integer or `< 1` explicit value
|
|
86
|
+
A non-integer or `< 1` explicit value falls back to `1` with a warning. The default leaves one core
|
|
98
87
|
for the OS / the thin passive supervisor and runs N capable workers on the rest (each serves HTTP
|
|
99
88
|
_and_ processes); on a single-core box it collapses to one process.
|
|
100
89
|
|
|
101
90
|
```bash
|
|
102
91
|
okdb # default: max(1, cores − 1) HTTP workers
|
|
103
92
|
okdb --workers 4 # 4 workers
|
|
104
|
-
okdb --no-cluster # single process
|
|
93
|
+
okdb --no-cluster # single process
|
|
105
94
|
OKDB_HTTP_WORKERS=3 okdb # 3 workers
|
|
106
95
|
```
|
|
107
96
|
|
|
@@ -126,7 +115,7 @@ registry's `listenAddr` shows it. There is no `--host` flag.
|
|
|
126
115
|
The config file also takes the constructor role flags as top-level booleans — the same names and
|
|
127
116
|
semantics as `new OKDB(path, { processors, engines, compaction })` (see
|
|
128
117
|
[Roles & Deployment](./deployment.md)). They apply to the single-process node and to **every**
|
|
129
|
-
|
|
118
|
+
cluster worker; absent keys keep the constructor default (`true`), and a non-boolean value is a
|
|
130
119
|
startup error. (The supervisor is always passive, whatever the config says.) There is no CLI
|
|
131
120
|
flag for them.
|
|
132
121
|
|
|
@@ -137,22 +126,17 @@ flag for them.
|
|
|
137
126
|
|
|
138
127
|
---
|
|
139
128
|
|
|
140
|
-
##
|
|
129
|
+
## Cross-process coordination
|
|
141
130
|
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
clock value and corrupt causal ordering.
|
|
147
|
-
|
|
148
|
-
Therefore, **if `shmbuf` is in fallback mode at startup, the CLI forces `workers:1`** regardless
|
|
149
|
-
of any flag, env var, or config, and logs a warning. This gate is non-negotiable.
|
|
131
|
+
Workers share the per-env Hybrid Logical Clock and changelog clock counter through a
|
|
132
|
+
cross-process shared-memory segment built into okdb's native core (nothing extra to install), so
|
|
133
|
+
every worker mints unique, causally ordered clocks. The 2.x "shmbuf native binding missing →
|
|
134
|
+
forced single-process" fallback does not apply in 3.0.
|
|
150
135
|
|
|
151
136
|
Check readiness with `okdb doctor`:
|
|
152
137
|
|
|
153
|
-
```
|
|
154
|
-
✓ cluster: ready (7 workers)
|
|
155
|
-
✓ cluster: unavailable (shmbuf native missing) — running single-process # fallback
|
|
138
|
+
```text
|
|
139
|
+
✓ cluster: ready (7 workers)
|
|
156
140
|
```
|
|
157
141
|
|
|
158
142
|
---
|
|
@@ -163,7 +147,7 @@ The cluster exposes two distinct event domains. Knowing which is which explains
|
|
|
163
147
|
client on a given worker will and won't see:
|
|
164
148
|
|
|
165
149
|
- **Data changes are cluster-wide.** Each worker runs a **change-feed**: it subscribes to the
|
|
166
|
-
|
|
150
|
+
bus `SYSTEM_POKE` signal and, per poke, reads the shared changelog to
|
|
167
151
|
reconstruct data-change events (item put/remove, index, view, type). A write committed on
|
|
168
152
|
worker A surfaces in worker B's SSE within the poke window. The bus carries the **signal**;
|
|
169
153
|
the **data** is read from shared LMDB — the changelog is the single source of truth.
|
|
@@ -171,12 +155,12 @@ client on a given worker will and won't see:
|
|
|
171
155
|
no privileged processor — each capable worker processes its own claimed leases and emits its own
|
|
172
156
|
ephemeral processing events (view-rebuild progress, FTS/index lifecycle, processor lifecycle) to
|
|
173
157
|
**its own** SSE. The cluster-wide picture is **read** from the durable `~processes` registry:
|
|
174
|
-
each worker reports its processing facet to the supervisor over **
|
|
158
|
+
each worker reports its processing facet to the supervisor over **IPC**, the supervisor
|
|
175
159
|
stamps it onto the worker's registry row, and any node serves `/api/processors/status` (and
|
|
176
160
|
its deprecated alias `/api/processing/status`) and `/api/cluster/status` from that shared registry. So a processing
|
|
177
161
|
event on worker A is not mirrored into worker B's live SSE, but A's processor **state** is
|
|
178
162
|
visible cluster-wide via the registry-sourced status routes.
|
|
179
|
-
- **Worker logs are forwarded to the supervisor's log pipeline** over
|
|
163
|
+
- **Worker logs are forwarded to the supervisor's log pipeline** over IPC (tagged `[wN]` per
|
|
180
164
|
worker) so the operator log is unified; they are **not** mirrored into other workers' SSE
|
|
181
165
|
streams (SSE log frames are per-process). Only data changes (via the changelog) are coherent
|
|
182
166
|
across every worker's SSE.
|
|
@@ -187,18 +171,22 @@ client on a given worker will and won't see:
|
|
|
187
171
|
|
|
188
172
|
## Lifecycle
|
|
189
173
|
|
|
190
|
-
- **Worker death →
|
|
191
|
-
|
|
192
|
-
-
|
|
193
|
-
|
|
194
|
-
- **
|
|
174
|
+
- **Worker death → the supervisor relaunches** it with bounded backoff
|
|
175
|
+
(`min(4000, 250·2^(failures−1))` ms; the base is `OKDB_CLUSTER_RESPAWN_BACKOFF_MS`) and a
|
|
176
|
+
crash-loop guard (≥ 5 deaths / 10 s per slot stops relaunching that slot; the others keep
|
|
177
|
+
serving). A worker that reports `listening` resets its slot's failure count.
|
|
178
|
+
- **Supervisor death → workers exit** (no orphan listeners or locks): the IPC channel closes, with
|
|
179
|
+
a `ppid == 1` backstop.
|
|
180
|
+
- **Shutdown** (SIGINT/SIGTERM): each worker stops accepting, drains in-flight requests, then
|
|
181
|
+
closes its instance; a worker still running 3.5 s later is sent SIGTERM.
|
|
195
182
|
|
|
196
183
|
---
|
|
197
184
|
|
|
198
185
|
## Embedded clustering
|
|
199
186
|
|
|
200
|
-
The **library default is single-process
|
|
201
|
-
|
|
187
|
+
The **library default is single-process**. `new OKDB(...).http.listen(port)` serves from the
|
|
188
|
+
calling process and returns an `http.Server`-compatible handle (`address()`, `close()`, the
|
|
189
|
+
`'listening'` event); the server itself runs inside okdb's native core.
|
|
202
190
|
|
|
203
191
|
> **Removed in 2.0:** the embedded `http.listen(port, { workers, makeOkdb, primaryOkdb })` API
|
|
204
192
|
> (it **throws `HTTP_CLUSTER_REMOVED`**). A library node never forks — HTTP clustering is a
|
|
@@ -209,13 +197,18 @@ Two ways to cluster an embedded app — both are the same **"N identical capable
|
|
|
209
197
|
|
|
210
198
|
1. **N independent nodes behind your balancer** (simplest). Run your entry as N separate processes,
|
|
211
199
|
each a full `new OKDB(path).http.listen(port_i)`, and put a load balancer in front. They
|
|
212
|
-
coordinate through the shared LMDB + bus + the lease — no
|
|
213
|
-
2. **Plain Node `cluster`** (one listen port
|
|
214
|
-
|
|
215
|
-
|
|
200
|
+
coordinate through the shared LMDB files + bus + the lease — no supervisor needed.
|
|
201
|
+
2. **Plain Node `cluster`** (one listen port). Fork your entry; each worker opens a **capable**
|
|
202
|
+
instance and calls `http.listen(port)`. Inside a Node `cluster` worker, okdb binds its own
|
|
203
|
+
`SO_REUSEPORT` socket on that port (the native server cannot take over Node's shared handle),
|
|
204
|
+
so the kernel balances connections across the workers.
|
|
205
|
+
|
|
206
|
+
<!-- not-runnable: starts worker processes and a long-running server -->
|
|
216
207
|
|
|
217
208
|
```js
|
|
218
209
|
const cluster = require('node:cluster');
|
|
210
|
+
const dbPath = './data';
|
|
211
|
+
const port = 8080;
|
|
219
212
|
|
|
220
213
|
if (cluster.isPrimary) {
|
|
221
214
|
// Thin supervisor: open a passive node BEFORE forking to seed the shared __tokenSecret,
|
|
@@ -225,13 +218,12 @@ if (cluster.isPrimary) {
|
|
|
225
218
|
for (let i = 0; i < 4; i++) cluster.fork();
|
|
226
219
|
} else {
|
|
227
220
|
// Each worker = an identical CAPABLE node: serves HTTP AND processes (claims leases,
|
|
228
|
-
// drains them
|
|
221
|
+
// drains them in bounded quanta). The lease distributes processing across them.
|
|
229
222
|
const w = new OKDB(dbPath, { processors: true });
|
|
230
223
|
await w.open();
|
|
231
|
-
w.http.listen(port); //
|
|
224
|
+
w.http.listen(port); // its own SO_REUSEPORT socket on the shared port
|
|
232
225
|
}
|
|
233
226
|
```
|
|
234
227
|
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
this for you.
|
|
228
|
+
For a turnkey cluster (supervisor, relaunch, registry rows, log forwarding), the `okdb` CLI does
|
|
229
|
+
all of this for you.
|
package/docs/index.md
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
**An embedded, document-centric database for Node.js.** No separate server. No network hop. Just open a path and write data.
|
|
4
4
|
|
|
5
|
-
OKDB
|
|
5
|
+
OKDB is built on [LMDB](https://www.symas.com/lmdb) — one of the fastest key-value stores ever built — and adds typed collections, composite secondary indexes, full-text search, vector embeddings, multi-node sync, a durable job queue, and binary blob storage. All in one process, all ACID.
|
|
6
6
|
|
|
7
7
|
---
|
|
8
8
|
|
|
@@ -10,13 +10,29 @@ OKDB wraps [LMDB](https://www.symas.com/lmdb) — one of the fastest key-value s
|
|
|
10
10
|
|
|
11
11
|
OKDB is a **structured layer on top of LMDB**. The design decision is simple: LMDB gives you memory-mapped, copy-on-write B+ trees with MVCC and crash-safe writes. OKDB adds everything a real application needs on top of that without changing the embedding model.
|
|
12
12
|
|
|
13
|
-
Think of it as a document store where reads are synchronous (
|
|
13
|
+
Think of it as a document store where reads are synchronous (served in-process from memory-mapped files), writes are atomic, and the derived structures — indexes, changelogs — are maintained inside the same transaction, while heavier derived work (FTS postings, vector graphs, views) is drained incrementally by background processors.
|
|
14
|
+
|
|
15
|
+
### okdb 3: a native core
|
|
16
|
+
|
|
17
|
+
okdb 3 keeps the JavaScript API of okdb 2.x and runs it on a native core written in Rust:
|
|
18
|
+
|
|
19
|
+
- **One native file.** `@kedem/okdb` ships the core as a Node addon inside the package (`native/linux-x64-gnu/`). There is no
|
|
20
|
+
JavaScript fallback; the package supports **Linux x64 with glibc 2.28+** (other platforms: `NATIVE_UNAVAILABLE`, see
|
|
21
|
+
[Getting Started](./getting-started.md#installation)).
|
|
22
|
+
- **Same API, same files.** Stores written by okdb 2.x open as-is (no migration, no index rebuild). What changed is listed in
|
|
23
|
+
[Upgrading to 3.0](./upgrade-3.0.md).
|
|
24
|
+
- **One thread per instance.** Each `OKDB` instance runs on the Node event loop: synchronous calls go straight to the core,
|
|
25
|
+
background work (processor drains, timers, lease heartbeats) runs in short slices between your callbacks.
|
|
26
|
+
- **Scale-out = more processes on the same path.** Processes coordinate through LMDB and shared memory (leases, the change log,
|
|
27
|
+
a local UDP bus for wake-ups); see [Roles & Deployment](./deployment.md).
|
|
28
|
+
- **The `okdb` CLI** is a small native launcher that runs inside the same core, and the same native file serves programs written
|
|
29
|
+
in other languages through a stable C interface ([C bridge](./c-bridge.md)).
|
|
14
30
|
|
|
15
31
|
---
|
|
16
32
|
|
|
17
33
|
## Strengths
|
|
18
34
|
|
|
19
|
-
- :zap: **Reads are synchronous** — no
|
|
35
|
+
- :zap: **Reads are synchronous** — no `await`, no round trips
|
|
20
36
|
- :lock: **ACID transactions** — inherited from LMDB; no partial state ever observable
|
|
21
37
|
- :package: **Self-contained** — everything in one process, one set of directories
|
|
22
38
|
- :refresh-cw: **Multi-node sync** — LWW replication over HTTP, no coordinator
|
|
@@ -29,9 +45,9 @@ Think of it as a document store where reads are synchronous (just a pointer dere
|
|
|
29
45
|
## What it is not
|
|
30
46
|
|
|
31
47
|
- Not a client/server database (no PostgreSQL-style network protocol)
|
|
32
|
-
- Not designed for browser or Bun (Node.js only)
|
|
48
|
+
- Not designed for browser or Bun (Node.js on Linux x64 only; other languages through the [C bridge](./c-bridge.md))
|
|
33
49
|
- Not an audit-log — the change-log is LWW-deduplicated per key, not an immutable history
|
|
34
|
-
- Not horizontally scalable in the traditional sense — LMDB
|
|
50
|
+
- Not horizontally scalable in the traditional sense — LMDB allows one write transaction at a time per environment
|
|
35
51
|
|
|
36
52
|
---
|
|
37
53
|
|
|
@@ -40,7 +56,7 @@ Think of it as a document store where reads are synchronous (just a pointer dere
|
|
|
40
56
|
| Concept | What it is |
|
|
41
57
|
| --------------- | -------------------------------------------------------------- |
|
|
42
58
|
| **Type** | A named collection of records (like a table/collection) |
|
|
43
|
-
| **Key** | String primary key for each record
|
|
59
|
+
| **Key** | String (or number) primary key for each record |
|
|
44
60
|
| **Value** | Any JSON-serialisable object |
|
|
45
61
|
| **Environment** | An isolated LMDB database directory |
|
|
46
62
|
| **Index** | A composite secondary index on one or more fields |
|
|
@@ -58,14 +74,14 @@ Your app code
|
|
|
58
74
|
▼
|
|
59
75
|
OKDB (orchestrator)
|
|
60
76
|
│
|
|
61
|
-
├── ~system env ← node identity, env registry
|
|
62
|
-
├──
|
|
63
|
-
├──
|
|
64
|
-
|
|
65
|
-
└── custom envs ← whatever you create
|
|
77
|
+
├── ~system env ← node identity, env registry, tokens, licenses
|
|
78
|
+
├── ~log env ← persisted log entries
|
|
79
|
+
├── default env ← your data, indexes, change log, queue jobs, files
|
|
80
|
+
└── custom envs ← whatever you create (each with its own queue, views, functions, ...)
|
|
66
81
|
```
|
|
67
82
|
|
|
68
|
-
All environments are LMDB directories on disk. Each has its own clock, changelog, and HLC-based sync cursor.
|
|
83
|
+
All environments are LMDB directories on disk. Each has its own clock, changelog, and HLC-based sync cursor. Feature stores
|
|
84
|
+
(full-text postings, vectors, time-machine history) live in their own LMDB files under the store root.
|
|
69
85
|
|
|
70
86
|
---
|
|
71
87
|
|
|
@@ -87,7 +103,7 @@ await okdb.put('notes', 'n2', { author: 'bob', body: 'Hey there', createdAt: Dat
|
|
|
87
103
|
const note = okdb.get('notes', 'n1');
|
|
88
104
|
|
|
89
105
|
// Index scan
|
|
90
|
-
for (const { key, value } of okdb.byIndex('notes', ['author'], { prefix: ['alice'] })) {
|
|
106
|
+
for (const { key, value } of okdb.byIndex('notes', ['author', 'createdAt'], { prefix: ['alice'] })) {
|
|
91
107
|
console.log(key, value);
|
|
92
108
|
}
|
|
93
109
|
|
|
@@ -100,6 +116,7 @@ await okdb.close();
|
|
|
100
116
|
|
|
101
117
|
- [Getting Started](./getting-started.md) — install, open, first writes
|
|
102
118
|
- [Roles & Deployment](./deployment.md) — role flags, single-process vs multi-process topologies, deferred processing
|
|
119
|
+
- [Upgrading to 3.0](./upgrade-3.0.md) — moving from okdb 2.x: the native package, platforms, coded errors, plugins
|
|
103
120
|
- [Data Model](./data-model.md) — types, keys, environments, storage layout
|
|
104
121
|
- [Querying](./querying.md) — reads, ranges, MongoDB-style filters
|
|
105
122
|
- [Indexes](./indexes.md) — secondary, composite, unique, geo
|
|
@@ -126,5 +143,6 @@ await okdb.close();
|
|
|
126
143
|
- [Diagnostics](./diagnostics.md) — opt-in triage tooling (write-origin ring, fatal report, loop lag)
|
|
127
144
|
- [Plugins](./plugins.md) — extending OKDB before open
|
|
128
145
|
- [Migration](./migration.md) — export, import, backup, blob integrity
|
|
129
|
-
- [
|
|
130
|
-
- [Upgrading to 2.
|
|
146
|
+
- [C bridge](./c-bridge.md) — the native core's C interface for non-Node hosts
|
|
147
|
+
- [Upgrading to 2.0](./upgrade-2.0.md) (history) — the node-model runtime redesign: constructor changes, workers dissolved, claimed responsibilities
|
|
148
|
+
- [Upgrading to 2.1](./upgrade-2.1.md) (history) — the safe-range contract: range iterators are synchronous, `READER_HELD_ACROSS_AWAIT`, the env-balloon fix
|
package/docs/indexes.md
CHANGED
|
@@ -7,6 +7,10 @@ Secondary indexes let you scan records by field values without touching every re
|
|
|
7
7
|
## Registering an index
|
|
8
8
|
|
|
9
9
|
```javascript
|
|
10
|
+
await okdb.registerType('articles');
|
|
11
|
+
await okdb.registerType('users');
|
|
12
|
+
await okdb.registerType('places');
|
|
13
|
+
|
|
10
14
|
// Single field
|
|
11
15
|
await okdb.registerIndex('articles', ['author']);
|
|
12
16
|
|
|
@@ -44,12 +48,16 @@ That means the correct field path is usually `location`, not `location.lat` + `l
|
|
|
44
48
|
|
|
45
49
|
Indexes build **asynchronously** from existing data after registration. Writes during the build are tracked and the index converges to READY. To wait for completion:
|
|
46
50
|
|
|
51
|
+
<!-- continues -->
|
|
52
|
+
|
|
47
53
|
```javascript
|
|
48
54
|
await okdb.indexReady('articles', ['author', 'publishedAt']);
|
|
49
55
|
```
|
|
50
56
|
|
|
51
57
|
Check status without waiting:
|
|
52
58
|
|
|
59
|
+
<!-- continues -->
|
|
60
|
+
|
|
53
61
|
```javascript
|
|
54
62
|
okdb.getIndexStatus('articles', ['author']);
|
|
55
63
|
// → 'creating' | 'resetting' | 'waiting' | 'ready' | 'dropping' | 'error'
|
|
@@ -60,11 +68,13 @@ okdb.getIndexStatus('articles', ['author']);
|
|
|
60
68
|
|
|
61
69
|
## Dropping an index
|
|
62
70
|
|
|
71
|
+
<!-- continues -->
|
|
72
|
+
|
|
63
73
|
```javascript
|
|
64
74
|
await okdb.dropIndex('articles', ['author', 'publishedAt']);
|
|
65
75
|
```
|
|
66
76
|
|
|
67
|
-
The index sub-database is deleted. Existing query code that references it will throw `OKDBIndexNotRegisteredError
|
|
77
|
+
The index sub-database is deleted. Existing query code that references it will throw an error with `code: 'INDEX_NOT_REGISTERED'` (`name: 'OKDBIndexNotRegisteredError'`).
|
|
68
78
|
|
|
69
79
|
---
|
|
70
80
|
|
|
@@ -87,17 +97,21 @@ You rarely need to deal with index names directly — all API methods accept `st
|
|
|
87
97
|
A composite index orders records by the first field, then the second within ties, and so on. This mirrors SQL's multi-column index semantics.
|
|
88
98
|
|
|
89
99
|
```javascript
|
|
100
|
+
await okdb.registerType('orders');
|
|
90
101
|
await okdb.registerIndex('orders', ['customerId', 'createdAt']);
|
|
102
|
+
await okdb.put('orders', 'o1', { customerId: 'c42', createdAt: Date.now() });
|
|
91
103
|
|
|
92
104
|
// All orders by customer 'c42', newest first
|
|
93
105
|
for (const { key, value } of okdb.byIndex('orders', ['customerId', 'createdAt'], {
|
|
94
|
-
prefix:
|
|
106
|
+
prefix: ['c42'],
|
|
95
107
|
reverse: true,
|
|
96
|
-
limit:
|
|
97
|
-
})) {
|
|
108
|
+
limit: 50,
|
|
109
|
+
})) {
|
|
110
|
+
console.log(key, value.createdAt);
|
|
111
|
+
}
|
|
98
112
|
```
|
|
99
113
|
|
|
100
|
-
|
|
114
|
+
A prefix scan on `['c42']` returns all entries for that customer regardless of their `createdAt` value.
|
|
101
115
|
|
|
102
116
|
---
|
|
103
117
|
|
|
@@ -106,6 +120,7 @@ The trailing `null` sentinel in the LMDB key format ensures that a prefix scan o
|
|
|
106
120
|
`countByIndex` counts matching index entries without touching the data store. It takes `start`/`end` (end exclusive — hence the `HIGH_SENTINEL` bound below) or, equivalently, `prefix`:
|
|
107
121
|
|
|
108
122
|
```javascript
|
|
123
|
+
await okdb.registerType('orders');
|
|
109
124
|
await okdb.registerIndex('orders', ['status']);
|
|
110
125
|
|
|
111
126
|
const pending = okdb.countByIndex('orders', ['status'], {
|
|
@@ -119,6 +134,9 @@ const open = okdb.countByIndex('orders', ['status', 'createdAt'], {
|
|
|
119
134
|
start: ['open'],
|
|
120
135
|
end: ['open', OKDB.HIGH_SENTINEL],
|
|
121
136
|
});
|
|
137
|
+
|
|
138
|
+
// Same count, prefix form
|
|
139
|
+
const open2 = okdb.countByIndex('orders', ['status', 'createdAt'], { prefix: ['open'] });
|
|
122
140
|
```
|
|
123
141
|
|
|
124
142
|
---
|
|
@@ -128,10 +146,14 @@ const open = okdb.countByIndex('orders', ['status', 'createdAt'], {
|
|
|
128
146
|
Field paths can use dots to reach nested properties:
|
|
129
147
|
|
|
130
148
|
```javascript
|
|
149
|
+
await okdb.registerType('users');
|
|
131
150
|
await okdb.registerIndex('users', ['address.city']);
|
|
151
|
+
await okdb.put('users', 'u1', { name: 'Ada', address: { city: 'Paris' } });
|
|
132
152
|
|
|
133
153
|
// Records where address.city === 'Paris'
|
|
134
|
-
for (const { key, value } of okdb.byIndex('users', ['address.city'], { prefix: ['Paris'] })) {
|
|
154
|
+
for (const { key, value } of okdb.byIndex('users', ['address.city'], { prefix: ['Paris'] })) {
|
|
155
|
+
console.log(key, value.name);
|
|
156
|
+
}
|
|
135
157
|
```
|
|
136
158
|
|
|
137
159
|
---
|
|
@@ -141,10 +163,15 @@ for (const { key, value } of okdb.byIndex('users', ['address.city'], { prefix: [
|
|
|
141
163
|
A unique index prevents two records from having the same value for the indexed fields.
|
|
142
164
|
|
|
143
165
|
```javascript
|
|
166
|
+
await okdb.registerType('users');
|
|
144
167
|
await okdb.registerIndex('users', ['email'], { unique: true });
|
|
168
|
+
await okdb.put('users', 'user-1', { email: 'alice@example.com' });
|
|
145
169
|
|
|
146
|
-
|
|
147
|
-
await okdb.put('users', 'user-2', { email: 'alice@example.com' });
|
|
170
|
+
try {
|
|
171
|
+
await okdb.put('users', 'user-2', { email: 'alice@example.com' });
|
|
172
|
+
} catch (err) {
|
|
173
|
+
console.log(err.code); // 'UNIQUE_CONSTRAINT' (name: 'OKDBUniqueConstraintError')
|
|
174
|
+
}
|
|
148
175
|
```
|
|
149
176
|
|
|
150
177
|
### Sync conflicts
|
|
@@ -155,14 +182,16 @@ When a duplicate arrives via sync (from another node), OKDB doesn't reject it
|
|
|
155
182
|
|
|
156
183
|
Violations are stored locally (not synced) and resolve automatically when the losing record is removed or updated.
|
|
157
184
|
|
|
185
|
+
<!-- continues -->
|
|
186
|
+
|
|
158
187
|
```javascript
|
|
159
|
-
// List all violations for a type
|
|
188
|
+
// List all violations for a type (optionally for one index: getUniqueViolations('users', ['email']))
|
|
160
189
|
const violations = okdb.getUniqueViolations('users');
|
|
161
190
|
|
|
162
191
|
// Check if any violations exist for a specific index
|
|
163
192
|
const hasProb = okdb.hasUniqueViolations('users', ['email']);
|
|
164
193
|
|
|
165
|
-
// Get a specific violation
|
|
194
|
+
// Get a specific violation (null when there is none)
|
|
166
195
|
const v = okdb.getUniqueViolation('users', ['email'], 'alice@example.com');
|
|
167
196
|
// v = { winner, entries, count, detectedAt, ... }
|
|
168
197
|
```
|
|
@@ -170,12 +199,16 @@ const v = okdb.getUniqueViolation('users', ['email'], 'alice@example.com');
|
|
|
170
199
|
Events emitted:
|
|
171
200
|
|
|
172
201
|
```javascript
|
|
173
|
-
okdb.events.on('unique:violation', ({ type, index, indexKey, winner }) => {
|
|
174
|
-
|
|
202
|
+
okdb.events.on('unique:violation', ({ type, index, indexKey, winner }) => {
|
|
203
|
+
console.log('duplicate', type, index, indexKey, 'winner:', winner);
|
|
204
|
+
});
|
|
205
|
+
okdb.events.on('unique:violation_resolved', ({ type, index, indexKey }) => {
|
|
206
|
+
console.log('resolved', type, index, indexKey);
|
|
207
|
+
});
|
|
175
208
|
```
|
|
176
209
|
|
|
177
210
|
:::note
|
|
178
|
-
Geo indexes cannot be unique. Geohash collisions make uniqueness semantics undefined — the registration
|
|
211
|
+
Geo indexes cannot be unique. Geohash collisions make uniqueness semantics undefined — the registration throws `INVALID_INDEX_OPTIONS`.
|
|
179
212
|
:::
|
|
180
213
|
|
|
181
214
|
---
|
|
@@ -185,17 +218,20 @@ Geo indexes cannot be unique. Geohash collisions make uniqueness semantics undef
|
|
|
185
218
|
Geospatial indexes use geohash encoding to map `{ lat, lon }` coordinates into a lexicographically ordered string space.
|
|
186
219
|
|
|
187
220
|
```javascript
|
|
221
|
+
await okdb.registerType('restaurants');
|
|
188
222
|
await okdb.registerIndex('restaurants', ['coords'], { type: 'geo', precision: 7 });
|
|
189
223
|
|
|
190
224
|
await okdb.put('restaurants', 'r1', { name: 'Le Bistro', coords: { lat: 48.87, lon: 2.31 } });
|
|
191
225
|
|
|
192
226
|
// Radius search
|
|
193
227
|
const results = okdb.geoQuery('restaurants', 'coords', {
|
|
194
|
-
lat:
|
|
195
|
-
lon:
|
|
196
|
-
radius: 1000,
|
|
228
|
+
lat: 48.87,
|
|
229
|
+
lon: 2.31,
|
|
230
|
+
radius: 1000, // metres
|
|
197
231
|
});
|
|
198
|
-
for (const { key, value, distance } of results) {
|
|
232
|
+
for (const { key, value, distance } of results) {
|
|
233
|
+
console.log(key, value.name, distance);
|
|
234
|
+
}
|
|
199
235
|
```
|
|
200
236
|
|
|
201
237
|
**Precision guide:**
|