Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Operate on entire index instead of 1 doc at a time

  • Loading branch information...
commit 39fe5006deacdc511130fe4fb9036770d9f35102 1 parent 05f5c44
@jeremyrsellars authored
Showing with 22 additions and 29 deletions.
  1. +20 −21 src/IndexSearcher.Test.coffee
  2. +2 −8 src/IndexSearcher.coffee
View
41 src/IndexSearcher.Test.coffee
@@ -1,13 +1,10 @@
-vows = require 'vows'
assert = require 'assert'
-indexSearcher = require './IndexSearcher.coffee'
-IndexSearcher = indexSearcher.IndexSearcher
+IndexSearcher = require('./IndexSearcher.coffee').IndexSearcher
BitArray = require 'bit-array'
-index = require './Index.coffee'
-Index = index.Index
+Index = require('./Index.coffee').Index
EmptyIndex = new Index
EmptyBitArray = new BitArray
@@ -21,38 +18,44 @@ AlternatingBitArray.set n, 1 for n in [1..7] by 2
First4OnBitArray = new BitArray
First4OnBitArray.set n, 1 for n in [0..3]
-
class AllTermsQuery
- IsMatchAtIndex: (index) -> true
+ search: (index) ->
+ b = new BitArray()
+ count = index.count()
+ b.set n, 1 for n in [0...count] if count
+ b
class AlternatingQuery
- IsMatchAtIndex: (index) -> index % 2
+ search: (index) ->
+ b = new BitArray()
+ count = index.count()
+ b.set n, n % 2 for n in [0...count] if count
+ b
class MatchLessThanQuery
- constructor: (value) -> @value = value
- IsMatchAtIndex: (index) -> index < @value
-
-class RequiresIndexSearcherQuery
- IsMatchAtIndex: (index, @searcher) ->
- assert.instanceOf @searcher, IndexSearcher
+ constructor: (@value) ->
return
+ search: (index) ->
+ b = new BitArray()
+ count = index.count()
+ b.set n, n < @value for n in [0...count] if count
+ b
describe 'IndexSearcher', ->
describe 'Given *new IndexSearcher(EmptyIndex)*' , ->
describe '*searchAllIndexes*', ->
it 'yields an *empty* bit array' , (done)->
searcher = new IndexSearcher EmptyIndex
- searcher.searchAllIndexes AllTermsQuery, (err, hits)->
+ searcher.searchAllIndexes new AllTermsQuery, (err, hits)->
assert.deepEqual hits, EmptyBitArray
done()
-
describe 'Given *new IndexSearcher(EightDocIndex)*' , ->
before (done)->
@searcher = new IndexSearcher EightDocIndex
done()
describe '*searchAllIndexes AllTermsQuery*', (done)->
it 'yields *8 matches*' , (done)->
- @searcher.searchAllIndexes new (AllTermsQuery), (err, hits)->
+ @searcher.searchAllIndexes new AllTermsQuery, (err, hits)->
assert.deepEqual hits, EightMatchesBitArray
done()
describe '*searchAllIndexes MatchLessThanQuery 4*', ->
@@ -65,7 +68,3 @@ describe 'IndexSearcher', ->
@searcher.searchAllIndexes new (AlternatingQuery), (err, hits)->
assert.deepEqual hits, AlternatingBitArray
done()
- describe '*searchAllIndexes RequiresIndexSearcherQuery*', ->
- it '*passes*' , (done)->
- @searcher.searchAllIndexes new (RequiresIndexSearcherQuery), (err, hits)->
- done()
View
10 src/IndexSearcher.coffee
@@ -1,14 +1,11 @@
BitArray = require 'bit-array'
-class IndexSearcher
+class module.exports.IndexSearcher
constructor: (index) ->
@index = index
searchAllIndexesSync: (query) ->
- b = new BitArray
- if @index.count()
- b.set n, query.IsMatchAtIndex(n, @) for n in [0..@index.count() - 1]
- return b
+ query.search @index
searchAllIndexes: (query, callback) ->
process.nextTick () =>
@@ -16,6 +13,3 @@ class IndexSearcher
callback null, x
return
return
-
-module.exports =
- IndexSearcher : IndexSearcher
Please sign in to comment.
Something went wrong with that request. Please try again.