Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

202 lines (174 sloc) 8.342 kb
# encoding: utf-8
#
require 'spec_helper'
describe 'Search#terminate_early' do
it 'terminates early' do
index = Picky::Index.new :terminate_early do
category :text1
category :text2
category :text3
category :text4
end
thing = Struct.new :id, :text1, :text2, :text3, :text4
index.add thing.new(1, 'hello', 'hello', 'hello', 'hello')
index.add thing.new(2, 'hello', 'hello', 'hello', 'hello')
index.add thing.new(3, 'hello', 'hello', 'hello', 'hello')
index.add thing.new(4, 'hello', 'hello', 'hello', 'hello')
index.add thing.new(5, 'hello', 'hello', 'hello', 'hello')
index.add thing.new(6, 'hello', 'hello', 'hello', 'hello')
try = Picky::Search.new index
try.search('hello').ids.should == [6, 5, 4, 3, 2, 1, 6, 5, 4, 3, 2, 1, 6, 5, 4, 3, 2, 1, 6, 5]
try = Picky::Search.new index
try.search('hello', 30).ids.should == [6, 5, 4, 3, 2, 1, 6, 5, 4, 3, 2, 1, 6, 5, 4, 3, 2, 1, 6, 5, 4, 3, 2, 1]
try = Picky::Search.new index do
terminate_early
end
try.search('hello', 3).ids.should == [6, 5, 4]
try = Picky::Search.new index do
terminate_early
end
try.search('hello', 9).ids.should == [6, 5, 4, 3, 2, 1, 6, 5, 4]
try = Picky::Search.new index do
terminate_early with_extra_allocations: 0
end
try.search('hello', 9).ids.should == [6, 5, 4, 3, 2, 1, 6, 5, 4]
try.search('hello', 9, 4).ids.should == [2, 1, 6, 5, 4, 3, 2, 1, 6]
try.search('hello', 9, 7).ids.should == [5, 4, 3, 2, 1, 6, 5, 4, 3]
try.search('hello', 9, 10).ids.should == [2, 1, 6, 5, 4, 3, 2, 1, 6]
try.search('hello', 9, 13).ids.should == [5, 4, 3, 2, 1, 6, 5, 4, 3]
try.search('hello', 9, 16).ids.should == [2, 1, 6, 5, 4, 3, 2, 1]
try.search('hello', 9, 19).ids.should == [5, 4, 3, 2, 1]
try.search('hello', 9, 22).ids.should == [2, 1]
try.search('hello', 9, 25).ids.should == []
try.search('hello', 9).to_hash[:allocations].size.should == 2
try.search('hello', 9, 4).to_hash[:allocations].size.should == 3
try.search('hello', 9, 7).to_hash[:allocations].size.should == 3
try.search('hello', 9, 10).to_hash[:allocations].size.should == 4
try.search('hello', 9, 13).to_hash[:allocations].size.should == 4
try.search('hello', 9, 16).to_hash[:allocations].size.should == 4
try.search('hello', 9, 19).to_hash[:allocations].size.should == 4
try.search('hello', 9, 22).to_hash[:allocations].size.should == 4
try.search('hello', 9, 25).to_hash[:allocations].size.should == 4
try = Picky::Search.new index do
terminate_early 0
end
try.search('hello').ids.should == [6, 5, 4, 3, 2, 1, 6, 5, 4, 3, 2, 1, 6, 5, 4, 3, 2, 1, 6, 5]
try = Picky::Search.new index do
terminate_early with_extra_allocations: 0
end
try.search('hello').ids.should == [6, 5, 4, 3, 2, 1, 6, 5, 4, 3, 2, 1, 6, 5, 4, 3, 2, 1, 6, 5]
try = Picky::Search.new index do
terminate_early 2
end
try.search('hello', 13).ids.should == [6, 5, 4, 3, 2, 1, 6, 5, 4, 3, 2, 1, 6]
try.search('hello', 13, 4).ids.should == [2, 1, 6, 5, 4, 3, 2, 1, 6, 5, 4, 3, 2]
try.search('hello', 13, 8).ids.should == [4, 3, 2, 1, 6, 5, 4, 3, 2, 1, 6, 5, 4]
try.search('hello', 13, 12).ids.should == [6, 5, 4, 3, 2, 1, 6, 5, 4, 3, 2, 1]
try.search('hello', 13, 16).ids.should == [2, 1, 6, 5, 4, 3, 2, 1]
try.search('hello', 13).to_hash[:allocations].size.should == 4
try.search('hello', 13, 4).to_hash[:allocations].size.should == 4
try.search('hello', 13, 8).to_hash[:allocations].size.should == 4
try.search('hello', 13, 12).to_hash[:allocations].size.should == 4
try.search('hello', 13, 16).to_hash[:allocations].size.should == 4
try.search('hello', 13).allocations.map(&:count).should == [6, 6, 6, 6]
try.search('hello', 13, 4).allocations.map(&:count).should == [6, 6, 6, 6]
try.search('hello', 13, 8).allocations.map(&:count).should == [6, 6, 6, 6]
try.search('hello', 13, 12).allocations.map(&:count).should == [6, 6, 6, 6]
try.search('hello', 13, 16).allocations.map(&:count).should == [6, 6, 6, 6]
try = Picky::Search.new index do
terminate_early with_extra_allocations: 2
end
try.search('hello').ids.should == [6, 5, 4, 3, 2, 1, 6, 5, 4, 3, 2, 1, 6, 5, 4, 3, 2, 1, 6, 5]
try = Picky::Search.new index do
terminate_early with_extra_allocations: 1234
end
try.search('hello').ids.should == [6, 5, 4, 3, 2, 1, 6, 5, 4, 3, 2, 1, 6, 5, 4, 3, 2, 1, 6, 5]
try = Picky::Search.new index do
terminate_early 1
end
try.search('hello', 1).ids.should == [6]
try.search('hello', 1, 4).ids.should == [2]
try.search('hello', 1, 8).ids.should == [4]
try.search('hello', 1, 12).ids.should == [6]
try.search('hello', 1, 16).ids.should == [2]
try.search('hello', 1).to_hash[:allocations].size.should == 2
try.search('hello', 1, 4).to_hash[:allocations].size.should == 2
try.search('hello', 1, 8).to_hash[:allocations].size.should == 3
try.search('hello', 1, 12).to_hash[:allocations].size.should == 4
try.search('hello', 1, 16).to_hash[:allocations].size.should == 4
try.search('hello', 1, 20).to_hash[:allocations].size.should == 4
try.search('hello', 1, 24).to_hash[:allocations].size.should == 4
try.search('hello', 1).allocations.map(&:count).should == [6, 6, nil, nil]
try.search('hello', 1, 4).allocations.map(&:count).should == [6, 6, nil, nil]
try.search('hello', 1, 8).allocations.map(&:count).should == [6, 6, 6, nil]
try.search('hello', 1, 12).allocations.map(&:count).should == [6, 6, 6, 6]
try.search('hello', 1, 16).allocations.map(&:count).should == [6, 6, 6, 6]
try.search('hello', 1, 20).allocations.map(&:count).should == [6, 6, 6, 6]
try.search('hello', 1, 24).allocations.map(&:count).should == [6, 6, 6, 6]
try.search('hello', 1, 0).to_hash.should == {
:allocations => [
[:terminate_early, 1.792, 6, [[:text1, "hello", "hello"]], [6]],
[:terminate_early, 1.792, 6, [[:text2, "hello", "hello"]], []]
],
:offset => 0,
:duration => anything,
:total => 12
}
try.search('hello', 1, 12).to_hash.should == {
:allocations => [
[:terminate_early, 1.792, 6, [[:text1, "hello", "hello"]], []],
[:terminate_early, 1.792, 6, [[:text2, "hello", "hello"]], []],
[:terminate_early, 1.792, 6, [[:text3, "hello", "hello"]], [6]],
[:terminate_early, 1.792, 6, [[:text4, "hello", "hello"]], []]
],
:offset => 12,
:duration => anything,
:total => 24
}
GC.start
try_slow = Picky::Search.new index
slow = performance_of do
try_slow.search 'hello'
end
try_fast = Picky::Search.new index do
terminate_early
end
fast = performance_of do
try_fast.search 'hello'
end
(slow/fast).should >= 1.1
try_slow = Picky::Search.new index
slow = performance_of do
try_slow.search 'hello hello'
end
try_fast = Picky::Search.new index do
terminate_early
end
fast = performance_of do
try_fast.search 'hello hello'
end
(slow/fast).should >= 1.4
try_slow = Picky::Search.new index
slow = performance_of do
try_slow.search 'hello hello hello'
end
try_fast = Picky::Search.new index do
terminate_early
end
fast = performance_of do
try_fast.search 'hello hello hello'
end
(slow/fast).should >= 1.7
try_slow = Picky::Search.new index
slow = performance_of do
try_slow.search 'hello hello hello hello'
end
try_fast = Picky::Search.new index do
terminate_early
end
fast = performance_of do
try_fast.search 'hello hello hello hello'
end
(slow/fast).should >= 2.0
end
end
Jump to Line
Something went wrong with that request. Please try again.