Skip to content

Commit

Permalink
Use index for where(XXX.defined => true)
Browse files Browse the repository at this point in the history
Closes #180
  • Loading branch information
nviennot committed Dec 26, 2015
1 parent e730dd6 commit 60294fc
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
7 changes: 6 additions & 1 deletion lib/no_brainer/criteria/where.rb
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ def get_usable_indexes(options={})
end

def find_strategy_canonical
clauses = get_candidate_clauses(:eq, :in, :between, :near, :intersects)
clauses = get_candidate_clauses(:eq, :in, :between, :near, :intersects, :defined)
return nil unless clauses.present?

usable_indexes = Hash[get_usable_indexes.map { |i| [[i.name], i] }]
Expand All @@ -384,6 +384,11 @@ def find_strategy_canonical
[:get_nearest, circle.center.to_rql, circle.options.merge(options)]
when :eq then [:get_all, [clause.value]]
when :in then [:get_all, clause.value]
when :defined then
next unless clause.value == true
next unless clause.key_modifier == :scalar && index.multi == false
[:between, [RethinkDB::RQL.new.minval, RethinkDB::RQL.new.maxval],
:left_bound => :open, :right_bound => :open]
when :between then [:between, [clause.value.min, clause.value.max],
:left_bound => :closed, :right_bound => :closed]
end
Expand Down
14 changes: 14 additions & 0 deletions spec/integration/criteria/where_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,20 @@
SimpleDocument.where(:field2.defined => 'FaLse').count.should == 1
end
end

context 'when using an index' do
before { SimpleDocument.index :field1 }
before { NoBrainer.sync_indexes }
after { NoBrainer.drop! }

it 'filters documents' do
SimpleDocument.where(:field1.defined => false).count.should == 1
SimpleDocument.where(:field1.defined => true).count.should == 2

SimpleDocument.where(:field1.defined => false).where_indexed?.should == false
SimpleDocument.where(:field1.defined => true).where_indexed?.should == true
end
end
end

context 'when using undefined' do
Expand Down

0 comments on commit 60294fc

Please sign in to comment.