Class: Seabright::Collection

Inherits:
Array
  • Object
show all
Includes:
CachedScripts
Defined in:
lib/redis_object/collection.rb

Constant Summary collapse

NilPattern =
'nilpattern:'

Class Method Summary collapse

Instance Method Summary collapse

Methods included from CachedScripts

included, #run_script

Constructor Details

#initialize(name, owner) ⇒ Collection

Returns a new instance of Collection.



217
218
219
220
# File 'lib/redis_object/collection.rb', line 217

def initialize(name,owner)
  @name = name.to_s
  @owner = owner
end

Class Method Details

.class_const_for(name) ⇒ Object



603
604
605
606
607
608
# File 'lib/redis_object/collection.rb', line 603

def class_const_for(name)
  if cls = RedisObject.constant_lookups[name.to_s.classify.to_sym]
    return cls
  end
  Object.const_get(name.to_s.classify.to_sym) rescue RedisObject
end

.load(name, owner) ⇒ Object



597
598
599
600
601
# File 'lib/redis_object/collection.rb', line 597

def load(name,owner)
  out = new(name,owner)
  out.replace class_const_for(name).store.zrange(out.key,0,-1)
  out
end

Instance Method Details

#<<(obj) ⇒ Object



573
574
575
576
577
# File 'lib/redis_object/collection.rb', line 573

def <<(obj)
  k = obj.class == String ? obj : obj.hkey
  store.zadd(key,store.zcount(key,"-inf", "+inf"),k)
  super(k)
end

#[](k) ⇒ Object



289
290
291
# File 'lib/redis_object/collection.rb', line 289

def [](k)
  find k
end

#class_constObject



583
584
585
# File 'lib/redis_object/collection.rb', line 583

def class_const
  self.class.class_const_for(@name)
end

#cleanup!Object



535
536
537
538
539
540
541
542
543
544
545
546
# File 'lib/redis_object/collection.rb', line 535

def cleanup!
  each_index do |key|
    unless a = class_const.find_by_key(at(key))
      Log.debug "Deleting #{key} because not #{a.inspect}"
      delete at(key)
    end
  end
  if size < 1
    Log.debug "Deleting collection #{@name} because empty"
    remove!
  end
end

#clear!Object



569
570
571
# File 'lib/redis_object/collection.rb', line 569

def clear!
  store.zrem(key,self.join(" "))
end

#convert_regex_to_lua(reg) ⇒ Object



488
489
490
# File 'lib/redis_object/collection.rb', line 488

def convert_regex_to_lua(reg)
  "#{reg.casefold? ? "i" : ""}pattern:#{reg.source.gsub("\\","")}"
end

#delete(obj) ⇒ Object



563
564
565
566
567
# File 'lib/redis_object/collection.rb', line 563

def delete(obj)
  k = obj.class == String ? obj : obj.hkey
  store.zrem(key,k)
  super(k)
end

#eachObject



518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
# File 'lib/redis_object/collection.rb', line 518

def each
  out = Enumerator.new do |y|
    each_index do |key|
      if a = class_const.find_by_key(at(key))
        y << a
      end
    end
  end
  if block_given?
    out.each do |a|
      yield a
    end
  else
    out
  end
end

#find(k) ⇒ Object



278
279
280
281
282
283
284
285
286
287
# File 'lib/redis_object/collection.rb', line 278

def find(k)
  if k.is_a? String
    return real_at(item_key(k))
  elsif k.is_a? Hash
    return match(k)
  elsif k.is_a? Integer
    return real_at(at(k))
  end
  return nil
end

#firstObject



510
511
512
# File 'lib/redis_object/collection.rb', line 510

def first
  class_const.find_by_key(super)
end

#indexed(idx, num = -1,, reverse = false) ⇒ Object



230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
# File 'lib/redis_object/collection.rb', line 230

def indexed(idx,num=-1,reverse=false)
  keys = keys_by_index(idx,num,reverse)
  out = ListEnumerator.new(keys) do |y|
    keys.each do |member|
      if a = class_const.find_by_key(member)
        y << a
      end
    end
  end
  if block_given?
    out.each do |itm|
      yield itm
    end
  else
    out
  end
