@quereus/store 0.3.6 → 0.3.7

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.
Files changed (54) hide show
  1. package/README.md +2 -2
  2. package/dist/src/common/ddl-generator.d.ts +15 -0
  3. package/dist/src/common/ddl-generator.d.ts.map +1 -0
  4. package/dist/src/common/ddl-generator.js +131 -0
  5. package/dist/src/common/ddl-generator.js.map +1 -0
  6. package/dist/src/common/encoding.d.ts +65 -0
  7. package/dist/src/common/encoding.d.ts.map +1 -0
  8. package/dist/src/common/encoding.js +339 -0
  9. package/dist/src/common/encoding.js.map +1 -0
  10. package/dist/src/common/events.d.ts +119 -0
  11. package/dist/src/common/events.d.ts.map +1 -0
  12. package/dist/src/common/events.js +158 -0
  13. package/dist/src/common/events.js.map +1 -0
  14. package/dist/src/common/index.d.ts +15 -0
  15. package/dist/src/common/index.d.ts.map +1 -0
  16. package/dist/src/common/index.js +23 -0
  17. package/dist/src/common/index.js.map +1 -0
  18. package/dist/src/common/key-builder.d.ts +58 -0
  19. package/dist/src/common/key-builder.d.ts.map +1 -0
  20. package/dist/src/common/key-builder.js +130 -0
  21. package/dist/src/common/key-builder.js.map +1 -0
  22. package/dist/src/common/kv-store.d.ts +150 -0
  23. package/dist/src/common/kv-store.d.ts.map +1 -0
  24. package/dist/src/common/kv-store.js +6 -0
  25. package/dist/src/common/kv-store.js.map +1 -0
  26. package/dist/src/common/memory-store.d.ts +37 -0
  27. package/dist/src/common/memory-store.d.ts.map +1 -0
  28. package/dist/src/common/memory-store.js +146 -0
  29. package/dist/src/common/memory-store.js.map +1 -0
  30. package/dist/src/common/serialization.d.ts +41 -0
  31. package/dist/src/common/serialization.d.ts.map +1 -0
  32. package/dist/src/common/serialization.js +111 -0
  33. package/dist/src/common/serialization.js.map +1 -0
  34. package/dist/src/common/store-connection.d.ts +34 -0
  35. package/dist/src/common/store-connection.d.ts.map +1 -0
  36. package/dist/src/common/store-connection.js +55 -0
  37. package/dist/src/common/store-connection.js.map +1 -0
  38. package/dist/src/common/store-module.d.ts +111 -0
  39. package/dist/src/common/store-module.d.ts.map +1 -0
  40. package/dist/src/common/store-module.js +331 -0
  41. package/dist/src/common/store-module.js.map +1 -0
  42. package/dist/src/common/store-table.d.ts +115 -0
  43. package/dist/src/common/store-table.d.ts.map +1 -0
  44. package/dist/src/common/store-table.js +472 -0
  45. package/dist/src/common/store-table.js.map +1 -0
  46. package/dist/src/common/transaction.d.ts +57 -0
  47. package/dist/src/common/transaction.d.ts.map +1 -0
  48. package/dist/src/common/transaction.js +156 -0
  49. package/dist/src/common/transaction.js.map +1 -0
  50. package/dist/src/index.d.ts +17 -0
  51. package/dist/src/index.d.ts.map +1 -0
  52. package/dist/src/index.js +18 -0
  53. package/dist/src/index.js.map +1 -0
  54. package/package.json +1 -1
