Skip to content

Commit

Permalink
Clarify behavior of indexing: 'depth'.
Browse files Browse the repository at this point in the history
Fixes #39
  • Loading branch information
mcollina committed Sep 20, 2016
1 parent f6007c2 commit c7d0e3d
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 6 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,9 @@ Options are:
* `indexing`: it can be either `insertion` (default) or `depth`;
if set to `insertion`, it will try to match entries in insertion order;
if set to `depth`, it will try to match entries with the most
properties first.
properties first. Depth indexing is guaranteed if the patterns
overlaps. If multiple matching patterns overlaps it checks on the
first overlapping group of patterns that matches.

-------------------------------------------------------
<a name="add"></a>
Expand Down
4 changes: 2 additions & 2 deletions bloomrun.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ BloomRun.prototype.add = function (pattern, payload) {
return this
}

var buckets = matchingBuckets(this._buckets, pattern)
var buckets = matchingBuckets(this._buckets, pattern, null, this._isDeep)
var bucket
var properties = this._properties

Expand All @@ -95,7 +95,7 @@ BloomRun.prototype.add = function (pattern, payload) {
}

BloomRun.prototype.remove = function (pattern, payload) {
var matches = matchingBuckets(this._buckets, pattern)
var matches = matchingBuckets(this._buckets, pattern, null, this._isDeep)
payload = payload || null

if (matches.length > 0) {
Expand Down
2 changes: 1 addition & 1 deletion lib/iterator.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ function Iterator (parent, obj, opts) {
if (onlyRegex(obj)) {
this.buckets = parent._buckets
} else {
this.buckets = matchingBuckets(parent._buckets, obj, parent._properties)
this.buckets = matchingBuckets(parent._buckets, obj, parent._properties, parent._isDeep)
}
} else {
this.buckets = parent._buckets
Expand Down
4 changes: 2 additions & 2 deletions lib/matchingBuckets.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

var genKeys = require('./genKeys.js')

function matchingBuckets (buckets, pattern, set) {
function matchingBuckets (buckets, pattern, set, isDeep) {
var keys = genKeys(pattern, set)
var acc = []

Expand All @@ -11,7 +11,7 @@ function matchingBuckets (buckets, pattern, set) {
if (buckets[b].filter.test(keys[i])) {
acc.push(buckets[b])
break
} else if (set) {
} else if (set && !isDeep) {
// if there are known properties, we can be 100% sure
// that if a bloom filter returns false, then we don't need
// to test the other keys
Expand Down
23 changes: 23 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -473,3 +473,26 @@ test('mad string equality', function (t) {
t.deepEqual(instance.lookup({ to: '1', some: 'pattern' }), 'first')
t.deepEqual(instance.lookup({ to: '2', some: 'pattern' }), 'second')
})

test('List matches partially, regardless of key order (1)', function (t) {
t.plan(1)
var instance = bloomrun({ indexing: 'depth' })

instance.add({ c: 'CCC' }, 1)
instance.add({ b: 'BBB' }, 2)
instance.add({ a: 'AAA', b: 'BBB', c: 'CCC' }, 3)
instance.add({ a: 'AAA' }, 4)

t.deepEqual(instance.list({ a: 'AAA', b: 'BBB', c: 'CCC', d: 'DDD' }), [3, 1, 4, 2])
})

test('List matches partially, regardless of key order (2)', function (t) {
t.plan(1)
var instance = bloomrun({ indexing: 'depth' })

instance.add({ c: 'CCC' }, 2)
instance.add({ c: 'CCC', d: 'DDD' }, 1)
instance.add({ a: 'AAA' }, 3)

t.deepEqual(instance.list({ a: 'AAA', b: 'BBB', c: 'CCC', d: 'DDD' }), [1, 2, 3])
})

0 comments on commit c7d0e3d

Please sign in to comment.