Module: Seabright::Indices::ClassMethods
- Defined in:
- lib/redis_object/indices.rb
Instance Method Summary collapse
- #has_index?(k) ⇒ Boolean
- #has_sort_index?(k) ⇒ Boolean
- #index(k) ⇒ Object
- #index_key(k, v) ⇒ Object
- #indexed(idx, num = -1,, reverse = false) ⇒ Object
- #indices ⇒ Object
- #intercept_sets_for_indices! ⇒ Object
- #latest ⇒ Object
- #reindex(k) ⇒ Object
- #reindex_all_indices! ⇒ Object
- #sort_by(k) ⇒ Object
- #sort_index_key(idx) ⇒ Object
- #sort_indices ⇒ Object
Instance Method Details
#has_index?(k) ⇒ Boolean
122 123 124 |
# File 'lib/redis_object/indices.rb', line 122 def has_index?(k) k and indices.include?(k.to_sym) end |
#has_sort_index?(k) ⇒ Boolean
126 127 128 |
# File 'lib/redis_object/indices.rb', line 126 def has_sort_index?(k) k and sort_indices.include?(k.to_sym) end |
#index(k) ⇒ Object
95 96 97 98 |
# File 'lib/redis_object/indices.rb', line 95 def index(k) indices << k.to_sym intercept_sets_for_indices! end |
#index_key(k, v) ⇒ Object
79 80 81 |
# File 'lib/redis_object/indices.rb', line 79 def index_key(k,v) "#{self.plname}::field_index::#{k}::#{v}" end |
#indexed(idx, num = -1,, reverse = false) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/redis_object/indices.rb', line 61 def indexed(idx,num=-1,reverse=false) kys = store.send(reverse ? :zrevrange : :zrange, sort_index_key(idx), 0, num-1) out = ListEnumerator.new(kys) do |yielder| kys.each do |member| if a = self.find_by_key(member) yielder << a end end end if block_given? out.each do |itm| yield itm end else out end end |
#indices ⇒ Object
87 88 89 |
# File 'lib/redis_object/indices.rb', line 87 def indices @@indices ||= Set.new end |
#intercept_sets_for_indices! ⇒ Object
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 |
# File 'lib/redis_object/indices.rb', line 21 def intercept_sets_for_indices! return if @intercepted_sets_for_indices self.class_eval do def set_index(k,v,hkey) if cur = get(k) store.srem(self.class.index_key(k,cur), hkey) end store.sadd(self.class.index_key(k,v), hkey) end def set_sort_index(k,v,hkey) store.zrem(self.class.sort_index_key(k), hkey) store.zadd(self.class.sort_index_key(k), score_format(k,v), hkey) end filter_sets do |obj, k, v| if obj.class.has_index?(k) obj.set_index k, v, obj.hkey end if obj.class.has_sort_index?(k) obj.set_sort_index k, v, obj.hkey end [k, v] end filter_msets do |obj, dat| dat.each do |k,v| obj.set_index(k, v, obj.hkey) if obj.class.has_index?(k) end dat.each do |k,v| obj.set_sort_index(k, v, obj.hkey) if obj.class.has_sort_index?(k) end dat end end @intercepted_sets_for_indices = true end |
#latest ⇒ Object
130 131 132 |
# File 'lib/redis_object/indices.rb', line 130 def latest indexed(:created_at,999,true).first end |
#reindex(k) ⇒ Object
105 106 107 108 109 110 111 112 113 114 |
# File 'lib/redis_object/indices.rb', line 105 def reindex(k) store.keys(index_key(k,"*")).each do |ik| store.del ik end all.each do |obj| if v = obj.get(k) obj.set_index(k, v, obj.hkey) end end end |
#reindex_all_indices! ⇒ Object
116 117 118 119 120 |
# File 'lib/redis_object/indices.rb', line 116 def reindex_all_indices! indices.each do |k| reindex(k) end end |
#sort_by(k) ⇒ Object
100 101 102 103 |
# File 'lib/redis_object/indices.rb', line 100 def sort_by(k) sort_indices << k.to_sym intercept_sets_for_indices! end |
#sort_index_key(idx) ⇒ Object
83 84 85 |
# File 'lib/redis_object/indices.rb', line 83 def sort_index_key(idx) "#{self.plname}::#{idx}" end |
#sort_indices ⇒ Object
91 92 93 |
# File 'lib/redis_object/indices.rb', line 91 def sort_indices @@sort_indices ||= Set.new end |