UNPKG

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

97 lines 4.98 kB
"use strict"; 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()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); /* eslint-disable @typescript-eslint/no-explicit-any */ const Keys_1 = require("../../config/Keys/Keys"); const Index_service_1 = require("./Index.service"); const IndexCache_service_1 = require("./IndexCache.service"); class InsertIndex extends Index_service_1.IndexManager { constructor(path) { super(path); this.indexCache = new IndexCache_service_1.IndexCache(path); } /** * Inserts a document identifier into one or more index files as defined by the global index meta. * * OPTIMIZED: Uses in-memory index cache for fast reads and atomic dual-write (memory + disk). * * The method: * 1. Calls `this.findMatchingIndexMeta(document)` to determine which index files should be updated. * 2. For each matched index entry: * - Gets current index data from memory cache (or loads from disk if not cached) * - Appends `${document.documentId}${General.DBMS_File_EXT}` to `indexEntries` * - Updates both memory cache and disk atomically via indexCache.updateIndex() * * @param document - Object representing the document to index. Must contain a `documentId` property (string | number). * @returns A Promise that resolves to: * - `true` if the last index file write operation returned a success status, * - `false` if the global index meta could not be read, no matching index meta entries were found, or the final write returned a falsy status. * * @throws Propagates any exceptions thrown by file reads/writes or conversion (e.g., IO or parse/serialize errors). * * @remarks * - Updates both memory cache and disk atomically * - Thread-safe via index cache locking mechanism * - Falls back to disk read on cache miss (cold start recovery) * * @example * // document must include documentId: * // { documentId: "abc123", ... } * const success = await indexService.InsertToIndex({ documentId: "abc123" }); */ InsertToIndex(document) { return __awaiter(this, void 0, void 0, function* () { const matchedIndex = yield this.findMatchingIndexMeta(document); if (matchedIndex) { if (matchedIndex.length == 0) { return false; } let status = false; for (const indexes of matchedIndex) { const indexName = indexes.indexFieldName; const documentFileName = `${document.documentId}${Keys_1.General.DBMS_File_EXT}`; // Get current index data from memory cache (or load from disk if not cached) let indexData = yield this.indexCache.getIndex(indexName); if (!indexData) { // Fallback: Load from disk if not in cache (cold start recovery) const indexContent = yield this.fileManager.ReadFile(indexes.path); if (indexContent.status) { indexData = this.converter.ToObject(indexContent.data); } else { // Index file doesn't exist - skip this index continue; } } if (!indexData) { continue; } const fieldValue = document[indexName]; // Add document to index entries const alreadyhave = Object.keys(indexData.indexEntries).some(keys => keys == fieldValue); if (alreadyhave) { indexData.indexEntries[fieldValue].push(documentFileName); } else { indexData.indexEntries[fieldValue] = [documentFileName]; } // Update both memory cache and disk atomically const updateSuccess = yield this.indexCache.updateIndex(indexName, indexData); status = updateSuccess; } return status; } return false; }); } } exports.default = InsertIndex; //# sourceMappingURL=InsertIndex.service.js.map