Put datafileSize in the persistence module where it belongs

pull/2/head
Louis Chatriot 12 years ago
parent 4058b23ddb
commit 829441becb
  1. 20
      lib/datastore.js
  2. 18
      lib/persistence.js
  3. 42
      test/db.test.js
  4. 6
      test/persistence.test.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

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

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

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

Loading…
Cancel
Save