ensureIndex init works right after a database load

pull/2/head
Louis Chatriot 12 years ago
parent a67cd4601e
commit b941485450
  1. 2
      benchmarks/commonUtilities.js
  2. 3
      lib/datastore.js
  3. 40
      test/db.test.js
  4. 2
      test/indexes.test.js

@ -179,7 +179,7 @@ module.exports.removeDocs = function (options, d, n, profiler, cb) {
function runFrom(i) {
if (i === n) { // Finished
console.log("Average time for one remove in a collection of " + n + " docs: " + (profiler.elapsedSinceLastStep() / n) + "ms");
console.log("Average time for one remove and one insert in a collection of " + n + " docs: " + (profiler.elapsedSinceLastStep() / n) + "ms");
profiler.step('Finished removing ' + n + ' docs');
return cb();
}

@ -407,7 +407,7 @@ Datastore.prototype.update = function () {
Datastore.prototype._remove = function (query, options, cb) {
var callback
, self = this
, candidates = this.getCandidates(query)
//, candidates = this.getCandidates(query)
, numRemoved = 0
, multi
, newData = []
@ -423,6 +423,7 @@ Datastore.prototype._remove = function (query, options, cb) {
if (model.match(d, query) && (multi || numRemoved === 0)) {
numRemoved += 1;
removedDocs.push({ $$deleted: true, _id: d._id });
self.removeFromIndexes(d);
} else {
newData.push(d);
}

@ -1107,4 +1107,44 @@ describe('Database', function () {
}); // ==== End of 'Remove' ==== //
describe.only('Using indexes', function () {
describe('ensureIndex', function () {
it('ensureIndex can be called right after a loadDatabase and be initialized and filled correctly', function (done) {
var now = new Date()
, 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 } })
;
d.data.length.should.equal(0);
d.datafileSize.should.equal(0);
fs.writeFile(testDb, rawData, 'utf8', function () {
d.loadDatabase(function () {
d.data.length.should.equal(3);
d.datafileSize.should.equal(3);
assert.deepEqual(d.indexes, {});
d.ensureIndex({ fieldName: '_id' });
d.indexes._id.fieldName.should.equal('_id');
d.indexes._id.unique.should.equal(false);
d.indexes._id.sparse.should.equal(false);
d.indexes._id.tree.getNumberOfKeys().should.equal(3);
d.indexes._id.tree.search('1')[0].should.equal(d.data[0]);
d.indexes._id.tree.search('2')[0].should.equal(d.data[1]);
d.indexes._id.tree.search('3')[0].should.equal(d.data[2]);
done();
});
});
});
});
}); // ==== End of 'Using indexes' ==== //
});

@ -7,7 +7,7 @@ var Index = require('../lib/indexes')
, model = require('../lib/model')
;
describe('Indexing', function () {
describe('Indexes', function () {
describe('Insertion', function () {

Loading…
Cancel
Save