end

#inject_key(key, list) ⇒ Object



475
476
477
478
479
480
481
482
483
484
485
486
# File 'lib/redis_object/collection.rb', line 475

def inject_key(key,list)
  out = []
  list.each do |i|
    if i == list.first
      out << i
    else
      out << key
      out << i
    end
  end
  out
end

#item_key(k) ⇒ Object



274
275
276
# File 'lib/redis_object/collection.rb', line 274

def item_key(k)
  "#{class_const}:#{k}_h"
end

#keyObject



591
592
593
# File 'lib/redis_object/collection.rb', line 591

def key
  "#{@owner ? "#{@owner.key}:" : ""}COLLECTION:#{@name}"
end

#keys_by_index(idx, num = -1,, reverse = false) ⇒ Object



261
262
263
264
265
266
267
268
# File 'lib/redis_object/collection.rb', line 261

def keys_by_index(idx,num=-1,reverse=false)
  keys = run_script(reverse ? :RevScript : :FwdScript, [temp_key, sort_index_key(idx), key, num>0 ? num - 1 : -1])
  ListEnumerator.new(keys.uniq) do |y|
    keys.each do |member|
      y << member
    end
  end
end

#lastObject



514
515
516
# File 'lib/redis_object/collection.rb', line 514

def last
  class_const.find_by_key(super)
end

#latestObject



226
227
228
# File 'lib/redis_object/collection.rb', line 226

def latest
  indexed(:created_at,5,true).first || first
end

#map(&block) ⇒ Object



548
549
550
# File 'lib/redis_object/collection.rb', line 548

def map(&block)
  each.map(&block)
end

#match(pkt, use_or = false) ⇒ Object



446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
# File 'lib/redis_object/collection.rb', line 446

def match(pkt, use_or=false)
  if use_or
    mtchr = :ColOrMatcher
  else
    mtchr = pkt.keys.count > 1 ? :ColMultiMatcher : :ColMatcher
  end
  pkt = pkt.flatten.reduce([]) do |i,v|
    x = case v
    when Regexp
      convert_regex_to_lua(v)
    when Array
      raise ArgumentError.new("An array can only be used with the find_or method") unless use_or
      inject_key(i.last, v)
    when NilClass
      NilPattern
    else
      v.to_s
    end
    i << x
    i
  end
  kys = run_script(mtchr,self,pkt.flatten)
  ListEnumerator.new(kys) do |y|
    kys.each do |k|
      y << class_const.find_by_key(k)
    end
  end
end

#objectsObject



506
507
508
# File 'lib/redis_object/collection.rb', line 506

def objects
  each.to_a
end

#push(obj) ⇒ Object



579
580
581
# File 'lib/redis_object/collection.rb', line 579

def push(obj)
  self << obj
end

#real_at(key) ⇒ Object

def match(pkt) Enumerator.new do |y| each do |i| if pkt.all? {|hk,va| i.get(hk)==va } y << i end end end end



502
503
504
# File 'lib/redis_object/collection.rb', line 502

def real_at(key)
  class_const.find_by_key(key)
end

#remove!Object



222
223
224
# File 'lib/redis_object/collection.rb', line 222

def remove!
  @owner.remove_collection! @name
end

#select(&block) ⇒ Object



552
553
554
555
556
557
558
559
560
561
# File 'lib/redis_object/collection.rb', line 552

def select(&block)
  return nil unless block_given?
  Enumerator.new do |y|
    each_index do |key|
      if (a = class_const.find_by_key(at(key))) && block.call(a)
        y << a
      end
    end
  end
end

#sort_index_key(idx) ⇒ Object



270
271
272
# File 'lib/redis_object/collection.rb', line 270

def sort_index_key(idx)
  class_const.sort_index_key(idx)
end

#storeObject



587
588
589
# File 'lib/redis_object/collection.rb', line 587

def store
  class_const.store
end

#temp_keyObject



248
249
250
# File 'lib/redis_object/collection.rb', line 248

def temp_key
  "#{key}::zintersect_temp::#{RedisObject.new_id(4)}"
end