Module: Dynamoid::Indexes::ClassMethods
- Defined in:
- lib/dynamoid/indexes.rb
Instance Method Summary collapse
- #find_index(hash, range = nil) ⇒ Object
-
#global_secondary_index(options = {}) ⇒ Object
Defines a Global Secondary index on a table.
-
#index_key(hash, range = nil) ⇒ String
Generates a convenient lookup key name for a hash/range index.
-
#index_name(hash, range = nil) ⇒ String
Generates a default index name.
- #indexed_hash_keys ⇒ Object
-
#indexes ⇒ Hash<String, Object>
Convenience method to return all indexes on the table.
-
#is_global_secondary_index?(hash, range = nil) ⇒ Boolean
Returns true iff the provided hash key combo is a global secondary index.
-
#is_local_secondary_index?(hash, range = nil) ⇒ Boolean
Returns true iff the provided hash key combo is a local secondary index.
-
#local_secondary_index(options = {}) ⇒ Object
Defines a local secondary index on a table.
Instance Method Details
#find_index(hash, range = nil) ⇒ Object
96 97 98 99 |
# File 'lib/dynamoid/indexes.rb', line 96 def find_index(hash, range=nil) index = self.indexes[index_key(hash, range)] index end |
#global_secondary_index(options = {}) ⇒ Object
Defines a Global Secondary index on a table. Keys can be specified as hash-only, or hash & range.
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/dynamoid/indexes.rb', line 30 def global_secondary_index(={}) unless .present? raise Dynamoid::Errors::InvalidIndex.new('empty index definition') end unless [:hash_key].present? raise Dynamoid::Errors::InvalidIndex.new( 'A global secondary index requires a :hash_key to be specified' ) end index_opts = { read_capacity: Dynamoid::Config.read_capacity, write_capacity: Dynamoid::Config.write_capacity }.merge() index_opts[:dynamoid_class] = self index_opts[:type] = :global_secondary index = Dynamoid::Indexes::Index.new(index_opts) gsi_key = index_key([:hash_key], [:range_key]) self.global_secondary_indexes[gsi_key] = index self end |
#index_key(hash, range = nil) ⇒ String
Generates a convenient lookup key name for a hash/range index. Should normally not be used directly.
129 130 131 132 133 134 135 |
# File 'lib/dynamoid/indexes.rb', line 129 def index_key(hash, range=nil) name = hash.to_s if range.present? name += "_#{range.to_s}" end name end |
#index_name(hash, range = nil) ⇒ String
Generates a default index name.
142 143 144 |
# File 'lib/dynamoid/indexes.rb', line 142 def index_name(hash, range=nil) "#{self.table_name}_index_#{self.index_key(hash, range)}" end |
#indexed_hash_keys ⇒ Object
154 155 156 157 158 |
# File 'lib/dynamoid/indexes.rb', line 154 def indexed_hash_keys self.global_secondary_indexes.map do |name, index| index.hash_key.to_s end end |
#indexes ⇒ Hash<String, Object>
Convenience method to return all indexes on the table.
150 151 152 |
# File 'lib/dynamoid/indexes.rb', line 150 def indexes self.local_secondary_indexes.merge(self.global_secondary_indexes) end |
#is_global_secondary_index?(hash, range = nil) ⇒ Boolean
Returns true iff the provided hash key combo is a global secondary index.
119 120 121 |
# File 'lib/dynamoid/indexes.rb', line 119 def is_global_secondary_index?(hash, range=nil) self.global_secondary_indexes[index_key(hash, range)].present? end |
#is_local_secondary_index?(hash, range = nil) ⇒ Boolean
Returns true iff the provided hash key combo is a local secondary index.
108 109 110 |
# File 'lib/dynamoid/indexes.rb', line 108 def is_local_secondary_index?(hash, range=nil) self.local_secondary_indexes[index_key(hash, range)].present? end |
#local_secondary_index(options = {}) ⇒ Object
Defines a local secondary index on a table. Will use the same primary hash key as the table.
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/dynamoid/indexes.rb', line 66 def local_secondary_index(={}) unless .present? raise Dynamoid::Errors::InvalidIndex.new('empty index definition') end primary_hash_key = self.hash_key primary_range_key = self.range_key index_range_key = [:range_key] unless index_range_key.present? raise Dynamoid::Errors::InvalidIndex.new('A local secondary index '\ 'requires a :range_key to be specified') end if primary_range_key.present? && index_range_key == primary_range_key raise Dynamoid::Errors::InvalidIndex.new('A local secondary index'\ ' must use a different :range_key than the primary key') end index_opts = .merge( dynamoid_class: self, type: :local_secondary, hash_key: primary_hash_key) index = Dynamoid::Indexes::Index.new(index_opts) key = index_key(primary_hash_key, index_range_key) self.local_secondary_indexes[key] = index self end |