BlobDB is a custom database implementation written in Node.js that leverages BSON for data serialization and provides asynchronous CRUD operations using worker threads and an operation queue to manage tasks. The database is designed to handle concurrent operations and ensure data integrity with file-based storage.
const BlobDB = require('./index.js');
(async () => {
const db = BlobDB;
await db.initialize(["myCollection"], {logLevel: 4, consoleOut: false});
await db.insert("myCollection", { name: 'Alice', age: 30 });
await db.insert("myCollection", { name: 'Robert', age: 21 });
const results_1 = await db.find("myCollection", {age: {$gte: 25}});
console.log(results_1);
await db.deleteMany("myCollection", {age: {$lt: 25}});
const results_2 = await db.find("myCollection", {});
await db.close();
})();new BlobDB(collections, params);-
collections
<Array[string]>: Collection-level identifiers. -
params
<object>:-
logLevel
<number, optional>: Logging level (-1: General-Success, 0: System, 1: General, 2: Warn, 3: Critical, 4: Error (default), 5: Debug). -
consoleOut
<boolean, optional>: Whether to output logs to the console (default: false).
-
async initialize();Collection-level initialization, setting up file structures.
async findOne(query);Finds a single document matching the query.
- query
<object>: Query object to match documents.
- Promise
<object>: The matched document.
async find(query);Finds all documents matching the query.
- query
<object>: Query object to match documents.
- Promise
<array>: An array of matched documents.
async deleteOne(query);Deletes a single document matching the query.
- query
<object>: Query object to match documents.
- Promise
<void>
async deleteMany(query);Deletes all documents matching the query.
- query
<object>: Query object to match documents.
- Promise
<void>
async updateOne(query, update, options);Updates a single document matching the query.
-
query
<object>: Query object to match documents. -
update
<object>: Update object containing the modifications. -
options
<object, optional>: Update options (e.g., { upsert: true }).
- Promise
<void>
async updateMany(query, update, options);Updates all documents matching the query.
-
query
<object>: Query object to match documents. -
update
<object>: Update object containing the modifications. -
options
<object, optional>: Update options (e.g. { upsert: true }).
- Promise
<void>
async insert(document);Inserts a new document into the datastore.
- document
<object>: The document to be inserted.
- Promise
<void>
async count(query);Counts the number of documents matching the query.
- query
<object>: Query object to match documents.
- Promise
<number>: The count of matched documents.
async drop();Collection-level drop, deleting all files and clearing all data within the collection file structure.
- Promise
<void>
close();Closes the collection-level reference, ensuring all operations are flushed to disk and all worker threads are terminated.
- Promise
<void>
#initializeFileStructure();Sets up the necessary file structures for the collection.
async #loadDocumentInformation(readStream, path);Loads document information from the specified file.
-
readStream
<fs.ReadStream>: The read stream for the file. -
path
<string>: The path of the file.
- Promise
<array>: An array of document information.
#insert(document, writePath, writeStream);Inserts a document into the specified write stream.
-
document
<object>: The document to be inserted. -
writePath
<string>: The path of the write stream. -
writeStream
<fs.WriteStream>: The write stream.
- Promise
<void>
#dispatchQueryAgent(query, operation, update = null, options = null);Dispatches a query to the appropriate worker thread.
-
query
<object>: The query object. -
operation
<string>: The operation to perform (e.g., 'findOne'). -
update
<object, optional>: Update object for update operations. -
options
<object, optional>: Additional options for the operation.
- Promise
<any>: The result of the operation.
#executeUpdate(update, document);Executes an update on a document.
-
update
<object>: The update object. -
document
<object>: The document to be updated.
- Promise
<void>
#executeDelete(index);Executes a delete operation on a document at the specified index.
- index
<number>: The index of the document to be deleted.
- Promise
<void>
#buildDelimitedWriteBuffer(value);Builds a write buffer with delimited values.
- value
<any>: The value to be written.
- Buffer
<Buffer>: The write buffer.
#insertDocumentMetaInformation(writeStream, value);Inserts document metadata information into the specified write stream.
-
writeStream
<fs.WriteStream>: The write stream. -
value
<any>: The value to be written.
Promise<void>
#encodeInteger(int);Encodes an integer into a buffer.
- int
<number|bigint>: The integer to be encoded.
- Buffer
<Buffer>: The encoded buffer.
#drop();Collection-level drop, deleting all files and clearing all data within the collection file structure.
#decodeInteger(buffer);Decodes an integer from a buffer.
- buffer
<Buffer>: The buffer containing the integer.
- int
<number|bigint>: The decoded integer.
#fileSizeInBytes(path);Gets the size of a file in bytes.
- path
<string>: The path of the file.
- Promise
<number>: The size of the file in bytes.
#eraseFileContents(path);Erases the contents of a file.
- path
<string>: The path of the file.
#replaceFile(srcPath, destPath);Replaces a file with another file.
-
srcPath
<string>: The source file path. -
destPath
<string>: The destination file path.
async #closeStreams(streams);Closes the specified streams.
- streams
<array>: An array of streams to be closed.
- Promise
<void>
#closeFD(fd);Closes the specified file descriptor.
- fd
<number>: The file descriptor to be closed.
- Promise
<void>
EOT (End of Transmission): 0x94
ESC0 (Escape Character 0): 0xCA
ESC1 (Escape Character 1): 0xF3