Can remove documents from an index

pull/2/head
Louis Chatriot 12 years ago
parent df3b6cdf61
commit abbaa1d308
  1. 18
      lib/indexes.js
  2. 45
      test/indexes.test.js

@ -1,11 +1,13 @@
var BinarySearchTree = require('binary-search-tree').BinarySearchTree var BinarySearchTree = require('binary-search-tree').BinarySearchTree
, model = require('./model') , model = require('./model')
, _ = require('underscore')
; ;
/** /**
* We can't use the one in model here since it doesn't work for arrays * We can't use the one in model here since it doesn't work for arrays
*/ */
function checkValueEquality (a, b) { function checkValueEquality (a, b) {
return a === b;
return model.compareThings(a, b) === 0; return model.compareThings(a, b) === 0;
} }
@ -30,7 +32,7 @@ function Index (options) {
/** /**
* Insert a new document in the index (synchronously) * Insert a new document in the index
*/ */
Index.prototype.insert = function (doc) { Index.prototype.insert = function (doc) {
var key = model.getDotValue(doc, this.fieldName); var key = model.getDotValue(doc, this.fieldName);
@ -45,6 +47,20 @@ Index.prototype.insert = function (doc) {
}; };
/**
* Remove a document from the index
*/
Index.prototype.remove = function (doc) {
var key = model.getDotValue(doc, this.fieldName);
if (key === undefined && this.sparse) {
this.nonindexedDocs = _.without(this.nonindexedDocs, doc);
return;
}
this.tree.delete(key, doc);
};
// Interface // Interface

@ -71,4 +71,49 @@ describe('Indexing', function () {
}); // ==== End of 'Insertion' ==== // }); // ==== End of 'Insertion' ==== //
describe('Removal', function () {
it('Can remove pointers from the index, even when multiple documents have the same key', function () {
var idx = new Index({ fieldName: 'tf' })
, doc1 = { a: 5, tf: 'hello' }
, doc2 = { a: 8, tf: 'world' }
, doc3 = { a: 2, tf: 'bloup' }
, doc4 = { a: 23, tf: 'world' }
;
idx.insert(doc1);
idx.insert(doc2);
idx.insert(doc3);
idx.insert(doc4);
idx.tree.getNumberOfKeys().should.equal(3);
idx.remove(doc1);
idx.tree.getNumberOfKeys().should.equal(2);
idx.tree.search('hello').length.should.equal(0);
idx.remove(doc2);
idx.tree.getNumberOfKeys().should.equal(2);
idx.tree.search('world').length.should.equal(1);
idx.tree.search('world')[0].should.equal(doc4);
});
it('If we have a sparse index, we remove the doc from the nonindexedDocs array', function () {
var idx = new Index({ fieldName: 'nope', sparse: true })
, doc1 = { a: 5, tf: 'hello' }
, doc2 = { a: 5, tf: 'world' }
;
idx.insert(doc1);
idx.insert(doc2);
idx.tree.getNumberOfKeys().should.equal(0);
assert.deepEqual(idx.nonindexedDocs, [doc1, doc2]);
idx.remove(doc1);
idx.tree.getNumberOfKeys().should.equal(0);
assert.deepEqual(idx.nonindexedDocs, [doc2]);
});
}); // ==== End of 'Removal' ==== //
}); });

Loading…
Cancel
Save