Class: Picky::Query::Allocations

Inherits:
Object
  • Object
show all
Defined in:
lib/picky/query/allocations.rb

Overview

Container class for Allocation s.

This class is asked by the Results class to compile and process a query. It then asks each Allocation to process their ids and scores.

It also offers convenience methods to access #ids of its Allocation s.

Instance Method Summary collapse

Constructor Details

#initialize(allocations = []) ⇒ Allocations



25
26
27
# File 'lib/picky/query/allocations.rb', line 25

def initialize allocations = []
  @allocations = allocations
end

Instance Method Details

#calculate_score(boosts) ⇒ Object

Score each allocation.



31
32
33
34
35
# File 'lib/picky/query/allocations.rb', line 31

def calculate_score boosts
  @allocations.each do |allocation|
    allocation.calculate_score boosts
  end
end

#calculate_totalObject



168
169
170
171
172
# File 'lib/picky/query/allocations.rb', line 168

def calculate_total
  inject(0) do |total, allocation|
    total + (allocation.count or return total)
  end
end

#ids(amount = 20) ⇒ Object

Returns the top amount ids.



92
93
94
95
96
# File 'lib/picky/query/allocations.rb', line 92

def ids amount = 20
  inject([]) do |total, allocation|
    total.size >= amount ? (return total.shift(amount)) : total + allocation.ids
  end
end

#keep_allocations(qualifiers_array) ⇒ Object

Keeps allocations.

Only those passed in are kept.

TODO Rewrite, speed up.



80
81
82
83
84
85
86
87
88
# File 'lib/picky/query/allocations.rb', line 80

def keep_allocations qualifiers_array
  return if qualifiers_array.empty?
  @allocations.select! do |allocation|
    allocation_qualifiers = allocation.combinations.to_qualifiers.clustered_uniq
    next(true) if qualifiers_array.any? do |qualifiers|
      allocation_qualifiers == qualifiers
    end
  end
end

#process!(amount, offset = 0, terminate_early = nil, sorting = nil) ⇒ Object

This is the main method of this class that will replace ids and count.

What it does is calculate the ids and counts of its allocations for being used in the results. It also calculates the total

Parameters:

* amount: the amount of ids to calculate
* offset: the offset from where in the result set to take the ids
* terminate_early: Whether to calculate all allocations.

Note: With an amount of 0, an offset > 0 doesn’t make much

sense, as seen in the live search.

Note: Each allocation caches its count, but not its ids (thrown away).

The ids are cached in this class.

Note: It’s possible that no ids are returned by an allocation, but a count. (In case of an offset)



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/picky/query/allocations.rb', line 116

def process! amount, offset = 0, terminate_early = nil, sorting = nil
  each do |allocation|
    sorting = nil if amount <= 0 # Stop sorting if the results aren't shown.
    calculated_ids = allocation.process! amount, offset, sorting
    if calculated_ids.empty?
      offset = offset - allocation.count unless offset.zero?
    else
      amount = amount - calculated_ids.size # we need less results from the following allocation
      offset = 0                            # we have already passed the offset
    end
    if terminate_early && amount <= 0
      break if terminate_early <= 0
      terminate_early -= 1
    end
  end
end

#process_unique!(amount, offset = 0, terminate_early = nil, sorting = nil) ⇒ Object

Same as #process! but removes duplicate ids from results.

Note that in the result later on an allocation won’t be included if it contains no ids (even in case they have been eliminated by the unique constraint in this method).

Note: Slower than #process! especially with large offsets.



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/picky/query/allocations.rb', line 141

def process_unique! amount, offset = 0, terminate_early = nil, sorting = nil
  unique_ids = []
  each do |allocation|
    sorting = nil if amount <= 0 # Stop sorting if the results aren't shown.
    calculated_ids = allocation.process_with_illegals! amount, 0, unique_ids, sorting
    projected_offset = offset - allocation.count
    unique_ids += calculated_ids # uniq this? <- No, slower than just leaving duplicates.
    if projected_offset <= 0
      allocation.ids.slice!(0, offset)
    end
    offset = projected_offset
    unless calculated_ids.empty?
      amount = amount - calculated_ids.size # we need less results from the following allocation
      offset = 0                            # we have already passed the offset
    end
    if terminate_early && amount <= 0
      break if terminate_early <= 0
      terminate_early -= 1
    end
  end
end

#reduce_to(amount) ⇒ Object

Reduces the amount of allocations to x.



45
46
47
# File 'lib/picky/query/allocations.rb', line 45

def reduce_to amount
  @allocations = @allocations.shift amount
end

#remove_allocations(qualifiers_array) ⇒ Object

Removes allocations.

Only those passed in are removed.

TODO Rewrite, speed up.



63
64
65
66
67
68
69
70
71
72
# File 'lib/picky/query/allocations.rb', line 63

def remove_allocations qualifiers_array
  return if qualifiers_array.empty?
  @allocations.select! do |allocation|
    allocation_qualifiers = allocation.combinations.to_qualifiers.clustered_uniq
    next(false) if qualifiers_array.any? do |qualifiers|
      allocation_qualifiers == qualifiers
    end
    allocation
  end
end

#remove_categories(categories = []) ⇒ Object

Removes categories from allocations.

Only those passed in are removed.



53
54
55
# File 'lib/picky/query/allocations.rb', line 53

def remove_categories categories = []
  @allocations.each { |allocation| allocation.remove categories } unless categories.empty?
end

#sort!Object

Sort the allocations.



39
40
41
# File 'lib/picky/query/allocations.rb', line 39

def sort!
  @allocations.sort!
end

#to_aObject



178
179
180
# File 'lib/picky/query/allocations.rb', line 178

def to_a
  @allocations
end

#to_resultObject

Allocations for results are in the form: [

allocation1.to_result,
allocation2.to_result
...

]



189
190
191
# File 'lib/picky/query/allocations.rb', line 189

def to_result
  @allocations.map { |allocation| allocation.to_result }.compact
end

#to_sObject

Simply inspects the internal allocations.



195
196
197
# File 'lib/picky/query/allocations.rb', line 195

def to_s
  to_result.inspect
end

#totalObject

The total is simply the sum of the counts of all allocations.



165
166
167
# File 'lib/picky/query/allocations.rb', line 165

def total
  @total ||= calculate_total
end

#uniq!Object



174
175
176
# File 'lib/picky/query/allocations.rb', line 174

def uniq!
  @allocations.uniq!
end