/** * Embedded Database - FFI Mode * * Direct FFI access to SochDB native library. * No server required - similar to Python SDK's Database class. */ import { NativeBindings } from './ffi/bindings'; import { EmbeddedTransaction } from './transaction'; import { Namespace, NamespaceConfig } from '../namespace'; /** * Accepted binary input for keys/values. Strings are UTF-8 encoded — mirrors the * Python SDK, which accepts `str` or `bytes`. */ export type BinaryInput = Buffer | Uint8Array | string; export interface EmbeddedDatabaseConfig { walEnabled?: boolean; syncMode?: 'full' | 'normal' | 'off'; memtableSizeBytes?: number; groupCommit?: boolean; indexPolicy?: 'write_optimized' | 'balanced' | 'scan_optimized' | 'append_only'; } /** * Embedded Database using direct FFI * * @example * ```typescript * import { EmbeddedDatabase } from '@sochdb/sochdb'; * * const db = await EmbeddedDatabase.open('./mydb'); * await db.put(Buffer.from('key'), Buffer.from('value')); * const value = await db.get(Buffer.from('key')); * await db.close(); * ``` */ export declare class EmbeddedDatabase { private handle; private bindings; private closed; private path; private concurrent; private _concurrentModeFallback; private constructor(); /** * Open a database at the specified path in standard mode * * For web applications with multiple processes, use `openConcurrent()` instead. * * @param path - Path to database directory * @param config - Optional configuration * @returns EmbeddedDatabase instance */ static open(path: string, config?: EmbeddedDatabaseConfig): EmbeddedDatabase; /** * Open a database in concurrent mode for multi-process web applications * * This mode allows multiple Node.js processes (e.g., PM2 cluster workers, * multiple Express instances) to access the database simultaneously. * * Features: * - Lock-free reads with ~100ns latency * - Multi-reader, single-writer coordination * - Automatic write serialization * * @example * ```typescript * import { EmbeddedDatabase } from '@sochdb/sochdb'; * import express from 'express'; * * // Open in concurrent mode - multiple workers can access * const db = EmbeddedDatabase.openConcurrent('./web_db'); * * const app = express(); * * app.get('/user/:id', async (req, res) => { * // Multiple concurrent requests can read simultaneously * const data = await db.get(Buffer.from(`user:${req.params.id}`)); * if (!data) { * res.status(404).json({ error: 'not found' }); * return; * } * res.send(data); * }); * * app.post('/user/:id', async (req, res) => { * // Writes are serialized automatically * await db.put(Buffer.from(`user:${req.params.id}`), req.body); * res.json({ status: 'ok' }); * }); * * // Start with PM2 cluster mode: * // pm2 start app.js -i max * app.listen(3000); * ``` * * @param path - Path to database directory * @param options - Optional configuration for concurrent mode * @returns EmbeddedDatabase instance in concurrent mode */ static openConcurrent(path: string, options?: { fallbackToStandard?: boolean; }): EmbeddedDatabase; /** * Check if database is opened in concurrent mode */ get isConcurrent(): boolean; /** * Check if concurrent mode fell back to standard mode */ get isConcurrentFallback(): boolean; /** * Check if concurrent mode is available in the native library */ static isConcurrentModeAvailable(): boolean; /** * Put a key-value pair (auto-transaction) */ put(key: BinaryInput, value: BinaryInput): Promise; /** * Get a value by key (auto-transaction) */ get(key: BinaryInput): Promise; /** * Delete a key (auto-transaction) */ delete(key: BinaryInput): Promise; /** * Put value at path (auto-transaction) */ putPath(path: string, value: BinaryInput): Promise; /** * Get value at path (auto-transaction) */ getPath(path: string): Promise; /** * Scan keys with prefix */ scanPrefix(prefix: Buffer): AsyncGenerator<[Buffer, Buffer]>; /** * Begin a transaction */ transaction(): EmbeddedTransaction; /** * Execute operations within a transaction (with auto-commit/abort) */ withTransaction(fn: (txn: EmbeddedTransaction) => Promise): Promise; /** * Force a checkpoint */ checkpoint(): Promise; /** * Get storage statistics */ stats(): Promise<{ memtableSizeBytes: bigint; walSizeBytes: bigint; activeTransactions: number; minActiveSnapshot: bigint; lastCheckpointLsn: bigint; }>; /** * Close the database */ close(): void; private ensureOpen; /** * Get internal handle (for transactions) * @internal */ getHandle(): any; /** * Get bindings instance (for transactions) * @internal */ getBindings(): NativeBindings; /** * Create a new namespace */ createNamespace(name: string, config?: Partial): Promise; /** * Get an existing namespace */ namespace(name: string): Promise; /** * Get or create a namespace */ getOrCreateNamespace(name: string, config?: Partial): Promise; /** * Delete a namespace and all its data */ deleteNamespace(name: string): Promise; /** * List all namespace names */ listNamespaces(): Promise; /** * Add a node to the graph overlay */ addNode(namespace: string, nodeId: string, nodeType: string, properties?: Record): Promise; /** * Add an edge between nodes */ addEdge(namespace: string, fromId: string, edgeType: string, toId: string, properties?: Record): Promise; /** * Traverse the graph from a starting node */ traverse(namespace: string, startNode: string, maxDepth?: number, order?: 'bfs' | 'dfs'): Promise<{ nodes: any[]; edges: any[]; }>; /** * Put a value in the semantic cache */ cachePut(cacheName: string, key: string, value: string, embedding: number[], ttlSeconds?: number): Promise; /** * Get a value from the semantic cache by embedding similarity */ cacheGet(cacheName: string, queryEmbedding: number[], threshold?: number): Promise; /** * Delete a cache entry */ cacheDelete(cacheName: string, key: string): Promise; /** * Clear all entries in a semantic cache */ cacheClear(cacheName: string): Promise; /** * Convert records to TOON format */ static toToon(tableName: string, records: Array>, fields?: string[]): string; /** * Convert records to JSON format */ static toJson(tableName: string, records: Array>, fields?: string[], compact?: boolean): string; /** * Parse JSON format */ static fromJson(jsonStr: string): { table: string; fields: string[]; records: Array>; }; private static cosineSimilarity; } //# sourceMappingURL=database.d.ts.map