Class: Toy::Dynamo::Table
- Inherits:
-
Object
- Object
- Toy::Dynamo::Table
- Defined in:
- lib/toy/dynamo/table.rb
Constant Summary collapse
- RETURNED_CONSUMED_CAPACITY =
{ :none => "NONE", :total => "TOTAL" }
- TYPE_INDICATOR =
{ :b => "B", :n => "N", :s => "S", :ss => "SS", :ns => "NS" }
- QUERY_SELECT =
{ :all => "ALL_ATTRIBUTES", :projected => "ALL_PROJECTED_ATTRIBUTES", :count => "COUNT", :specific => "SPECIFIC_ATTRIBUTES" }
- COMPARISON_OPERATOR =
{ :eq => "EQ", :le => "LE", :lt => "LT", :ge => "GE", :gt => "GT", :begins_with => "BEGINS_WITH", :between => "BETWEEN", # Scan only :ne => "NE", :not_null => "NOT_NULL", :null => "NULL", :contains => "CONTAINS", :not_contains => "NOT_CONTAINS", :in => "IN" }
- COMPARISON_OPERATOR_SCAN_ONLY =
[ :ne, :not_null, :null, :contains, :not_contains, :in ]
Instance Attribute Summary collapse
-
#client ⇒ Object
readonly
Returns the value of attribute client.
-
#hash_key ⇒ Object
readonly
Returns the value of attribute hash_key.
-
#range_keys ⇒ Object
readonly
Returns the value of attribute range_keys.
-
#schema_loaded_from_dynamo ⇒ Object
readonly
Returns the value of attribute schema_loaded_from_dynamo.
-
#table_schema ⇒ Object
readonly
Returns the value of attribute table_schema.
Instance Method Summary collapse
- #attr_with_type(attr_name, value) ⇒ Object
- #batch_get_item(keys, options = {}) ⇒ Object
- #create(options = {}) ⇒ Object
- #delete(options = {}) ⇒ Object
- #delete_item(hash_key_value, options = {}) ⇒ Object
- #describe(options = {}) ⇒ Object
- #get_item(hash_key, options = {}) ⇒ Object
- #hash_key_condition_param(value) ⇒ Object
- #hash_key_item_param(value) ⇒ Object
-
#initialize(table_schema, client, options = {}) ⇒ Table
constructor
A new instance of Table.
- #load_schema ⇒ Object
-
#query(hash_key_value, options = {}) ⇒ Object
options * consistent_read * return_consumed_capacity * order * select * range.
- #resize(options = {}) ⇒ Object
-
#scan(options = {}) ⇒ Object
Perform a table scan docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_Scan.html.
-
#table_name ⇒ Object
Call proc or return string.
- #type_from_value(value) ⇒ Object
- #write(hash_key_value, attributes, options = {}) ⇒ Object
Constructor Details
#initialize(table_schema, client, options = {}) ⇒ Table
Returns a new instance of Table.
53 54 55 56 57 |
# File 'lib/toy/dynamo/table.rb', line 53 def initialize(table_schema, client, ={}) @table_schema = table_schema @client = client self.load_schema unless [:novalidate] end |
Instance Attribute Details
#client ⇒ Object (readonly)
Returns the value of attribute client.
5 6 7 |
# File 'lib/toy/dynamo/table.rb', line 5 def client @client end |
#hash_key ⇒ Object (readonly)
Returns the value of attribute hash_key.
5 6 7 |
# File 'lib/toy/dynamo/table.rb', line 5 def hash_key @hash_key end |
#range_keys ⇒ Object (readonly)
Returns the value of attribute range_keys.
5 6 7 |
# File 'lib/toy/dynamo/table.rb', line 5 def range_keys @range_keys end |
#schema_loaded_from_dynamo ⇒ Object (readonly)
Returns the value of attribute schema_loaded_from_dynamo.
5 6 7 |
# File 'lib/toy/dynamo/table.rb', line 5 def schema_loaded_from_dynamo @schema_loaded_from_dynamo end |
#table_schema ⇒ Object (readonly)
Returns the value of attribute table_schema.
5 6 7 |
# File 'lib/toy/dynamo/table.rb', line 5 def table_schema @table_schema end |
Instance Method Details
#attr_with_type(attr_name, value) ⇒ Object
110 111 112 |
# File 'lib/toy/dynamo/table.rb', line 110 def attr_with_type(attr_name, value) { attr_name => { TYPE_INDICATOR[type_from_value(value)] => value.to_s } } end |
#batch_get_item(keys, options = {}) ⇒ Object
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 |
# File 'lib/toy/dynamo/table.rb', line 206 def batch_get_item(keys, ={}) [:return_consumed_capacity] ||= :none [:select] ||= [] [:consistent_read] = false unless [:consistent_read] raise ArgumentError, "must include between 1 - 100 keys" if keys.size == 0 || keys.size > 100 keys_request = [] keys.each do |k| key_request = {} if @range_keys.present? hash_value = k[:hash_value] else raise ArgumentError, "expected keys to be in the form of ['hash key here'] for table with no range keys" if hash_value.is_a?(Hash) hash_value = k end raise ArgumentError, "every key must include a :hash_value" if hash_value.blank? key_request[@hash_key[:attribute_name]] = { @hash_key[:attribute_type] => hash_value.to_s } if @range_keys.present? range_value = k[:range_value] raise ArgumentError, "every key must include a :range_value" if range_value.blank? range_key = @range_keys.find{|k| k[:primary_range_key] } key_request[range_key[:attribute_name]] = { range_key[:attribute_type] => range_value.to_s } end keys_request << key_request end request_items_request = {} request_items_request.merge!( :keys => keys_request ) request_items_request.merge!( :attributes_to_get => [[:select]].flatten ) unless [:select].blank? request_items_request.merge!( :consistent_read => [:consistent_read] ) if [:consistent_read] batch_get_item_request = { :request_items => { ([:table_name] || self.table_name) => request_items_request }, :return_consumed_capacity => RETURNED_CONSUMED_CAPACITY[[:return_consumed_capacity]] } @client.batch_get_item(batch_get_item_request) end |
#create(options = {}) ⇒ Object
417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 |
# File 'lib/toy/dynamo/table.rb', line 417 def create(={}) if @client.list_tables[:table_names].include?([:table_name] || self.table_name) raise "Table #{[:table_name] || self.table_name} already exists" end @client.create_table(@table_schema.merge({ :table_name => [:table_name] || self.table_name })) while ( = self.describe())[:table][:table_status] == "CREATING" sleep 1 end self.load_schema end |
#delete(options = {}) ⇒ Object
438 439 440 441 442 443 444 445 446 447 448 449 |
# File 'lib/toy/dynamo/table.rb', line 438 def delete(={}) return false unless @client.list_tables[:table_names].include?([:table_name] || self.table_name) @client.delete_table(:table_name => [:table_name] || self.table_name) begin while ( = self.describe) && [:table][:table_status] == "DELETING" sleep 1 end rescue AWS::DynamoDB::Errors::ResourceNotFoundException => e Toy::Dynamo::Config.logger.info "Table deleted" end true end |
#delete_item(hash_key_value, options = {}) ⇒ Object
302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 |
# File 'lib/toy/dynamo/table.rb', line 302 def delete_item(hash_key_value, ={}) key_request = { @hash_key[:attribute_name] => { @hash_key[:attribute_type] => hash_key_value.to_s } } if @range_keys range_key = @range_keys.find{|k| k[:primary_range_key]} raise ArgumentError, "range_key was not provided to the delete_item command" if [:range_value].blank? key_request.merge!({ range_key[:attribute_name] => { range_key[:attribute_type] => [:range_value].to_s } }) end delete_item_request = { :table_name => [:table_name] || self.table_name, :key => key_request } @client.delete_item(delete_item_request) end |
#describe(options = {}) ⇒ Object
434 435 436 |
# File 'lib/toy/dynamo/table.rb', line 434 def describe(={}) @client.describe_table(:table_name => [:table_name] || self.table_name) end |
#get_item(hash_key, options = {}) ⇒ Object
114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/toy/dynamo/table.rb', line 114 def get_item(hash_key, ={}) [:consistent_read] = false unless [:consistent_read] [:return_consumed_capacity] ||= :none # "NONE" # || "TOTAL" [:select] ||= [] get_item_request = { :table_name => [:table_name] || self.table_name, :key => hash_key_item_param(hash_key), :consistent_read => [:consistent_read], :return_consumed_capacity => RETURNED_CONSUMED_CAPACITY[[:return_consumed_capacity]] } get_item_request.merge!( :attributes_to_get => [[:select]].flatten ) unless [:select].blank? @client.get_item(get_item_request) end |
#hash_key_condition_param(value) ⇒ Object
99 100 101 102 103 104 105 106 107 108 |
# File 'lib/toy/dynamo/table.rb', line 99 def hash_key_condition_param(value) hash_key = @table_schema[:key_schema].find{|h| h[:key_type] == "HASH"}[:attribute_name] hash_key_type = @table_schema[:attribute_definitions].find{|h| h[:attribute_name] == hash_key}[:attribute_type] { hash_key => { :attribute_value_list => [hash_key_type => value], :comparison_operator => COMPARISON_OPERATOR[:eq] } } end |
#hash_key_item_param(value) ⇒ Object
93 94 95 96 97 |
# File 'lib/toy/dynamo/table.rb', line 93 def hash_key_item_param(value) hash_key = @table_schema[:key_schema].find{|h| h[:key_type] == "HASH"}[:attribute_name] hash_key_type = @table_schema[:attribute_definitions].find{|h| h[:attribute_name] == hash_key}[:attribute_type] { hash_key => { hash_key_type => value } } end |
#load_schema ⇒ Object
59 60 61 62 63 64 65 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 |
# File 'lib/toy/dynamo/table.rb', line 59 def load_schema @schema_loaded_from_dynamo = self.describe @schema_loaded_from_dynamo[:table][:key_schema].each do |key| key_attr = @table_schema[:attribute_definitions].find{|h| h[:attribute_name] == key[:attribute_name]} next if key_attr.nil? key_schema_attr = { :attribute_name => key[:attribute_name], :attribute_type => key_attr[:attribute_type] } if key[:key_type] == "HASH" @hash_key = key_schema_attr else (@range_keys ||= []) << key_schema_attr.merge(:primary_range_key => true) end end if @schema_loaded_from_dynamo[:table][:local_secondary_indexes] @schema_loaded_from_dynamo[:table][:local_secondary_indexes].each do |key| lsi_range_key = key[:key_schema].find{|h| h[:key_type] == "RANGE" } lsi_range_attribute = @table_schema[:attribute_definitions].find{|h| h[:attribute_name] == lsi_range_key[:attribute_name]} next if lsi_range_attribute.nil? (@range_keys ||= []) << { :attribute_name => lsi_range_key[:attribute_name], :attribute_type => lsi_range_attribute[:attribute_type], :index_name => key[:index_name] } end end @schema_loaded_from_dynamo end |
#query(hash_key_value, options = {}) ⇒ Object
options
* consistent_read
* return_consumed_capacity
* order
* select
* range
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 |
# File 'lib/toy/dynamo/table.rb', line 135 def query(hash_key_value, ={}) [:consistent_read] = false unless [:consistent_read] [:return_consumed_capacity] ||= :none # "NONE" # || "TOTAL" [:order] ||= :desc #options[:index_name] ||= :none #AWS::DynamoDB::Errors::ValidationException: ALL_PROJECTED_ATTRIBUTES can be used only when Querying using an IndexName #options[:limit] ||= 10 #options[:exclusive_start_key] key_conditions = {} key_conditions.merge!(hash_key_condition_param(hash_key_value)) query_request = { :table_name => [:table_name] || self.table_name, :key_conditions => key_conditions, :consistent_read => [:consistent_read], :return_consumed_capacity => RETURNED_CONSUMED_CAPACITY[[:return_consumed_capacity]], :scan_index_forward => ([:order] == :asc) } if [:range] raise ArgumentError, "Expected a 1 element Hash for :range (ex {:age.gt => 13})" unless [:range].is_a?(Hash) && [:range].keys.size == 1 range_key_name, comparison_operator = [:range].keys.first.split(".") raise ArgumentError, "Comparison operator must be one of (#{(COMPARISON_OPERATOR.keys - COMPARISON_OPERATOR_SCAN_ONLY).join(", ")})" unless COMPARISON_OPERATOR.keys.include?(comparison_operator.to_sym) range_key = @range_keys.find{|k| k[:attribute_name] == range_key_name} raise ArgumentError, ":range key must be a valid Range attribute" unless range_key raise ArgumentError, ":range key must be a Range if using the operator BETWEEN" if comparison_operator == "between" && ![:range].values.first.is_a?(Range) if range_key.has_key?(:index_name) # Local Secondary Index #options[:select] = :projected unless options[:select].present? query_request.merge!(:index_name => range_key[:index_name]) end range_value = [:range].values.first range_attribute_list = [] if comparison_operator == "between" range_attribute_list << { range_key[:attribute_type] => range_value.min } range_attribute_list << { range_key[:attribute_type] => range_value.max } else # TODO - support Binary? range_attribute_list = [{ range_key[:attribute_type] => range_value.to_s }] end key_conditions.merge!({ range_key[:attribute_name] => { :attribute_value_list => range_attribute_list, :comparison_operator => COMPARISON_OPERATOR[comparison_operator.to_sym] } }) end # Default if not already set [:select] ||= :all # :all, :projected, :count, [] if [:select].is_a?(Array) attrs_to_select = [[:select].map(&:to_s)].flatten attrs_to_select << @hash_key[:attribute_name] attrs_to_select << @range_keys.find{|k| k[:primary_range_key] }[:attribute_name] if @range_keys query_request.merge!({ :select => QUERY_SELECT[:specific], :attributes_to_get => attrs_to_select.uniq }) else query_request.merge!({ :select => QUERY_SELECT[[:select]] }) end query_request.merge!({ :limit => [:limit].to_i }) if .has_key?(:limit) query_request.merge!({ :exclusive_start_key => [:exclusive_start_key] }) if [:exclusive_start_key] @client.query(query_request) end |
#resize(options = {}) ⇒ Object
451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 |
# File 'lib/toy/dynamo/table.rb', line 451 def resize(={}) return false unless @client.list_tables[:table_names].include?([:table_name] || self.table_name) @client.update_table({ :provisioned_throughput => { :read_capacity_units => ([:read_capacity_units] || @table_schema[:provisioned_throughput][:read_capacity_units]).to_i, :write_capacity_units => ([:write_capacity_units] || @table_schema[:provisioned_throughput][:write_capacity_units]).to_i }, :table_name => [:table_name] || self.table_name }) while ( = self.describe) && [:table][:table_status] == "UPDATING" sleep 1 end Toy::Dynamo::Config.logger.info "Table resized to #{[:table][:provisioned_throughput]}" true end |
#scan(options = {}) ⇒ Object
Perform a table scan docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_Scan.html
346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 |
# File 'lib/toy/dynamo/table.rb', line 346 def scan(={}) [:return_consumed_capacity] ||= :none # "NONE" # || "TOTAL" # Default if not already set [:select] ||= :all # :all, :projected, :count, [] scan_request = { :table_name => [:table_name] || self.table_name, :return_consumed_capacity => RETURNED_CONSUMED_CAPACITY[[:return_consumed_capacity]] } scan_request.merge!({ :limit => [:limit].to_i }) if .has_key?(:limit) scan_request.merge!({ :exclusive_start_key => [:exclusive_start_key] }) if [:exclusive_start_key] if [:select].is_a?(Array) attrs_to_select = [[:select].map(&:to_s)].flatten attrs_to_select << @hash_key[:attribute_name] attrs_to_select << @range_keys.find{|k| k[:primary_range_key] }[:attribute_name] if @range_keys scan_request.merge!({ :select => QUERY_SELECT[:specific], :attributes_to_get => attrs_to_select.uniq }) else scan_request.merge!({ :select => QUERY_SELECT[[:select]] }) end # :scan_filter => { :name.begins_with => "a" } scan_filter = {} if [:scan_filter].present? [:scan_filter].each_pair.each do |k,v| # Hard to validate attribute types here, so infer by type sent and assume the user knows their own attrs key_name, comparison_operator = k.split(".") raise ArgumentError, "Comparison operator must be one of (#{COMPARISON_OPERATOR.keys.join(", ")})" unless COMPARISON_OPERATOR.keys.include?(comparison_operator.to_sym) raise ArgumentError, "scan_filter value must be a Range if using the operator BETWEEN" if comparison_operator == "between" && !v.is_a?(Range) raise ArgumentError, "scan_filter value must be a Array if using the operator IN" if comparison_operator == "in" && !v.is_a?(Array) attribute_value_list = [] if comparison_operator == "in" v.each do |in_v| attribute_value_list << attr_with_type(key_name, in_v).values.last end elsif comparison_operator == "between" attribute_value_list << attr_with_type(key_name, range_value.min).values.last attribute_value_list << attr_with_type(key_name, range_value.max).values.last else attribute_value_list << attr_with_type(key_name, v).values.last end scan_filter.merge!({ key_name => { :comparison_operator => COMPARISON_OPERATOR[comparison_operator.to_sym], :attribute_value_list => attribute_value_list } }) end scan_request.merge!(:scan_filter => scan_filter) end scan_request.merge!({ :segment => [:segment] }) if [:segment] scan_request.merge!({ :total_segments => [:total_segments].to_i }) if [:total_segments] @client.scan(scan_request) end |
#table_name ⇒ Object
Call proc or return string
409 410 411 412 413 414 415 |
# File 'lib/toy/dynamo/table.rb', line 409 def table_name if @table_schema[:table_name].respond_to?(:call) @table_schema[:table_name].call else @table_schema[:table_name] end end |
#type_from_value(value) ⇒ Object
324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 |
# File 'lib/toy/dynamo/table.rb', line 324 def type_from_value(value) case when value.kind_of?(AWS::DynamoDB::Binary) then :b when value.respond_to?(:to_str) then :s when value.kind_of?(Numeric) then :n when value.respond_to?(:each) indicator = nil value.each do |v| member_indicator = type_indicator(v) raise ArgumentError, "nested collections" if member_indicator.to_s.size > 1 raise ArgumentError, "mixed types" if indicator and member_indicator != indicator indicator = member_indicator end indicator ||= :s :"#{indicator}s" else raise ArgumentError, "unsupported attribute type #{value.class}" end end |
#write(hash_key_value, attributes, options = {}) ⇒ Object
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 |
# File 'lib/toy/dynamo/table.rb', line 243 def write(hash_key_value, attributes, ={}) [:return_consumed_capacity] ||= :none [:update_item] = false unless [:update_item] if [:update_item] # UpdateItem key_request = { @hash_key[:attribute_name] => { @hash_key[:attribute_type] => hash_key_value.to_s } } if @range_keys range_key = @range_keys.find{|k| k[:primary_range_key]} range_key_value = attributes[range_key[:attribute_name]] raise ArgumentError, "range_key was not provided to the write command" if range_key_value.blank? key_request.merge!({ range_key[:attribute_name] => { range_key[:attribute_type] => range_key_value.to_s } }) end attrs_to_update = {} attributes.each_pair do |k,v| next if @range_keys && k == range_key[:attribute_name] if v.blank? attrs_to_update.merge!({ k => { :action => "DELETE" } }) else attrs_to_update.merge!({ k => { :value => attr_with_type(k,v).values.last, :action => "PUT" } }) end end update_item_request = { :table_name => [:table_name] || self.table_name, :key => key_request, :attribute_updates => attrs_to_update, :return_consumed_capacity => RETURNED_CONSUMED_CAPACITY[[:return_consumed_capacity]] } @client.update_item(update_item_request) else # PutItem items = {} attributes.each_pair do |k,v| next if v.blank? # If empty string or nil, skip... items.merge!(attr_with_type(k,v)) end items.merge!(hash_key_item_param(hash_key_value)) put_item_request = { :table_name => [:table_name] || self.table_name, :item => items, :return_consumed_capacity => RETURNED_CONSUMED_CAPACITY[[:return_consumed_capacity]] } @client.put_item(put_item_request) end end |