Housekeeping!

pull/2/head
James M. Greene 9 years ago
parent 5d190f17c5
commit aefe40d0a5
  1. 9
      lib/datastore.js
  2. 21
      lib/indexes.js
  3. 8
      lib/storage.js
  4. 12
      test/db.test.js
  5. 12
      test/executor.test.js
  6. 1
      test/indexes.test.js
  7. 8
      test/persistence.test.js

@ -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);

@ -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) {
@ -262,9 +254,8 @@ Index.prototype.getMatching = function (value) {
Object.keys(_res).forEach(function (_id) {
res.push(_res[_id]);
});
return res;
}
return res;
};

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

@ -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"));
});
});
@ -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
@ -399,11 +399,13 @@ 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();
});
@ -411,9 +413,9 @@ describe('Database', 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'));
}
});
});

@ -25,6 +25,7 @@ function testThrowInCallback (d, done) {
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]);
}
@ -33,7 +34,7 @@ function testThrowInCallback (d, done) {
});
});
throw 'Some error';
throw new Error('Some error');
});
}
@ -61,13 +62,18 @@ function testRightOrder (d, done) {
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');
});
});
});

@ -121,6 +121,7 @@ describe('Indexes', function () {
assert.deepEqual(idx.tree.search('bloup'), []);
});
describe('Array fields', function () {
it('Inserts one entry per array element in the index', function () {

@ -670,7 +670,7 @@ describe('Persistence', function () {
fs.existsSync(testDb).should.equal(true);
fs.existsSync(testDb + '~').should.equal(false);
if (!contents.match(/^{"hello":"world","_id":"[0-9a-zA-Z]{16}"}\n$/)) {
throw "Datafile contents not as expected";
throw new Error("Datafile contents not as expected");
}
done();
});
@ -697,7 +697,7 @@ describe('Persistence', function () {
fs.existsSync(testDb).should.equal(true);
fs.existsSync(testDb + '~').should.equal(false);
if (!contents.match(/^{"hello":"world","_id":"[0-9a-zA-Z]{16}"}\n$/)) {
throw "Datafile contents not as expected";
throw new Error("Datafile contents not as expected");
}
done();
});
@ -721,7 +721,7 @@ describe('Persistence', function () {
fs.existsSync(testDb).should.equal(true);
fs.existsSync(testDb + '~').should.equal(false);
if (!contents.match(/^{"hello":"world","_id":"[0-9a-zA-Z]{16}"}\n$/)) {
throw "Datafile contents not as expected";
throw new Error("Datafile contents not as expected");
}
done();
});
@ -743,7 +743,7 @@ describe('Persistence', function () {
fs.existsSync(dbFile).should.equal(true);
fs.existsSync(dbFile + '~').should.equal(false);
if (contents != "") {
throw "Datafile contents not as expected";
throw new Error("Datafile contents not as expected");
}
done();
});

Loading…
Cancel
Save