https://github.com/ericnorris/node-cdb
A cdb implementation for node.js
https://github.com/ericnorris/node-cdb
Last synced: 12 months ago
JSON representation
A cdb implementation for node.js
- Host: GitHub
- URL: https://github.com/ericnorris/node-cdb
- Owner: ericnorris
- License: mit
- Created: 2014-08-19T13:21:07.000Z (almost 12 years ago)
- Default Branch: master
- Last Pushed: 2018-06-03T22:11:21.000Z (about 8 years ago)
- Last Synced: 2024-11-18T19:46:56.057Z (over 1 year ago)
- Language: JavaScript
- Size: 39.1 KB
- Stars: 9
- Watchers: 5
- Forks: 6
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# node-constant-db [](https://travis-ci.org/ericnorris/node-cdb)
A [cdb](http://cr.yp.to/cdb.html) implementation in node.js, supporting both read and write capabilities.
## Installation
`npm install constant-db`
## Changes from v1.0.0
* Renamed `getRecord()` to `get()`
* Renamed `putRecord()` to `put()`
* Added `getNext()`
* Dropped promise support
* Completely rewritten! `get()` calls should be much faster.
## Example
Writable cdb:
```javascript
var writable = require('constant-db').writable;
var writer = new writable('./cdbfile');
writer.open(function cdbOpened(err) {
writer.put('meow', 'hello world');
writer.close(function cdbClosed(err) {
console.log('hooray!');
});
});
```
Readable cdb:
```javascript
var readable = require('constant-db').readable;
var reader = new readable('./cdbfile');
reader.open(function cdbOpened(err) {
reader.get('meow', function gotRecord(err, data) {
console.log(data); // results in 'hello world!'
reader.close(function cdbClosed(err) {
console.log('awesome!');
});
});
});
```
## Documentation
### Readable cdb
To create a new readable instance:
`new require('constant-db').readable(file);`
`open(callback(err, cdb))`
Opens the file for reading, and immediately caches the header table for the cdb (2048 bytes).
`get(key, [offset], callback(err, data))`
Attempts to find the specified key, and calls the callback with an error (if not found) or the data for that key (if found). If an offset is specified, the cdb will return data for the *nth* record matching that key.
`getNext(callback(err, data))`
Continues the previous `get()` call, finding the next record under the key `get()` was called with. This should be slightly faster than calling `get()` with an offset.
`close(callback(err, cdb))`
Closes the file. No more records can be read after closing.
### Writable cdb
To create a new writable instance:
`new require('constant-cdb').writable(file);`
`open(callback(err, cdb))`
Opens the file for writing. This will overwrite any file that currently exists, or create a new one if necessary.
`put(key, data)`
Writes a record to the cdb.
`close(callback(err, cdb))`
Finalizes the cdb and closes the file. Calling `close()` is necessary to write out the header and subtables required for the cdb!
## Benchmark
`node benchmarks/cdb-random-benchmark.js`