activeredis 0.0.5 → 0.0.6
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/activeredis.rb +10 -5
- data/lib/activeredis/version.rb +1 -1
- metadata +2 -3
- data/lib/old_activeredis.rb +0 -326
data/lib/activeredis.rb
CHANGED
@@ -237,7 +237,9 @@ module ActiveRedis
|
|
237
237
|
# TODO Interim fix, "QUEUED" is find(id) rescue
|
238
238
|
ids = connection.zrange "#{key_namespace}:all", 0, count
|
239
239
|
ids.each do |id|
|
240
|
-
|
240
|
+
record = find(id)
|
241
|
+
records << record if record
|
242
|
+
#records << find(id)
|
241
243
|
end
|
242
244
|
records
|
243
245
|
end
|
@@ -259,7 +261,8 @@ module ActiveRedis
|
|
259
261
|
def self.find(id)
|
260
262
|
return find_all if id == :all
|
261
263
|
exists = connection.zscore "#{key_namespace}:all", id
|
262
|
-
|
264
|
+
return nil unless exists
|
265
|
+
#raise RecordNotFound.new("Couldn't find #{self.name} with ID=#{id}") unless exists
|
263
266
|
attributes = connection.hgetall "#{key_namespace}:#{id}:attributes"
|
264
267
|
obj = self.new attributes, id
|
265
268
|
return obj
|
@@ -269,7 +272,7 @@ module ActiveRedis
|
|
269
272
|
finded = []
|
270
273
|
records = find_all
|
271
274
|
records.each do |record|
|
272
|
-
if record.attributes[field.to_s] == value.to_s
|
275
|
+
if record && record.attributes[field.to_s] == value.to_s
|
273
276
|
finded << record
|
274
277
|
end
|
275
278
|
end
|
@@ -282,7 +285,7 @@ module ActiveRedis
|
|
282
285
|
ids = connection.zrange "#{key_namespace}:all", 0, count
|
283
286
|
ids.each do |id|
|
284
287
|
record = find(id)
|
285
|
-
if record.attributes[field.to_s] == value.to_s
|
288
|
+
if record && record.attributes[field.to_s] == value.to_s
|
286
289
|
return record
|
287
290
|
end
|
288
291
|
end
|
@@ -290,11 +293,13 @@ module ActiveRedis
|
|
290
293
|
end
|
291
294
|
|
292
295
|
def self.fast_find_by_param(field, value)
|
293
|
-
|
296
|
+
find_id = nil
|
297
|
+
if ActiveRedis.fast_find_field == field
|
294
298
|
find_id = connection.hget "#{key_namespace}:fast_find_field", value
|
295
299
|
elsif field == :no
|
296
300
|
find_id = connection.hget "#{key_namespace}:no", value
|
297
301
|
end
|
302
|
+
find_id
|
298
303
|
end
|
299
304
|
|
300
305
|
def self.method_missing(name, *args)
|
data/lib/activeredis/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: activeredis
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.6
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -10,7 +10,7 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date: 2013-
|
13
|
+
date: 2013-04-16 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: redis
|
@@ -79,7 +79,6 @@ files:
|
|
79
79
|
- benchmark_results.txt
|
80
80
|
- lib/activeredis.rb
|
81
81
|
- lib/activeredis/version.rb
|
82
|
-
- lib/old_activeredis.rb
|
83
82
|
- sample/user.rb
|
84
83
|
- spec/active_redis_spec.rb
|
85
84
|
- spec/spec_helper.rb
|
data/lib/old_activeredis.rb
DELETED
@@ -1,326 +0,0 @@
|
|
1
|
-
require 'redis'
|
2
|
-
require 'time'
|
3
|
-
require 'active_model'
|
4
|
-
|
5
|
-
require 'active_model/version'
|
6
|
-
|
7
|
-
class Module
|
8
|
-
def module_attr_reader(*syms)
|
9
|
-
syms.each do |sym|
|
10
|
-
class_eval(<<-EOS, __FILE__, __LINE__ + 1)
|
11
|
-
@@#{sym} = nil unless defined?(@@#{sym})
|
12
|
-
def self.#{sym}()
|
13
|
-
return @@#{sym}
|
14
|
-
end
|
15
|
-
EOS
|
16
|
-
end
|
17
|
-
end
|
18
|
-
def module_attr_writer(*syms)
|
19
|
-
syms.each do |sym|
|
20
|
-
class_eval(<<-EOS, __FILE__, __LINE__ + 1)
|
21
|
-
def self.#{sym}=(obj)
|
22
|
-
@@#{sym} = obj
|
23
|
-
end
|
24
|
-
EOS
|
25
|
-
end
|
26
|
-
end
|
27
|
-
def module_attr_accessor(*syms)
|
28
|
-
module_attr_reader(*syms)
|
29
|
-
module_attr_writer(*syms)
|
30
|
-
end
|
31
|
-
end
|
32
|
-
|
33
|
-
module ActiveRedis
|
34
|
-
module_attr_accessor :host, :port
|
35
|
-
@@host = "localhost"
|
36
|
-
@@port = "6379"
|
37
|
-
|
38
|
-
class ActiveRedisError < StandardError
|
39
|
-
end
|
40
|
-
|
41
|
-
# Raised when Active Redis cannot find record by given id or set of ids.
|
42
|
-
class RecordNotFound < ActiveRedisError
|
43
|
-
end
|
44
|
-
|
45
|
-
class Base
|
46
|
-
include ActiveModel::Validations
|
47
|
-
include ActiveModel::Dirty
|
48
|
-
include ActiveModel::Serialization
|
49
|
-
include ActiveModel::Serializers::JSON
|
50
|
-
include ActiveModel::Naming
|
51
|
-
|
52
|
-
QUEUED = "QUEUED"
|
53
|
-
|
54
|
-
# RAILSISM
|
55
|
-
# Returns a hash of all the attributes with their names as keys and the values of the attributes as values
|
56
|
-
# --> means: Strings as keys!
|
57
|
-
# --> initialize stringifies
|
58
|
-
#
|
59
|
-
# called by to_json for_example
|
60
|
-
attr_reader :attributes
|
61
|
-
attr_reader :id
|
62
|
-
attr :frozen
|
63
|
-
|
64
|
-
class << self; attr_accessor :_fields; end
|
65
|
-
|
66
|
-
# INSTANCE METHODS
|
67
|
-
|
68
|
-
def initialize(attributes = {}, id = nil)
|
69
|
-
@id = id if id
|
70
|
-
@attributes = {}
|
71
|
-
initialize_attributes(attributes)
|
72
|
-
frozen = false
|
73
|
-
end
|
74
|
-
|
75
|
-
# Object's attributes' keys are converted to strings because of railsisms.
|
76
|
-
# Because of activeredisism, also values are converted to strings for consistency.
|
77
|
-
def initialize_attributes(attributes)
|
78
|
-
fields = Hash[[self.class._fields, Array.new(self.class._fields.size)].transpose]
|
79
|
-
fields.stringify_keys! # NEEDS to be strings for railsisms
|
80
|
-
attributes.stringify_keys! # NEEDS to be strings for railsisms
|
81
|
-
attributes.each_pair { |key, value| attributes[key] = value.to_s }
|
82
|
-
fields.merge!(attributes)
|
83
|
-
@attributes.merge!(fields)
|
84
|
-
@attributes.each_pair do |key, value|
|
85
|
-
self.class.define_field key
|
86
|
-
end
|
87
|
-
end
|
88
|
-
|
89
|
-
def save
|
90
|
-
creation = new_record?
|
91
|
-
@id = self.class.fetch_new_identifier if creation
|
92
|
-
while true
|
93
|
-
begin
|
94
|
-
connection.multi
|
95
|
-
rescue
|
96
|
-
sleep(0.1)
|
97
|
-
redo
|
98
|
-
end
|
99
|
-
break
|
100
|
-
end
|
101
|
-
if @attributes.size > 0
|
102
|
-
@attributes.each_pair { |key, value|
|
103
|
-
if key.to_sym == :updated_at
|
104
|
-
value = Time.now.to_s
|
105
|
-
end
|
106
|
-
connection.hset("#{key_namespace}:attributes", key, value)
|
107
|
-
}
|
108
|
-
end
|
109
|
-
connection.zadd("#{class_namespace}:all", @id, @id)
|
110
|
-
connection.exec
|
111
|
-
return true
|
112
|
-
end
|
113
|
-
|
114
|
-
def update_attributes(attributes)
|
115
|
-
attributes.stringify_keys! # NEEDS to be strings for railsisms
|
116
|
-
attributes.each_pair { |key, value| attributes[key] = value.to_s }
|
117
|
-
@attributes.merge!(attributes)
|
118
|
-
attributes.each_pair do |key, value|
|
119
|
-
self.class.define_field key
|
120
|
-
end
|
121
|
-
save
|
122
|
-
end
|
123
|
-
|
124
|
-
def reload
|
125
|
-
@attributes = connection.hgetall "#{key_namespace}:attributes"
|
126
|
-
end
|
127
|
-
|
128
|
-
def new_record?
|
129
|
-
@id == nil
|
130
|
-
end
|
131
|
-
|
132
|
-
def key_namespace
|
133
|
-
"#{self.class.key_namespace}:#{self.id}"
|
134
|
-
end
|
135
|
-
|
136
|
-
def class_namespace
|
137
|
-
"#{self.class.key_namespace}"
|
138
|
-
end
|
139
|
-
|
140
|
-
def connection
|
141
|
-
self.class.connection
|
142
|
-
end
|
143
|
-
|
144
|
-
def destroy
|
145
|
-
connection.multi do
|
146
|
-
connection.del "#{key_namespace}:attributes"
|
147
|
-
connection.zrem "#{class_namespace}:all", @id
|
148
|
-
@frozen = true
|
149
|
-
end
|
150
|
-
return true
|
151
|
-
end
|
152
|
-
|
153
|
-
def frozen?
|
154
|
-
@frozen
|
155
|
-
end
|
156
|
-
|
157
|
-
def add_attribute(name, value=nil)
|
158
|
-
initialize_attributes({name => value})
|
159
|
-
end
|
160
|
-
|
161
|
-
def [](field)
|
162
|
-
send(field)
|
163
|
-
end
|
164
|
-
|
165
|
-
def []=(field, value)
|
166
|
-
send(field+"=", value)
|
167
|
-
end
|
168
|
-
|
169
|
-
#
|
170
|
-
# CLASS METHODS
|
171
|
-
#
|
172
|
-
|
173
|
-
def self.create(attributes)
|
174
|
-
self.new(attributes).save
|
175
|
-
end
|
176
|
-
|
177
|
-
def self.define_field(field)
|
178
|
-
define_method field.to_sym do
|
179
|
-
if field.to_sym == :updated_at
|
180
|
-
Time.parse(@attributes["#{field}"])
|
181
|
-
else
|
182
|
-
@attributes["#{field}"]
|
183
|
-
end
|
184
|
-
end
|
185
|
-
|
186
|
-
define_method "#{field}=".to_sym do |new_value|
|
187
|
-
@attributes["#{field}"] = new_value.to_s
|
188
|
-
end
|
189
|
-
end
|
190
|
-
|
191
|
-
# Run this method to declare the fields of your model.
|
192
|
-
def self.fields(*fields)
|
193
|
-
self._fields ||= []
|
194
|
-
self._fields = fields
|
195
|
-
end
|
196
|
-
|
197
|
-
def self.get_fields
|
198
|
-
self._fields
|
199
|
-
end
|
200
|
-
|
201
|
-
def self.key_namespace
|
202
|
-
"#{self}"
|
203
|
-
end
|
204
|
-
|
205
|
-
def self.fetch_new_identifier
|
206
|
-
self.connection.incr self.identifier_sequencer
|
207
|
-
end
|
208
|
-
|
209
|
-
def self.identifier_sequencer
|
210
|
-
"#{key_namespace}:sequence"
|
211
|
-
end
|
212
|
-
|
213
|
-
def self.inherited(child)
|
214
|
-
#puts "Redis.new(:host => #{ActiveRedis.host}, :port => #{ActiveRedis.port})"
|
215
|
-
@@redis = Redis.new(:host => ActiveRedis.host, :port => ActiveRedis.port)
|
216
|
-
@@class = child
|
217
|
-
end
|
218
|
-
|
219
|
-
def self.redis_information
|
220
|
-
connection.info # call_command [:info]
|
221
|
-
end
|
222
|
-
|
223
|
-
def self.connection
|
224
|
-
@@redis
|
225
|
-
end
|
226
|
-
|
227
|
-
def self.count
|
228
|
-
begin
|
229
|
-
size = connection.zcard "#{key_namespace}:all"
|
230
|
-
while size == QUEUED
|
231
|
-
sleep(0.1)
|
232
|
-
size = connection.zcard "#{key_namespace}:all"
|
233
|
-
end
|
234
|
-
return size
|
235
|
-
rescue RuntimeError => e
|
236
|
-
return 0
|
237
|
-
end
|
238
|
-
end
|
239
|
-
|
240
|
-
def self.find_all()
|
241
|
-
record = []
|
242
|
-
# TODO Interim fix, "QUEUED" is find(id) rescue
|
243
|
-
while true
|
244
|
-
ids = nil
|
245
|
-
while true
|
246
|
-
ids = connection.zrange "#{key_namespace}:all", 0, count
|
247
|
-
break if ids != QUEUED
|
248
|
-
sleep(0.1)
|
249
|
-
end
|
250
|
-
begin
|
251
|
-
ids.each do |id|
|
252
|
-
record << find(id)
|
253
|
-
end
|
254
|
-
rescue
|
255
|
-
redo
|
256
|
-
end
|
257
|
-
break
|
258
|
-
end
|
259
|
-
record
|
260
|
-
end
|
261
|
-
|
262
|
-
def self.all
|
263
|
-
find_all
|
264
|
-
end
|
265
|
-
|
266
|
-
def self.delete_all
|
267
|
-
records = find_all
|
268
|
-
records.each do |record|
|
269
|
-
record.destroy
|
270
|
-
end
|
271
|
-
end
|
272
|
-
|
273
|
-
def self.find(id)
|
274
|
-
return find_all if id == :all
|
275
|
-
exists = connection.zscore "#{key_namespace}:all", id
|
276
|
-
raise RecordNotFound.new("Couldn't find #{self.name} with ID=#{id}") unless exists
|
277
|
-
attributes = connection.hgetall "#{key_namespace}:#{id}:attributes"
|
278
|
-
obj = self.new attributes, id
|
279
|
-
return obj
|
280
|
-
end
|
281
|
-
|
282
|
-
def self.find_all_by_param(field, value)
|
283
|
-
finded = []
|
284
|
-
records = find_all
|
285
|
-
records.each do |record|
|
286
|
-
if record.attributes[field.to_s] == value.to_s
|
287
|
-
finded << record
|
288
|
-
end
|
289
|
-
end
|
290
|
-
return finded
|
291
|
-
end
|
292
|
-
|
293
|
-
def self.find_by_param(field, value)
|
294
|
-
records = find_all_by_param(field, value)
|
295
|
-
if records.size > 0
|
296
|
-
return records[0]
|
297
|
-
else
|
298
|
-
nil
|
299
|
-
end
|
300
|
-
end
|
301
|
-
|
302
|
-
def self.method_missing(name, *args)
|
303
|
-
return find_by_param($1.to_sym, args[0]) if name.to_s =~ /^find_by_(.*)/
|
304
|
-
return find_all_by_param($1.to_sym, args[0]) if name.to_s =~ /^find_all_by_(.*)/
|
305
|
-
super
|
306
|
-
end
|
307
|
-
|
308
|
-
def self.delete_unused_field
|
309
|
-
ids = connection.zrange "#{key_namespace}:all", 0, count
|
310
|
-
if ids.size > 0
|
311
|
-
attributes = connection.hgetall "#{key_namespace}:#{ids[0]}:attributes"
|
312
|
-
now_keys = self.get_fields
|
313
|
-
array = []
|
314
|
-
now_keys.each do |key|
|
315
|
-
array << key.to_s
|
316
|
-
end
|
317
|
-
attributes.reject! {|key| array.include? key }
|
318
|
-
attributes.keys.each do |delete_key|
|
319
|
-
ids.each do |id|
|
320
|
-
connection.hdel "#{key_namespace}:#{id}:attributes", delete_key
|
321
|
-
end
|
322
|
-
end
|
323
|
-
end
|
324
|
-
end
|
325
|
-
end
|
326
|
-
end
|