Skip to content

Commit

Permalink
Housekeeping!
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesMGreene committed Jan 7, 2016
1 parent 5d190f1 commit aefe40d
Show file tree
Hide file tree
Showing 7 changed files with 151 additions and 144 deletions.
15 changes: 10 additions & 5 deletions lib/datastore.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,16 @@ Datastore.prototype.resetIndexes = function (newData) {
* @param {Function} cb Optional callback, signature: err
*/
Datastore.prototype.ensureIndex = function (options, cb) {
var callback = cb || function () {};
var err
, callback = cb || function () {};

options = options || {};

if (!options.fieldName) { return callback({ missingFieldName: true }); }
if (!options.fieldName) {
err = new Error("Cannot create an index without a fieldName");
err.missingFieldName = true;
return callback(err);
}
if (this.indexes[options.fieldName]) { return callback(null); }

this.indexes[options.fieldName] = new Index(options);
Expand Down Expand Up @@ -535,9 +540,9 @@ Datastore.prototype._update = function (query, updateQuery, options, cb) {
}
, function () { // Perform the update
var modifiedDoc
, candidates = self.getCandidates(query)
, modifications = []
;
, candidates = self.getCandidates(query)
, modifications = []
;

// Preparing update (if an error is thrown here neither the datafile nor
// the in-memory indexes are affected)
Expand Down
21 changes: 6 additions & 15 deletions lib/indexes.js
Original file line number Diff line number Diff line change
Expand Up @@ -230,28 +230,20 @@ Index.prototype.revertUpdate = function (oldDoc, newDoc) {
};


// Append all elements in toAppend to array
function append (array, toAppend) {
var i;

for (i = 0; i < toAppend.length; i += 1) {
array.push(toAppend[i]);
}
}


/**
* Get all documents in index whose key match value (if it is a Thing) or one of the elements of value (if it is an array of Things)
* @param {Thing} value Value to match the key against
* @return {Array of documents}
*/
Index.prototype.getMatching = function (value) {
var self = this;
var _res, res
, self = this;

if (!util.isArray(value)) {
return this.tree.search(value);
res = self.tree.search(value);
} else {
var _res = {}, res = [];
_res = {};
res = [];

value.forEach(function (v) {
self.getMatching(v).forEach(function (doc) {
Expand All @@ -262,9 +254,8 @@ Index.prototype.getMatching = function (value) {
Object.keys(_res).forEach(function (_id) {
res.push(_res[_id]);
});

return res;
}
return res;
};


Expand Down
8 changes: 5 additions & 3 deletions lib/storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,13 @@ storage.flushToStorage = function (options, callback) {
if (err) { return callback(err); }
fs.fsync(fd, function (errFS) {
fs.close(fd, function (errC) {
var e = null;
if (errFS || errC) {
return callback({ errorOnFsync: errFS, errorOnClose: errC });
} else {
return callback(null);
e = new Error('Failed to flush to storage');
e.errorOnFsync = errFS;
e.errorOnClose = errC;
}
return callback(e);
});
});
});
Expand Down
12 changes: 7 additions & 5 deletions test/db.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ describe('Database', function () {
db = new Datastore({ filename: autoDb, autoload: true, onload: onload })

db.find({}, function (err, docs) {
done("Find should not be executed since autoload failed");
done(new Error("Find should not be executed since autoload failed"));
});
});

Expand Down Expand Up @@ -390,7 +390,7 @@ describe('Database', function () {
*
* Note: maybe using an in-memory only NeDB would give us an easier solution
*/
it('If the callback throws an uncaught execption, dont catch it inside findOne, this is userspace concern', function (done) {
it('If the callback throws an uncaught exception, do not catch it inside findOne, this is userspace concern', function (done) {
var tryCount = 0
, currentUncaughtExceptionHandlers = process.listeners('uncaughtException')
, i
Expand All @@ -399,21 +399,23 @@ describe('Database', function () {
process.removeAllListeners('uncaughtException');

process.on('uncaughtException', function MINE (ex) {
process.removeAllListeners('uncaughtException');

for (i = 0; i < currentUncaughtExceptionHandlers.length; i += 1) {
process.on('uncaughtException', currentUncaughtExceptionHandlers[i]);
}

ex.should.equal('SOME EXCEPTION');
ex.message.should.equal('SOME EXCEPTION');
done();
});

d.insert({ a: 5 }, function () {
d.findOne({ a : 5}, function (err, doc) {
if (tryCount === 0) {
tryCount += 1;
throw 'SOME EXCEPTION';
throw new Error('SOME EXCEPTION');
} else {
done('Callback was called twice');
done(new Error('Callback was called twice'));
}
});
});
Expand Down
30 changes: 18 additions & 12 deletions test/executor.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,26 @@ var should = require('chai').should()
// We prevent Mocha from catching the exception we throw on purpose by remembering all current handlers, remove them and register them back after test ends
function testThrowInCallback (d, done) {
var currentUncaughtExceptionHandlers = process.listeners('uncaughtException');

process.removeAllListeners('uncaughtException');

process.on('uncaughtException', function (err) {
// Do nothing with the error which is only there to test we stay on track
});

d.find({}, function (err) {
d.find({}, function (err) {
process.nextTick(function () {
d.insert({ bar: 1 }, function (err) {
process.removeAllListeners('uncaughtException');
for (var i = 0; i < currentUncaughtExceptionHandlers.length; i += 1) {
process.on('uncaughtException', currentUncaughtExceptionHandlers[i]);
}

done();
});
});
throw 'Some error';

throw new Error('Some error');
});
}

Expand All @@ -51,29 +52,34 @@ function testRightOrder (d, done) {

d.find({}, function (err, docs) {
docs.length.should.equal(0);

d.insert({ a: 1 }, function () {
d.update({ a: 1 }, { a: 2 }, {}, function () {
d.find({}, function (err, docs) {
docs[0].a.should.equal(2);

process.nextTick(function () {
d.update({ a: 2 }, { a: 3 }, {}, function () {
d.find({}, function (err, docs) {
docs[0].a.should.equal(3);
done();

process.removeAllListeners('uncaughtException');
for (var i = 0; i < currentUncaughtExceptionHandlers.length; i += 1) {
process.on('uncaughtException', currentUncaughtExceptionHandlers[i]);
}

done();
});
});
});
throw 'Some error';

throw new Error('Some error');
});
});
});
});
}
}



// Note: The following test does not have any assertion because it
Expand Down
Loading

0 comments on commit aefe40d

Please sign in to comment.