axiodb
Version:
The Pure JavaScript Alternative to SQLite. Embedded NoSQL database for Node.js with MongoDB-style queries, zero native dependencies, built-in InMemoryCache, and web GUI. Perfect for desktop apps, CLI tools, and embedded systems. No compilation, no platfor
418 lines • 19.4 kB
JavaScript
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
/* eslint-disable @typescript-eslint/no-explicit-any */
const outers_1 = require("outers");
const Keys_1 = require("../../config/Keys/Keys");
const FileManager_1 = __importDefault(require("../../engine/Filesystem/FileManager"));
const Converter_helper_1 = __importDefault(require("../../Helper/Converter.helper"));
const Crypto_helper_1 = require("../../Helper/Crypto.helper");
const response_helper_1 = __importDefault(require("../../Helper/response.helper"));
const memory_operation_1 = __importDefault(require("../../Memory/memory.operation"));
const LockManager_service_1 = __importDefault(require("./LockManager.service"));
const TransactionIndexManager_service_1 = __importDefault(require("./TransactionIndexManager.service"));
const TransactionRegistry_service_1 = __importDefault(require("./TransactionRegistry.service"));
const WriteAheadLog_service_1 = __importDefault(require("./WriteAheadLog.service"));
class Transaction {
constructor(collectionPath, isEncrypted = false, encryptionKey) {
this.operations = [];
this.timeoutMs = 30000;
this.lockedDocuments = [];
this.savepoints = new Map();
this.pendingWALEntries = [];
this.resolvedOperations = [];
this.transactionId = new outers_1.ClassBased.UniqueGenerator(15).RandomWord(true);
this.collectionPath = collectionPath;
this.isEncrypted = isEncrypted;
this.encryptionKey = encryptionKey;
this.startTime = Date.now();
this.WAL = new WriteAheadLog_service_1.default(collectionPath, this.transactionId, isEncrypted, encryptionKey);
this.LockManager = new LockManager_service_1.default(collectionPath);
this.Registry = new TransactionRegistry_service_1.default(collectionPath);
this.IndexManager = new TransactionIndexManager_service_1.default(collectionPath, this.transactionId, isEncrypted, encryptionKey);
this.ResponseHelper = new response_helper_1.default();
this.Converter = new Converter_helper_1.default();
this.FileManager = new FileManager_1.default();
if (this.isEncrypted && encryptionKey) {
this.cryptoInstance = new Crypto_helper_1.CryptoHelper(encryptionKey);
}
}
/**
* Creates a savepoint at the current state of the transaction.
* Allows partial rollback to this point using rollbackTo().
*
* @param name - Unique name for the savepoint
* @returns The Transaction instance for chaining
* @throws Error if savepoint name already exists
*/
savepoint(name) {
if (!name || typeof name !== 'string') {
throw new Error("Savepoint name must be a non-empty string");
}
if (this.savepoints.has(name)) {
throw new Error(`Savepoint '${name}' already exists`);
}
const sp = {
name,
operationIndex: this.operations.length,
timestamp: new Date().toISOString(),
lockedDocumentsSnapshot: [...this.lockedDocuments]
};
this.savepoints.set(name, sp);
return this;
}
/**
* Rolls back the transaction to a specific savepoint.
* All operations after the savepoint are discarded.
*
* @param name - Name of the savepoint to rollback to
* @returns The Transaction instance for chaining
* @throws Error if savepoint doesn't exist
*/
rollbackTo(name) {
const sp = this.savepoints.get(name);
if (!sp) {
throw new Error(`Savepoint '${name}' not found`);
}
// Truncate operations to savepoint
this.operations = this.operations.slice(0, sp.operationIndex);
// Remove savepoints created after this one
const savepointsToRemove = [];
for (const [spName, savepoint] of this.savepoints) {
if (savepoint.operationIndex > sp.operationIndex) {
savepointsToRemove.push(spName);
}
}
savepointsToRemove.forEach(spName => this.savepoints.delete(spName));
return this;
}
/**
* Releases a savepoint without rolling back.
* The savepoint is removed but operations remain.
*
* @param name - Name of the savepoint to release
* @returns The Transaction instance for chaining
*/
releaseSavepoint(name) {
if (!this.savepoints.has(name)) {
throw new Error(`Savepoint '${name}' not found`);
}
this.savepoints.delete(name);
return this;
}
insert(data) {
if (!data || typeof data !== 'object') {
throw new Error("Data must be a valid object");
}
const documentId = new outers_1.ClassBased.UniqueGenerator(15).RandomWord(true);
const operation = {
type: 'INSERT',
documentId,
data: Object.assign(Object.assign({}, data), { documentId, updatedAt: new Date().toISOString() }),
};
this.operations.push(operation);
return this;
}
update(query, data) {
if (!query || typeof query !== 'object') {
throw new Error("Query must be a valid object");
}
if (!data || typeof data !== 'object') {
throw new Error("Data must be a valid object");
}
const operation = {
type: 'UPDATE',
query,
data: Object.assign(Object.assign({}, data), { updatedAt: new Date().toISOString() }),
};
this.operations.push(operation);
return this;
}
delete(query) {
if (!query || typeof query !== 'object') {
throw new Error("Query must be a valid object");
}
const operation = {
type: 'DELETE',
query,
};
this.operations.push(operation);
return this;
}
commit() {
return __awaiter(this, void 0, void 0, function* () {
try {
if (Date.now() - this.startTime > this.timeoutMs) {
yield this.rollback();
return this.ResponseHelper.Error("Transaction timeout - automatically rolled back");
}
if (this.operations.length === 0) {
return this.ResponseHelper.Error("No operations to commit");
}
yield this.WAL.createWAL();
const metadata = {
transactionId: this.transactionId,
collectionPath: this.collectionPath,
status: 'ACTIVE',
startTime: new Date(this.startTime).toISOString(),
lockedDocuments: [],
isolationLevel: 'READ_COMMITTED',
};
yield this.Registry.registerTransaction(metadata);
yield this.resolveAndLockDocuments();
yield this.Registry.updateTransactionStatus(this.transactionId, 'PREPARING');
yield this.executeOperations();
yield this.Registry.updateTransactionStatus(this.transactionId, 'COMMITTED');
yield this.applyChanges();
yield this.IndexManager.commitIndexUpdates();
yield this.LockManager.releaseAllLocks(this.lockedDocuments);
// Selective cache invalidation: only clear cache for this collection
yield memory_operation_1.default.invalidateByCollection(this.collectionPath);
yield this.WAL.deleteWAL();
yield this.Registry.removeTransaction(this.transactionId);
return this.ResponseHelper.Success({
message: "Transaction committed successfully",
transactionId: this.transactionId,
operationsCount: this.operations.length,
});
}
catch (error) {
yield this.rollback();
return this.ResponseHelper.Error(error);
}
});
}
rollback() {
return __awaiter(this, void 0, void 0, function* () {
try {
yield this.WAL.undo();
yield this.IndexManager.rollbackIndexUpdates();
yield this.LockManager.releaseAllLocks(this.lockedDocuments);
yield this.WAL.deleteWAL();
yield this.Registry.removeTransaction(this.transactionId);
return this.ResponseHelper.Success({
message: "Transaction rolled back successfully",
transactionId: this.transactionId,
});
}
catch (error) {
return this.ResponseHelper.Error(error);
}
});
}
resolveAndLockDocuments() {
return __awaiter(this, void 0, void 0, function* () {
const documentsToLock = new Set();
for (const op of this.operations) {
if (op.type === 'INSERT' && op.documentId) {
documentsToLock.add(op.documentId);
}
else if ((op.type === 'UPDATE' || op.type === 'DELETE') && op.query) {
const documentIds = yield this.IndexManager.resolveQueryToDocumentIds(op.query);
documentIds.forEach((id) => documentsToLock.add(id));
}
}
for (const documentId of documentsToLock) {
const lockResult = yield this.LockManager.acquireLock(documentId, this.transactionId, this.startTime);
if (!lockResult.status) {
const errorMsg = 'message' in lockResult ? lockResult.message : "Failed to acquire lock";
throw new Error(errorMsg);
}
this.lockedDocuments.push(documentId);
}
});
}
executeOperations() {
return __awaiter(this, void 0, void 0, function* () {
const resolvedOperations = [];
for (const op of this.operations) {
if (op.type === 'INSERT') {
resolvedOperations.push(op);
}
else if (op.type === 'UPDATE' && op.query && op.data) {
const documentIds = yield this.IndexManager.resolveQueryToDocumentIds(op.query);
for (const documentId of documentIds) {
const fileName = `${documentId}${Keys_1.General.DBMS_File_EXT}`;
const filePath = `${this.collectionPath}/${fileName}`;
const fileExists = yield this.FileManager.FileExists(filePath);
if (!fileExists.status) {
continue;
}
const readResult = yield this.FileManager.ReadFile(filePath);
if (!readResult.status) {
continue;
}
let oldDataStr = readResult.data;
if (this.isEncrypted && this.cryptoInstance) {
oldDataStr = yield this.cryptoInstance.decrypt(oldDataStr);
}
const oldData = this.Converter.ToObject(oldDataStr);
const newData = Object.assign(Object.assign({}, oldData), op.data);
resolvedOperations.push({
type: 'UPDATE',
documentId,
fileName,
oldData,
data: newData,
});
}
}
else if (op.type === 'DELETE' && op.query) {
const documentIds = yield this.IndexManager.resolveQueryToDocumentIds(op.query);
for (const documentId of documentIds) {
const fileName = `${documentId}${Keys_1.General.DBMS_File_EXT}`;
const filePath = `${this.collectionPath}/${fileName}`;
const fileExists = yield this.FileManager.FileExists(filePath);
if (!fileExists.status) {
continue;
}
const readResult = yield this.FileManager.ReadFile(filePath);
if (!readResult.status) {
continue;
}
let oldDataStr = readResult.data;
if (this.isEncrypted && this.cryptoInstance) {
oldDataStr = yield this.cryptoInstance.decrypt(oldDataStr);
}
const oldData = this.Converter.ToObject(oldDataStr);
resolvedOperations.push({
type: 'DELETE',
documentId,
fileName,
oldData,
});
}
}
}
yield this.IndexManager.stageIndexUpdates(resolvedOperations);
// Store resolved operations for use in applyChanges
this.resolvedOperations = resolvedOperations;
for (const op of resolvedOperations) {
const fileName = op.fileName || `${op.documentId}${Keys_1.General.DBMS_File_EXT}`;
const filePath = `${this.collectionPath}/${fileName}`;
let beforeData;
if (op.type === 'UPDATE' || op.type === 'DELETE') {
const readResult = yield this.FileManager.ReadFile(filePath);
if (readResult.status) {
beforeData = readResult.data;
}
}
let afterData;
if ((op.type === 'INSERT' || op.type === 'UPDATE') && op.data) {
afterData = this.Converter.ToString(op.data);
if (this.isEncrypted && this.cryptoInstance) {
afterData = yield this.cryptoInstance.encrypt(afterData);
}
}
const walEntry = {
transactionId: this.transactionId,
timestamp: new Date().toISOString(),
operationType: op.type,
documentId: op.documentId,
fileName,
beforeData,
afterData,
checksum: '',
};
yield this.WAL.appendLog(walEntry);
const tempFilePath = `${filePath}.tmp-${this.transactionId}`;
if (op.type === 'INSERT' || op.type === 'UPDATE') {
yield this.FileManager.WriteFile(tempFilePath, afterData);
}
}
});
}
applyChanges() {
return __awaiter(this, void 0, void 0, function* () {
const fs = yield Promise.resolve().then(() => __importStar(require('fs/promises')));
for (const op of this.resolvedOperations) {
const documentId = op.documentId || '';
const fileName = `${documentId}${Keys_1.General.DBMS_File_EXT}`;
const filePath = `${this.collectionPath}/${fileName}`;
const tempFilePath = `${filePath}.tmp-${this.transactionId}`;
if (op.type === 'INSERT' || op.type === 'UPDATE') {
const tempExists = yield this.FileManager.FileExists(tempFilePath);
if (tempExists.status) {
yield fs.rename(tempFilePath, filePath);
}
}
else if (op.type === 'DELETE') {
const fileExists = yield this.FileManager.FileExists(filePath);
if (fileExists.status) {
yield this.FileManager.DeleteFile(filePath);
}
}
}
});
}
static recoverTransactions(collectionPath) {
return __awaiter(this, void 0, void 0, function* () {
try {
const registry = new TransactionRegistry_service_1.default(collectionPath);
const activeTransactions = yield registry.getActiveTransactions();
for (const txnMeta of activeTransactions) {
const wal = new WriteAheadLog_service_1.default(collectionPath, txnMeta.transactionId);
const lockManager = new LockManager_service_1.default(collectionPath);
if (txnMeta.status === 'COMMITTED' || txnMeta.status === 'PREPARING') {
yield wal.redo();
}
else {
yield wal.undo();
}
yield lockManager.releaseAllLocks(txnMeta.lockedDocuments);
yield wal.deleteWAL();
yield registry.removeTransaction(txnMeta.transactionId);
}
}
catch (_a) {
return;
}
});
}
}
exports.default = Transaction;
//# sourceMappingURL=Transaction.service.js.map