Can get an array of all pointers in an index

pull/2/head
Louis Chatriot 12 years ago
parent 1b7366abd3
commit ae0937520b
  1. 4
      lib/datastore.js
  2. 23
      lib/indexes.js
  3. 2
      package.json
  4. 15
      test/indexes.test.js

@ -23,7 +23,10 @@ function Datastore (filename) {
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
this.indexes = {};
this.ensureIndex({ fieldName: '_id', unique: true });
}
@ -114,6 +117,7 @@ Datastore.prototype.removeFromIndexes = function (doc) {
/**
* Update a document in all indexes
* TODO: enable rollback if an update fails
*/
Datastore.prototype.updateIndexes = function (doc, newDoc) {
var self = this;

@ -134,12 +134,33 @@ Index.prototype.update = function (oldDoc, newDoc) {
* For now only works with field equality (i.e. can't use the index for $lt query for example)
* And doesn't return non indexed docs
* @param {Thing} value Value to match the key against
* @return {Array od documents}
* @return {Array of documents}
*/
Index.prototype.getMatching = function (value) {
return this.tree.search(value);
};
/**
* Get all elements in the index
* @return {Array of documents}
*/
Index.prototype.getAll = function () {
var res = [];
this.tree.executeOnEveryNode(function (node) {
var i;
for (i = 0; i < node.data.length; i += 1) {
res.push(node.data[i]);
}
});
return res;
};
// Interface
module.exports = Index;

@ -22,7 +22,7 @@
"dependencies": {
"async": "~0.2.8",
"underscore": "~1.4.4",
"binary-search-tree": "0.1.2"
"binary-search-tree": "0.1.3"
},
"devDependencies": {
"chai": "1.0.x",

@ -363,4 +363,19 @@ describe('Indexes', function () {
}); // ==== End of 'Resetting' ==== //
it('Get all elements in the index', function () {
var idx = new Index({ fieldName: 'a' })
, doc1 = { a: 5, tf: 'hello' }
, doc2 = { a: 8, tf: 'world' }
, doc3 = { a: 2, tf: 'bloup' }
;
idx.insert(doc1);
idx.insert(doc2);
idx.insert(doc3);
assert.deepEqual(idx.getAll(), [{ a: 2, tf: 'bloup' }, { a: 5, tf: 'hello' }, { a: 8, tf: 'world' }]);
});
});

Loading…
Cancel
Save