familia 0.6.4 → 0.6.8
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGES.txt +18 -0
- data/Rakefile +1 -1
- data/VERSION.yml +1 -1
- data/familia.gemspec +2 -2
- data/lib/familia/object.rb +13 -4
- data/lib/familia/redisobject.rb +131 -39
- data/lib/familia.rb +4 -1
- metadata +4 -4
data/CHANGES.txt
CHANGED
@@ -1,5 +1,23 @@
|
|
1
1
|
FAMILIA, CHANGES
|
2
2
|
|
3
|
+
#### 0.6.8 (2011-01-08) ###############################
|
4
|
+
|
5
|
+
* CHANGE: RedisObject#members calls compact to remove nils
|
6
|
+
* ADDED: RedisObject#to_redis/from_redis print message for nils to STDERR
|
7
|
+
* ADDED: RedisObjects#membersraw for List, Set, and SortedSet
|
8
|
+
|
9
|
+
|
10
|
+
#### 0.6.7 (2011-01-02) ###############################
|
11
|
+
|
12
|
+
* FIXED: RedisObject#ttl, grab from instance first.
|
13
|
+
|
14
|
+
#### 0.6.6 (2011-01-02) ###############################
|
15
|
+
|
16
|
+
* FIXED: Classes that include Familia now use the suffix defined for that class.
|
17
|
+
* FIXED: Familia::String#value= calls update_expiration
|
18
|
+
* CHANGE: RedisObject classes grab the ttl from @opts[:class]
|
19
|
+
* ADDED: Familia.classes can return RedisObject.classes
|
20
|
+
|
3
21
|
|
4
22
|
#### 0.6.4 (2010-12-31) ###############################
|
5
23
|
|
data/Rakefile
CHANGED
@@ -40,7 +40,7 @@ end
|
|
40
40
|
|
41
41
|
|
42
42
|
Rake::RDocTask.new do |rdoc|
|
43
|
-
version = "#{config[:MAJOR]}.#{config[:MINOR]}.#{config[:PATCH]}
|
43
|
+
version = "#{config[:MAJOR]}.#{config[:MINOR]}.#{config[:PATCH]}"
|
44
44
|
rdoc.rdoc_dir = "doc"
|
45
45
|
rdoc.title = "#{name} #{version}"
|
46
46
|
rdoc.rdoc_files.include("README*")
|
data/VERSION.yml
CHANGED
data/familia.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{familia}
|
8
|
-
s.version = "0.6.
|
8
|
+
s.version = "0.6.8"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Delano Mandelbaum"]
|
12
|
-
s.date = %q{
|
12
|
+
s.date = %q{2011-01-08}
|
13
13
|
s.description = %q{Organize and store ruby objects in Redis}
|
14
14
|
s.email = %q{delano@solutious.com}
|
15
15
|
s.extra_rdoc_files = [
|
data/lib/familia/object.rb
CHANGED
@@ -103,7 +103,6 @@ module Familia
|
|
103
103
|
raise ArgumentError, "Name is blank" if name.to_s.empty?
|
104
104
|
name = name.to_s.to_sym
|
105
105
|
opts ||= {}
|
106
|
-
opts[:suffix] ||= nil
|
107
106
|
opts[:parent] ||= self
|
108
107
|
# TODO: investigate using metaclass.redis_objects
|
109
108
|
class_redis_objects_order << name
|
@@ -181,6 +180,9 @@ module Familia
|
|
181
180
|
end
|
182
181
|
def prefix=(a) @prefix = a end
|
183
182
|
def prefix(a=nil) @prefix = a if a; @prefix || self.name.downcase.to_sym end
|
183
|
+
# TODO: grab db, ttl, uri from parent
|
184
|
+
#def parent=(a) @parent = a end
|
185
|
+
#def parent(a=nil) @parent = a if a; @parent end
|
184
186
|
def index(i=nil, &blk)
|
185
187
|
@index = i || blk if i || !blk.nil?
|
186
188
|
@index ||= Familia.index
|
@@ -274,8 +276,10 @@ module Familia
|
|
274
276
|
def find suffix='*'
|
275
277
|
list = Familia.redis(self.uri).keys(rediskey('*', suffix)) || []
|
276
278
|
end
|
277
|
-
# idx can be a value or an Array of values used to create the index.
|
278
|
-
|
279
|
+
# idx can be a value or an Array of values used to create the index.
|
280
|
+
# We don't enforce a default suffix; that's left up to the instance.
|
281
|
+
# A nil +suffix+ will not be included in the key.
|
282
|
+
def rediskey idx, suffix=self.suffix
|
279
283
|
raise RuntimeError, "No index for #{self}" if idx.to_s.empty?
|
280
284
|
idx = Familia.join *idx if Array === idx
|
281
285
|
idx &&= idx.to_s
|
@@ -362,7 +366,12 @@ module Familia
|
|
362
366
|
end
|
363
367
|
keynames
|
364
368
|
end
|
365
|
-
|
369
|
+
# +suffix+ is the value to be used at the end of the redis key
|
370
|
+
# + ignored+ is literally ignored. It's around to maintain
|
371
|
+
# consistency with the class version of this method.
|
372
|
+
# (RedisObject#rediskey may call against a class or instance).
|
373
|
+
def rediskey(suffix=nil, ignored=nil)
|
374
|
+
Familia.info "[#{self.class}] something was ignored" unless ignored.nil?
|
366
375
|
raise Familia::NoIndex, self.class if index.to_s.empty?
|
367
376
|
if suffix.nil?
|
368
377
|
suffix = self.class.suffix.kind_of?(Proc) ?
|
data/lib/familia/redisobject.rb
CHANGED
@@ -89,8 +89,8 @@ module Familia
|
|
89
89
|
#Familia.ld [name, opts, caller[0]].inspect
|
90
90
|
self.extend @opts[:extend] if Module === @opts[:extend]
|
91
91
|
@db = @opts.delete(:db)
|
92
|
-
@ttl = @opts.delete(:ttl)
|
93
92
|
@parent = @opts.delete(:parent)
|
93
|
+
@ttl = @opts.delete(:ttl)
|
94
94
|
@redis ||= @opts.delete(:redis)
|
95
95
|
init if respond_to? :init
|
96
96
|
end
|
@@ -100,10 +100,6 @@ module Familia
|
|
100
100
|
parent? ? parent.redis : Familia.redis(db)
|
101
101
|
end
|
102
102
|
|
103
|
-
def ttl
|
104
|
-
@ttl || self.class.ttl
|
105
|
-
end
|
106
|
-
|
107
103
|
# Returns the most likely value for db, checking (in this order):
|
108
104
|
# * the value from :class if it's a Familia object
|
109
105
|
# * the value from :parent
|
@@ -126,10 +122,21 @@ module Familia
|
|
126
122
|
@db
|
127
123
|
end
|
128
124
|
|
125
|
+
def ttl
|
126
|
+
@ttl ||
|
127
|
+
(parent.ttl if parent?) ||
|
128
|
+
(@opts[:class].ttl if class?) ||
|
129
|
+
(self.class.ttl if self.class.respond_to?(:ttl))
|
130
|
+
end
|
131
|
+
|
129
132
|
# returns a redis key based on the parent
|
130
133
|
# object so it will include the proper index.
|
131
134
|
def rediskey
|
132
|
-
parent? ? parent.rediskey(name) : [name].flatten.compact.join(Familia.delim)
|
135
|
+
parent? ? parent.rediskey(name, nil) : [name].flatten.compact.join(Familia.delim)
|
136
|
+
end
|
137
|
+
|
138
|
+
def class?
|
139
|
+
!@opts[:class].to_s.empty? && @opts[:class].kind_of?(Familia)
|
133
140
|
end
|
134
141
|
|
135
142
|
def parent?
|
@@ -137,8 +144,8 @@ module Familia
|
|
137
144
|
end
|
138
145
|
|
139
146
|
def update_expiration(ttl=nil)
|
140
|
-
ttl ||=
|
141
|
-
return
|
147
|
+
ttl ||= self.ttl
|
148
|
+
return if ttl.to_i.zero? # nil will be zero
|
142
149
|
Familia.ld "#{rediskey} to #{ttl}"
|
143
150
|
expire ttl.to_i
|
144
151
|
end
|
@@ -190,7 +197,7 @@ module Familia
|
|
190
197
|
|
191
198
|
def to_redis v
|
192
199
|
return v unless @opts[:class]
|
193
|
-
case @opts[:class]
|
200
|
+
ret = case @opts[:class]
|
194
201
|
when String, Fixnum, Float, Gibbler::Digest
|
195
202
|
v
|
196
203
|
else
|
@@ -208,34 +215,43 @@ module Familia
|
|
208
215
|
raise Familia::Problem, "No such method: #{v.class}.#{dump_method}"
|
209
216
|
end
|
210
217
|
end
|
218
|
+
if ret.nil?
|
219
|
+
Familia.info "[#{self.class}\#to_redis] nil returned for #{@opts[:class]}\##{name}"
|
220
|
+
end
|
221
|
+
ret
|
211
222
|
end
|
212
223
|
|
213
224
|
def from_redis v
|
214
225
|
return @opts[:default] if v.nil?
|
215
226
|
return v unless @opts[:class]
|
216
|
-
case @opts[:class]
|
227
|
+
ret = case @opts[:class]
|
217
228
|
when String
|
218
229
|
v.to_s
|
219
230
|
when Fixnum, Float
|
220
231
|
@opts[:class].induced_from v
|
221
232
|
else
|
222
|
-
|
223
|
-
@opts[:
|
224
|
-
|
225
|
-
if @opts[:class].respond_to? load_method
|
226
|
-
@opts[:class].send load_method, v
|
233
|
+
begin
|
234
|
+
if @opts[:reference] == true
|
235
|
+
@opts[:class].from_redis v
|
227
236
|
else
|
228
|
-
|
237
|
+
if @opts[:class].respond_to? load_method
|
238
|
+
@opts[:class].send load_method, v
|
239
|
+
else
|
240
|
+
raise Familia::Problem, "No such method: #{@opts[:class]}##{load_method}"
|
241
|
+
end
|
229
242
|
end
|
243
|
+
rescue => ex
|
244
|
+
Familia.info v
|
245
|
+
Familia.info "Parse error for #{rediskey} (#{load_method}): #{ex.message}"
|
246
|
+
Familia.info ex.backtrace
|
247
|
+
nil
|
230
248
|
end
|
231
249
|
end
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
nil
|
250
|
+
if ret.nil?
|
251
|
+
Familia.info "[#{self.class}\#from_redis] nil returned for #{@opts[:class]}\##{name}"
|
252
|
+
end
|
253
|
+
ret
|
237
254
|
end
|
238
|
-
|
239
255
|
end
|
240
256
|
|
241
257
|
|
@@ -299,9 +315,11 @@ module Familia
|
|
299
315
|
alias_method :del, :delete
|
300
316
|
|
301
317
|
def range sidx=0, eidx=-1
|
302
|
-
redis.lrange(rediskey, sidx, eidx).collect
|
303
|
-
|
304
|
-
|
318
|
+
redis.lrange(rediskey, sidx, eidx).collect { |v| from_redis(v) }.compact
|
319
|
+
end
|
320
|
+
|
321
|
+
def rangeraw sidx=0, eidx=-1
|
322
|
+
redis.lrange(rediskey, sidx, eidx)
|
305
323
|
end
|
306
324
|
|
307
325
|
def members count=-1
|
@@ -311,6 +329,11 @@ module Familia
|
|
311
329
|
alias_method :all, :members
|
312
330
|
alias_method :to_a, :members
|
313
331
|
|
332
|
+
def membersraw count=-1
|
333
|
+
count -= 1 if count > 0
|
334
|
+
rangeraw 0, count
|
335
|
+
end
|
336
|
+
|
314
337
|
#def revmembers count=1 #TODO
|
315
338
|
# range -count, 0
|
316
339
|
#end
|
@@ -323,6 +346,14 @@ module Familia
|
|
323
346
|
range.each_with_index &blk
|
324
347
|
end
|
325
348
|
|
349
|
+
def eachraw &blk
|
350
|
+
rangeraw.each &blk
|
351
|
+
end
|
352
|
+
|
353
|
+
def eachraw_with_index &blk
|
354
|
+
rangeraw.each_with_index &blk
|
355
|
+
end
|
356
|
+
|
326
357
|
def collect &blk
|
327
358
|
range.collect &blk
|
328
359
|
end
|
@@ -330,6 +361,14 @@ module Familia
|
|
330
361
|
def select &blk
|
331
362
|
range.select &blk
|
332
363
|
end
|
364
|
+
|
365
|
+
def collectraw &blk
|
366
|
+
rangeraw.collect &blk
|
367
|
+
end
|
368
|
+
|
369
|
+
def selectraw &blk
|
370
|
+
rangeraw.select &blk
|
371
|
+
end
|
333
372
|
|
334
373
|
def at idx
|
335
374
|
from_redis redis.lindex(rediskey, idx)
|
@@ -389,12 +428,14 @@ module Familia
|
|
389
428
|
end
|
390
429
|
|
391
430
|
def members
|
392
|
-
redis.smembers(rediskey).collect
|
393
|
-
from_redis v
|
394
|
-
end
|
431
|
+
redis.smembers(rediskey).collect { |v| from_redis(v) }.compact
|
395
432
|
end
|
396
433
|
alias_method :all, :members
|
397
434
|
alias_method :to_a, :members
|
435
|
+
|
436
|
+
def membersraw
|
437
|
+
redis.smembers(rediskey)
|
438
|
+
end
|
398
439
|
|
399
440
|
def each &blk
|
400
441
|
members.each &blk
|
@@ -411,6 +452,22 @@ module Familia
|
|
411
452
|
def select &blk
|
412
453
|
members.select &blk
|
413
454
|
end
|
455
|
+
|
456
|
+
def eachraw &blk
|
457
|
+
membersraw.each &blk
|
458
|
+
end
|
459
|
+
|
460
|
+
def eachraw_with_index &blk
|
461
|
+
membersraw.each_with_index &blk
|
462
|
+
end
|
463
|
+
|
464
|
+
def collectraw &blk
|
465
|
+
membersraw.collect &blk
|
466
|
+
end
|
467
|
+
|
468
|
+
def selectraw &blk
|
469
|
+
membersraw.select &blk
|
470
|
+
end
|
414
471
|
|
415
472
|
def member? v
|
416
473
|
redis.sismember rediskey, to_redis(v)
|
@@ -420,6 +477,7 @@ module Familia
|
|
420
477
|
def delete v
|
421
478
|
redis.srem rediskey, to_redis(v)
|
422
479
|
end
|
480
|
+
alias_method :remove, :delete
|
423
481
|
alias_method :rem, :delete
|
424
482
|
alias_method :del, :delete
|
425
483
|
|
@@ -514,11 +572,21 @@ module Familia
|
|
514
572
|
end
|
515
573
|
alias_method :to_a, :members
|
516
574
|
alias_method :all, :members
|
575
|
+
|
576
|
+
def membersraw count=-1, opts={}
|
577
|
+
count -= 1 if count > 0
|
578
|
+
rangeraw 0, count, opts
|
579
|
+
end
|
517
580
|
|
518
581
|
def revmembers count=-1, opts={}
|
519
582
|
count -= 1 if count > 0
|
520
583
|
revrange 0, count, opts
|
521
584
|
end
|
585
|
+
|
586
|
+
def revmembersraw count=-1, opts={}
|
587
|
+
count -= 1 if count > 0
|
588
|
+
revrangeraw 0, count, opts
|
589
|
+
end
|
522
590
|
|
523
591
|
def each &blk
|
524
592
|
members.each &blk
|
@@ -536,26 +604,48 @@ module Familia
|
|
536
604
|
members.select &blk
|
537
605
|
end
|
538
606
|
|
607
|
+
def eachraw &blk
|
608
|
+
membersraw.each &blk
|
609
|
+
end
|
610
|
+
|
611
|
+
def eachraw_with_index &blk
|
612
|
+
membersraw.each_with_index &blk
|
613
|
+
end
|
614
|
+
|
615
|
+
def collectraw &blk
|
616
|
+
membersraw.collect &blk
|
617
|
+
end
|
618
|
+
|
619
|
+
def selectraw &blk
|
620
|
+
membersraw.select &blk
|
621
|
+
end
|
622
|
+
|
539
623
|
def range sidx, eidx, opts={}
|
540
|
-
opts
|
541
|
-
redis.zrange(rediskey, sidx, eidx, opts).collect do |v|
|
542
|
-
from_redis v
|
543
|
-
end
|
624
|
+
rangeraw(sidx, eidx, opts).collect { |v| from_redis(v) }.compact
|
544
625
|
end
|
545
626
|
|
546
627
|
def revrange sidx, eidx, opts={}
|
628
|
+
revrangeraw(sidx, eidx, opts).collect { |v| from_redis(v) }.compact
|
629
|
+
end
|
630
|
+
|
631
|
+
def rangeraw sidx, eidx, opts={}
|
547
632
|
opts[:with_scores] = true if opts[:withscores]
|
548
|
-
redis.
|
549
|
-
|
550
|
-
|
633
|
+
redis.zrange(rediskey, sidx, eidx, opts)
|
634
|
+
end
|
635
|
+
|
636
|
+
def revrangeraw sidx, eidx, opts={}
|
637
|
+
opts[:with_scores] = true if opts[:withscores]
|
638
|
+
redis.zrevrange(rediskey, sidx, eidx, opts)
|
551
639
|
end
|
552
640
|
|
553
641
|
# e.g. obj.metrics.rangebyscore (now-12.hours), now, :limit => [0, 10]
|
554
642
|
def rangebyscore sscore, escore, opts={}
|
643
|
+
rangebyscoreraw(sscore, escore, opts).collect { |v| from_redis( v) }.compact
|
644
|
+
end
|
645
|
+
|
646
|
+
def rangebyscoreraw sscore, escore, opts={}
|
555
647
|
opts[:with_scores] = true if opts[:withscores]
|
556
|
-
redis.zrangebyscore(rediskey, sscore, escore, opts)
|
557
|
-
from_redis v
|
558
|
-
end
|
648
|
+
redis.zrangebyscore(rediskey, sscore, escore, opts)
|
559
649
|
end
|
560
650
|
|
561
651
|
def remrangebyrank srank, erank
|
@@ -722,7 +812,9 @@ module Familia
|
|
722
812
|
end
|
723
813
|
|
724
814
|
def value= v
|
725
|
-
redis.set rediskey, to_redis(v)
|
815
|
+
ret = redis.set rediskey, to_redis(v)
|
816
|
+
update_expiration
|
817
|
+
ret
|
726
818
|
end
|
727
819
|
alias_method :replace, :value=
|
728
820
|
alias_method :set, :value=
|
data/lib/familia.rb
CHANGED
@@ -32,13 +32,16 @@ module Familia
|
|
32
32
|
@dump_method = :to_json
|
33
33
|
@load_method = :from_json
|
34
34
|
class << self
|
35
|
-
attr_reader :
|
35
|
+
attr_reader :clients, :uri
|
36
36
|
attr_accessor :debug, :secret, :delim, :dump_method, :load_method
|
37
37
|
attr_writer :apiversion
|
38
38
|
def debug?() @debug == true end
|
39
39
|
def info *msg
|
40
40
|
STDERR.puts *msg
|
41
41
|
end
|
42
|
+
def classes with_redis_objects=false
|
43
|
+
with_redis_objects ? [@classes, RedisObject.classes].flatten : @classes
|
44
|
+
end
|
42
45
|
def ld *msg
|
43
46
|
info *msg if debug?
|
44
47
|
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: familia
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 23
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 6
|
9
|
-
-
|
10
|
-
version: 0.6.
|
9
|
+
- 8
|
10
|
+
version: 0.6.8
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Delano Mandelbaum
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date:
|
18
|
+
date: 2011-01-08 00:00:00 -05:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|