From 4c9113712fbd9ff611d9a35b53b548e60f0be182 Mon Sep 17 00:00:00 2001 From: Louis Chatriot Date: Tue, 28 May 2013 20:08:29 +0200 Subject: [PATCH] Can return all matching documents from the index --- lib/indexes.js | 8 +++++++- test/indexes.test.js | 23 +++++++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/lib/indexes.js b/lib/indexes.js index 930e1ce..52a573e 100644 --- a/lib/indexes.js +++ b/lib/indexes.js @@ -75,8 +75,14 @@ Index.prototype.update = function (oldDoc, newDoc) { /** - * Search + * Get all documents in index that match the query on fieldName + * For now only works with field equality (i.e. can't use the index for $lt query for example) + * @param {Thing} value Value to match the key against + * @return {Array od documents} */ +Index.prototype.getMatching = function (value) { + return this.tree.search(value); +}; // Interface diff --git a/test/indexes.test.js b/test/indexes.test.js index 1c2eaae..28db8df 100644 --- a/test/indexes.test.js +++ b/test/indexes.test.js @@ -192,4 +192,27 @@ describe('Indexing', function () { }); // ==== End of 'Update' ==== // + + describe('Get matching documents', function () { + + it('Get all documents where fieldName is equal to the given value, or an empty array if no match', 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); + + assert.deepEqual(idx.getMatching('bloup'), [doc3]); + assert.deepEqual(idx.getMatching('world'), [doc2, doc4]); + assert.deepEqual(idx.getMatching('nope'), []); + }); + + }); // ==== End of 'Get matching documents' ==== // + });