@@ -0,0 +1,111 @@
1
+ /**
2
+ * Generic Store Module for Quereus.
3
+ *
4
+ * A platform-agnostic VirtualTableModule that uses a KVStoreProvider
5
+ * to create StoreTable instances. This enables any storage backend
6
+ * (LevelDB, IndexedDB, React Native, etc.) to be used with the same
7
+ * table implementation.
8
+ */
9
+ import type { Database, TableSchema, TableIndexSchema, VirtualTableModule, BaseModuleConfig, BestAccessPlanRequest, BestAccessPlanResult } from '@quereus/quereus';
10
+ import type { KVStore, KVStoreProvider } from './kv-store.js';
11
+ import type { StoreEventEmitter } from './events.js';
12
+ import { TransactionCoordinator } from './transaction.js';
13
+ import { StoreTable, type StoreTableConfig, type StoreTableModule } from './store-table.js';
14
+ /**
15
+ * Configuration options for StoreModule tables.
16
+ */
17
+ export interface StoreModuleConfig extends BaseModuleConfig {
18
+ /** Collation for text keys. Default: 'NOCASE'. */
19
+ collation?: 'BINARY' | 'NOCASE';
20
+ /** Additional platform-specific options. */
21
+ [key: string]: unknown;
22
+ }
23
+ /**
24
+ * Generic store module that works with any KVStoreProvider.
25
+ *
26
+ * Usage:
27
+ * ```typescript
28
+ * import { StoreModule } from '@quereus/plugin-store';
29
+ * import { createLevelDBProvider } from '@quereus/store-leveldb';
30
+ *
31
+ * const provider = createLevelDBProvider({ basePath: './data' });
32
+ * const module = new StoreModule(provider);
33
+ * db.registerVtabModule('store', module);
34
+ * ```
35
+ */
36
+ export declare class StoreModule implements VirtualTableModule<StoreTable, StoreModuleConfig>, StoreTableModule {
37
+ private provider;
38
+ private stores;
39
+ private coordinators;
40
+ private tables;
41
+ private eventEmitter?;
42
+ constructor(provider: KVStoreProvider, eventEmitter?: StoreEventEmitter);
43
+ /**
44
+ * Get the event emitter for this module.
45
+ */
46
+ getEventEmitter(): StoreEventEmitter | undefined;
47
+ /**
48
+ * Get the KVStoreProvider used by this module.
49
+ */
50
+ getProvider(): KVStoreProvider;
51
+ /**
52
+ * Creates a new store-backed table.
53
+ * Called by CREATE TABLE.
54
+ */
55
+ create(db: Database, tableSchema: TableSchema): StoreTable;
56
+ /**
57
+ * Connects to an existing store-backed table.
58
+ * Called when loading schema from persistent storage.
59
+ */
60
+ connect(db: Database, _pAux: unknown, _moduleName: string, schemaName: string, tableName: string, options: StoreModuleConfig, importedTableSchema?: TableSchema): StoreTable;
61
+ /**
62
+ * Destroys a store table and its storage.
63
+ */
64
+ destroy(_db: Database, _pAux: unknown, _moduleName: string, schemaName: string, tableName: string): Promise<void>;
65
+ /**
66
+ * Creates an index on a store-backed table.
67
+ */
68
+ createIndex(_db: Database, schemaName: string, tableName: string, indexSchema: TableIndexSchema): Promise<void>;
69
+ /**
70
+ * Build index entries for all existing rows in a table.
71
+ */
72
+ private buildIndexEntries;
73
+ /**
74
+ * Modern access planning interface.
75
+ */
76
+ getBestAccessPlan(_db: Database, tableInfo: TableSchema, request: BestAccessPlanRequest): BestAccessPlanResult;
77
+ /**
78
+ * Get or create a store for a table.
79
+ */
80
+ getStore(tableKey: string, _config: StoreTableConfig): Promise<KVStore>;
81
+ /**
82
+ * Get or create a transaction coordinator for a table.
83
+ */
84
+ getCoordinator(tableKey: string, config: StoreTableConfig): Promise<TransactionCoordinator>;
85
+ /**
86
+ * Save table DDL to persistent storage (both table store and catalog).
87
+ */
88
+ saveTableDDL(tableSchema: TableSchema): Promise<void>;
89
+ /**
90
+ * Load all DDL statements from the catalog store.
91
+ * Used to restore persisted tables on startup.
92
+ */
93
+ loadAllDDL(): Promise<string[]>;
94
+ /**
95
+ * Remove DDL from the catalog store when a table is dropped.
96
+ */
97
+ removeTableDDL(schemaName: string, tableName: string): Promise<void>;
98
+ /**
99
+ * Parse module configuration from vtab args.
100
+ */
101
+ private parseConfig;
102
+ /**
103
+ * Close all stores.
104
+ */
105
+ closeAll(): Promise<void>;
106
+ /**
107
+ * Get a table by schema and name.
108
+ */
109
+ getTable(schemaName: string, tableName: string): StoreTable | undefined;
110
+ }
111
+ //# sourceMappingURL=store-module.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"store-module.d.ts","sourceRoot":"","sources":["../../../src/common/store-module.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,KAAK,EACV,QAAQ,EACR,WAAW,EACX,gBAAgB,EAChB,kBAAkB,EAClB,gBAAgB,EAChB,qBAAqB,EACrB,oBAAoB,EAErB,MAAM,kBAAkB,CAAC;AAG1B,OAAO,KAAK,EAAE,OAAO,EAAE,eAAe,EAAE,MAAM,eAAe,CAAC;AAC9D,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAC;AACrD,OAAO,EAAE,sBAAsB,EAAE,MAAM,kBAAkB,CAAC;AAC1D,OAAO,EAAE,UAAU,EAAE,KAAK,gBAAgB,EAAE,KAAK,gBAAgB,EAAE,MAAM,kBAAkB,CAAC;AAK5F;;GAEG;AACH,MAAM,WAAW,iBAAkB,SAAQ,gBAAgB;IACzD,kDAAkD;IAClD,SAAS,CAAC,EAAE,QAAQ,GAAG,QAAQ,CAAC;IAChC,4CAA4C;IAC5C,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC;CACxB;AAED;;;;;;;;;;;;GAYG;AACH,qBAAa,W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gBAAgB,GAAG,OAAO,CAAC,OAAO,CAAC;IAU7E;;OAEG;IACG,cAAc,CAAC,QAAQ,EAAE,MAAM,EAAE,MAAM,EAAE,gBAAgB,GAAG,OAAO,CAAC,sBAAsB,CAAC;IAUjG;;OAEG;IACG,YAAY,CAAC,WAAW,EAAE,WAAW,GAAG,OAAO,CAAC,IAAI,CAAC;IAkB3D;;;OAGG;IACG,UAAU,IAAI,OAAO,CAAC,MAAM,EAAE,CAAC;IAcrC;;OAEG;IACG,cAAc,CAAC,UAAU,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAM1E;;OAEG;IACH,OAAO,CAAC,WAAW;IAMnB;;OAEG;IACG,QAAQ,IAAI,OAAO,CAAC,IAAI,CAAC;IAW/B;;OAEG;IACH,QAAQ,CAAC,UAAU,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,GAAG,UAAU,GAAG,SAAS;CAIxE"}
@@ -0,0 +1,331 @@
1
+ /**
2
+ * Generic Store Module for Quereus.
3
+ *
4
+ * A platform-agnostic VirtualTableModule that uses a KVStoreProvider
5
+ * to create StoreTable instances. This enables any storage backend
6
+ * (LevelDB, IndexedDB, React Native, etc.) to be used with the same
7
+ * table implementation.
8
+ */
9
+ import { AccessPlanBuilder, QuereusError, StatusCode } from '@quereus/quereus';
10
+ import { TransactionCoordinator } from './transaction.js';
11
+ import { StoreTable } from './store-table.js';
12
+ import { buildMetaKey, buildMetaScanBounds, buildIndexKey, buildTableScanBounds } from './key-builder.js';
13
+ import { serializeRow, deserializeRow } from './serialization.js';
14
+ import { generateTableDDL } from './ddl-generator.js';
15
+ /**
16
+ * Generic store module that works with any KVStoreProvider.
17
+ *
18
+ * Usage:
19
+ * ```typescript
20
+ * import { StoreModule } from '@quereus/plugin-store';
21
+ * import { createLevelDBProvider } from '@quereus/store-leveldb';
22
+ *
23
+ * const provider = createLevelDBProvider({ basePath: './data' });
24
+ * const module = new StoreModule(provider);
25
+ * db.registerVtabModule('store', module);
26
+ * ```
27
+ */
28
+ export class StoreModule {
29
+ provider;
30
+ stores = new Map();
31
+ coordinators = new Map();
32
+ tables = new Map();
33
+ eventEmitter;
34
+ constructor(provider, eventEmitter) {
35
+ this.provider = provider;
36
+ this.eventEmitter = eventEmitter;
37
+ }
38
+ /**
39
+ * Get the event emitter for this module.
40
+ */
41
+ getEventEmitter() {
42
+ return this.eventEmitter;
43
+ }
44
+ /**
45
+ * Get the KVStoreProvider used by this module.
46
+ */
47
+ getProvider() {
48
+ return this.provider;
49
+ }
50
+ /**
51
+ * Creates a new store-backed table.
52
+ * Called by CREATE TABLE.
53
+ */
54
+ create(db, tableSchema) {
55
+ const tableKey = `${tableSchema.schemaName}.${tableSchema.name}`.toLowerCase();
56
+ if (this.tables.has(tableKey)) {
57
+ throw new QuereusError(`Store table '${tableSchema.name}' already exists in schema '${tableSchema.schemaName}'`, StatusCode.ERROR);
58
+ }
59
+ const config = this.parseConfig(tableSchema.vtabArgs);
60
+ const table = new StoreTable(db, this, tableSchema, config, this.eventEmitter);
61
+ this.tables.set(tableKey, table);
62
+ // Emit schema change event for table creation
63
+ this.eventEmitter?.emitSchemaChange({
64
+ type: 'create',
65
+ objectType: 'table',
66
+ schemaName: tableSchema.schemaName,
67
+ objectName: tableSchema.name,
68
+ ddl: generateTableDDL(tableSchema),
69
+ });
70
+ return table;
71
+ }
72
+ /**
73
+ * Connects to an existing store-backed table.
74
+ * Called when loading schema from persistent storage.
75
+ */
76
+ connect(db, _pAux, _moduleName, schemaName, tableName, options, importedTableSchema) {
77
+ const tableKey = `${schemaName}.${tableName}`.toLowerCase();
78
+ // Check if we already have this table connected
79
+ const existing = this.tables.get(tableKey);
80
+ if (existing) {
81
+ return existing;
82
+ }
83
+ // Convert options to Record<string, SqlValue> for vtabArgs
84
+ const vtabArgs = {};
85
+ if (options?.collation !== undefined)
86
+ vtabArgs.collation = options.collation;
87
+ // Use the imported schema if provided, otherwise create a minimal one
88
+ const tableSchema = importedTableSchema ?? {
89
+ name: tableName,
90
+ schemaName: schemaName,
91
+ columns: Object.freeze([]),
92
+ columnIndexMap: new Map(),
93
+ primaryKeyDefinition: [],
94
+ checkConstraints: Object.freeze([]),
95
+ isTemporary: false,
96
+ isView: false,
97
+ vtabModuleName: 'store',
98
+ vtabArgs,
99
+ vtabModule: this,
100
+ estimatedRows: 0,
101
+ };
102
+ const config = this.parseConfig(vtabArgs);
103
+ const table = new StoreTable(db, this, tableSchema, config, this.eventEmitter, true // isConnected - DDL already exists in storage
104
+ );
105
+ this.tables.set(tableKey, table);
106
+ return table;
107
+ }
108
+ /**
109
+ * Destroys a store table and its storage.
110
+ */
111
+ async destroy(_db, _pAux, _moduleName, schemaName, tableName) {
112
+ const tableKey = `${schemaName}.${tableName}`.toLowerCase();
113
+ const table = this.tables.get(tableKey);
114
+ if (table) {
115
+ await table.disconnect();
116
+ this.tables.delete(tableKey);
117
+ }
118
+ // Close the store via provider
119
+ await this.provider.closeStore(schemaName, tableName);
120
+ this.stores.delete(tableKey);
121
+ this.coordinators.delete(tableKey);
122
+ // Emit schema change event for table drop
123
+ this.eventEmitter?.emitSchemaChange({
124
+ type: 'drop',
125
+ objectType: 'table',
126
+ schemaName,
127
+ objectName: tableName,
128
+ });
129
+ }
130
+ /**
131
+ * Creates an index on a store-backed table.
132
+ */
133
+ async createIndex(_db, schemaName, tableName, indexSchema) {
134
+ const tableKey = `${schemaName}.${tableName}`.toLowerCase();
135
+ const table = this.tables.get(tableKey);
136
+ if (!table) {
137
+ throw new QuereusError(`Store table '${tableName}' not found in schema '${schemaName}'`, StatusCode.NOTFOUND);
138
+ }
139
+ const store = await this.getStore(tableKey, table.getConfig());
140
+ const tableSchema = table.getSchema();
141
+ // Store index metadata
142
+ const indexMetaKey = buildMetaKey('index', schemaName, tableName, indexSchema.name);
143
+ const indexMetaValue = serializeRow([
144
+ indexSchema.name,
145
+ JSON.stringify(indexSchema.columns),
146
+ ]);
147
+ await store.put(indexMetaKey, indexMetaValue);
148
+ // Build index entries for existing rows
149
+ await this.buildIndexEntries(store, tableSchema, indexSchema);
150
+ // Emit schema change event
151
+ this.eventEmitter?.emitSchemaChange({
152
+ type: 'create',
153
+ objectType: 'index',
154
+ schemaName,
155
+ objectName: indexSchema.name,
156
+ });
157
+ }
158
+ /**
159
+ * Build index entries for all existing rows in a table.
160
+ */
161
+ async buildIndexEntries(store, tableSchema, indexSchema) {
162
+ const encodeOptions = { collation: 'NOCASE' };
163
+ // Scan all data rows
164
+ const bounds = buildTableScanBounds(tableSchema.schemaName, tableSchema.name);
165
+ const batch = store.batch();
166
+ for await (const entry of store.iterate(bounds)) {
167
+ const row = deserializeRow(entry.value);
168
+ // Extract PK values
169
+ const pkValues = tableSchema.primaryKeyDefinition.map(pk => row[pk.index]);
170
+ // Extract index column values
171
+ const indexValues = indexSchema.columns.map(col => row[col.index]);
172
+ // Build and store index key
173
+ const indexKey = buildIndexKey(tableSchema.schemaName, tableSchema.name, indexSchema.name, indexValues, pkValues, encodeOptions);
174
+ batch.put(indexKey, new Uint8Array(0)); // Index value is empty
175
+ }
176
+ await batch.write();
177
+ }
178
+ /**
179
+ * Modern access planning interface.
180
+ */
181
+ getBestAccessPlan(_db, tableInfo, request) {
182
+ const estimatedRows = request.estimatedRows ?? 1000;
183
+ // Check for primary key equality constraints
184
+ const pkColumns = tableInfo.primaryKeyDefinition.map(pk => pk.index);
185
+ const pkFilters = request.filters.filter(f => f.columnIndex !== undefined &&
186
+ pkColumns.includes(f.columnIndex) &&
187
+ f.op === '=');
188
+ if (pkFilters.length === pkColumns.length && pkColumns.length > 0) {
189
+ // Full PK match - point lookup
190
+ const handledFilters = request.filters.map(f => pkFilters.some(pf => pf.columnIndex === f.columnIndex && pf.op === f.op));
191
+ return AccessPlanBuilder
192
+ .eqMatch(1, 0.1)
193
+ .setHandledFilters(handledFilters)
194
+ .setIsSet(true)
195
+ .setExplanation('Store primary key lookup')
196
+ .build();
197
+ }
198
+ // Check for range constraints on PK
199
+ const rangeOps = ['<', '<=', '>', '>='];
200
+ const rangeFilters = request.filters.filter(f => f.columnIndex !== undefined &&
201
+ pkColumns.includes(f.columnIndex) &&
202
+ rangeOps.includes(f.op));
203
+ if (rangeFilters.length > 0) {
204
+ // Range scan on PK
205
+ const handledFilters = request.filters.map(f => rangeFilters.some(rf => rf.columnIndex === f.columnIndex && rf.op === f.op));
206
+ const rangeRows = Math.max(1, Math.floor(estimatedRows * 0.3));
207
+ return AccessPlanBuilder
208
+ .rangeScan(rangeRows, 0.2)
209
+ .setHandledFilters(handledFilters)
210
+ .setExplanation('Store primary key range scan')
211
+ .build();
212
+ }
213
+ // Check for secondary index usage
214
+ const indexes = tableInfo.indexes || [];
215
+ for (const index of indexes) {
216
+ const indexColumns = index.columns.map(c => c.index);
217
+ const indexFilters = request.filters.filter(f => f.columnIndex !== undefined &&
218
+ indexColumns.includes(f.columnIndex) &&
219
+ f.op === '=');
220
+ if (indexFilters.length > 0) {
221
+ const handledFilters = request.filters.map(f => indexFilters.some(idf => idf.columnIndex === f.columnIndex && idf.op === f.op));
222
+ const matchedRows = Math.max(1, Math.floor(estimatedRows * 0.1));
223
+ return AccessPlanBuilder
224
+ .eqMatch(matchedRows, 0.3)
225
+ .setHandledFilters(handledFilters)
226
+ .setExplanation(`Store index scan on ${index.name}`)
227
+ .build();
228
+ }
229
+ }
230
+ // Fallback to full scan
231
+ return AccessPlanBuilder
232
+ .fullScan(estimatedRows)
233
+ .setHandledFilters(new Array(request.filters.length).fill(false))
234
+ .setExplanation('Store full table scan')
235
+ .build();
236
+ }
237
+ // --- StoreTableModule interface implementation ---
238
+ /**
239
+ * Get or create a store for a table.
240
+ */
241
+ async getStore(tableKey, _config) {
242
+ let store = this.stores.get(tableKey);
243
+ if (!store) {
244
+ const [schemaName, tableName] = tableKey.split('.');
245
+ store = await this.provider.getStore(schemaName, tableName);
246
+ this.stores.set(tableKey, store);
247
+ }
248
+ return store;
249
+ }
250
+ /**
251
+ * Get or create a transaction coordinator for a table.
252
+ */
253
+ async getCoordinator(tableKey, config) {
254
+ let coordinator = this.coordinators.get(tableKey);
255
+ if (!coordinator) {
256
+ const store = await this.getStore(tableKey, config);
257
+ coordinator = new TransactionCoordinator(store, this.eventEmitter);
258
+ this.coordinators.set(tableKey, coordinator);
259
+ }
260
+ return coordinator;
261
+ }
262
+ /**
263
+ * Save table DDL to persistent storage (both table store and catalog).
264
+ */
265
+ async saveTableDDL(tableSchema) {
266
+ const tableKey = `${tableSchema.schemaName}.${tableSchema.name}`.toLowerCase();
267
+ const ddl = generateTableDDL(tableSchema);
268
+ const metaKey = buildMetaKey('ddl', tableSchema.schemaName, tableSchema.name);
269
+ const encoder = new TextEncoder();
270
+ const encodedDDL = encoder.encode(ddl);
271
+ // Save to table's own store
272
+ const store = this.stores.get(tableKey);
273
+ if (store) {
274
+ await store.put(metaKey, encodedDDL);
275
+ }
276
+ // Also save to catalog store for discovery
277
+ const catalogStore = await this.provider.getCatalogStore();
278
+ await catalogStore.put(metaKey, encodedDDL);
279
+ }
280
+ /**
281
+ * Load all DDL statements from the catalog store.
282
+ * Used to restore persisted tables on startup.
283
+ */
284
+ async loadAllDDL() {
285
+ const catalogStore = await this.provider.getCatalogStore();
286
+ const bounds = buildMetaScanBounds('ddl');
287
+ const decoder = new TextDecoder();
288
+ const ddlStatements = [];
289
+ for await (const entry of catalogStore.iterate(bounds)) {
290
+ const ddl = decoder.decode(entry.value);
291
+ ddlStatements.push(ddl);
292
+ }
293
+ return ddlStatements;
294
+ }
295
+ /**
296
+ * Remove DDL from the catalog store when a table is dropped.
297
+ */
298
+ async removeTableDDL(schemaName, tableName) {
299
+ const metaKey = buildMetaKey('ddl', schemaName, tableName);
300
+ const catalogStore = await this.provider.getCatalogStore();
301
+ await catalogStore.delete(metaKey);
302
+ }
303
+ /**
304
+ * Parse module configuration from vtab args.
305
+ */
306
+ parseConfig(args) {
307
+ return {
308
+ collation: args?.collation || 'NOCASE',
309
+ };
310
+ }
311
+ /**
312
+ * Close all stores.
313
+ */
314
+ async closeAll() {
315
+ for (const table of this.tables.values()) {
316
+ await table.disconnect();
317
+ }
318
+ this.tables.clear();
319
+ this.coordinators.clear();
320
+ await this.provider.closeAll();
321
+ this.stores.clear();
322
+ }
323
+ /**
324
+ * Get a table by schema and name.
325
+ */
326
+ getTable(schemaName, tableName) {
327
+ const tableKey = `${schemaName}.${tableName}`.toLowerCase();
328
+ return this.tables.get(tableKey);
329
+ }
330
+ }
331
+ //# sourceMappingURL=store-module.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"store-module.js","sourceRoot":"","sources":["../../../src/common/store-module.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAYH,OAAO,EAAE,iBAAiB,EAAE,YAAY,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AAI/E,OAAO,EAAE,sBAAsB,EAAE,MAAM,kBAAkB,CAAC;AAC1D,OAAO,EAAE,UAAU,EAAgD,MAAM,kBAAkB,CAAC;AAC5F,OAAO,EAAE,YAAY,EAAE,mBAAmB,EAAE,aAAa,EAAE,oBAAoB,EAAE,MAAM,kBAAkB,CAAC;AAC1G,OAAO,EAAE,YAAY,EAAE,cAAc,EAAE,MAAM,oBAAoB,CAAC;AAClE,OAAO,EAAE,gBAAgB,EAAE,MAAM,oBAAoB,CAAC;AAYtD;;;;;;;;;;;;GAYG;AACH,MAAM,OAAO,WAAW;IACd,QAAQ,CAAkB;IAC1B,MAAM,GAAyB,IAAI,GAAG,EAAE,CAAC;IACzC,YAAY,GAAwC,IAAI,GAAG,EAAE,CAAC;IAC9D,MAAM,GAA4B,IAAI,GAAG,EAAE,CAAC;IAC5C,YAAY,CAAqB;IAEzC,YAAY,QAAyB,EAAE,YAAgC;QACrE,IAAI,CAAC,QAAQ,GAAG,QAAQ,CAAC;QACzB,IAAI,CAAC,YAAY,GAAG,YAAY,CAAC;IACnC,CAAC;IAED;;OAEG;IACH,eAAe;QACb,OAAO,IAAI,CAAC,YAAY,CAAC;IAC3B,CAAC;IAED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C,MAAM,CAAC,OAAO,CAAC,CAAC;IACrC,CAAC;IAED;;OAEG;IACK,WAAW,CAAC,IAA0C;QAC5D,OAAO;YACL,SAAS,EAAG,IAAI,EAAE,SAAiC,IAAI,QAAQ;SAChE,CAAC;IACJ,CAAC;IAED;;OAEG;IACH,KAAK,CAAC,QAAQ;QACZ,KAAK,MAAM,KAAK,IAAI,IAAI,CAAC,MAAM,CAAC,MAAM,EAAE,EAAE,CAAC;YACzC,MAAM,KAAK,CAAC,UAAU,EAAE,CAAC;QAC3B,CAAC;QACD,IAAI,CAAC,MAAM,CAAC,KAAK,EAAE,CAAC;QACpB,IAAI,CAAC,YAAY,CAAC,KAAK,EAAE,CAAC;QAE1B,MAAM,IAAI,CAAC,QAAQ,CAAC,QAAQ,EAAE,CAAC;QAC/B,IAAI,CAAC,MAAM,CAAC,KAAK,EAAE,CAAC;IACtB,CAAC;IAED;;OAEG;IACH,QAAQ,CAAC,UAAkB,EAAE,SAAiB;QAC5C,MAAM,QAAQ,GAAG,GAAG,UAAU,IAAI,SAAS,EAAE,CAAC,WAAW,EAAE,CAAC;QAC5D,OAAO,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC;IACnC,CAAC;CACF"}
@@ -0,0 +1,115 @@
1
+ /**
2
+ * Generic KVStore-backed Virtual Table implementation.
3
+ *
4
+ * This is a platform-agnostic table implementation that works with any
5
+ * KVStore implementation (LevelDB, IndexedDB, or custom stores).
6
+ */
7
+ import { VirtualTable, IndexConstraintOp, type Database, type TableSchema, type Row, type FilterInfo, type SqlValue, type VirtualTableConnection, type UpdateArgs } from '@quereus/quereus';
8
+ import type { KVStore } from './kv-store.js';
9
+ import type { StoreEventEmitter } from './events.js';
10
+ import type { TransactionCoordinator } from './transaction.js';
11
+ import { StoreConnection } from './store-connection.js';
12
+ import { type TableStats } from './serialization.js';
13
+ import type { EncodeOptions } from './encoding.js';
14
+ /**
15
+ * Configuration for a store table.
16
+ */
17
+ export interface StoreTableConfig {
18
+ /** Collation for text keys. Default: 'NOCASE'. */
19
+ collation?: 'BINARY' | 'NOCASE';
20
+ /** Additional platform-specific options. */
21
+ [key: string]: unknown;
22
+ }
23
+ /**
24
+ * Interface for the store module that manages this table.
25
+ * Provides access to stores and coordinators.
26
+ */
27
+ export interface StoreTableModule {
28
+ /** Get a store for a table. */
29
+ getStore(tableKey: string, config: StoreTableConfig): Promise<KVStore>;
30
+ /** Get a coordinator for a table. */
31
+ getCoordinator(tableKey: string, config: StoreTableConfig): Promise<TransactionCoordinator>;
32
+ /** Save table DDL to persistent storage. */
33
+ saveTableDDL(tableSchema: TableSchema): Promise<void>;
34
+ }
35
+ /**
36
+ * Generic KVStore-backed virtual table.
37
+ *
38
+ * This class provides the core table functionality shared across all
39
+ * storage backends. Platform-specific behavior is delegated to the
40
+ * StoreTableModule.
41
+ */
42
+ export declare class StoreTable extends VirtualTable {
43
+ protected storeModule: StoreTableModule;
44
+ protected config: StoreTableConfig;
45
+ protected store: KVStore | null;
46
+ protected coordinator: TransactionCoordinator | null;
47
+ protected connection: StoreConnection | null;
48
+ protected eventEmitter?: StoreEventEmitter;
49
+ protected encodeOptions: EncodeOptions;
50
+ protected ddlSaved: boolean;
51
+ protected cachedStats: TableStats | null;
52
+ protected pendingStatsDelta: number;
53
+ protected mutationCount: number;
54
+ protected statsFlushPending: boolean;
55
+ constructor(db: Database, storeModule: StoreTableModule, tableSchema: TableSchema, config: StoreTableConfig, eventEmitter?: StoreEventEmitter, isConnected?: boolean);
56
+ /** Get the table configuration. */
57
+ getConfig(): StoreTableConfig;
58
+ /** Get the table schema. */
59
+ getSchema(): TableSchema;
60
+ /**
61
+ * Ensure the store is open and DDL is persisted.
62
+ */
63
+ protected ensureStore(): Promise<KVStore>;
64
+ /**
65
+ * Ensure the coordinator is available and connection is registered.
66
+ */
67
+ protected ensureCoordinator(): Promise<TransactionCoordinator>;
68
+ /** Apply pending stats on commit. */
69
+ protected applyPendingStats(): void;
70
+ /** Discard pending stats on rollback. */
71
+ protected discardPendingStats(): void;
72
+ /** Flush statistics to persistent storage. */
73
+ protected flushStats(): Promise<void>;
74
+ /** Create a new connection for transaction support. */
75
+ createConnection(): Promise<VirtualTableConnection>;
76
+ /** Get the current connection. */
77
+ getConnection(): VirtualTableConnection | undefined;
78
+ /** Extract primary key values from a row. */
79
+ protected extractPK(row: Row): SqlValue[];
80
+ /** Query the table with optional filters. */
81
+ query(filterInfo: FilterInfo): AsyncIterable<Row>;
82
+ /** Analyze filter info to determine PK access pattern. */
83
+ protected analyzePKAccess(filterInfo: FilterInfo): PKAccessPattern;
84
+ /** Scan a range of PK values. */
85
+ protected scanPKRange(store: KVStore, _access: PKAccessPattern, filterInfo: FilterInfo): AsyncIterable<Row>;
86
+ /** Check if a row matches the filter constraints. */
87
+ protected matchesFilters(row: Row, filterInfo: FilterInfo): boolean;
88
+ /** Compare two values according to an operator. */
89
+ protected compareValues(a: SqlValue, op: IndexConstraintOp, b: SqlValue): boolean;
90
+ /** Perform an update operation (INSERT, UPDATE, DELETE). */
91
+ update(args: UpdateArgs): Promise<Row | undefined>;
92
+ /** Update secondary indexes after a row change. */
93
+ protected updateSecondaryIndexes(coordinator: TransactionCoordinator, inTransaction: boolean, oldRow: Row | null, newRow: Row | null, pk: SqlValue[]): Promise<void>;
94
+ /** Check if two PK arrays are equal. */
95
+ protected keysEqual(a: SqlValue[], b: SqlValue[]): boolean;
96
+ /** Disconnect from the store. */
97
+ disconnect(): Promise<void>;
98
+ /** Get the current estimated row count. */
99
+ getEstimatedRowCount(): Promise<number>;
100
+ /** Track a mutation and schedule lazy stats persistence. */
101
+ protected trackMutation(delta: number, inTransaction?: boolean): void;
102
+ }
103
+ /** PK access pattern analysis result. */
104
+ interface PKAccessPattern {
105
+ type: 'point' | 'range' | 'scan';
106
+ values?: SqlValue[];
107
+ columnIndex?: number;
108
+ constraints?: Array<{
109
+ columnIndex: number;
110
+ op: IndexConstraintOp;
111
+ value?: SqlValue;
112
+ }>;
113
+ }
114
+ export {};
115
+ //# sourceMappingURL=store-table.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"store-table.d.ts","sourceRoot":"","sources":["../../../src/common/store-table.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EACL,YAAY,EACZ,iBAAiB,EAKjB,KAAK,QAAQ,EACb,KAAK,WAAW,EAChB,KAAK,GAAG,EACR,KAAK,UAAU,EACf,KAAK,QAAQ,EACb,KAAK,sBAAsB,EAC3B,KAAK,UAAU,EAEhB,MAAM,kBAAkB,CAAC;AAE1B,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,eAAe,CAAC;AAC7C,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAC;AACrD,OAAO,KAAK,EAAE,sBAAsB,EAAE,MAAM,kBAAkB,CAAC;AAC/D,OAAO,EAAE,eAAe,EAAE,MAAM,uBAAuB,CAAC;AAOxD,OAAO,EAKL,KAAK,UAAU,EAChB,MAAM,oBAAoB,CAAC;AAC5B,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,eAAe,CAAC;AAKnD;;GAEG;AACH,MAAM,WAAW,gBAAgB;IAC/B,kDAAkD;IAClD,SAAS,CAAC,EAAE,QAAQ,GAAG,QAAQ,CAAC;IAChC,4CAA4C;IAC5C,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC;CACxB;AAED;;;GAGG;AACH,MAAM,WAAW,gBAAgB;IAC/B,+BAA+B;IAC/B,QAAQ,CAAC,QAAQ,EAAE,MAAM,EAAE,MAAM,EAAE,gBAAgB,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC;IACvE,qCAAqC;IACrC,cAAc,CAAC,QAAQ,EAAE,MAAM,EAAE,MAAM,EAAE,gBAAgB,GAAG,OAAO,CAAC,sBAAsB,CAAC,CAAC;IAC5F,4CAA4C;IAC5C,YAAY,CAAC,WAAW,EAAE,WAAW,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;CACvD;AAED;;;;;;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