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
+55 -6
View File
@@ -38,9 +38,9 @@ suite "migrate":
check migrate(db) == LatestVersion
check LatestVersion == migrations[^1].version
check schemaVersion(db) == LatestVersion
check LatestVersion == 4
check LatestVersion == 5
let rows = appliedMigrations(db)
check rows.len == 4
check rows.len == 5
check rows[0][0] == 1
check rows[0][1] == "meta"
check rows[1][0] == 2
@@ -49,6 +49,8 @@ suite "migrate":
check rows[2][1] == "world"
check rows[3][0] == 4
check rows[3][1] == "tokens"
check rows[4][0] == 5
check rows[4][1] == "explored"
# the steps' own DDL ran: meta and the auth tables exist and are empty
check db.getValue(sql"SELECT count(*) FROM meta") == "0"
check db.getValue(sql"SELECT count(*) FROM player") == "0"
@@ -59,12 +61,14 @@ suite "migrate":
check db.getValue(sql"SELECT count(*) FROM wall") == "0"
# step 4 creates the token table (DESIGN 4.4 `entity`), empty
check db.getValue(sql"SELECT count(*) FROM entity") == "0"
# step 5 creates the explored chunk store (DESIGN 4.4), empty
check db.getValue(sql"SELECT count(*) FROM explored_chunk") == "0"
test "re-open is a no-op: no step applied, history and timestamps untouched":
let d = tmpDir()
block:
let db = openFresh(d)
check migrate(db) == 4
check migrate(db) == 5
close(db)
let db = openFresh(d)
defer:
@@ -73,7 +77,7 @@ suite "migrate":
let before = appliedMigrations(db)
check migrate(db) == 0
check migrate(db) == 0
check schemaVersion(db) == 4
check schemaVersion(db) == 5
check appliedMigrations(db) == before
test "history and user_version must agree":
@@ -147,9 +151,54 @@ suite "migrate":
check schemaVersion(db) == 2
check db.getValue(sql"SELECT count(*) FROM sqlite_master WHERE name = 'scene'") == "0"
check migrate(db) == 2 # steps 3 and 4 run
check schemaVersion(db) == 4
check migrate(db) == 3 # steps 3, 4 and 5 run
check schemaVersion(db) == 5
check db.getValue(sql"SELECT count(*) FROM scene") == "1"
check db.getValue(sql"SELECT next_wall_id FROM scene WHERE id = 1") == "1"
check db.getValue(sql"SELECT count(*) FROM player WHERE role = 'gm'") == "1"
check db.getValue(sql"SELECT count(*) FROM entity") == "0"
test "a slice-5 DB migrates on: explored_chunk appears and the world survives":
# A fixture DB from the previous schema version (4, the slice-5 server): scene, a player, a key
# and a token already in it. Step 5 must add the explored store and leave all of it alone.
let d = tmpDir()
let db = openFresh(d)
defer:
close(db)
removeDir(d)
db.exec(sql(MigrationHistoryDdl))
for m in migrations[0 ..< 4]:
db.exec(sql"BEGIN")
for stmt in m.stmts: db.exec(sql(stmt))
db.exec(sql"INSERT INTO schema_migration (version, name, applied_at) VALUES (?, ?, 'x')",
m.version, m.name)
db.exec(sql("PRAGMA user_version = " & $m.version))
db.exec(sql"COMMIT")
db.exec(sql"INSERT INTO player (id, name, role, colour, created_at) VALUES (1, 'GM', 'gm', '#111', 'x')")
db.exec(sql"INSERT INTO player (id, name, role, colour, created_at) VALUES (2, 'Alice', 'player', '#222', 'x')")
db.exec(sql"INSERT INTO player_key (player_id, secret, created_at) VALUES (2, 'K-1', 'x')")
db.exec(sql"""INSERT INTO entity (id, scene_id, kind, descriptive_name, actual_name, x, y,
owner_player_id, created_at)
VALUES ('tok000000001', 1, 'pc', 'Alice', 'Alice', 400, 400, 2, 'x')""")
check schemaVersion(db) == 4
check db.getValue(sql"SELECT count(*) FROM sqlite_master WHERE name = 'explored_chunk'") == "0"
check migrate(db) == 1 # only step 5 runs
check schemaVersion(db) == 5
check appliedMigrations(db).len == 5
check appliedMigrations(db)[4] == (version: 5, name: "explored")
# the explored store exists, with both FKs and the 512-byte chunk invariant
check db.getValue(sql"SELECT count(*) FROM explored_chunk") == "0"
check db.getValue(sql"SELECT sql FROM sqlite_master WHERE name = 'explored_chunk'").contains("length(bits) = 512")
# the previous version's data is untouched
check db.getValue(sql"SELECT name FROM player WHERE id = 2") == "Alice"
check db.getValue(sql"SELECT secret FROM player_key WHERE player_id = 2") == "K-1"
check db.getValue(sql"SELECT x || ',' || y FROM entity WHERE id = 'tok000000001'") == "400.0,400.0"
# a chunk row follows its player and its scene out (ON DELETE CASCADE)
var bits = newSeq[byte](512)
let ps = db.prepare("INSERT INTO explored_chunk (player_id, scene_id, cx, cy, bits) VALUES (2, 1, 0, 0, ?)")
db.exec(ps, bits)
finalize(ps)
check db.getValue(sql"SELECT count(*) FROM explored_chunk") == "1"
db.exec(sql"DELETE FROM player WHERE id = 2")
check db.getValue(sql"SELECT count(*) FROM explored_chunk") == "0"