Skip to content

Commit f21c707

Browse files
authored
fix(logbook): index frame and batch queries (#129570)
1 parent 46e4520 commit f21c707

2 files changed

Lines changed: 113 additions & 0 deletions

File tree

‎extensions/logbook/src/store.test.ts‎

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,14 @@ import type { LogbookCardDraft } from "./types.js";
99

1010
const DAY = "2026-07-03";
1111

12+
function queryPlanDetails(database: DatabaseSync, sql: string): string[] {
13+
return (
14+
database.prepare(`EXPLAIN QUERY PLAN ${sql}`).all() as Array<{
15+
detail: string;
16+
}>
17+
).map((row) => row.detail);
18+
}
19+
1220
function draft(overrides: Partial<LogbookCardDraft> = {}): LogbookCardDraft {
1321
const base = new Date(`${DAY}T10:00:00`).getTime();
1422
return {
@@ -102,6 +110,107 @@ describe("LogbookStore", () => {
102110
}
103111
});
104112

113+
it.each([
114+
{
115+
operation: "batch frame reads",
116+
sql: "SELECT id FROM frames WHERE batch_id = 1 ORDER BY captured_at_ms ASC",
117+
expectedIndex: "idx_logbook_frames_batch",
118+
scannedTable: "frames",
119+
ordered: true,
120+
},
121+
{
122+
operation: "observation replacement",
123+
sql: "DELETE FROM observations WHERE batch_id = 1",
124+
expectedIndex: "idx_logbook_observations_batch",
125+
scannedTable: "observations",
126+
ordered: false,
127+
},
128+
{
129+
operation: "frame pruning foreign-key maintenance",
130+
sql: "DELETE FROM frames WHERE id = 1",
131+
expectedIndex: "idx_logbook_cards_keyframe",
132+
scannedTable: "cards",
133+
ordered: false,
134+
},
135+
{
136+
operation: "latest frame reads",
137+
sql: "SELECT id FROM frames ORDER BY captured_at_ms DESC LIMIT 1",
138+
expectedIndex: "idx_logbook_frames_captured_at",
139+
scannedTable: "frames",
140+
ordered: true,
141+
},
142+
{
143+
operation: "frame range reads",
144+
sql: "SELECT id FROM frames WHERE captured_at_ms >= 1 AND captured_at_ms < 2 ORDER BY captured_at_ms ASC",
145+
expectedIndex: "idx_logbook_frames_captured_at",
146+
scannedTable: "frames",
147+
ordered: true,
148+
},
149+
])(
150+
"uses the supporting index for $operation",
151+
({ sql, expectedIndex, scannedTable, ordered }) => {
152+
const database = new DatabaseSync(path.join(dir, "logbook.sqlite"), { readOnly: true });
153+
try {
154+
const plan = queryPlanDetails(database, sql);
155+
expect(plan).toEqual(expect.arrayContaining([expect.stringContaining(expectedIndex)]));
156+
expect(
157+
plan.some(
158+
(detail) => detail.startsWith(`SCAN ${scannedTable}`) && !detail.includes(" USING "),
159+
),
160+
).toBe(false);
161+
if (ordered) {
162+
expect(plan).not.toEqual(
163+
expect.arrayContaining([expect.stringContaining("USE TEMP B-TREE FOR ORDER BY")]),
164+
);
165+
}
166+
} finally {
167+
database.close();
168+
}
169+
},
170+
);
171+
172+
it("restores missing schema-1 indexes on reopen without changing the version", () => {
173+
store.close();
174+
const databasePath = path.join(dir, "logbook.sqlite");
175+
const database = new DatabaseSync(databasePath);
176+
database.exec(`
177+
DROP INDEX IF EXISTS idx_logbook_frames_captured_at;
178+
DROP INDEX IF EXISTS idx_logbook_frames_batch;
179+
DROP INDEX IF EXISTS idx_logbook_observations_batch;
180+
DROP INDEX IF EXISTS idx_logbook_cards_keyframe;
181+
`);
182+
expect(database.prepare("PRAGMA user_version").get()).toEqual({ user_version: 1 });
183+
database.close();
184+
185+
store = new LogbookStore(dir);
186+
187+
const reopened = new DatabaseSync(databasePath, { readOnly: true });
188+
try {
189+
const indexes = reopened
190+
.prepare(
191+
`SELECT name FROM sqlite_schema
192+
WHERE type = 'index'
193+
AND name IN (
194+
'idx_logbook_frames_batch',
195+
'idx_logbook_frames_captured_at',
196+
'idx_logbook_observations_batch',
197+
'idx_logbook_cards_keyframe'
198+
)
199+
ORDER BY name`,
200+
)
201+
.all();
202+
expect(indexes).toEqual([
203+
{ name: "idx_logbook_cards_keyframe" },
204+
{ name: "idx_logbook_frames_batch" },
205+
{ name: "idx_logbook_frames_captured_at" },
206+
{ name: "idx_logbook_observations_batch" },
207+
]);
208+
expect(reopened.prepare("PRAGMA user_version").get()).toEqual({ user_version: 1 });
209+
} finally {
210+
reopened.close();
211+
}
212+
});
213+
105214
it("rejects values that violate STRICT column types", () => {
106215
const database = new DatabaseSync(path.join(dir, "logbook.sqlite"));
107216
try {

‎extensions/logbook/src/store.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,9 @@ CREATE TABLE IF NOT EXISTS frames (
5454
batch_id INTEGER REFERENCES batches(id) ON DELETE SET NULL
5555
) STRICT;
5656
CREATE INDEX IF NOT EXISTS idx_logbook_frames_day ON frames (day, captured_at_ms);
57+
CREATE INDEX IF NOT EXISTS idx_logbook_frames_captured_at ON frames (captured_at_ms);
5758
CREATE INDEX IF NOT EXISTS idx_logbook_frames_unbatched ON frames (batch_id) WHERE batch_id IS NULL;
59+
CREATE INDEX IF NOT EXISTS idx_logbook_frames_batch ON frames (batch_id, captured_at_ms) WHERE batch_id IS NOT NULL;
5860
CREATE TABLE IF NOT EXISTS observations (
5961
id INTEGER PRIMARY KEY AUTOINCREMENT,
6062
batch_id INTEGER NOT NULL REFERENCES batches(id) ON DELETE CASCADE,
@@ -64,6 +66,7 @@ CREATE TABLE IF NOT EXISTS observations (
6466
text TEXT NOT NULL
6567
) STRICT;
6668
CREATE INDEX IF NOT EXISTS idx_logbook_observations_day ON observations (day, start_ms);
69+
CREATE INDEX IF NOT EXISTS idx_logbook_observations_batch ON observations (batch_id);
6770
CREATE TABLE IF NOT EXISTS cards (
6871
id INTEGER PRIMARY KEY AUTOINCREMENT,
6972
day TEXT NOT NULL,
@@ -80,6 +83,7 @@ CREATE TABLE IF NOT EXISTS cards (
8083
updated_ms INTEGER NOT NULL
8184
) STRICT;
8285
CREATE INDEX IF NOT EXISTS idx_logbook_cards_day ON cards (day, start_ms);
86+
CREATE INDEX IF NOT EXISTS idx_logbook_cards_keyframe ON cards (keyframe_id) WHERE keyframe_id IS NOT NULL;
8387
CREATE TABLE IF NOT EXISTS standups (
8488
day TEXT PRIMARY KEY,
8589
text TEXT NOT NULL,

0 commit comments

Comments
 (0)