@push.rocks/smartmongo 2.2.0 → 4.0.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/dist_ts/00_commitinfo_data.js +1 -1
- package/dist_ts/index.d.ts +1 -1
- package/dist_ts/index.js +3 -3
- package/dist_ts/tsmdb/engine/AggregationEngine.js +189 -0
- package/dist_ts/{congodb → tsmdb}/engine/IndexEngine.d.ts +23 -3
- package/dist_ts/tsmdb/engine/IndexEngine.js +678 -0
- package/dist_ts/tsmdb/engine/QueryEngine.js +271 -0
- package/dist_ts/tsmdb/engine/QueryPlanner.d.ts +64 -0
- package/dist_ts/tsmdb/engine/QueryPlanner.js +308 -0
- package/dist_ts/tsmdb/engine/SessionEngine.d.ts +117 -0
- package/dist_ts/tsmdb/engine/SessionEngine.js +232 -0
- package/dist_ts/{congodb → tsmdb}/engine/TransactionEngine.d.ts +1 -1
- package/dist_ts/tsmdb/engine/TransactionEngine.js +287 -0
- package/dist_ts/tsmdb/engine/UpdateEngine.js +461 -0
- package/dist_ts/{congodb/errors/CongoErrors.d.ts → tsmdb/errors/TsmdbErrors.d.ts} +16 -16
- package/dist_ts/tsmdb/errors/TsmdbErrors.js +155 -0
- package/dist_ts/{congodb → tsmdb}/index.d.ts +11 -4
- package/dist_ts/tsmdb/index.js +31 -0
- package/dist_ts/tsmdb/server/CommandRouter.d.ts +87 -0
- package/dist_ts/tsmdb/server/CommandRouter.js +222 -0
- package/dist_ts/{congodb/server/CongoServer.d.ts → tsmdb/server/TsmdbServer.d.ts} +6 -6
- package/dist_ts/tsmdb/server/TsmdbServer.js +229 -0
- package/dist_ts/{congodb → tsmdb}/server/WireProtocol.d.ts +1 -1
- package/dist_ts/tsmdb/server/WireProtocol.js +298 -0
- package/dist_ts/{congodb → tsmdb}/server/handlers/AdminHandler.d.ts +1 -1
- package/dist_ts/tsmdb/server/handlers/AdminHandler.js +668 -0
- package/dist_ts/{congodb → tsmdb}/server/handlers/AggregateHandler.d.ts +1 -1
- package/dist_ts/tsmdb/server/handlers/AggregateHandler.js +277 -0
- package/dist_ts/{congodb → tsmdb}/server/handlers/DeleteHandler.d.ts +1 -1
- package/dist_ts/tsmdb/server/handlers/DeleteHandler.js +95 -0
- package/dist_ts/{congodb → tsmdb}/server/handlers/FindHandler.d.ts +1 -1
- package/dist_ts/tsmdb/server/handlers/FindHandler.js +291 -0
- package/dist_ts/{congodb → tsmdb}/server/handlers/HelloHandler.d.ts +1 -1
- package/dist_ts/{congodb → tsmdb}/server/handlers/HelloHandler.js +2 -2
- package/dist_ts/{congodb → tsmdb}/server/handlers/IndexHandler.d.ts +1 -1
- package/dist_ts/tsmdb/server/handlers/IndexHandler.js +183 -0
- package/dist_ts/{congodb → tsmdb}/server/handlers/InsertHandler.d.ts +1 -1
- package/dist_ts/tsmdb/server/handlers/InsertHandler.js +79 -0
- package/dist_ts/{congodb → tsmdb}/server/handlers/UpdateHandler.d.ts +1 -1
- package/dist_ts/tsmdb/server/handlers/UpdateHandler.js +296 -0
- package/dist_ts/tsmdb/server/handlers/index.js +10 -0
- package/dist_ts/{congodb → tsmdb}/server/index.d.ts +2 -2
- package/dist_ts/tsmdb/server/index.js +7 -0
- package/dist_ts/{congodb → tsmdb}/storage/FileStorageAdapter.d.ts +27 -3
- package/dist_ts/tsmdb/storage/FileStorageAdapter.js +465 -0
- package/dist_ts/{congodb → tsmdb}/storage/IStorageAdapter.d.ts +7 -2
- package/dist_ts/{congodb → tsmdb}/storage/IStorageAdapter.js +1 -1
- package/dist_ts/{congodb → tsmdb}/storage/MemoryStorageAdapter.d.ts +3 -2
- package/dist_ts/tsmdb/storage/MemoryStorageAdapter.js +378 -0
- package/dist_ts/{congodb → tsmdb}/storage/OpLog.d.ts +1 -1
- package/dist_ts/tsmdb/storage/OpLog.js +221 -0
- package/dist_ts/tsmdb/storage/WAL.d.ts +117 -0
- package/dist_ts/tsmdb/storage/WAL.js +286 -0
- package/dist_ts/tsmdb/tsmdb.plugins.js +14 -0
- package/dist_ts/{congodb → tsmdb}/types/interfaces.d.ts +3 -3
- package/dist_ts/{congodb → tsmdb}/types/interfaces.js +1 -1
- package/dist_ts/tsmdb/utils/checksum.d.ts +30 -0
- package/dist_ts/tsmdb/utils/checksum.js +77 -0
- package/dist_ts/tsmdb/utils/index.d.ts +1 -0
- package/dist_ts/tsmdb/utils/index.js +2 -0
- package/package.json +1 -1
- package/readme.hints.md +7 -12
- package/readme.md +25 -25
- package/ts/00_commitinfo_data.ts +1 -1
- package/ts/index.ts +2 -2
- package/ts/{congodb → tsmdb}/engine/AggregationEngine.ts +1 -1
- package/ts/tsmdb/engine/IndexEngine.ts +798 -0
- package/ts/{congodb → tsmdb}/engine/QueryEngine.ts +1 -1
- package/ts/tsmdb/engine/QueryPlanner.ts +393 -0
- package/ts/tsmdb/engine/SessionEngine.ts +292 -0
- package/ts/{congodb → tsmdb}/engine/TransactionEngine.ts +12 -12
- package/ts/{congodb → tsmdb}/engine/UpdateEngine.ts +1 -1
- package/ts/{congodb/errors/CongoErrors.ts → tsmdb/errors/TsmdbErrors.ts} +34 -34
- package/ts/{congodb → tsmdb}/index.ts +16 -7
- package/ts/{congodb → tsmdb}/server/CommandRouter.ts +114 -5
- package/ts/{congodb/server/CongoServer.ts → tsmdb/server/TsmdbServer.ts} +11 -8
- package/ts/{congodb → tsmdb}/server/WireProtocol.ts +1 -1
- package/ts/{congodb → tsmdb}/server/handlers/AdminHandler.ts +116 -11
- package/ts/{congodb → tsmdb}/server/handlers/AggregateHandler.ts +1 -1
- package/ts/{congodb → tsmdb}/server/handlers/DeleteHandler.ts +18 -3
- package/ts/{congodb → tsmdb}/server/handlers/FindHandler.ts +43 -14
- package/ts/{congodb → tsmdb}/server/handlers/HelloHandler.ts +1 -1
- package/ts/{congodb → tsmdb}/server/handlers/IndexHandler.ts +1 -1
- package/ts/{congodb → tsmdb}/server/handlers/InsertHandler.ts +7 -1
- package/ts/{congodb → tsmdb}/server/handlers/UpdateHandler.ts +34 -5
- package/ts/{congodb → tsmdb}/server/index.ts +2 -2
- package/ts/{congodb → tsmdb}/storage/FileStorageAdapter.ts +90 -7
- package/ts/{congodb → tsmdb}/storage/IStorageAdapter.ts +8 -2
- package/ts/{congodb → tsmdb}/storage/MemoryStorageAdapter.ts +14 -2
- package/ts/{congodb → tsmdb}/storage/OpLog.ts +1 -1
- package/ts/tsmdb/storage/WAL.ts +375 -0
- package/ts/{congodb → tsmdb}/types/interfaces.ts +3 -3
- package/ts/tsmdb/utils/checksum.ts +88 -0
- package/ts/tsmdb/utils/index.ts +1 -0
- package/dist_ts/congodb/congodb.plugins.js +0 -14
- package/dist_ts/congodb/engine/AggregationEngine.js +0 -189
- package/dist_ts/congodb/engine/IndexEngine.js +0 -376
- package/dist_ts/congodb/engine/QueryEngine.js +0 -271
- package/dist_ts/congodb/engine/TransactionEngine.js +0 -287
- package/dist_ts/congodb/engine/UpdateEngine.js +0 -461
- package/dist_ts/congodb/errors/CongoErrors.js +0 -155
- package/dist_ts/congodb/index.js +0 -26
- package/dist_ts/congodb/server/CommandRouter.d.ts +0 -51
- package/dist_ts/congodb/server/CommandRouter.js +0 -132
- package/dist_ts/congodb/server/CongoServer.js +0 -227
- package/dist_ts/congodb/server/WireProtocol.js +0 -298
- package/dist_ts/congodb/server/handlers/AdminHandler.js +0 -568
- package/dist_ts/congodb/server/handlers/AggregateHandler.js +0 -277
- package/dist_ts/congodb/server/handlers/DeleteHandler.js +0 -83
- package/dist_ts/congodb/server/handlers/FindHandler.js +0 -261
- package/dist_ts/congodb/server/handlers/IndexHandler.js +0 -183
- package/dist_ts/congodb/server/handlers/InsertHandler.js +0 -76
- package/dist_ts/congodb/server/handlers/UpdateHandler.js +0 -270
- package/dist_ts/congodb/server/handlers/index.js +0 -10
- package/dist_ts/congodb/server/index.js +0 -7
- package/dist_ts/congodb/storage/FileStorageAdapter.js +0 -396
- package/dist_ts/congodb/storage/MemoryStorageAdapter.js +0 -367
- package/dist_ts/congodb/storage/OpLog.js +0 -221
- package/ts/congodb/engine/IndexEngine.ts +0 -479
- /package/dist_ts/{congodb → tsmdb}/engine/AggregationEngine.d.ts +0 -0
- /package/dist_ts/{congodb → tsmdb}/engine/QueryEngine.d.ts +0 -0
- /package/dist_ts/{congodb → tsmdb}/engine/UpdateEngine.d.ts +0 -0
- /package/dist_ts/{congodb → tsmdb}/server/handlers/index.d.ts +0 -0
- /package/dist_ts/{congodb/congodb.plugins.d.ts → tsmdb/tsmdb.plugins.d.ts} +0 -0
- /package/ts/{congodb → tsmdb}/server/handlers/index.ts +0 -0
- /package/ts/{congodb/congodb.plugins.ts → tsmdb/tsmdb.plugins.ts} +0 -0
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import * as plugins from '../
|
|
1
|
+
import * as plugins from '../tsmdb.plugins.js';
|
|
2
2
|
import type { IStorageAdapter } from '../storage/IStorageAdapter.js';
|
|
3
3
|
import type { Document, IStoredDocument, ITransactionOptions } from '../types/interfaces.js';
|
|
4
|
-
import {
|
|
4
|
+
import { TsmdbTransactionError, TsmdbWriteConflictError } from '../errors/TsmdbErrors.js';
|
|
5
5
|
|
|
6
6
|
/**
|
|
7
7
|
* Transaction state
|
|
@@ -70,7 +70,7 @@ export class TransactionEngine {
|
|
|
70
70
|
async getSnapshot(txnId: string, dbName: string, collName: string): Promise<IStoredDocument[]> {
|
|
71
71
|
const txn = this.transactions.get(txnId);
|
|
72
72
|
if (!txn || txn.status !== 'active') {
|
|
73
|
-
throw new
|
|
73
|
+
throw new TsmdbTransactionError('Transaction is not active');
|
|
74
74
|
}
|
|
75
75
|
|
|
76
76
|
const ns = `${dbName}.${collName}`;
|
|
@@ -148,7 +148,7 @@ export class TransactionEngine {
|
|
|
148
148
|
recordInsert(txnId: string, dbName: string, collName: string, doc: IStoredDocument): void {
|
|
149
149
|
const txn = this.transactions.get(txnId);
|
|
150
150
|
if (!txn || txn.status !== 'active') {
|
|
151
|
-
throw new
|
|
151
|
+
throw new TsmdbTransactionError('Transaction is not active');
|
|
152
152
|
}
|
|
153
153
|
|
|
154
154
|
const ns = `${dbName}.${collName}`;
|
|
@@ -174,7 +174,7 @@ export class TransactionEngine {
|
|
|
174
174
|
): void {
|
|
175
175
|
const txn = this.transactions.get(txnId);
|
|
176
176
|
if (!txn || txn.status !== 'active') {
|
|
177
|
-
throw new
|
|
177
|
+
throw new TsmdbTransactionError('Transaction is not active');
|
|
178
178
|
}
|
|
179
179
|
|
|
180
180
|
const ns = `${dbName}.${collName}`;
|
|
@@ -203,7 +203,7 @@ export class TransactionEngine {
|
|
|
203
203
|
recordDelete(txnId: string, dbName: string, collName: string, doc: IStoredDocument): void {
|
|
204
204
|
const txn = this.transactions.get(txnId);
|
|
205
205
|
if (!txn || txn.status !== 'active') {
|
|
206
|
-
throw new
|
|
206
|
+
throw new TsmdbTransactionError('Transaction is not active');
|
|
207
207
|
}
|
|
208
208
|
|
|
209
209
|
const ns = `${dbName}.${collName}`;
|
|
@@ -231,10 +231,10 @@ export class TransactionEngine {
|
|
|
231
231
|
async commitTransaction(txnId: string): Promise<void> {
|
|
232
232
|
const txn = this.transactions.get(txnId);
|
|
233
233
|
if (!txn) {
|
|
234
|
-
throw new
|
|
234
|
+
throw new TsmdbTransactionError('Transaction not found');
|
|
235
235
|
}
|
|
236
236
|
if (txn.status !== 'active') {
|
|
237
|
-
throw new
|
|
237
|
+
throw new TsmdbTransactionError(`Cannot commit transaction in state: ${txn.status}`);
|
|
238
238
|
}
|
|
239
239
|
|
|
240
240
|
// Check for write conflicts
|
|
@@ -245,7 +245,7 @@ export class TransactionEngine {
|
|
|
245
245
|
const hasConflicts = await this.storage.hasConflicts(dbName, collName, ids, txn.startTime);
|
|
246
246
|
if (hasConflicts) {
|
|
247
247
|
txn.status = 'aborted';
|
|
248
|
-
throw new
|
|
248
|
+
throw new TsmdbWriteConflictError();
|
|
249
249
|
}
|
|
250
250
|
}
|
|
251
251
|
|
|
@@ -281,7 +281,7 @@ export class TransactionEngine {
|
|
|
281
281
|
async abortTransaction(txnId: string): Promise<void> {
|
|
282
282
|
const txn = this.transactions.get(txnId);
|
|
283
283
|
if (!txn) {
|
|
284
|
-
throw new
|
|
284
|
+
throw new TsmdbTransactionError('Transaction not found');
|
|
285
285
|
}
|
|
286
286
|
if (txn.status !== 'active') {
|
|
287
287
|
// Already committed or aborted, just return
|
|
@@ -336,7 +336,7 @@ export class TransactionEngine {
|
|
|
336
336
|
await this.abortTransaction(txnId);
|
|
337
337
|
this.endTransaction(txnId);
|
|
338
338
|
|
|
339
|
-
if (error instanceof
|
|
339
|
+
if (error instanceof TsmdbWriteConflictError && attempt < maxRetries - 1) {
|
|
340
340
|
// Retry on write conflict
|
|
341
341
|
lastError = error;
|
|
342
342
|
continue;
|
|
@@ -346,6 +346,6 @@ export class TransactionEngine {
|
|
|
346
346
|
}
|
|
347
347
|
}
|
|
348
348
|
|
|
349
|
-
throw lastError || new
|
|
349
|
+
throw lastError || new TsmdbTransactionError('Transaction failed after max retries');
|
|
350
350
|
}
|
|
351
351
|
}
|
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Base error class for all
|
|
2
|
+
* Base error class for all TsmDB errors
|
|
3
3
|
* Mirrors MongoDB driver error hierarchy
|
|
4
4
|
*/
|
|
5
|
-
export class
|
|
5
|
+
export class TsmdbError extends Error {
|
|
6
6
|
public code?: number;
|
|
7
7
|
public codeName?: string;
|
|
8
8
|
|
|
9
9
|
constructor(message: string, code?: number, codeName?: string) {
|
|
10
10
|
super(message);
|
|
11
|
-
this.name = '
|
|
11
|
+
this.name = 'TsmdbError';
|
|
12
12
|
this.code = code;
|
|
13
13
|
this.codeName = codeName;
|
|
14
14
|
Object.setPrototypeOf(this, new.target.prototype);
|
|
@@ -18,33 +18,33 @@ export class CongoError extends Error {
|
|
|
18
18
|
/**
|
|
19
19
|
* Error thrown during connection issues
|
|
20
20
|
*/
|
|
21
|
-
export class
|
|
21
|
+
export class TsmdbConnectionError extends TsmdbError {
|
|
22
22
|
constructor(message: string) {
|
|
23
23
|
super(message);
|
|
24
|
-
this.name = '
|
|
24
|
+
this.name = 'TsmdbConnectionError';
|
|
25
25
|
}
|
|
26
26
|
}
|
|
27
27
|
|
|
28
28
|
/**
|
|
29
29
|
* Error thrown when an operation times out
|
|
30
30
|
*/
|
|
31
|
-
export class
|
|
31
|
+
export class TsmdbTimeoutError extends TsmdbError {
|
|
32
32
|
constructor(message: string) {
|
|
33
33
|
super(message, 50, 'MaxTimeMSExpired');
|
|
34
|
-
this.name = '
|
|
34
|
+
this.name = 'TsmdbTimeoutError';
|
|
35
35
|
}
|
|
36
36
|
}
|
|
37
37
|
|
|
38
38
|
/**
|
|
39
39
|
* Error thrown during write operations
|
|
40
40
|
*/
|
|
41
|
-
export class
|
|
41
|
+
export class TsmdbWriteError extends TsmdbError {
|
|
42
42
|
public writeErrors?: IWriteError[];
|
|
43
43
|
public result?: any;
|
|
44
44
|
|
|
45
45
|
constructor(message: string, code?: number, writeErrors?: IWriteError[]) {
|
|
46
46
|
super(message, code);
|
|
47
|
-
this.name = '
|
|
47
|
+
this.name = 'TsmdbWriteError';
|
|
48
48
|
this.writeErrors = writeErrors;
|
|
49
49
|
}
|
|
50
50
|
}
|
|
@@ -52,13 +52,13 @@ export class CongoWriteError extends CongoError {
|
|
|
52
52
|
/**
|
|
53
53
|
* Error thrown for duplicate key violations
|
|
54
54
|
*/
|
|
55
|
-
export class
|
|
55
|
+
export class TsmdbDuplicateKeyError extends TsmdbWriteError {
|
|
56
56
|
public keyPattern?: Record<string, 1>;
|
|
57
57
|
public keyValue?: Record<string, any>;
|
|
58
58
|
|
|
59
59
|
constructor(message: string, keyPattern?: Record<string, 1>, keyValue?: Record<string, any>) {
|
|
60
60
|
super(message, 11000);
|
|
61
|
-
this.name = '
|
|
61
|
+
this.name = 'TsmdbDuplicateKeyError';
|
|
62
62
|
this.codeName = 'DuplicateKey';
|
|
63
63
|
this.keyPattern = keyPattern;
|
|
64
64
|
this.keyValue = keyValue;
|
|
@@ -68,13 +68,13 @@ export class CongoDuplicateKeyError extends CongoWriteError {
|
|
|
68
68
|
/**
|
|
69
69
|
* Error thrown for bulk write failures
|
|
70
70
|
*/
|
|
71
|
-
export class
|
|
71
|
+
export class TsmdbBulkWriteError extends TsmdbError {
|
|
72
72
|
public writeErrors: IWriteError[];
|
|
73
73
|
public result: any;
|
|
74
74
|
|
|
75
75
|
constructor(message: string, writeErrors: IWriteError[], result: any) {
|
|
76
76
|
super(message, 65);
|
|
77
|
-
this.name = '
|
|
77
|
+
this.name = 'TsmdbBulkWriteError';
|
|
78
78
|
this.writeErrors = writeErrors;
|
|
79
79
|
this.result = result;
|
|
80
80
|
}
|
|
@@ -83,20 +83,20 @@ export class CongoBulkWriteError extends CongoError {
|
|
|
83
83
|
/**
|
|
84
84
|
* Error thrown during transaction operations
|
|
85
85
|
*/
|
|
86
|
-
export class
|
|
86
|
+
export class TsmdbTransactionError extends TsmdbError {
|
|
87
87
|
constructor(message: string, code?: number) {
|
|
88
88
|
super(message, code);
|
|
89
|
-
this.name = '
|
|
89
|
+
this.name = 'TsmdbTransactionError';
|
|
90
90
|
}
|
|
91
91
|
}
|
|
92
92
|
|
|
93
93
|
/**
|
|
94
94
|
* Error thrown when a transaction is aborted due to conflict
|
|
95
95
|
*/
|
|
96
|
-
export class
|
|
96
|
+
export class TsmdbWriteConflictError extends TsmdbTransactionError {
|
|
97
97
|
constructor(message: string = 'Write conflict during transaction') {
|
|
98
98
|
super(message, 112);
|
|
99
|
-
this.name = '
|
|
99
|
+
this.name = 'TsmdbWriteConflictError';
|
|
100
100
|
this.codeName = 'WriteConflict';
|
|
101
101
|
}
|
|
102
102
|
}
|
|
@@ -104,20 +104,20 @@ export class CongoWriteConflictError extends CongoTransactionError {
|
|
|
104
104
|
/**
|
|
105
105
|
* Error thrown for invalid arguments
|
|
106
106
|
*/
|
|
107
|
-
export class
|
|
107
|
+
export class TsmdbArgumentError extends TsmdbError {
|
|
108
108
|
constructor(message: string) {
|
|
109
109
|
super(message);
|
|
110
|
-
this.name = '
|
|
110
|
+
this.name = 'TsmdbArgumentError';
|
|
111
111
|
}
|
|
112
112
|
}
|
|
113
113
|
|
|
114
114
|
/**
|
|
115
115
|
* Error thrown when an operation is not supported
|
|
116
116
|
*/
|
|
117
|
-
export class
|
|
117
|
+
export class TsmdbNotSupportedError extends TsmdbError {
|
|
118
118
|
constructor(message: string) {
|
|
119
119
|
super(message, 115);
|
|
120
|
-
this.name = '
|
|
120
|
+
this.name = 'TsmdbNotSupportedError';
|
|
121
121
|
this.codeName = 'CommandNotSupported';
|
|
122
122
|
}
|
|
123
123
|
}
|
|
@@ -125,20 +125,20 @@ export class CongoNotSupportedError extends CongoError {
|
|
|
125
125
|
/**
|
|
126
126
|
* Error thrown when cursor is exhausted or closed
|
|
127
127
|
*/
|
|
128
|
-
export class
|
|
128
|
+
export class TsmdbCursorError extends TsmdbError {
|
|
129
129
|
constructor(message: string) {
|
|
130
130
|
super(message);
|
|
131
|
-
this.name = '
|
|
131
|
+
this.name = 'TsmdbCursorError';
|
|
132
132
|
}
|
|
133
133
|
}
|
|
134
134
|
|
|
135
135
|
/**
|
|
136
136
|
* Error thrown when a namespace (database.collection) is invalid
|
|
137
137
|
*/
|
|
138
|
-
export class
|
|
138
|
+
export class TsmdbNamespaceError extends TsmdbError {
|
|
139
139
|
constructor(message: string) {
|
|
140
140
|
super(message, 73);
|
|
141
|
-
this.name = '
|
|
141
|
+
this.name = 'TsmdbNamespaceError';
|
|
142
142
|
this.codeName = 'InvalidNamespace';
|
|
143
143
|
}
|
|
144
144
|
}
|
|
@@ -146,10 +146,10 @@ export class CongoNamespaceError extends CongoError {
|
|
|
146
146
|
/**
|
|
147
147
|
* Error thrown when an index operation fails
|
|
148
148
|
*/
|
|
149
|
-
export class
|
|
149
|
+
export class TsmdbIndexError extends TsmdbError {
|
|
150
150
|
constructor(message: string, code?: number) {
|
|
151
151
|
super(message, code || 86);
|
|
152
|
-
this.name = '
|
|
152
|
+
this.name = 'TsmdbIndexError';
|
|
153
153
|
}
|
|
154
154
|
}
|
|
155
155
|
|
|
@@ -164,18 +164,18 @@ export interface IWriteError {
|
|
|
164
164
|
}
|
|
165
165
|
|
|
166
166
|
/**
|
|
167
|
-
* Convert any error to a
|
|
167
|
+
* Convert any error to a TsmdbError
|
|
168
168
|
*/
|
|
169
|
-
export function
|
|
170
|
-
if (error instanceof
|
|
169
|
+
export function toTsmdbError(error: any): TsmdbError {
|
|
170
|
+
if (error instanceof TsmdbError) {
|
|
171
171
|
return error;
|
|
172
172
|
}
|
|
173
|
-
const
|
|
173
|
+
const tsmdbError = new TsmdbError(error.message || String(error));
|
|
174
174
|
if (error.code) {
|
|
175
|
-
|
|
175
|
+
tsmdbError.code = error.code;
|
|
176
176
|
}
|
|
177
177
|
if (error.codeName) {
|
|
178
|
-
|
|
178
|
+
tsmdbError.codeName = error.codeName;
|
|
179
179
|
}
|
|
180
|
-
return
|
|
180
|
+
return tsmdbError;
|
|
181
181
|
}
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
//
|
|
2
|
-
// Use the official MongoDB driver to connect to
|
|
1
|
+
// TsmDB - MongoDB Wire Protocol compatible in-memory database server
|
|
2
|
+
// Use the official MongoDB driver to connect to TsmdbServer
|
|
3
3
|
|
|
4
4
|
// Re-export plugins for external use
|
|
5
|
-
import * as plugins from './
|
|
5
|
+
import * as plugins from './tsmdb.plugins.js';
|
|
6
6
|
export { plugins };
|
|
7
7
|
|
|
8
8
|
// Export BSON types for convenience
|
|
@@ -12,13 +12,15 @@ export { ObjectId, Binary, Timestamp, Long, Decimal128, UUID } from 'bson';
|
|
|
12
12
|
export * from './types/interfaces.js';
|
|
13
13
|
|
|
14
14
|
// Export errors
|
|
15
|
-
export * from './errors/
|
|
15
|
+
export * from './errors/TsmdbErrors.js';
|
|
16
16
|
|
|
17
17
|
// Export storage adapters
|
|
18
18
|
export type { IStorageAdapter } from './storage/IStorageAdapter.js';
|
|
19
19
|
export { MemoryStorageAdapter } from './storage/MemoryStorageAdapter.js';
|
|
20
20
|
export { FileStorageAdapter } from './storage/FileStorageAdapter.js';
|
|
21
21
|
export { OpLog } from './storage/OpLog.js';
|
|
22
|
+
export { WAL } from './storage/WAL.js';
|
|
23
|
+
export type { IWalEntry, TWalOperation } from './storage/WAL.js';
|
|
22
24
|
|
|
23
25
|
// Export engines
|
|
24
26
|
export { QueryEngine } from './engine/QueryEngine.js';
|
|
@@ -26,12 +28,19 @@ export { UpdateEngine } from './engine/UpdateEngine.js';
|
|
|
26
28
|
export { AggregationEngine } from './engine/AggregationEngine.js';
|
|
27
29
|
export { IndexEngine } from './engine/IndexEngine.js';
|
|
28
30
|
export { TransactionEngine } from './engine/TransactionEngine.js';
|
|
31
|
+
export { QueryPlanner } from './engine/QueryPlanner.js';
|
|
32
|
+
export type { IQueryPlan, TQueryPlanType } from './engine/QueryPlanner.js';
|
|
33
|
+
export { SessionEngine } from './engine/SessionEngine.js';
|
|
34
|
+
export type { ISession, ISessionEngineOptions } from './engine/SessionEngine.js';
|
|
29
35
|
|
|
30
|
-
// Export server (the main entry point for using
|
|
31
|
-
export {
|
|
32
|
-
export type {
|
|
36
|
+
// Export server (the main entry point for using TsmDB)
|
|
37
|
+
export { TsmdbServer } from './server/TsmdbServer.js';
|
|
38
|
+
export type { ITsmdbServerOptions } from './server/TsmdbServer.js';
|
|
33
39
|
|
|
34
40
|
// Export wire protocol utilities (for advanced usage)
|
|
35
41
|
export { WireProtocol } from './server/WireProtocol.js';
|
|
36
42
|
export { CommandRouter } from './server/CommandRouter.js';
|
|
37
43
|
export type { ICommandHandler, IHandlerContext, ICursorState } from './server/CommandRouter.js';
|
|
44
|
+
|
|
45
|
+
// Export utilities
|
|
46
|
+
export * from './utils/checksum.js';
|
|
@@ -1,7 +1,10 @@
|
|
|
1
|
-
import * as plugins from '../
|
|
1
|
+
import * as plugins from '../tsmdb.plugins.js';
|
|
2
2
|
import type { IStorageAdapter } from '../storage/IStorageAdapter.js';
|
|
3
3
|
import type { IParsedCommand } from './WireProtocol.js';
|
|
4
|
-
import type {
|
|
4
|
+
import type { TsmdbServer } from './TsmdbServer.js';
|
|
5
|
+
import { IndexEngine } from '../engine/IndexEngine.js';
|
|
6
|
+
import { TransactionEngine } from '../engine/TransactionEngine.js';
|
|
7
|
+
import { SessionEngine } from '../engine/SessionEngine.js';
|
|
5
8
|
|
|
6
9
|
// Import handlers
|
|
7
10
|
import { HelloHandler } from './handlers/HelloHandler.js';
|
|
@@ -18,10 +21,20 @@ import { AdminHandler } from './handlers/AdminHandler.js';
|
|
|
18
21
|
*/
|
|
19
22
|
export interface IHandlerContext {
|
|
20
23
|
storage: IStorageAdapter;
|
|
21
|
-
server:
|
|
24
|
+
server: TsmdbServer;
|
|
22
25
|
database: string;
|
|
23
26
|
command: plugins.bson.Document;
|
|
24
27
|
documentSequences?: Map<string, plugins.bson.Document[]>;
|
|
28
|
+
/** Get or create an IndexEngine for a collection */
|
|
29
|
+
getIndexEngine: (collName: string) => IndexEngine;
|
|
30
|
+
/** Transaction engine instance */
|
|
31
|
+
transactionEngine: TransactionEngine;
|
|
32
|
+
/** Current transaction ID (if in a transaction) */
|
|
33
|
+
txnId?: string;
|
|
34
|
+
/** Session ID (from lsid) */
|
|
35
|
+
sessionId?: string;
|
|
36
|
+
/** Session engine instance */
|
|
37
|
+
sessionEngine: SessionEngine;
|
|
25
38
|
}
|
|
26
39
|
|
|
27
40
|
/**
|
|
@@ -36,19 +49,61 @@ export interface ICommandHandler {
|
|
|
36
49
|
*/
|
|
37
50
|
export class CommandRouter {
|
|
38
51
|
private storage: IStorageAdapter;
|
|
39
|
-
private server:
|
|
52
|
+
private server: TsmdbServer;
|
|
40
53
|
private handlers: Map<string, ICommandHandler> = new Map();
|
|
41
54
|
|
|
42
55
|
// Cursor state for getMore operations
|
|
43
56
|
private cursors: Map<bigint, ICursorState> = new Map();
|
|
44
57
|
private cursorIdCounter: bigint = BigInt(1);
|
|
45
58
|
|
|
46
|
-
|
|
59
|
+
// Index engine cache: db.collection -> IndexEngine
|
|
60
|
+
private indexEngines: Map<string, IndexEngine> = new Map();
|
|
61
|
+
|
|
62
|
+
// Transaction engine (shared across all handlers)
|
|
63
|
+
private transactionEngine: TransactionEngine;
|
|
64
|
+
|
|
65
|
+
// Session engine (shared across all handlers)
|
|
66
|
+
private sessionEngine: SessionEngine;
|
|
67
|
+
|
|
68
|
+
constructor(storage: IStorageAdapter, server: TsmdbServer) {
|
|
47
69
|
this.storage = storage;
|
|
48
70
|
this.server = server;
|
|
71
|
+
this.transactionEngine = new TransactionEngine(storage);
|
|
72
|
+
this.sessionEngine = new SessionEngine();
|
|
73
|
+
// Link session engine to transaction engine for auto-abort on session expiry
|
|
74
|
+
this.sessionEngine.setTransactionEngine(this.transactionEngine);
|
|
49
75
|
this.registerHandlers();
|
|
50
76
|
}
|
|
51
77
|
|
|
78
|
+
/**
|
|
79
|
+
* Get or create an IndexEngine for a database.collection
|
|
80
|
+
*/
|
|
81
|
+
getIndexEngine(dbName: string, collName: string): IndexEngine {
|
|
82
|
+
const key = `${dbName}.${collName}`;
|
|
83
|
+
let engine = this.indexEngines.get(key);
|
|
84
|
+
if (!engine) {
|
|
85
|
+
engine = new IndexEngine(dbName, collName, this.storage);
|
|
86
|
+
this.indexEngines.set(key, engine);
|
|
87
|
+
}
|
|
88
|
+
return engine;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Clear index engine cache for a collection (used when collection is dropped)
|
|
93
|
+
*/
|
|
94
|
+
clearIndexEngineCache(dbName: string, collName?: string): void {
|
|
95
|
+
if (collName) {
|
|
96
|
+
this.indexEngines.delete(`${dbName}.${collName}`);
|
|
97
|
+
} else {
|
|
98
|
+
// Clear all engines for the database
|
|
99
|
+
for (const key of this.indexEngines.keys()) {
|
|
100
|
+
if (key.startsWith(`${dbName}.`)) {
|
|
101
|
+
this.indexEngines.delete(key);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
52
107
|
/**
|
|
53
108
|
* Register all command handlers
|
|
54
109
|
*/
|
|
@@ -120,6 +175,29 @@ export class CommandRouter {
|
|
|
120
175
|
async route(parsedCommand: IParsedCommand): Promise<plugins.bson.Document> {
|
|
121
176
|
const { commandName, command, database, documentSequences } = parsedCommand;
|
|
122
177
|
|
|
178
|
+
// Extract session ID from lsid using SessionEngine helper
|
|
179
|
+
let sessionId = SessionEngine.extractSessionId(command.lsid);
|
|
180
|
+
let txnId: string | undefined;
|
|
181
|
+
|
|
182
|
+
// If we have a session ID, register/touch the session
|
|
183
|
+
if (sessionId) {
|
|
184
|
+
this.sessionEngine.getOrCreateSession(sessionId);
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
// Check if this starts a new transaction
|
|
188
|
+
if (command.startTransaction && sessionId) {
|
|
189
|
+
txnId = this.transactionEngine.startTransaction(sessionId);
|
|
190
|
+
this.sessionEngine.startTransaction(sessionId, txnId, command.txnNumber);
|
|
191
|
+
} else if (sessionId && this.sessionEngine.isInTransaction(sessionId)) {
|
|
192
|
+
// Continue existing transaction
|
|
193
|
+
txnId = this.sessionEngine.getTransactionId(sessionId);
|
|
194
|
+
// Verify transaction is still active
|
|
195
|
+
if (txnId && !this.transactionEngine.isActive(txnId)) {
|
|
196
|
+
this.sessionEngine.endTransaction(sessionId);
|
|
197
|
+
txnId = undefined;
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
|
|
123
201
|
// Create handler context
|
|
124
202
|
const context: IHandlerContext = {
|
|
125
203
|
storage: this.storage,
|
|
@@ -127,6 +205,11 @@ export class CommandRouter {
|
|
|
127
205
|
database,
|
|
128
206
|
command,
|
|
129
207
|
documentSequences,
|
|
208
|
+
getIndexEngine: (collName: string) => this.getIndexEngine(database, collName),
|
|
209
|
+
transactionEngine: this.transactionEngine,
|
|
210
|
+
sessionEngine: this.sessionEngine,
|
|
211
|
+
txnId,
|
|
212
|
+
sessionId,
|
|
130
213
|
};
|
|
131
214
|
|
|
132
215
|
// Find handler
|
|
@@ -164,6 +247,32 @@ export class CommandRouter {
|
|
|
164
247
|
};
|
|
165
248
|
}
|
|
166
249
|
}
|
|
250
|
+
|
|
251
|
+
/**
|
|
252
|
+
* Close the command router and cleanup resources
|
|
253
|
+
*/
|
|
254
|
+
close(): void {
|
|
255
|
+
// Close session engine (stops cleanup interval, clears sessions)
|
|
256
|
+
this.sessionEngine.close();
|
|
257
|
+
// Clear cursors
|
|
258
|
+
this.cursors.clear();
|
|
259
|
+
// Clear index engine cache
|
|
260
|
+
this.indexEngines.clear();
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
/**
|
|
264
|
+
* Get session engine (for administrative purposes)
|
|
265
|
+
*/
|
|
266
|
+
getSessionEngine(): SessionEngine {
|
|
267
|
+
return this.sessionEngine;
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
/**
|
|
271
|
+
* Get transaction engine (for administrative purposes)
|
|
272
|
+
*/
|
|
273
|
+
getTransactionEngine(): TransactionEngine {
|
|
274
|
+
return this.transactionEngine;
|
|
275
|
+
}
|
|
167
276
|
}
|
|
168
277
|
|
|
169
278
|
/**
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import * as net from 'net';
|
|
2
|
-
import * as plugins from '../
|
|
2
|
+
import * as plugins from '../tsmdb.plugins.js';
|
|
3
3
|
import { WireProtocol, OP_QUERY } from './WireProtocol.js';
|
|
4
4
|
import { CommandRouter } from './CommandRouter.js';
|
|
5
5
|
import { MemoryStorageAdapter } from '../storage/MemoryStorageAdapter.js';
|
|
@@ -9,7 +9,7 @@ import type { IStorageAdapter } from '../storage/IStorageAdapter.js';
|
|
|
9
9
|
/**
|
|
10
10
|
* Server configuration options
|
|
11
11
|
*/
|
|
12
|
-
export interface
|
|
12
|
+
export interface ITsmdbServerOptions {
|
|
13
13
|
/** Port to listen on (default: 27017) */
|
|
14
14
|
port?: number;
|
|
15
15
|
/** Host to bind to (default: 127.0.0.1) */
|
|
@@ -36,25 +36,25 @@ interface IConnectionState {
|
|
|
36
36
|
}
|
|
37
37
|
|
|
38
38
|
/**
|
|
39
|
-
*
|
|
39
|
+
* TsmdbServer - MongoDB Wire Protocol compatible server
|
|
40
40
|
*
|
|
41
41
|
* This server implements the MongoDB wire protocol (OP_MSG) to allow
|
|
42
42
|
* official MongoDB drivers to connect and perform operations.
|
|
43
43
|
*
|
|
44
44
|
* @example
|
|
45
45
|
* ```typescript
|
|
46
|
-
* import {
|
|
46
|
+
* import { TsmdbServer } from '@push.rocks/smartmongo/tsmdb';
|
|
47
47
|
* import { MongoClient } from 'mongodb';
|
|
48
48
|
*
|
|
49
|
-
* const server = new
|
|
49
|
+
* const server = new TsmdbServer({ port: 27017 });
|
|
50
50
|
* await server.start();
|
|
51
51
|
*
|
|
52
52
|
* const client = new MongoClient('mongodb://127.0.0.1:27017');
|
|
53
53
|
* await client.connect();
|
|
54
54
|
* ```
|
|
55
55
|
*/
|
|
56
|
-
export class
|
|
57
|
-
private options: Required<
|
|
56
|
+
export class TsmdbServer {
|
|
57
|
+
private options: Required<ITsmdbServerOptions>;
|
|
58
58
|
private server: net.Server | null = null;
|
|
59
59
|
private storage: IStorageAdapter;
|
|
60
60
|
private commandRouter: CommandRouter;
|
|
@@ -63,7 +63,7 @@ export class CongoServer {
|
|
|
63
63
|
private isRunning = false;
|
|
64
64
|
private startTime: Date = new Date();
|
|
65
65
|
|
|
66
|
-
constructor(options:
|
|
66
|
+
constructor(options: ITsmdbServerOptions = {}) {
|
|
67
67
|
this.options = {
|
|
68
68
|
port: options.port ?? 27017,
|
|
69
69
|
host: options.host ?? '127.0.0.1',
|
|
@@ -154,6 +154,9 @@ export class CongoServer {
|
|
|
154
154
|
}
|
|
155
155
|
this.connections.clear();
|
|
156
156
|
|
|
157
|
+
// Close command router (cleans up session engine, cursors, etc.)
|
|
158
|
+
this.commandRouter.close();
|
|
159
|
+
|
|
157
160
|
// Close storage
|
|
158
161
|
await this.storage.close();
|
|
159
162
|
|