diff --git a/lib/datastore.js b/lib/datastore.js index 139ec2d..f3fecaa 100644 --- a/lib/datastore.js +++ b/lib/datastore.js @@ -47,11 +47,6 @@ function Datastore (options) { this.executor = new Executor(); if (this.inMemoryOnly) { this.executor.ready = true; } - // We keep internally the number of lines in the datafile - // This will be used when/if I implement autocompacting when the datafile grows too big - // For now it is not urgent as autocompaction happens upon every restart - this.datafileSize = 0; - // Indexed by field name, dot notation can be used // _id is always indexed and since _ids are generated randomly the underlying // binary is always well-balanced @@ -62,6 +57,14 @@ function Datastore (options) { } +/** + * Load the database from the datafile, and trigger the execution of buffered commands if any + */ +Datastore.prototype.loadDatabase = function () { + this.executor.push({ this: this.persistence, fn: this.persistence.loadDatabase, arguments: arguments }, true); +}; + + /** * Get an array of all the data in the database */ @@ -235,13 +238,6 @@ Datastore.prototype.getCandidates = function (query) { }; - -Datastore.prototype.loadDatabase = function () { - this.executor.push({ this: this.persistence, fn: this.persistence._loadDatabase, arguments: arguments }, true); -}; - - - /** * Insert a new document * @param {Function} cb Optional callback, signature: err, insertedDoc diff --git a/lib/persistence.js b/lib/persistence.js index 149507d..3859f1c 100644 --- a/lib/persistence.js +++ b/lib/persistence.js @@ -1,5 +1,8 @@ /** * Handle every persistence-related task + * The interface Datastore expects to be implemented is + * * Persistence.loadDatabase(callback) and callback has signature err + * * Persistence.persistNewState(newDocs, callback) where newDocs is an array of documents and callback has signature err */ var fs = require('fs') @@ -25,6 +28,11 @@ function Persistence (options) { if (this.filename && options.nodeWebkitAppName) { this.filename = Persistence.getNWAppFilename(options.nodeWebkitAppName, this.filename); } + + // We keep internally the number of lines in the datafile + // This will be used when/if I implement autocompacting when the datafile grows too big + // For now it is not urgent as autocompaction happens upon every restart + this.datafileSize = 0; }; @@ -109,7 +117,7 @@ Persistence.prototype.persistNewState = function (newDocs, cb) { // In-memory only datastore if (self.inMemoryOnly) { return callback(null); } - self.db.datafileSize += newDocs.length; + self.datafileSize += newDocs.length; newDocs.forEach(function (doc) { toPersist += model.serialize(doc) + '\n'; @@ -166,13 +174,13 @@ Persistence.treatRawData = function (rawData) { * * @api private Use loadDatabase */ -Persistence.prototype._loadDatabase = function (cb) { +Persistence.prototype.loadDatabase = function (cb) { var callback = cb || function () {} , self = this ; self.db.resetIndexes(); - self.db.datafileSize = 0; + self.datafileSize = 0; // In-memory only datastore if (self.inMemoryOnly) { return callback(null); } @@ -191,11 +199,11 @@ Persistence.prototype._loadDatabase = function (cb) { self.db.resetIndexes(treatedData); } catch (e) { self.db.resetIndexes(); // Rollback any index which didn't fail - self.db.datafileSize = 0; + self.datafileSize = 0; return cb(e); } - self.db.datafileSize = treatedData.length; + self.datafileSize = treatedData.length; self.db.persistence.persistCachedDatabase(cb); }); }); diff --git a/test/db.test.js b/test/db.test.js index c37bfb1..78f74d5 100644 --- a/test/db.test.js +++ b/test/db.test.js @@ -32,7 +32,7 @@ describe('Database', function () { , function (cb) { d.loadDatabase(function (err) { assert.isNull(err); - d.datafileSize.should.equal(0); + d.persistence.datafileSize.should.equal(0); d.getAllData().length.should.equal(0); return cb(); }); @@ -171,16 +171,16 @@ describe('Database', function () { }); it('datafileSize is incremented by 1 upon every insert', function (done) { - d.datafileSize.should.equal(0); + d.persistence.datafileSize.should.equal(0); d.getAllData().length.should.equal(0); d.insert({ a: 3 }, function () { - d.datafileSize.should.equal(1); + d.persistence.datafileSize.should.equal(1); d.getAllData().length.should.equal(1); d.insert({ a: 3 }, function () { - d.datafileSize.should.equal(2); + d.persistence.datafileSize.should.equal(2); d.getAllData().length.should.equal(2); d.insert({ a: 3 }, function () { - d.datafileSize.should.equal(3); + d.persistence.datafileSize.should.equal(3); d.getAllData().length.should.equal(3); done(); }); @@ -890,15 +890,15 @@ describe('Database', function () { d.insert({ a: 2 }, function () { d.insert({ a: 3 }, function () { d.insert({ a: 5 }, function () { - d.datafileSize.should.equal(3); + d.persistence.datafileSize.should.equal(3); d.getAllData().length.should.equal(3); d.update({ a: 3 }, { $set: { a: 4 } }, {}, function () { - d.datafileSize.should.equal(4); + d.persistence.datafileSize.should.equal(4); d.getAllData().length.should.equal(3); d.update({ a: { $in: [2, 4] } }, { $set: { a: 5 } }, { multi: true }, function () { - d.datafileSize.should.equal(6); + d.persistence.datafileSize.should.equal(6); d.getAllData().length.should.equal(3); done(); @@ -1062,15 +1062,15 @@ describe('Database', function () { d.insert({ a: 2 }, function () { d.insert({ a: 3 }, function () { d.insert({ a: 5 }, function () { - d.datafileSize.should.equal(3); + d.persistence.datafileSize.should.equal(3); d.getAllData().length.should.equal(3); d.remove({ a: 3 }, {}, function () { - d.datafileSize.should.equal(4); + d.persistence.datafileSize.should.equal(4); d.getAllData().length.should.equal(2); d.remove({ a: { $in: [2, 5] } }, { multi: true }, function () { - d.datafileSize.should.equal(6); + d.persistence.datafileSize.should.equal(6); d.getAllData().length.should.equal(0); done(); @@ -1096,12 +1096,12 @@ describe('Database', function () { ; d.getAllData().length.should.equal(0); - d.datafileSize.should.equal(0); + d.persistence.datafileSize.should.equal(0); fs.writeFile(testDb, rawData, 'utf8', function () { d.loadDatabase(function () { d.getAllData().length.should.equal(3); - d.datafileSize.should.equal(3); + d.persistence.datafileSize.should.equal(3); assert.deepEqual(Object.keys(d.indexes), ['_id']); @@ -1125,12 +1125,12 @@ describe('Database', function () { ; d.getAllData().length.should.equal(0); - d.datafileSize.should.equal(0); + d.persistence.datafileSize.should.equal(0); fs.writeFile(testDb, rawData, 'utf8', function () { d.loadDatabase(function () { d.getAllData().length.should.equal(2); - d.datafileSize.should.equal(2); + d.persistence.datafileSize.should.equal(2); assert.deepEqual(Object.keys(d.indexes), ['_id']); @@ -1182,7 +1182,7 @@ describe('Database', function () { ; d.getAllData().length.should.equal(0); - d.datafileSize.should.equal(0); + d.persistence.datafileSize.should.equal(0); d.ensureIndex({ fieldName: 'z' }); d.indexes.z.fieldName.should.equal('z'); @@ -1198,7 +1198,7 @@ describe('Database', function () { ; d.getAllData().length.should.equal(3); - d.datafileSize.should.equal(3); + d.persistence.datafileSize.should.equal(3); d.indexes.z.tree.getNumberOfKeys().should.equal(3); d.indexes.z.tree.search('1')[0].should.equal(doc1); @@ -1218,7 +1218,7 @@ describe('Database', function () { ; d.getAllData().length.should.equal(0); - d.datafileSize.should.equal(0); + d.persistence.datafileSize.should.equal(0); d.ensureIndex({ fieldName: 'z' }); d.ensureIndex({ fieldName: 'a' }); @@ -1233,7 +1233,7 @@ describe('Database', function () { ; d.getAllData().length.should.equal(3); - d.datafileSize.should.equal(3); + d.persistence.datafileSize.should.equal(3); d.indexes.z.tree.getNumberOfKeys().should.equal(3); d.indexes.z.tree.search('1')[0].should.equal(doc1); @@ -1258,7 +1258,7 @@ describe('Database', function () { ; d.getAllData().length.should.equal(0); - d.datafileSize.should.equal(0); + d.persistence.datafileSize.should.equal(0); d.ensureIndex({ fieldName: 'z', unique: true }); d.indexes.z.tree.getNumberOfKeys().should.equal(0); @@ -1268,7 +1268,7 @@ describe('Database', function () { err.errorType.should.equal('uniqueViolated'); err.key.should.equal("1"); d.getAllData().length.should.equal(0); - d.datafileSize.should.equal(0); + d.persistence.datafileSize.should.equal(0); d.indexes.z.tree.getNumberOfKeys().should.equal(0); done(); diff --git a/test/persistence.test.js b/test/persistence.test.js index 1f683f3..d4f440d 100644 --- a/test/persistence.test.js +++ b/test/persistence.test.js @@ -32,7 +32,7 @@ describe('Persistence', function () { , function (cb) { d.loadDatabase(function (err) { assert.isNull(err); - d.datafileSize.should.equal(0); + d.persistence.datafileSize.should.equal(0); d.getAllData().length.should.equal(0); return cb(); }); @@ -162,12 +162,12 @@ describe('Persistence', function () { ; d.getAllData().length.should.equal(0); - d.datafileSize.should.equal(0); + d.persistence.datafileSize.should.equal(0); fs.writeFile(testDb, rawData, 'utf8', function () { d.loadDatabase(function () { d.getAllData().length.should.equal(3); - d.datafileSize.should.equal(3); + d.persistence.datafileSize.should.equal(3); d.find({}, function (err, docs) { docs.sort(function (a, b) { return a._id - b._id; });