picky 4.22.0 → 4.23.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/picky/query/allocation.rb +4 -2
- data/lib/picky/query/allocations.rb +4 -4
- data/lib/picky/results.rb +11 -3
- data/spec/functional/sort_by_spec.rb +62 -0
- data/spec/functional/terminate_early_spec.rb +1 -1
- data/spec/lib/picky_spec.rb +1 -1
- data/spec/lib/query/allocations_spec.rb +36 -36
- data/spec/lib/results_spec.rb +2 -2
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e15709ee827c97bd8f7fa31bb40fea1b75e9dafc
|
4
|
+
data.tar.gz: 017a417c0c3b37fbf61950b12542cbd76b4127f8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 12c089ea8f406347266fe790b383e6e5e00ffb582ebfe852bea83bacb8be7cb1e4a807a04a84c7489aa6857f9456fc8c7639dbb21ab3e265880c2f0c68e234fe
|
7
|
+
data.tar.gz: ccc498547476f2e420a9dfa4a8ee13fba61c16a11b002d71990883f39ade4d5b77c3ab396c5f12420ce0582873d5d0530a7bdeedf9aee0bc1daa14e44cf776d0
|
@@ -84,21 +84,23 @@ module Picky
|
|
84
84
|
# * amount: The amount of ids to calculate.
|
85
85
|
# * offset: The offset to calculate them from.
|
86
86
|
#
|
87
|
-
def process! amount, offset
|
87
|
+
def process! amount, offset, sorting = nil
|
88
88
|
calculated_ids = calculate_ids amount, offset
|
89
|
+
calculated_ids.sort_by! &sorting if sorting
|
89
90
|
@count = calculated_ids.size # cache the count before throwing away the ids
|
90
91
|
@ids = calculated_ids.slice!(offset, amount) || [] # slice out the relevant part
|
91
92
|
end
|
92
93
|
# Same as the above, but with illegal ids. Parameter added:
|
93
94
|
# * illegal_ids: ids to ignore.
|
94
95
|
#
|
95
|
-
def process_with_illegals! amount, offset, illegal_ids
|
96
|
+
def process_with_illegals! amount, offset, illegal_ids, sorting = nil
|
96
97
|
# Note: Fairly inefficient calculation since it
|
97
98
|
# assumes the worst case that the ids contain
|
98
99
|
# all illegal ids.
|
99
100
|
#
|
100
101
|
calculated_ids = calculate_ids amount + illegal_ids.size, offset
|
101
102
|
calculated_ids = calculated_ids - illegal_ids
|
103
|
+
calculated_ids.sort_by! &sorting if sorting
|
102
104
|
@count = calculated_ids.size # cache the count before throwing away the ids
|
103
105
|
@ids = calculated_ids.slice!(offset, amount) || [] # slice out the relevant part
|
104
106
|
end
|
@@ -113,9 +113,9 @@ module Picky
|
|
113
113
|
#
|
114
114
|
# Note: It's possible that no ids are returned by an allocation, but a count. (In case of an offset)
|
115
115
|
#
|
116
|
-
def process! amount, offset = 0, terminate_early = nil
|
116
|
+
def process! amount, offset = 0, terminate_early = nil, sorting = nil
|
117
117
|
each do |allocation|
|
118
|
-
calculated_ids = allocation.process! amount, offset
|
118
|
+
calculated_ids = allocation.process! amount, offset, sorting
|
119
119
|
if calculated_ids.empty?
|
120
120
|
offset = offset - allocation.count unless offset.zero?
|
121
121
|
else
|
@@ -137,10 +137,10 @@ module Picky
|
|
137
137
|
#
|
138
138
|
# Note: Slower than #process! especially with large offsets.
|
139
139
|
#
|
140
|
-
def process_unique! amount, offset = 0, terminate_early = nil
|
140
|
+
def process_unique! amount, offset = 0, terminate_early = nil, sorting = nil
|
141
141
|
unique_ids = []
|
142
142
|
each do |allocation|
|
143
|
-
calculated_ids = allocation.process_with_illegals! amount, 0, unique_ids
|
143
|
+
calculated_ids = allocation.process_with_illegals! amount, 0, unique_ids, sorting
|
144
144
|
projected_offset = offset - allocation.count
|
145
145
|
unique_ids += calculated_ids # uniq this? <- No, slower than just leaving duplicates.
|
146
146
|
if projected_offset <= 0
|
data/lib/picky/results.rb
CHANGED
@@ -12,7 +12,8 @@ module Picky
|
|
12
12
|
attr_writer :duration
|
13
13
|
attr_reader :offset,
|
14
14
|
:amount,
|
15
|
-
:query
|
15
|
+
:query,
|
16
|
+
:sorting
|
16
17
|
|
17
18
|
# Takes instances of Query::Allocations as param.
|
18
19
|
#
|
@@ -24,6 +25,13 @@ module Picky
|
|
24
25
|
@extra_allocations = extra_allocations
|
25
26
|
@unique = unique
|
26
27
|
end
|
28
|
+
|
29
|
+
# Provide a block which
|
30
|
+
# accepts a result id.
|
31
|
+
#
|
32
|
+
def sort_by &sorting
|
33
|
+
@sorting = sorting
|
34
|
+
end
|
27
35
|
|
28
36
|
def allocations
|
29
37
|
prepare! *(@prepared || [@extra_allocations, @unique])
|
@@ -39,8 +47,8 @@ module Picky
|
|
39
47
|
return if @prepared == [extra_allocations, unique] # cached?
|
40
48
|
@prepared = [extra_allocations, unique] # cache!
|
41
49
|
unique ?
|
42
|
-
@allocations.process_unique!(amount, offset, extra_allocations) :
|
43
|
-
@allocations.process!(amount, offset, extra_allocations)
|
50
|
+
@allocations.process_unique!(amount, offset, extra_allocations, sorting) :
|
51
|
+
@allocations.process!(amount, offset, extra_allocations, sorting)
|
44
52
|
end
|
45
53
|
|
46
54
|
def each &block
|
@@ -0,0 +1,62 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
#
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
describe "id option" do
|
6
|
+
|
7
|
+
it 'can be given a different id (in-ruby based)' do
|
8
|
+
data = Picky::Index.new :id do
|
9
|
+
id :id
|
10
|
+
category :text, partial: Picky::Partial::Postfix.new(from: 1)
|
11
|
+
category :number
|
12
|
+
end
|
13
|
+
|
14
|
+
require 'ostruct'
|
15
|
+
|
16
|
+
thing = OpenStruct.new id: 1, number: 2, text: "aabcdef bcdef"
|
17
|
+
other = OpenStruct.new id: 2, number: 1, text: "abcdef bbcdef"
|
18
|
+
|
19
|
+
data.add thing
|
20
|
+
data.add other
|
21
|
+
|
22
|
+
sorting_order = {
|
23
|
+
thing.id => thing,
|
24
|
+
other.id => other
|
25
|
+
}
|
26
|
+
|
27
|
+
try = Picky::Search.new data
|
28
|
+
|
29
|
+
# Sort by number.
|
30
|
+
#
|
31
|
+
results = try.search("a")
|
32
|
+
|
33
|
+
results.sort_by { |id| sorting_order[id].number }
|
34
|
+
|
35
|
+
results.ids.should == [2,1]
|
36
|
+
|
37
|
+
# Sort by text.
|
38
|
+
#
|
39
|
+
results = try.search("a")
|
40
|
+
|
41
|
+
results.sort_by { |id| sorting_order[id].text }
|
42
|
+
|
43
|
+
results.ids.should == [1,2]
|
44
|
+
|
45
|
+
# Sort by number.
|
46
|
+
#
|
47
|
+
results = try.search("a* b")
|
48
|
+
|
49
|
+
results.sort_by { |id| sorting_order[id].number }
|
50
|
+
|
51
|
+
results.ids.should == [2,1]
|
52
|
+
|
53
|
+
# Sort by text.
|
54
|
+
#
|
55
|
+
results = try.search("a* b")
|
56
|
+
|
57
|
+
results.sort_by { |id| sorting_order[id].text }
|
58
|
+
|
59
|
+
results.ids.should == [1,2]
|
60
|
+
end
|
61
|
+
|
62
|
+
end
|
data/spec/lib/picky_spec.rb
CHANGED
@@ -93,15 +93,15 @@ describe Picky::Query::Allocations do
|
|
93
93
|
@offset = 1
|
94
94
|
end
|
95
95
|
it 'should call the process! method right' do
|
96
|
-
@allocation1.should_receive(:process!).once.with(5,1).and_return [2, 3, 4]
|
97
|
-
@allocation2.should_receive(:process!).once.with(2,0).and_return [5, 6]
|
96
|
+
@allocation1.should_receive(:process!).once.with(5,1,nil).and_return [2, 3, 4]
|
97
|
+
@allocation2.should_receive(:process!).once.with(2,0,nil).and_return [5, 6]
|
98
98
|
@allocation3.should_receive(:process!).never
|
99
99
|
|
100
100
|
@allocations.process! @amount, @offset, 0
|
101
101
|
end
|
102
102
|
it 'should call the process! method right including unique' do
|
103
|
-
@allocation1.should_receive(:process_with_illegals!).once.with(5,0,[]).and_return [2, 3, 4]
|
104
|
-
@allocation2.should_receive(:process_with_illegals!).once.with(2,0,[2,3,4]).and_return [5, 6]
|
103
|
+
@allocation1.should_receive(:process_with_illegals!).once.with(5,0,[],nil).and_return [2, 3, 4]
|
104
|
+
@allocation2.should_receive(:process_with_illegals!).once.with(2,0,[2,3,4],nil).and_return [5, 6]
|
105
105
|
@allocation3.should_receive(:process_with_illegals!).never
|
106
106
|
|
107
107
|
@allocations.process_unique! @amount, @offset, 0
|
@@ -113,7 +113,7 @@ describe Picky::Query::Allocations do
|
|
113
113
|
@offset = 0
|
114
114
|
end
|
115
115
|
it 'should call the process! method right' do
|
116
|
-
@allocation1.should_receive(:process!).once.with(1,0).and_return [1]
|
116
|
+
@allocation1.should_receive(:process!).once.with(1,0,nil).and_return [1]
|
117
117
|
@allocation2.should_receive(:process!).never
|
118
118
|
@allocation3.should_receive(:process!).never
|
119
119
|
|
@@ -126,9 +126,9 @@ describe Picky::Query::Allocations do
|
|
126
126
|
@offset = 1
|
127
127
|
end
|
128
128
|
it 'should call the process! method right' do
|
129
|
-
@allocation1.should_receive(:process!).once.with(5,1).and_return [2, 3, 4]
|
130
|
-
@allocation2.should_receive(:process!).once.with(2,0).and_return [5, 6]
|
131
|
-
@allocation3.should_receive(:process!).once.with(0,0).and_return []
|
129
|
+
@allocation1.should_receive(:process!).once.with(5,1,nil).and_return [2, 3, 4]
|
130
|
+
@allocation2.should_receive(:process!).once.with(2,0,nil).and_return [5, 6]
|
131
|
+
@allocation3.should_receive(:process!).once.with(0,0,nil).and_return []
|
132
132
|
|
133
133
|
@allocations.process! @amount, @offset, 1
|
134
134
|
end
|
@@ -139,8 +139,8 @@ describe Picky::Query::Allocations do
|
|
139
139
|
@offset = 0
|
140
140
|
end
|
141
141
|
it 'should call the process! method right' do
|
142
|
-
@allocation1.should_receive(:process!).once.with(1,0).and_return [1]
|
143
|
-
@allocation2.should_receive(:process!).once.with(0,0).and_return []
|
142
|
+
@allocation1.should_receive(:process!).once.with(1,0,nil).and_return [1]
|
143
|
+
@allocation2.should_receive(:process!).once.with(0,0,nil).and_return []
|
144
144
|
@allocation3.should_receive(:process!).never
|
145
145
|
|
146
146
|
@allocations.process! @amount, @offset, 1
|
@@ -152,7 +152,7 @@ describe Picky::Query::Allocations do
|
|
152
152
|
@offset = 0
|
153
153
|
end
|
154
154
|
it 'should call the process! method right' do
|
155
|
-
@allocation1.should_receive(:process!).once.with(4,0).and_return [1, 2, 3, 4]
|
155
|
+
@allocation1.should_receive(:process!).once.with(4,0,nil).and_return [1, 2, 3, 4]
|
156
156
|
@allocation2.should_receive(:process!).never
|
157
157
|
@allocation3.should_receive(:process!).never
|
158
158
|
|
@@ -165,8 +165,8 @@ describe Picky::Query::Allocations do
|
|
165
165
|
@offset = 0
|
166
166
|
end
|
167
167
|
it 'should call the process! method right' do
|
168
|
-
@allocation1.should_receive(:process!).once.with(4,0).and_return [1, 2, 3, 4]
|
169
|
-
@allocation2.should_receive(:process!).once.with(0,0).and_return []
|
168
|
+
@allocation1.should_receive(:process!).once.with(4,0,nil).and_return [1, 2, 3, 4]
|
169
|
+
@allocation2.should_receive(:process!).once.with(0,0,nil).and_return []
|
170
170
|
@allocation3.should_receive(:process!).never
|
171
171
|
|
172
172
|
@allocations.process! @amount, @offset, 1
|
@@ -178,8 +178,8 @@ describe Picky::Query::Allocations do
|
|
178
178
|
@offset = 0
|
179
179
|
end
|
180
180
|
it 'should call the process! method right' do
|
181
|
-
@allocation1.should_receive(:process!).once.with(5,0).and_return [1, 2, 3, 4]
|
182
|
-
@allocation2.should_receive(:process!).once.with(1,0).and_return [5]
|
181
|
+
@allocation1.should_receive(:process!).once.with(5,0,nil).and_return [1, 2, 3, 4]
|
182
|
+
@allocation2.should_receive(:process!).once.with(1,0,nil).and_return [5]
|
183
183
|
@allocation3.should_receive(:process!).never
|
184
184
|
|
185
185
|
@allocations.process! @amount, @offset, 0
|
@@ -191,9 +191,9 @@ describe Picky::Query::Allocations do
|
|
191
191
|
@offset = 0
|
192
192
|
end
|
193
193
|
it 'should call the process! method right' do
|
194
|
-
@allocation1.should_receive(:process!).once.with(5,0).and_return [1, 2, 3, 4]
|
195
|
-
@allocation2.should_receive(:process!).once.with(1,0).and_return [5]
|
196
|
-
@allocation3.should_receive(:process!).once.with(0,0).and_return []
|
194
|
+
@allocation1.should_receive(:process!).once.with(5,0,nil).and_return [1, 2, 3, 4]
|
195
|
+
@allocation2.should_receive(:process!).once.with(1,0,nil).and_return [5]
|
196
|
+
@allocation3.should_receive(:process!).once.with(0,0,nil).and_return []
|
197
197
|
|
198
198
|
@allocations.process! @amount, @offset, 1
|
199
199
|
end
|
@@ -204,9 +204,9 @@ describe Picky::Query::Allocations do
|
|
204
204
|
@offset = 0
|
205
205
|
end
|
206
206
|
it 'should call the process! method right' do
|
207
|
-
@allocation1.should_receive(:process!).once.with(8,0).and_return [1, 2, 3, 4]
|
208
|
-
@allocation2.should_receive(:process!).once.with(4,0).and_return [5, 6, 7]
|
209
|
-
@allocation3.should_receive(:process!).once.with(1,0).and_return [8]
|
207
|
+
@allocation1.should_receive(:process!).once.with(8,0,nil).and_return [1, 2, 3, 4]
|
208
|
+
@allocation2.should_receive(:process!).once.with(4,0,nil).and_return [5, 6, 7]
|
209
|
+
@allocation3.should_receive(:process!).once.with(1,0,nil).and_return [8]
|
210
210
|
|
211
211
|
@allocations.process! @amount, @offset, 1
|
212
212
|
end
|
@@ -218,9 +218,9 @@ describe Picky::Query::Allocations do
|
|
218
218
|
@offset = 2
|
219
219
|
end
|
220
220
|
it 'should call the process! method right' do
|
221
|
-
@allocation1.should_receive(:process!).once.with(6,2).and_return [3,4]
|
222
|
-
@allocation2.should_receive(:process!).once.with(4,0).and_return [5,6,7]
|
223
|
-
@allocation3.should_receive(:process!).once.with(1,0).and_return [8]
|
221
|
+
@allocation1.should_receive(:process!).once.with(6,2,nil).and_return [3,4]
|
222
|
+
@allocation2.should_receive(:process!).once.with(4,0,nil).and_return [5,6,7]
|
223
|
+
@allocation3.should_receive(:process!).once.with(1,0,nil).and_return [8]
|
224
224
|
|
225
225
|
@allocations.process! @amount, @offset
|
226
226
|
end
|
@@ -231,9 +231,9 @@ describe Picky::Query::Allocations do
|
|
231
231
|
@offset = 8
|
232
232
|
end
|
233
233
|
it 'should call the process! method right' do
|
234
|
-
@allocation1.should_receive(:process!).once.with(10,8).and_return []
|
235
|
-
@allocation2.should_receive(:process!).once.with(10,4).and_return []
|
236
|
-
@allocation3.should_receive(:process!).once.with(10,1).and_return [9]
|
234
|
+
@allocation1.should_receive(:process!).once.with(10,8,nil).and_return []
|
235
|
+
@allocation2.should_receive(:process!).once.with(10,4,nil).and_return []
|
236
|
+
@allocation3.should_receive(:process!).once.with(10,1,nil).and_return [9]
|
237
237
|
|
238
238
|
@allocations.process! @amount, @offset
|
239
239
|
end
|
@@ -243,9 +243,9 @@ describe Picky::Query::Allocations do
|
|
243
243
|
@amount = 0
|
244
244
|
end
|
245
245
|
it 'should return an empty array always' do
|
246
|
-
@allocation1.should_receive(:process!).once.with(0,0).and_return []
|
247
|
-
@allocation2.should_receive(:process!).once.with(0,0).and_return []
|
248
|
-
@allocation3.should_receive(:process!).once.with(0,0).and_return []
|
246
|
+
@allocation1.should_receive(:process!).once.with(0,0,nil).and_return []
|
247
|
+
@allocation2.should_receive(:process!).once.with(0,0,nil).and_return []
|
248
|
+
@allocation3.should_receive(:process!).once.with(0,0,nil).and_return []
|
249
249
|
|
250
250
|
@allocations.process! @amount
|
251
251
|
end
|
@@ -259,9 +259,9 @@ describe Picky::Query::Allocations do
|
|
259
259
|
@offset = 0
|
260
260
|
end
|
261
261
|
it 'should return certain ids' do
|
262
|
-
@allocation1.should_receive(:process!).once.with(3,0).and_return [1,2,3]
|
263
|
-
@allocation2.should_receive(:process!).once.with(0,0).and_return []
|
264
|
-
@allocation3.should_receive(:process!).once.with(0,0).and_return []
|
262
|
+
@allocation1.should_receive(:process!).once.with(3,0,nil).and_return [1,2,3]
|
263
|
+
@allocation2.should_receive(:process!).once.with(0,0,nil).and_return []
|
264
|
+
@allocation3.should_receive(:process!).once.with(0,0,nil).and_return []
|
265
265
|
|
266
266
|
@allocations.process! @amount, @offset
|
267
267
|
end
|
@@ -271,9 +271,9 @@ describe Picky::Query::Allocations do
|
|
271
271
|
@offset = 3
|
272
272
|
end
|
273
273
|
it 'should return certain ids' do
|
274
|
-
@allocation1.should_receive(:process!).once.with(3,3).and_return [4]
|
275
|
-
@allocation2.should_receive(:process!).once.with(2,0).and_return [5,6]
|
276
|
-
@allocation3.should_receive(:process!).once.with(0,0).and_return []
|
274
|
+
@allocation1.should_receive(:process!).once.with(3,3,nil).and_return [4]
|
275
|
+
@allocation2.should_receive(:process!).once.with(2,0,nil).and_return [5,6]
|
276
|
+
@allocation3.should_receive(:process!).once.with(0,0,nil).and_return []
|
277
277
|
|
278
278
|
@allocations.process! @amount, @offset
|
279
279
|
end
|
data/spec/lib/results_spec.rb
CHANGED
@@ -8,13 +8,13 @@ describe Picky::Results do
|
|
8
8
|
@results = described_class.new :unimportant, :amount, :offset, @allocations
|
9
9
|
end
|
10
10
|
it "forwards" do
|
11
|
-
@allocations.should_receive(:process!).once.with :amount, :offset, nil
|
11
|
+
@allocations.should_receive(:process!).once.with :amount, :offset, nil, nil
|
12
12
|
@allocations.should_receive(:ids).once.with :anything
|
13
13
|
|
14
14
|
@results.ids :anything
|
15
15
|
end
|
16
16
|
it "forwards and uses amount if nothing given" do
|
17
|
-
@allocations.should_receive(:process!).once.with :amount, :offset, nil
|
17
|
+
@allocations.should_receive(:process!).once.with :amount, :offset, nil, nil
|
18
18
|
@allocations.should_receive(:ids).once.with :amount
|
19
19
|
|
20
20
|
@results.ids
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: picky
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 4.
|
4
|
+
version: 4.23.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Florian Hanke
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-10-30 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -261,6 +261,7 @@ files:
|
|
261
261
|
- spec/functional/remap_qualifiers_spec.rb
|
262
262
|
- spec/functional/results_spec.rb
|
263
263
|
- spec/functional/similarity_spec.rb
|
264
|
+
- spec/functional/sort_by_spec.rb
|
264
265
|
- spec/functional/sorting_spec.rb
|
265
266
|
- spec/functional/speed_spec.rb
|
266
267
|
- spec/functional/static_spec.rb
|
@@ -442,6 +443,7 @@ test_files:
|
|
442
443
|
- spec/functional/remap_qualifiers_spec.rb
|
443
444
|
- spec/functional/results_spec.rb
|
444
445
|
- spec/functional/similarity_spec.rb
|
446
|
+
- spec/functional/sort_by_spec.rb
|
445
447
|
- spec/functional/sorting_spec.rb
|
446
448
|
- spec/functional/speed_spec.rb
|
447
449
|
- spec/functional/static_spec.rb
|