Skip to content

DTGillespie/BlobDB

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

45 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

BlobDB

Overview

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.

Usage Example

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();
})();

API Documentation


Constructor:

new BlobDB(collections, params);

Parameters:

  • 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).

Public Methods

async initialize();

Collection-level initialization, setting up file structures.

Parameters: N/A

Returns: N/A


async findOne(query);

Finds a single document matching the query.

Parameters:

  • query<object>: Query object to match documents.

Returns:

  • Promise<object>: The matched document.

async find(query);

Finds all documents matching the query.

Parameters:

  • query<object>: Query object to match documents.

Returns:

  • Promise<array>: An array of matched documents.

async deleteOne(query);

Deletes a single document matching the query.

Parameters:

  • query<object>: Query object to match documents.

Returns:

  • Promise<void>

async deleteMany(query);

Deletes all documents matching the query.

Parameters:

  • query<object>: Query object to match documents.

Returns:

  • Promise<void>

async updateOne(query, update, options);

Updates a single document matching the query.

Parameters:

  • query<object>: Query object to match documents.

  • update<object>: Update object containing the modifications.

  • options<object, optional>: Update options (e.g., { upsert: true }).

Returns:

  • Promise<void>

async updateMany(query, update, options);

Updates all documents matching the query.

Parameters:

  • query<object>: Query object to match documents.

  • update<object>: Update object containing the modifications.

  • options<object, optional>: Update options (e.g. { upsert: true }).

Returns:

  • Promise<void>

async insert(document);

Inserts a new document into the datastore.

Parameters:

  • document<object>: The document to be inserted.

Returns:

  • Promise<void>

async count(query);

Counts the number of documents matching the query.

Parameters:

  • query<object>: Query object to match documents.

Returns:

  • Promise<number>: The count of matched documents.

async drop();

Collection-level drop, deleting all files and clearing all data within the collection file structure.

Parameters: N/A

Returns:

  • Promise<void>

close();

Closes the collection-level reference, ensuring all operations are flushed to disk and all worker threads are terminated.

Parameters: N/A

Returns:

  • Promise<void>

Private Methods

#initializeFileStructure();

Sets up the necessary file structures for the collection.

Parameters: N/A

Returns: N/A


async #loadDocumentInformation(readStream, path);

Loads document information from the specified file.

Parameters:

  • readStream<fs.ReadStream>: The read stream for the file.

  • path<string>: The path of the file.

Returns:

  • Promise<array>: An array of document information.

#insert(document, writePath, writeStream);

Inserts a document into the specified write stream.

Parameters:

  • document<object>: The document to be inserted.

  • writePath<string>: The path of the write stream.

  • writeStream<fs.WriteStream>: The write stream.

Returns:

  • Promise<void>

#dispatchQueryAgent(query, operation, update = null, options = null);

Dispatches a query to the appropriate worker thread.

Parameters:

  • 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.

Returns:

  • Promise<any>: The result of the operation.

#haltThreads();

Halts all worker threads.

Parameters: N/A

Returns: N/A


#executeUpdate(update, document);

Executes an update on a document.

Parameters:

  • update<object>: The update object.

  • document<object>: The document to be updated.

Returns:

  • Promise<void>

#executeDelete(index);

Executes a delete operation on a document at the specified index.

Parameters:

  • index<number>: The index of the document to be deleted.

Returns:

  • Promise<void>

#buildDelimitedWriteBuffer(value);

Builds a write buffer with delimited values.

Parameters:

  • value<any>: The value to be written.

Returns:

  • Buffer<Buffer>: The write buffer.

#insertDocumentMetaInformation(writeStream, value);

Inserts document metadata information into the specified write stream.

Parameters:

  • writeStream<fs.WriteStream>: The write stream.

  • value<any>: The value to be written.

Returns:

Promise<void>


#encodeInteger(int);

Encodes an integer into a buffer.

Parameters:

  • int<number|bigint>: The integer to be encoded.

Returns:

  • Buffer<Buffer>: The encoded buffer.

#drop();

Collection-level drop, deleting all files and clearing all data within the collection file structure.

Parameters: N/A

Returns: N/A


#decodeInteger(buffer);

Decodes an integer from a buffer.

Parameters:

  • buffer<Buffer>: The buffer containing the integer.

Returns:

  • int<number|bigint>: The decoded integer.

#fileSizeInBytes(path);

Gets the size of a file in bytes.

Parameters:

  • path<string>: The path of the file.

Returns:

  • Promise<number>: The size of the file in bytes.

async #flushToDisc();

Flushes all changes to disk.

Parameters: N/A

Returns:

  • Promise<void>

#eraseFileContents(path);

Erases the contents of a file.

Parameters:

  • path<string>: The path of the file.

Returns: N/A


#replaceFile(srcPath, destPath);

Replaces a file with another file.

Parameters:

  • srcPath<string>: The source file path.

  • destPath<string>: The destination file path.

Returns: N/A


async #closeStreams(streams);

Closes the specified streams.

Parameters:

  • streams<array>: An array of streams to be closed.

Returns:

  • Promise<void>

#closeFD(fd);

Closes the specified file descriptor.

Parameters:

  • fd<number>: The file descriptor to be closed.

Returns:

  • Promise<void>

Control Characters

EOT (End of Transmission): 0x94
ESC0 (Escape Character 0): 0xCA
ESC1 (Escape Character 1): 0xF3

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors