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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
|
# File 'lib/acts_as_hashish/hashish.rb', line 60
def hashish_list(options = {})
page_no = options[:page_no] || 1
page_size = options[:page_size] || 10
max_time = options[:to] || '+inf'
min_time = options[:from] || '-inf'
filters = options[:filters] || {}
sort_by = options[:sort_by] || nil
sort_order = options[:sort_order] || nil
offset = (page_no - 1) * page_size
limit = offset + page_size
seq = Hashish.redis_connection.incr("#{@options[:key_prefix]}#SEQUENCER")
inter = []
unless filters.empty?
filters.each do |key, value|
if value.is_a?(Array)
union = []
union_key = "#{@options[:key_prefix]}#UNION##{key}##{seq}"
value.each do |v|
union << "#{@options[:key_prefix]}!#{key}=#{v}"
end
Hashish.redis_connection.zunionstore(union_key, union.uniq)
Hashish.redis_connection.expire(union_key, Hashish.redis_search_keys_ttl)
inter << union_key
else
inter << "#{@options[:key_prefix]}!#{key}=#{value}"
end
end
end
full_list = "#{@options[:key_prefix]}*"
if min_time == '-inf' and max_time == '+inf'
inter << full_list
else
all_items_key = "#{@options[:key_prefix]}#ALL_ITEMS##{seq}"
Hashish.redis_connection.zunionstore(all_items_key, [full_list])
Hashish.redis_connection.expire(all_items_key, Hashish.redis_search_keys_ttl * 60)
Hashish.redis_connection.zremrangebyscore(all_items_key, "-inf", "(#{min_time}") if min_time != '-inf'
Hashish.redis_connection.zremrangebyscore(all_items_key, "(#{max_time}", "+inf") if max_time != '+inf'
inter << all_items_key
end
result_key = "#{@options[:key_prefix]}#RESULT##{seq}"
Hashish.redis_connection.zinterstore(result_key, inter, :aggregate => 'max')
Hashish.redis_connection.expire(result_key, Hashish.redis_search_keys_ttl * 60)
result = nil
if sort_by
custom_sort_key = "#{@options[:key_prefix]}#CUSTOM_SORT##{seq}"
Hashish.redis_connection.sort(result_key, :by => "*$#{sort_by}",:get => '*', :store => custom_sort_key, :order => sort_order)
Hashish.redis_connection.expire(custom_sort_key, Hashish.redis_search_keys_ttl * 60)
result = Hashish.redis_connection.lrange(custom_sort_key, offset, limit -1)
else
res_keys = Hashish.redis_connection.send("z#{(sort_order == 'DESC' ? 'rev' : '')}range".to_sym, result_key, offset, limit - 1)
if res_keys.empty?
result = []
else
result = Hashish.redis_connection.mget(*res_keys)
end
end
result.compact.map{|x| JSON.parse(x) rescue x}
end
|