Made treatRawData tests pass

pull/2/head
Louis Chatriot 11 years ago
parent 099584d30e
commit 34a3ba2046
  1. 7
      lib/datastore.js
  2. 2
      lib/model.js
  3. 24
      lib/persistence.js
  4. 6
      test/db.test.js
  5. 3
      test/model.test.js
  6. 12
      test/persistence.test.js

@ -52,7 +52,7 @@ function Datastore (options) {
// binary is always well-balanced
this.indexes = {};
this.indexes._id = new Index({ fieldName: '_id', unique: true });
if (this.autoload) { this.loadDatabase(); }
}
@ -111,7 +111,10 @@ Datastore.prototype.ensureIndex = function (options, cb) {
return callback(e);
}
return callback(null);
this.persistence.persistNewState([{ $$indexCreated: options }], function (err) {
if (err) { return callback(err); }
return callback(null);
});
};

@ -26,7 +26,7 @@ var dateToJSON = function () { return { $$date: this.getTime() }; }
* But you really need to want it to trigger such behaviour, even when warned not to use '$' at the beginning of the field names...
*/
function checkKey (k, v) {
if (k[0] === '$' && !(k === '$$date' && typeof v === 'number') && !(k === '$$deleted' && v === true)) {
if (k[0] === '$' && !(k === '$$date' && typeof v === 'number') && !(k === '$$deleted' && v === true) && !(k === '$$indexCreated')) {
throw 'Field names cannot begin with the $ character';
}

@ -11,6 +11,7 @@ var fs = require('fs')
, async = require('async')
, mkdirp = require('mkdirp')
, customUtils = require('./customUtils')
, Index = require('./indexes')
;
@ -193,8 +194,10 @@ Persistence.prototype.persistNewState = function (newDocs, cb) {
Persistence.treatRawData = function (rawData) {
var data = rawData.split('\n')
, dataById = {}
, res = []
, i;
, tdata = []
, i
, indexes = {}
;
for (i = 0; i < data.length; i += 1) {
var doc;
@ -207,16 +210,18 @@ Persistence.treatRawData = function (rawData) {
} else {
dataById[doc._id] = doc;
}
} else if (doc.$$indexCreated && doc.$$indexCreated.fieldName != undefined) {
indexes[doc.$$indexCreated.fieldName] = doc.$$indexCreated;
}
} catch (e) {
}
}
Object.keys(dataById).forEach(function (k) {
res.push(dataById[k]);
tdata.push(dataById[k]);
});
return res;
return { data: tdata, indexes: indexes };
};
@ -246,6 +251,9 @@ Persistence.prototype.ensureDatafileIntegrity = function (callback) {
/**
* Load the database
* 1) Create all indexes
* 2) Insert all data
* 3) Compact the database
* This means pulling data out of the data file or creating it if it doesn't exist
* Also, all data is persisted right away, which has the effect of compacting the database file
* This operation is very quick at startup for a big collection (60ms for ~10k docs)
@ -269,8 +277,14 @@ Persistence.prototype.loadDatabase = function (cb) {
if (err) { return cb(err); }
var treatedData = Persistence.treatRawData(rawData);
// Recreate all indexes in the datafile
Object.keys(treatedData.indexes).forEach(function (key) {
self.db.indexes[key] = new Index(treatedData.indexes[key]);
});
// Fill cached database (i.e. all indexes) with data
try {
self.db.resetIndexes(treatedData);
self.db.resetIndexes(treatedData.data);
} catch (e) {
self.db.resetIndexes(); // Rollback any index which didn't fail
return cb(e);

@ -2009,7 +2009,7 @@ describe('Database', function () {
}); // ==== End of 'Removing indexes upon document update' ==== //
describe.only('Persisting indexes', function () {
describe.skip('Persisting indexes', function () {
it('If the persistIndexes options is used, indexes are persisted to a separate file and recreated upon reload', function (done) {
var persDb = "workspace/persistIndexes.db"
@ -2017,7 +2017,7 @@ describe('Database', function () {
;
if (fs.existsSync(persDb)) { fs.writeFileSync(persDb, '', 'utf8'); }
db = new Datastore({ filename: persDb, autoload: true, persistIndexes: true });
db = new Datastore({ filename: persDb, autoload: true });
Object.keys(db.indexes).length.should.equal(1);
Object.keys(db.indexes)[0].should.equal("_id");
@ -2035,7 +2035,7 @@ describe('Database', function () {
db.indexes.planet.getAll().length.should.equal(2);
// After a reload the indexes are recreated
db = new Datastore({ filename: persDb, persistIndexes: true });
db = new Datastore({ filename: persDb });
db.loadDatabase(function (err) {
assert.isNull(err);
Object.keys(db.indexes).length.should.equal(2);

@ -112,11 +112,12 @@ describe('Model', function () {
c.test[2].again.should.equal('yes');
});
it('Reject field names beginning with a $ sign or containing a dot, except the two edge cases', function () {
it('Reject field names beginning with a $ sign or containing a dot, except the three edge cases', function () {
var a1 = { $something: 'totest' }
, a2 = { "with.dot": 'totest' }
, e1 = { $$date: 4321 }
, e2 = { $$deleted: true }
, e3 = { $$indexCreated: "indexName" }
, b;
// Normal cases

@ -46,7 +46,7 @@ describe('Persistence', function () {
, rawData = model.serialize({ _id: "1", a: 2, ages: [1, 5, 12] }) + '\n' +
model.serialize({ _id: "2", hello: 'world' }) + '\n' +
model.serialize({ _id: "3", nested: { today: now } })
, treatedData = Persistence.treatRawData(rawData)
, treatedData = Persistence.treatRawData(rawData).data
;
treatedData.sort(function (a, b) { return a._id - b._id; });
@ -61,7 +61,7 @@ describe('Persistence', function () {
, rawData = model.serialize({ _id: "1", a: 2, ages: [1, 5, 12] }) + '\n' +
'garbage\n' +
model.serialize({ _id: "3", nested: { today: now } })
, treatedData = Persistence.treatRawData(rawData)
, treatedData = Persistence.treatRawData(rawData).data
;
treatedData.sort(function (a, b) { return a._id - b._id; });
@ -75,7 +75,7 @@ describe('Persistence', function () {
, rawData = model.serialize({ _id: "1", a: 2, ages: [1, 5, 12] }) + '\n' +
model.serialize({ _id: "2", hello: 'world' }) + '\n' +
model.serialize({ nested: { today: now } })
, treatedData = Persistence.treatRawData(rawData)
, treatedData = Persistence.treatRawData(rawData).data
;
treatedData.sort(function (a, b) { return a._id - b._id; });
@ -89,7 +89,7 @@ describe('Persistence', function () {
, rawData = model.serialize({ _id: "1", a: 2, ages: [1, 5, 12] }) + '\n' +
model.serialize({ _id: "2", hello: 'world' }) + '\n' +
model.serialize({ _id: "1", nested: { today: now } })
, treatedData = Persistence.treatRawData(rawData)
, treatedData = Persistence.treatRawData(rawData).data
;
treatedData.sort(function (a, b) { return a._id - b._id; });
@ -104,7 +104,7 @@ describe('Persistence', function () {
model.serialize({ _id: "2", hello: 'world' }) + '\n' +
model.serialize({ _id: "1", $$deleted: true }) + '\n' +
model.serialize({ _id: "3", today: now })
, treatedData = Persistence.treatRawData(rawData)
, treatedData = Persistence.treatRawData(rawData).data
;
treatedData.sort(function (a, b) { return a._id - b._id; });
@ -118,7 +118,7 @@ describe('Persistence', function () {
, rawData = model.serialize({ _id: "1", a: 2, ages: [1, 5, 12] }) + '\n' +
model.serialize({ _id: "2", $$deleted: true }) + '\n' +
model.serialize({ _id: "3", today: now })
, treatedData = Persistence.treatRawData(rawData)
, treatedData = Persistence.treatRawData(rawData).data
;
treatedData.sort(function (a, b) { return a._id - b._id; });

Loading…
Cancel
Save