Module: Chimera::Indexes::ClassMethods
- Defined in:
- lib/chimera/indexes.rb
Instance Method Summary collapse
- #defined_indexes ⇒ Object
- #digest(val) ⇒ Object
- #find_with_index(name, opts_or_query = nil) ⇒ Object
-
#index(name, type = :find) ⇒ Object
available types include: find, unique, search, geo.
- #key_for_all_index ⇒ Object
- #key_for_index(type, name, val) ⇒ Object
Instance Method Details
#defined_indexes ⇒ Object
11 12 13 |
# File 'lib/chimera/indexes.rb', line 11 def defined_indexes @defined_indexes || {} end |
#digest(val) ⇒ Object
79 80 81 |
# File 'lib/chimera/indexes.rb', line 79 def digest(val) Digest::SHA1.hexdigest(val) end |
#find_with_index(name, opts_or_query = nil) ⇒ Object
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 60 61 62 63 64 65 66 |
# File 'lib/chimera/indexes.rb', line 22 def find_with_index(name, opts_or_query=nil) if opts_or_query.is_a?(Hash) q = opts_or_query[:q].to_s else q = opts_or_query.to_s end if name.to_sym == :all llen = self.connection(:redis).llen(self.key_for_all_index) curr = 0 while(curr < llen) keys = self.connection(:redis).lrange(self.key_for_all_index, curr, curr+9).compact find_many(keys).each { |obj| yield(obj) } curr += 10 end elsif props = self.defined_indexes[name.to_sym] case props[:type] when :find then if q and !q.blank? index_key = self.key_for_index(:find, name, q) self.find_many(self.connection(:redis).smembers(index_key)) end when :unique then if q and !q.blank? index_key = self.key_for_index(:unique, name, q) Array(self.find(self.connection(:redis).get(index_key))) end when :search then keys = [] q.split(" ").each do |word| word = word.downcase.stem next if Chimera::Indexes::SEARCH_EXCLUDE_LIST.include?(word) keys << self.key_for_index(:search, name, word) end if keys.size > 0 if opts_or_query.is_a?(Hash) and opts_or_query[:type] == :union self.find_many(self.connection(:redis).sunion(keys.join(" "))) else self.find_many(self.connection(:redis).sinter(keys.join(" "))) end end when :geo then find_with_geo_index(name, opts_or_query) end # case end # if props end |
#index(name, type = :find) ⇒ Object
available types include:
find, unique, search, geo
17 18 19 20 |
# File 'lib/chimera/indexes.rb', line 17 def index(name, type = :find) @defined_indexes ||= {} @defined_indexes[name.to_sym] = type end |
#key_for_all_index ⇒ Object
75 76 77 |
# File 'lib/chimera/indexes.rb', line 75 def key_for_all_index "#{self.to_s}::Indexes::All" end |
#key_for_index(type, name, val) ⇒ Object
68 69 70 71 72 73 |
# File 'lib/chimera/indexes.rb', line 68 def key_for_index(type, name, val) case type.to_sym when :find, :unique, :search then "#{self.to_s}::Indexes::#{type}::#{name}::#{digest(val)}" end end |