server: the chunked explored store (#21)

Migration step 5 creates explored_chunk: one 512-byte bitmap per (player, scene, 64x64-cell
chunk), both FKs cascading. server/explored.nim owns the format (row-major, LSB-first bits),
dirty-chunk detection, the upsert and the startup restore. tests/texplored.nim pins the layout,
the whole-chunk encoding, the upsert replacement, the per-player keying, the restore and the
write cost (0.015 ms avg / 0.035 ms p99); tests/tdb.nim covers step 5 and a schema-4 fixture.
This commit is contained in:
2026-11-15 09:58:16 +01:00
parent 5d38e92c03
commit 22014a36dc
4 changed files with 453 additions and 7 deletions
+27 -1
View File
@@ -12,7 +12,11 @@
## `scene.next_wall_id`, which never decreases (#3 FIXED).
## Step 4 ("tokens", M3 slice 5 / #20) creates `entity` for the map figures (DESIGN 4.4, 8). The
## character link, the name-reveal flags and the three property buckets are slices 7-8 (#22, #23) and
## are not columns yet; `entity_status` and `explored_chunk` arrive with slice 7 and slice 6.
## are not columns yet; `entity_status` arrives with slice 7.
## Step 5 ("explored", M3 slice 6 / #21) creates `explored_chunk`, the chunked explored bitmaps
## (DESIGN 4.4, 8): one row per (player, scene, 64x64-cell chunk), 512 B per row, LSB-first
## row-major bits (server/explored.nim). Both FKs cascade, so removing a player or a scene takes its
## explored cells with it.
## One connection, used under the server's global lock (4.3); this module does no locking itself.
import std/[strutils, times]
import db_connector/db_sqlite
@@ -108,6 +112,15 @@ CREATE TABLE entity (
vision_radius REAL, -- NULL = scene vision_radius_default (Q7)
flags TEXT NOT NULL DEFAULT '{}', -- JSON object; the `vision` flag lives here (8)
created_at TEXT NOT NULL
)"""]),
Migration(version: 5, name: "explored", stmts: @["""
CREATE TABLE explored_chunk (
player_id INTEGER NOT NULL REFERENCES player(id) ON DELETE CASCADE,
scene_id INTEGER NOT NULL REFERENCES scene(id) ON DELETE CASCADE,
cx INTEGER NOT NULL, -- chunk coordinate: cell div 64 (server/explored.nim)
cy INTEGER NOT NULL,
bits BLOB NOT NULL CHECK (length(bits) = 512), -- 64x64 cells, LSB-first row-major
PRIMARY KEY (player_id, scene_id, cx, cy)
)"""]),
]
LatestVersion* = migrations[^1].version
@@ -123,6 +136,19 @@ proc openDatabase*(path: string): DbConn =
proc schemaVersion*(db: DbConn): int =
parseInt(db.getValue(sql"PRAGMA user_version"))
template withTransaction*(db: DbConn, body: untyped) =
## One explicit transaction (DESIGN 4.2 step 6): COMMIT on success, ROLLBACK and re-raise on any
## failure. A template rather than a proc taking a closure: call sites run under the global lock
## inside `{.gcsafe.}` handlers, where a closure would need its own gcsafe proof.
db.exec(sql"BEGIN")
try:
body
db.exec(sql"COMMIT")
except CatchableError:
try: db.exec(sql"ROLLBACK")
except CatchableError: discard # the original error is the one worth reporting
raise
proc appliedMigrations*(db: DbConn): seq[MigrationRow] =
for r in db.getAllRows(sql"SELECT version, name FROM schema_migration ORDER BY version"):
result.add (version: parseInt(r[0]), name: r[1])