Skip to content

Commit

Permalink
Results rewrite.
Browse files Browse the repository at this point in the history
  • Loading branch information
floere committed May 3, 2014
1 parent 7f8cbbe commit d34d2c0
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 50 deletions.
19 changes: 10 additions & 9 deletions server/lib/picky/results.rb
Expand Up @@ -17,21 +17,18 @@ class Results

# Takes instances of Query::Allocations as param.
#
def initialize query = nil, amount = 0, offset = 0, allocations = Query::Allocations.new
def initialize query = nil, amount = 0, offset = 0, allocations = Query::Allocations.new, extra_allocations = nil, unique = false
@amount = amount
@query = query
@offset = offset
@allocations = allocations
@extra_allocations = extra_allocations
@unique = unique
end

# Create new results and calculate the ids.
#
# TODO Remove and calculate lazily via results.ids (rewrite all references to use Result.new).
#
def self.from query, amount, offset, allocations, extra_allocations = nil, unique = false
results = new query, amount, offset, allocations
results.prepare! extra_allocations, unique
results
def allocations
prepare! @extra_allocations, @unique
@allocations
end

# This starts the actual processing.
Expand All @@ -40,6 +37,8 @@ def self.from query, amount, offset, allocations, extra_allocations = nil, uniqu
# and no ids are calculated.
#
def prepare! extra_allocations = nil, unique = false
return if @prepared == [extra_allocations, unique]
@prepared = [extra_allocations, unique]
unique ?
allocations.process_unique!(amount, offset, extra_allocations) :
allocations.process!(amount, offset, extra_allocations)
Expand All @@ -55,6 +54,8 @@ def each &block
# should not be done repeatedly. Just keep
# a reference to the result.
#
# TODO Rewrite such that this triggers calculation, not prepare!
#
def ids only = amount
allocations.ids only
end
Expand Down
18 changes: 9 additions & 9 deletions server/lib/picky/search.rb
Expand Up @@ -138,7 +138,7 @@ def terminate_early extra_allocations = 0
def boost boosts
@boosts = extract_boosts boosts
end

# Ignore given categories and/or combinations of
# categories.
#
Expand All @@ -158,7 +158,7 @@ def ignore *allocations_and_categories
end
end
end

# Exclusively keep combinations of
# categories.
#
Expand All @@ -171,7 +171,7 @@ def ignore *allocations_and_categories
def only *allocations_and_categories
indexes.keep_allocations *allocations_and_categories
end

# Ignore the given token if it cannot be matched to a category.
# The default behaviour is that if a token does not match to
# any category, the query will not return anything (since a
Expand Down Expand Up @@ -236,12 +236,12 @@ def search_with tokens, ids = 20, offset = 0, original_text = nil, unique = fals
# Note: Internal method, use #search to search.
#
def execute tokens, ids, offset, original_text = nil, unique = false
Results.from original_text,
ids,
offset,
sorted_allocations(tokens, @max_allocations),
@extra_allocations,
unique
Results.new original_text,
ids,
offset,
sorted_allocations(tokens, @max_allocations),
@extra_allocations,
unique
end

# Forwards the tokenizing to the query tokenizer.
Expand Down
19 changes: 19 additions & 0 deletions server/spec/functional/results_spec.rb
Expand Up @@ -41,4 +41,23 @@
try.search("text:ohai").map(&:score).should == [0.693, 0.0]
end

it 'can re-prepare with different parameters' do
index1.category :text
index2.category :text

thing = OpenStruct.new id: 1, text: "ohai"
other = OpenStruct.new id: 2, text: "ohai kthxbye"

index1.add thing
index1.add other
index2.add thing

results = try.search "text:ohai"
results.ids.should == [2, 1, 1]

results.prepare! nil, true
results.ids.should == [2, 1]
results.ids.object_id.should_not == results.ids.object_id # Not cached.
end

end
41 changes: 9 additions & 32 deletions server/spec/lib/results_spec.rb
Expand Up @@ -2,49 +2,25 @@

describe Picky::Results do

describe "from" do
before(:each) do
@results = double :results
described_class.stub :new => @results

@results.stub :prepare!
end
it "should generate a result" do
described_class.from("some query", 20, 0, @allocations).should == @results
end
end

describe "ids" do
before(:each) do
@allocations = double :allocations
@results = described_class.new :unimportant, :amount, :unimportant, @allocations
@results = described_class.new :unimportant, :amount, :offset, @allocations
end
it "forwards" do
@allocations.should_receive(:process!).once.with :amount, :offset, nil
@allocations.should_receive(:ids).once.with :anything

@results.ids :anything
end
it "forwards and uses amount if nothing given" do
@allocations.should_receive(:process!).once.with :amount, :offset, nil
@allocations.should_receive(:ids).once.with :amount

@results.ids
end
end

describe "each" do
before(:each) do
@allocations = [1, 2, 3]
@results = described_class.new :unimportant, :amount, :unimportant, @allocations
end
it "forwards" do
expected = [1, 2, 3]
@results.each do |i|
expected.delete(i).should == i
end
expected.should be_empty
end
end

describe 'to_s time format' do
it 'is in the right format' do
described_class.new("some_query").to_s.should match(/\d{2}\-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/)
Expand All @@ -67,12 +43,12 @@
context 'with results' do
before(:each) do
@allocations = double :allocations,
:process! => nil,
:size => 12
:process! => nil,
:size => 12

@results = described_class.new "some_query", 20, 1234, @allocations
@results.stub :duration => 0.1234567890,
:total => 12345678
:total => 12345678
end
it 'should output a specific log' do
@results.to_s.should == '>|2011-08-16 10:07:33|0.123457|some_query |12345678|1234|12|'
Expand Down Expand Up @@ -107,7 +83,8 @@

describe "accessors" do
before(:each) do
@results = described_class.new :query, :amount, :offset, :allocations
@allocations = double :allocations, :process! => :allocations
@results = described_class.new :query, :amount, :offset, @allocations
end
it "should have accessors for query" do
@results.query.should == :query
Expand All @@ -119,7 +96,7 @@
@results.offset.should == :offset
end
it "should have accessors for allocations" do
@results.allocations.should == :allocations
@results.allocations.should == @allocations
end
it "should have accessors for duration" do
@results.duration = :some_duration
Expand Down

0 comments on commit d34d2c0

Please sign in to comment.