No SQL document storage database
npm install ultradbWorks on 64 bit linux platforms. Other platforms not tested.
Some examples of usage:
* logging data
* storing sessions, large numger of documents
* building custom database data structures (trees, chain lists, ...)
* microservices
* services
* storing training data of ai, ann, ... storing trained results...
* building more advanced databases with ultradb as their backend
* everywhere where you need document ID without additional querying for it (you receive document ID always as a result of adding document to database)
It:
* is small - arround 300kB
* is written in C for node js intentionally - it is not a wrapper, or port of another database
* is multicore processors friendly - scales up with node processes, multiple node instances/forks can work simultaneously on same database file
* is pretty simple and straight forward - not intended to hide any kind of abstraction from you
* supports text, integers, floats and data buffers
* supports cursors that allows to iterate through documents starting from newest and allows paging
* is VERY FAST, try it on SSD drive, compare with other databases, tell me the results - I'm sure you'll be pleased when you'll compare it with other databases for node js
* is Binary JSON (BSON) and other binary formats friendly
If performance and/or costs of your infrastructure is for you a key factor you should consider use of Ultra DB. It is simple to use library, but requires little more attention from developer and more extensive testing, since there is not much control over what you do with database (for example there is no guarantee that database will trigger an error when trying to update document with non existing ID within a range of IDs in database, more over it might corrupt database). But in return you get a database, that runs within node process (no extra process, no extra overheads for TCP/IP stack, no waisteless copying data, no parsing queries, no switching to additional db tasks, no callbacks and promisses overheads ... and so on). With very little effort you can make pretty advanced services serving many thousands requests a second on a single cheap VPS machine.
Feel free to drop a line of code, write tests, prepare documentation, tutorials, guidelines, come up with ideas and so on!
It is unsafe to allow access to documents by their id from any kind of public api (unless all documents are fixed size and you control modulo of document ID). In case of public API additional step of document id validation should be taken (combining document ID and random key saved in the document as public document ID, accessing via another fixed size documents lookup database, hashing id or hasing id combined with kind of checksum ... and so on). Current API profides set of document ID checksum functions (8 - 32 bit) and unique key for checksums for each database file - this will be helpfull to prevent accessing non existing documents.
If document id's are used internally only and you have full control of how are they used, then you don't have to worry about above.
If you need full ACID database you have to design data and recovery mechanisms yourself. But if you only add new records and read them, you're safe, you might consider this database kinda of ACID in that case.
If you're going to use db in nodejs cluster make sure you use transactions where it needs to be synchronized across processes.
Since transactions are blocking other processes that want to use particular database file/files it is a good idea to split data to databases for different document types each.
Newly added documents are hidden till their content is filled into a database.
64 bit integers currently are actually limited to 53 bits. This is due javascript limitation for numbers, not database itself.
Before you start, check examples located in "tests" directory on github repo:
https://github.com/DanielMazurkiewicz/ultradb/tree/master/tests
```
Document ID points indirectly to address of most significant byte of document length. Most significant byte contains additional 3 bit information:
* most significant bit if set to 1 means that document is hidden
* second and third most significant bit encodes document length field size:
- 00 : 1 byte
- 01 : 2 bytes
- 10 : 4 bytes
- 11 : 8 bytes
Since Document ID is indirect address it is technically possible to remove phisically documents from the beginning of database while keeping same Document IDs.
``
const UltraDB = require('ultradb');
`
const testDb = UltraDB('./myDatabase.udb');
`
This will open database.
`
const test1Db = UltraDB('./myDatabase.udb, 2048');
`
This will open database with specified page size, default is 16384. Page size has an impact on performance, the bigger the better performance. The best if it is multiplication of expected average document size.
``
testDb.close();
Database doesn't require to be closed so in most of cases you can skip this, however, if you're going to open dynamically a lot of databases it is good practice to close the ones you're not using any more.
``
const documentId = testDb.addUtf8z("My frirst ultradb document");
This will add new document to database, and if successfull, it will return its id;
``
const documentContent = testDb.getUtf8z(documentId);
This will retrieve document from database.
If document is hidden it will return "null"
If document doesn't exist it will return "undefined"
Note that "documentId" parameter is examined only briefly, so providing appropriate value is on your responsibility. Providing inappropriate value might lead to strange behaviour.
Note that it is your responsibility to use appropriate method to read appropriate data type, database doesn't encode document type.
``
const testDb = UltraDB('./dbTestCursor.udb').default('Utf8z');
Adds shorten names for methods containing given default string. For above example:
``
const documentId = testDb.add('some document');`
will be equivalent to`
const documentId = testDb.addUtf8z('some document');
Method returns current database instance
``
testDb.isEmpty();
Returns true if database is empty
``
const documentId = testDb.last();`
It returns "undefined" if there is no documents in database.
This method skips all hidden documents. If you need to get latest document, including hidden ones use:`
const documentId = testDb.lastOfAll();
``
const previousDocumentId = testDb.previous(documentId);`
It returns "undefined" if there is no previous document to given one
This method skips all hidden documents. If you need to get previous document, including hidden ones use:`
const previousDocumentId = testDb.previousOfAll(documentId);
Example: Iterate through all documents
``
for (let id = testDb.last(); id != undefined; id = testDb.previous(id)) {
console.log(testDb.get(id));
}
Note that "documentId" parameter is examined only briefly, so providing appropriate value is on your responsibility. Providing inappropriate value might lead to strange behaviour.
``
testDb.roll();
Writes new documents from begining of database file (with a guarantee of assigning non existing yet new document id's for new documents). It replaces oldest added documents in database.
``
testDb.hide(documentId);
Note that "documentId" parameter is examined only briefly, so providing appropriate value is on your responsibility. Providing inappropriate value might lead to strange behaviour or database corruption.
``
testDb.unhide(documentId);
Note that "documentId" parameter is examined only briefly, so providing appropriate value is on your responsibility. Providing inappropriate value might lead to strange behaviour or database corruption.
`
testDb.start();
//code that needs to be run exclusively on database so other processes have to wait
testDb.stop();
`
Method "start" informs that transaction has started, and method "stop" informs that transaction has ended.
Note that simple adding new documents to database and reading them is process safe, so there is no need to use transactions. Same if your node application is single process accessing particular database.
`
const testDb1 = UltraDb('./testdb1.udb');
const testDb2 = UltraDb('./testdb2.udb');
const testDb3 = UltraDb('./testdb3.udb');
const multiTransaction123 = UltraDB.prepareMultiTransaction(testDb1, testDb2, testDb3);
// ...
UltraDB.start(multiTransaction123);
//code that needs to be run exclusively on given set of databases
UltraDB.stop(multiTransaction123);
`
Using this methods prevents cross transaction lock while transaction on multiple databases.
(methods with skipped description simply have same functionality as previously described, but on different size of data)
```
pkg install python2
pkg install make
pkg install clang
Currently not working on Android due to Androids lack of support for sys/shm. :(