Skip to content

Commit

Permalink
performance improvements; updated documentation; updated version to r…
Browse files Browse the repository at this point in the history
…elease;
  • Loading branch information
mcmartins committed Jan 28, 2016
1 parent a732fac commit 8be59b1
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 29 deletions.
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@

# README

JSON FileSystem Database is a JSON database such as MongoDB backed by FileSystem.<br/>
JSON FileSystem Database is a JSON Document database, such as MongoDB.<br/>
All methods are asynchronous and accessing / filtering is executed in parallel using [async](https://github.com/caolan/async).<br/><br/>
Currently supports 2 types of data drivers: **Memory** and **Disk**.<br/>
Memory driver is very performant. It is possible to flush data to disk (configurable).<br/>
Disk driver is less performant an is implemented with **Pessimistic Transaction Locking** approach.<br/>
All methods are asynchronous and accessing / filtering is executed in parallel using [async](https://github.com/caolan/async).<br/>
Disk driver is less performant. Holds all data in disk, and reads/writes everytime you interact with the collection. Is implemented with **Pessimistic Transaction Locking** approach.<br/><br/>
Based on [Jalalhejazi](https://github.com/Jalalhejazi), [jsonfs](https://github.com/Jalalhejazi/jsonfs).

# Dependencies
Expand Down Expand Up @@ -113,7 +113,7 @@ Support for [Query and Projection Operators](https://docs.mongodb.org/manual/ref
...
```

Support provided by [json-criteria](https://github.com/mirek/node-json-criteria).
Support provided using [json-criteria](https://github.com/mirek/node-json-criteria).

# Options

Expand Down Expand Up @@ -146,10 +146,10 @@ database.Collection.remove(criteria, {multi: false}, callback);
When initializing the Driver you can pass 4 options:

```bash
options.path - The path to store the collection files. Accepts a String. Defaults to '/tmp/'.
options.path - The path to store the db collection files. Defaults to '/tmp/'.
options.driver - One of ['memory', 'disk'], defaults to disk
options.memory.flush - if you want to flush it to disk, defaults to false, only used if driver is 'memory'
options.memory.flushInterval - time interval to flush memory to disk defaults to 10s, only used if driver is 'memory'
options.memory.flush - if you want to flush it to disk, only used if driver is 'memory'. Defaults to false
options.memory.flushInterval - Time interval to flush memory to disk, only used if driver is 'memory'. Defaults to 10000ms (10s)
```
## Collections options
Expand Down
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ module.exports.connect = function (collections, options, callback) {
}
// in parallel initialize each collection
async.each(collections, function attachOrCreate(collection, next) {
console.log('Collection \'%s\' is about to be attached', collection);
var filePath = path.join(self._db._path, collection + '.json');
console.log('\'%s\' Collection is about to be attached', collection);
util.fileSystem.exists(filePath, function afterCheck(exists) {
if (!exists) {
util.fileSystem.write(filePath, function afterWriteFile(err) {
Expand Down
32 changes: 18 additions & 14 deletions lib/memory.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@
'use strict';

var util = require('./util');
var async = require('async');

function defaultCallback(err) {
if (err) {
console.log(err);
}
}

/**
* Provides in memory storage and regular flush to disk.
Expand All @@ -49,7 +56,7 @@ function Memory(options) {
}
self.set(content);
if (self.flush) {
console.log('Driver will flush data every %sms', self.flushInterval);
console.log('\'Memory\' driver will flush data every %sms', self.flushInterval);
// set interval to flush
setInterval(function flushToDisk() {
util.fileSystem.lock(self.flushFile, function afterLock(err) {
Expand Down Expand Up @@ -81,14 +88,14 @@ function Memory(options) {
* @param callback
*/
Memory.prototype.set = function set(content, callback) {
callback = callback || function afterUnlockFile(err) {
if (err) {
console.log('Oopss we couldn\'t release the lock!');
throw err;
callback = callback || defaultCallback;
var self = this;
async.series([
function(next) {
self.memoryTable = util.clone(content);
return next(undefined, self.memoryTable);
}
};
this.memoryTable = util.clone(content);
return callback(undefined, this.memoryTable);
], callback);
};

/**
Expand All @@ -97,6 +104,7 @@ Memory.prototype.set = function set(content, callback) {
* @param callback
*/
Memory.prototype.get = function get(callback) {
callback = callback || defaultCallback;
return callback(undefined, this.memoryTable);
};

Expand All @@ -106,6 +114,7 @@ Memory.prototype.get = function get(callback) {
* @param callback
*/
Memory.prototype.lock = function lock(callback) {
callback = callback || defaultCallback;
return callback(undefined);
};

Expand All @@ -115,12 +124,7 @@ Memory.prototype.lock = function lock(callback) {
* @param callback
*/
Memory.prototype.unlock = function unlock(callback) {
callback = callback || function afterUnlockFile(err) {
if (err) {
console.log('Oopss we couldn\'t release the lock!');
throw err;
}
};
callback = callback || defaultCallback;
return callback(undefined);
};

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "jsondbfs",
"description": "JSON Filesystem Database.",
"author": "Manuel Martins <[email protected]>",
"version": "0.4.1",
"version": "0.4.2",
"license": "Apache-2.0",
"engine": "node >= 0.12.0",
"main": "./index",
Expand Down
6 changes: 3 additions & 3 deletions test/disk-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ describe('JSONDBFS Disk Driver', function testSpec() {
});

it('should fail creating a connection to an invalid path', function test(done) {
JSONDBFSDriver.connect(['Collection'], {
JSONDBFSDriver.connect(['DiskCollection'], {
path: '/invalid'
}, function afterConnect(err, db) {
assert.notEqual(err, undefined);
Expand Down Expand Up @@ -140,7 +140,7 @@ describe('JSONDBFS Disk Driver', function testSpec() {
});

it('should create a new collection passing a non array as collection', function test(done) {
JSONDBFSDriver.connect('CollectionAsString', function afterConnect(err, db) {
JSONDBFSDriver.connect('DiskCollectionAsString', function afterConnect(err, db) {
assert.equal(err, undefined);
assert.notEqual(db, undefined);
return done();
Expand All @@ -156,7 +156,7 @@ describe('JSONDBFS Disk Driver', function testSpec() {
});

it('should create a new collection using override options', function test(done) {
JSONDBFSDriver.connect(['Override'], {
JSONDBFSDriver.connect(['DiskOverride'], {
path: '/tmp/',
driver: 'disk'
}, function afterConnect(err, db) {
Expand Down
6 changes: 3 additions & 3 deletions test/memory-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ describe('JSONDBFS Memory Driver', function testSpec() {
});

it('should fail creating a connection to an invalid path', function test(done) {
JSONDBFSDriver.connect(['Collection'], {
JSONDBFSDriver.connect(['MemoryCollection'], {
path: '/invalid',
driver: 'memory'
}, function afterConnect(err, db) {
Expand Down Expand Up @@ -151,7 +151,7 @@ describe('JSONDBFS Memory Driver', function testSpec() {
});

it('should create a new collection passing a non array as collection', function test(done) {
JSONDBFSDriver.connect('CollectionAsString', {
JSONDBFSDriver.connect('MemoryCollectionAsString', {
driver: 'memory'
}, function afterConnect(err, db) {
assert.equal(err, undefined);
Expand All @@ -171,7 +171,7 @@ describe('JSONDBFS Memory Driver', function testSpec() {
});

it('should create a new collection using override options', function test(done) {
JSONDBFSDriver.connect(['Override'], {
JSONDBFSDriver.connect(['MemoryOverride'], {
path: '/tmp/',
driver: 'memory'
}, function afterConnect(err, db) {
Expand Down

0 comments on commit 8be59b1

Please sign in to comment.