/
results.rb
98 lines (84 loc) · 2.44 KB
/
results.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
module Picky
# This is the internal results object. Usually, to_marshal, or to_json
# is called on it to get a string for the answer.
#
class Results
# Duration is set externally by the query.
#
attr_writer :duration
attr_reader :allocations,
:offset,
:amount,
:query
# Takes instances of Query::Allocations as param.
#
def initialize query = nil, amount = 0, offset = 0, allocations = Query::Allocations.new
@amount = amount
@query = query
@offset = offset
@allocations = allocations
end
# Create new results and calculate the ids.
#
def self.from query, amount, offset, allocations, extra_allocations = nil, unique = false
results = new query, amount, offset, allocations
results.prepare! extra_allocations, unique
results
end
# This starts the actual processing.
#
# Without this, the allocations are not processed,
# and no ids are calculated.
#
def prepare! extra_allocations = nil, unique = false
unique ?
allocations.process_unique!(amount, offset, extra_allocations) :
allocations.process!(amount, offset, extra_allocations)
end
# Delegates to allocations.
#
# Note that this is an expensive call and
# should not be done repeatedly. Just keep
# a reference to the result.
#
def ids only = amount
allocations.ids only
end
# The total results. Delegates to the allocations.
#
def total
@total ||= allocations.total || 0
end
# Duration default is 0.
#
def duration
@duration || 0
end
# Returns a hash with the allocations, offset, duration and total.
#
def to_hash
{ allocations: allocations.to_result,
offset: offset,
duration: duration,
total: total }
end
# Convert to json format.
#
def to_json options = {}
MultiJson.encode to_hash, options
end
# For logging.
#
@@log_time_format = "%Y-%m-%d %H:%M:%S".freeze
def to_s
"#{log_type}|#{Time.now.strftime @@log_time_format}|#{'%8f' % duration}|#{'%-50s' % query}|#{'%8d' % total}|#{'%4d' % offset}|#{'%2d' % allocations.size}|"
end
# The first character in the blog designates what type of query it is.
#
# No calculated ids means: No results.
#
def log_type
amount.zero?? :'.' : :'>'
end
end
end