tero 0.0.4 → 0.0.5

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.
@@ -23,10 +23,12 @@ export declare class WriteAheadLog {
23
23
  private flushBuffer;
24
24
  private checkLogRotation;
25
25
  private rotateLog;
26
+ private cleanupOldArchives;
26
27
  getLogEntries(fromLSN?: number): LogEntry[];
27
28
  forceFlush(): void;
28
29
  getCurrentLSN(): number;
29
- clearCommittedTransaction(transactionId: string): void;
30
+ clearTransaction(transactionId: string): void;
31
+ truncateLog(): void;
30
32
  }
31
33
  export declare class LockManager {
32
34
  private locks;
@@ -1,4 +1,4 @@
1
- import { existsSync, readFileSync, writeFileSync, unlinkSync, mkdirSync, statSync } from "fs";
1
+ import { existsSync, readFileSync, writeFileSync, unlinkSync, mkdirSync, readdirSync, statSync } from "fs";
2
2
  import { join, dirname } from "path";
3
3
  import { randomUUID } from "crypto";
4
4
  import { createHash } from "crypto";
@@ -8,7 +8,7 @@ export class WriteAheadLog {
8
8
  currentLSN = 0;
9
9
  logBuffer = [];
10
10
  BUFFER_SIZE = 100;
11
- LOG_FILE_SIZE_LIMIT = 10 * 1024 * 1024; // 10MB
11
+ LOG_FILE_SIZE_LIMIT = 1 * 1024 * 1024; // 10MB
12
12
  constructor(dbPath) {
13
13
  this.logPath = join(dbPath, '.wal');
14
14
  this.initializeWAL();
@@ -115,6 +115,29 @@ export class WriteAheadLog {
115
115
  // Start new log with checkpoint
116
116
  writeFileSync(this.logPath, '');
117
117
  this.writeLog({ operation: 'CHECKPOINT', transactionId: 'SYSTEM' });
118
+ // Clean up old archives to prevent indefinite growth
119
+ this.cleanupOldArchives(3);
120
+ }
121
+ catch (error) {
122
+ // Silent failure for production
123
+ }
124
+ }
125
+ cleanupOldArchives(keepCount) {
126
+ try {
127
+ const dir = dirname(this.logPath);
128
+ if (!existsSync(dir))
129
+ return;
130
+ const files = readdirSync(dir);
131
+ const archiveFiles = files
132
+ .filter(f => f.startsWith('.wal.'))
133
+ .map(f => join(dir, f))
134
+ .sort();
135
+ while (archiveFiles.length > keepCount) {
136
+ const oldestFile = archiveFiles.shift();
137
+ if (oldestFile && existsSync(oldestFile)) {
138
+ unlinkSync(oldestFile);
139
+ }
140
+ }
118
141
  }
119
142
  catch (error) {
120
143
  // Silent failure for production
@@ -158,7 +181,7 @@ export class WriteAheadLog {
158
181
  getCurrentLSN() {
159
182
  return this.currentLSN - 1;
160
183
  }
161
- clearCommittedTransaction(transactionId) {
184
+ clearTransaction(transactionId) {
162
185
  try {
163
186
  // First, flush any pending buffer entries to disk
164
187
  this.flushBuffer();
@@ -168,9 +191,9 @@ export class WriteAheadLog {
168
191
  // Read current log content
169
192
  const logContent = readFileSync(this.logPath, 'utf-8');
170
193
  const lines = logContent.trim().split('\n').filter(line => line.trim());
171
- // Filter out entries for the committed transaction
194
+ // Filter out entries for the completed transaction
172
195
  const filteredLines = [];
173
- let transactionCompleted = false;
196
+ let transactionProcessed = false;
174
197
  for (const line of lines) {
175
198
  try {
176
199
  const entry = JSON.parse(line);
@@ -179,13 +202,8 @@ export class WriteAheadLog {
179
202
  filteredLines.push(line);
180
203
  }
181
204
  else {
182
- // For the committed transaction, only keep the COMMIT entry as a marker
183
- // This preserves the fact that the transaction was committed for recovery purposes
184
- if (entry.operation === 'COMMIT') {
185
- filteredLines.push(line);
186
- transactionCompleted = true;
187
- }
188
- // Skip BEGIN, WRITE, DELETE entries for committed transactions
205
+ // Remove ALL entries for the completed transaction to compact log
206
+ transactionProcessed = true;
189
207
  }
190
208
  }
191
209
  catch (error) {
@@ -194,14 +212,24 @@ export class WriteAheadLog {
194
212
  }
195
213
  }
196
214
  // Only rewrite the log if we actually found and processed the transaction
197
- if (transactionCompleted) {
215
+ if (transactionProcessed) {
198
216
  const newContent = filteredLines.length > 0 ? filteredLines.join('\n') + '\n' : '';
199
217
  writeFileSync(this.logPath, newContent);
200
218
  }
201
219
  }
202
220
  catch (error) {
203
- // Silent failure for production - WAL cleanup is an optimization, not critical
204
- // The system will still work correctly even if cleanup fails
221
+ // Silent failure for production - WAL cleanup is an optimization
222
+ }
223
+ }
224
+ truncateLog() {
225
+ try {
226
+ this.flushBuffer();
227
+ writeFileSync(this.logPath, '');
228
+ // Clean up all archives since the log is truncated and no active transactions exist
229
+ this.cleanupOldArchives(0);
230
+ }
231
+ catch (error) {
232
+ // Silent failure
205
233
  }
206
234
  }
207
235
  }
@@ -615,8 +643,12 @@ export class ACIDStorageEngine {
615
643
  }
616
644
  // Update transaction status
617
645
  transaction.status = 'committed';
618
- // Clear WAL entries for this committed transaction
619
- this.wal.clearCommittedTransaction(transactionId);
646
+ // Clear WAL entries for this completed transaction
647
+ this.wal.clearTransaction(transactionId);
648
+ // Truncate log completely if no more active transactions
649
+ if (this.getActiveTransactions().length === 0) {
650
+ this.wal.truncateLog();
651
+ }
620
652
  // Release all locks
621
653
  this.lockManager.releaseAllLocks(transactionId);
622
654
  }
@@ -639,6 +671,12 @@ export class ACIDStorageEngine {
639
671
  });
640
672
  // Update transaction status
641
673
  transaction.status = 'aborted';
674
+ // Clear WAL entries for this aborted transaction
675
+ this.wal.clearTransaction(transactionId);
676
+ // Truncate log completely if no more active transactions
677
+ if (this.getActiveTransactions().length === 0) {
678
+ this.wal.truncateLog();
679
+ }
642
680
  // Release all locks
643
681
  this.lockManager.releaseAllLocks(transactionId);
644
682
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tero",
3
- "version": "0.0.4",
3
+ "version": "0.0.5",
4
4
  "description": "Tero is a JSON database that provides ACID transactions, schema validation, automated cloud backup, and automatic cloud recovery",
5
5
  "main": "dist/index.js",
6
6
  "type": "module",
@@ -15,7 +15,7 @@
15
15
  },
16
16
  "homepage": "https://github.com/codedynasty-dev/tero.git",
17
17
  "scripts": {
18
- "build": "npm run test && tsc",
18
+ "build": "tsc && npm run test",
19
19
  "dev": "tsc --watch",
20
20
  "test": "node run-tests.js",
21
21
  "test:production": "node local_tests/production-test.js",