Class: Dynamoid::AdapterPlugin::AwsSdkV2
- Inherits:
-
Object
- Object
- Dynamoid::AdapterPlugin::AwsSdkV2
- Defined in:
- lib/dynamoid/adapter_plugin/aws_sdk_v2.rb
Overview
The AwsSdkV2 adapter provides support for the aws-sdk version 2 for ruby.
Defined Under Namespace
Classes: ItemUpdater, Table
Constant Summary collapse
- EQ =
"EQ".freeze
- RANGE_MAP =
{ range_greater_than: 'GT', range_less_than: 'LT', range_gte: 'GE', range_lte: 'LE', range_begins_with: 'BEGINS_WITH', range_between: 'BETWEEN', range_eq: 'EQ' }
- HASH_KEY =
"HASH".freeze
- RANGE_KEY =
"RANGE".freeze
- STRING_TYPE =
"S".freeze
- NUM_TYPE =
"N".freeze
- BINARY_TYPE =
"B".freeze
- TABLE_STATUSES =
{ creating: "CREATING", updating: "UPDATING", deleting: "DELETING", active: "ACTIVE" }.freeze
- PARSE_TABLE_STATUS =
->(resp, lookup = :table) { # lookup is table for describe_table API # lookup is table_description for create_table API # because Amazon, damnit. resp.send(lookup).table_status }
Instance Attribute Summary collapse
-
#table_cache ⇒ Object
readonly
Returns the value of attribute table_cache.
Instance Method Summary collapse
-
#batch_delete_item(options) ⇒ Object
Delete many items at once from DynamoDB.
-
#batch_get_item(table_ids, options = {}) ⇒ Hash
Get many items at once from DynamoDB.
-
#batch_write_item(table_name, objects, options = {}) ⇒ Object
Puts or deletes multiple items in one or more tables.
-
#client ⇒ Object
Return the client object.
-
#connect! ⇒ Aws::DynamoDB::Client
Establish the connection to DynamoDB.
- #connection_config ⇒ Object
- #count(table_name) ⇒ Object
-
#create_table(table_name, key = :id, options = {}) ⇒ Object
Create a table on DynamoDB.
-
#create_table_synchronously(table_name, key = :id, options = {}) ⇒ Object
Create a table on DynamoDB synchronously.
-
#delete_item(table_name, key, options = {}) ⇒ Object
Removes an item from DynamoDB.
-
#delete_table(table_name, options = {}) ⇒ Object
Deletes an entire table from DynamoDB.
- #delete_table_synchronously(table_name, options = {}) ⇒ Object
-
#get_item(table_name, key, options = {}) ⇒ Hash
Fetches an item from DynamoDB.
-
#list_tables ⇒ Object
List all tables on DynamoDB.
-
#put_item(table_name, object, options = {}) ⇒ Object
Persists an item on DynamoDB.
-
#query(table_name, opts = {}) ⇒ Enumerable
Query the DynamoDB table.
-
#scan(table_name, scan_hash, select_opts = {}) ⇒ Enumerable
Scan the DynamoDB table.
-
#truncate(table_name) ⇒ Object
Truncates all records in the given table.
-
#update_item(table_name, key, options = {}) {|iu = ItemUpdater.new(table, key, range_key)| ... } ⇒ Object
Edits an existing item’s attributes, or adds a new item to the table if it does not already exist.
Instance Attribute Details
#table_cache ⇒ Object (readonly)
Returns the value of attribute table_cache.
33 34 35 |
# File 'lib/dynamoid/adapter_plugin/aws_sdk_v2.rb', line 33 def table_cache @table_cache end |
Instance Method Details
#batch_delete_item(options) ⇒ Object
Delete many items at once from DynamoDB. More efficient than delete each item individually.
or
Dynamoid::AdapterPlugin::AwsSdkV2.batch_delete_item('table1' => [['hk1', 'rk2'], ['hk1', 'rk2']]]))
@todo: Provide support for passing options to underlying delete_item docs.aws.amazon.com/sdkforruby/api/Aws/DynamoDB/Client.html#delete_item-instance_method
165 166 167 168 169 170 171 172 173 |
# File 'lib/dynamoid/adapter_plugin/aws_sdk_v2.rb', line 165 def batch_delete_item() .each_pair do |table_name, ids| table = describe_table(table_name) ids.each do |id| client.delete_item(table_name: table_name, key: key_stanza(table, *id)) end end nil end |
#batch_get_item(table_ids, options = {}) ⇒ Hash
Get many items at once from DynamoDB. More efficient than getting each item individually.
@todo: Provide support for passing options to underlying batch_get_item docs.aws.amazon.com/sdkforruby/api/Aws/DynamoDB/Client.html#batch_get_item-instance_method
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/dynamoid/adapter_plugin/aws_sdk_v2.rb', line 111 def batch_get_item(table_ids, = {}) request_items = Hash.new{|h, k| h[k] = []} return request_items if table_ids.all?{|k, v| v.empty?} ret = Hash.new([].freeze) # Default for tables where no rows are returned table_ids.each do |t, ids| next if ids.empty? tbl = describe_table(t) hk = tbl.hash_key.to_s rng = tbl.range_key.to_s Array(ids).each_slice(Dynamoid::Config.batch_size) do |ids| request_items = Hash.new{|h, k| h[k] = []} keys = if rng.present? Array(ids).map do |h,r| { hk => h, rng => r } end else Array(ids).map do |id| { hk => id } end end request_items[t] = { keys: keys } results = client.batch_get_item( request_items: request_items ) results.data[:responses].each do |table, rows| ret[table] += rows.collect { |r| result_item_to_hash(r) } end end end ret end |
#batch_write_item(table_name, objects, options = {}) ⇒ Object
Puts or deletes multiple items in one or more tables
See: docs.aws.amazon.com/sdkforruby/api/Aws/DynamoDB/Client.html#batch_write_item-instance_method
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/dynamoid/adapter_plugin/aws_sdk_v2.rb', line 76 def batch_write_item table_name, objects, = {} request_items = [] ||= {} objects.each do |o| request_items << { "put_request" => { item: o } } end begin client.batch_write_item( { request_items: { table_name => request_items, }, return_consumed_capacity: "TOTAL", return_item_collection_metrics: "SIZE" }.merge!() ) rescue Aws::DynamoDB::Errors::ConditionalCheckFailedException => e raise Dynamoid::Errors::ConditionalCheckFailedException, e end end |
#client ⇒ Object
Return the client object.
65 66 67 |
# File 'lib/dynamoid/adapter_plugin/aws_sdk_v2.rb', line 65 def client @client end |
#connect! ⇒ Aws::DynamoDB::Client
Establish the connection to DynamoDB.
38 39 40 41 |
# File 'lib/dynamoid/adapter_plugin/aws_sdk_v2.rb', line 38 def connect! @client = Aws::DynamoDB::Client.new(connection_config) @table_cache = {} end |
#connection_config ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/dynamoid/adapter_plugin/aws_sdk_v2.rb', line 43 def connection_config @connection_hash = {} if Dynamoid::Config.endpoint? @connection_hash[:endpoint] = Dynamoid::Config.endpoint end if Dynamoid::Config.access_key? @connection_hash[:access_key_id] = Dynamoid::Config.access_key end if Dynamoid::Config.secret_key? @connection_hash[:secret_access_key] = Dynamoid::Config.secret_key end if Dynamoid::Config.region? @connection_hash[:region] = Dynamoid::Config.region end @connection_hash end |
#count(table_name) ⇒ Object
544 545 546 |
# File 'lib/dynamoid/adapter_plugin/aws_sdk_v2.rb', line 544 def count(table_name) describe_table(table_name, true).item_count end |
#create_table(table_name, key = :id, options = {}) ⇒ Object
Create a table on DynamoDB. This usually takes a long time to complete.
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 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/dynamoid/adapter_plugin/aws_sdk_v2.rb', line 185 def create_table(table_name, key = :id, = {}) Dynamoid.logger.info "Creating #{table_name} table. This could take a while." read_capacity = [:read_capacity] || Dynamoid::Config.read_capacity write_capacity = [:write_capacity] || Dynamoid::Config.write_capacity secondary_indexes = .slice( :local_secondary_indexes, :global_secondary_indexes ) ls_indexes = [:local_secondary_indexes] gs_indexes = [:global_secondary_indexes] key_schema = { :hash_key_schema => { key => ([:hash_key_type] || :string) }, :range_key_schema => [:range_key] } attribute_definitions = build_all_attribute_definitions( key_schema, secondary_indexes ) key_schema = aws_key_schema( key_schema[:hash_key_schema], key_schema[:range_key_schema] ) client_opts = { table_name: table_name, provisioned_throughput: { read_capacity_units: read_capacity, write_capacity_units: write_capacity }, key_schema: key_schema, attribute_definitions: attribute_definitions } if ls_indexes.present? client_opts[:local_secondary_indexes] = ls_indexes.map do |index| index_to_aws_hash(index) end end if gs_indexes.present? client_opts[:global_secondary_indexes] = gs_indexes.map do |index| index_to_aws_hash(index) end end resp = client.create_table(client_opts) [:sync] = true if !.has_key?(:sync) && ls_indexes.present? || gs_indexes.present? until_past_table_status(table_name) if [:sync] && (status = PARSE_TABLE_STATUS.call(resp, :table_description)) && status != TABLE_STATUSES[:creating] # Response to original create_table, which, if options[:sync] # may have an outdated table_description.table_status of "CREATING" resp rescue Aws::DynamoDB::Errors::ResourceInUseException => e Dynamoid.logger.error "Table #{table_name} cannot be created as it already exists" end |
#create_table_synchronously(table_name, key = :id, options = {}) ⇒ Object
Create a table on DynamoDB synchronously. This usually takes a long time to complete. CreateTable is normally an asynchronous operation. You can optionally define secondary indexes on the new table,
as part of the CreateTable operation.
If you want to create multiple tables with secondary indexes on them,
you must create the tables sequentially.
Only one table with secondary indexes can be
in the CREATING state at any given time.
See: docs.aws.amazon.com/sdkforruby/api/Aws/DynamoDB/Client.html#create_table-instance_method
261 262 263 |
# File 'lib/dynamoid/adapter_plugin/aws_sdk_v2.rb', line 261 def create_table_synchronously(table_name, key = :id, = {}) create_table(table_name, key, .merge(sync: true)) end |
#delete_item(table_name, key, options = {}) ⇒ Object
Removes an item from DynamoDB.
@todo: Provide support for various options docs.aws.amazon.com/sdkforruby/api/Aws/DynamoDB/Client.html#delete_item-instance_method
274 275 276 277 278 279 280 281 282 283 284 285 286 |
# File 'lib/dynamoid/adapter_plugin/aws_sdk_v2.rb', line 274 def delete_item(table_name, key, = {}) ||= {} range_key = [:range_key] conditions = [:conditions] table = describe_table(table_name) client.delete_item( table_name: table_name, key: key_stanza(table, key, range_key), expected: expected_stanza(conditions) ) rescue Aws::DynamoDB::Errors::ConditionalCheckFailedException => e raise Dynamoid::Errors::ConditionalCheckFailedException, e end |
#delete_table(table_name, options = {}) ⇒ Object
Deletes an entire table from DynamoDB.
294 295 296 297 298 299 300 301 302 303 |
# File 'lib/dynamoid/adapter_plugin/aws_sdk_v2.rb', line 294 def delete_table(table_name, = {}) resp = client.delete_table(table_name: table_name) until_past_table_status(table_name, :deleting) if [:sync] && (status = PARSE_TABLE_STATUS.call(resp, :table_description)) && status != TABLE_STATUSES[:deleting] table_cache.delete(table_name) rescue Aws::DynamoDB::Errors::ResourceInUseException => e Dynamoid.logger.error "Table #{table_name} cannot be deleted as it is in use" raise e end |
#delete_table_synchronously(table_name, options = {}) ⇒ Object
305 306 307 |
# File 'lib/dynamoid/adapter_plugin/aws_sdk_v2.rb', line 305 def delete_table_synchronously(table_name, = {}) delete_table(table_name, .merge(sync: true)) end |
#get_item(table_name, key, options = {}) ⇒ Hash
Provide support for various options docs.aws.amazon.com/sdkforruby/api/Aws/DynamoDB/Client.html#get_item-instance_method
Fetches an item from DynamoDB.
322 323 324 325 326 327 328 329 330 331 |
# File 'lib/dynamoid/adapter_plugin/aws_sdk_v2.rb', line 322 def get_item(table_name, key, = {}) ||= {} table = describe_table(table_name) range_key = .delete(:range_key) item = client.get_item(table_name: table_name, key: key_stanza(table, key, range_key) )[:item] item ? result_item_to_hash(item) : nil end |
#list_tables ⇒ Object
Provide limit support docs.aws.amazon.com/sdkforruby/api/Aws/DynamoDB/Client.html#update_item-instance_method
List all tables on DynamoDB.
368 369 370 |
# File 'lib/dynamoid/adapter_plugin/aws_sdk_v2.rb', line 368 def list_tables client.list_tables[:table_names] end |
#put_item(table_name, object, options = {}) ⇒ Object
Persists an item on DynamoDB.
See: docs.aws.amazon.com/sdkforruby/api/Aws/DynamoDB/Client.html#put_item-instance_method
380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 |
# File 'lib/dynamoid/adapter_plugin/aws_sdk_v2.rb', line 380 def put_item(table_name, object, = {}) item = {} ||= {} object.each do |k, v| next if v.nil? || (v.respond_to?(:empty?) && v.empty?) item[k.to_s] = v end begin client.put_item( { table_name: table_name, item: item, expected: expected_stanza() }.merge!() ) rescue Aws::DynamoDB::Errors::ConditionalCheckFailedException => e raise Dynamoid::Errors::ConditionalCheckFailedException, e end end |
#query(table_name, opts = {}) ⇒ Enumerable
Provide support for various other options docs.aws.amazon.com/sdkforruby/api/Aws/DynamoDB/Client.html#query-instance_method
Query the DynamoDB table. This employs DynamoDB’s indexes so is generally faster than scanning, but is only really useful for range queries, since it can only find by one hash key at once. Only provide one range key to the hash.
420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 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 474 475 476 477 478 479 480 481 482 |
# File 'lib/dynamoid/adapter_plugin/aws_sdk_v2.rb', line 420 def query(table_name, opts = {}) table = describe_table(table_name) hk = (opts[:hash_key].present? ? opts[:hash_key] : table.hash_key).to_s rng = (opts[:range_key].present? ? opts[:range_key] : table.range_key).to_s q = opts.slice( :consistent_read, :scan_index_forward, :limit, :select, :index_name ) opts.delete(:consistent_read) opts.delete(:scan_index_forward) opts.delete(:limit) opts.delete(:select) opts.delete(:index_name) opts.delete(:next_token).tap do |token| break unless token q[:exclusive_start_key] = { hk => token[:hash_key_element], rng => token[:range_key_element] } end key_conditions = { hk => { # TODO: Provide option for other operators like NE, IN, LE, etc comparison_operator: EQ, attribute_value_list: [ opts.delete(:hash_value).freeze ] } } opts.each_pair do |k, v| # TODO: ATM, only few comparison operators are supported, provide support for all operators next unless(op = RANGE_MAP[k]) key_conditions[rng] = { comparison_operator: op, attribute_value_list: [ opts.delete(k).freeze ].flatten # Flatten as BETWEEN operator specifies array of two elements } end q[:table_name] = table_name q[:key_conditions] = key_conditions Enumerator.new { |y| loop do results = client.query(q) results.items.each { |row| y << result_item_to_hash(row) } if(lk = results.last_evaluated_key) q[:exclusive_start_key] = lk else break end end } end |
#scan(table_name, scan_hash, select_opts = {}) ⇒ Enumerable
Scan the DynamoDB table. This is usually a very slow operation as it naively filters all data on the DynamoDB servers.
@todo: Provide support for various options docs.aws.amazon.com/sdkforruby/api/Aws/DynamoDB/Client.html#scan-instance_method
495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 |
# File 'lib/dynamoid/adapter_plugin/aws_sdk_v2.rb', line 495 def scan(table_name, scan_hash, select_opts = {}) limit = select_opts.delete(:limit) batch = select_opts.delete(:batch_size) request = { table_name: table_name } request[:limit] = batch || limit if batch || limit request[:scan_filter] = scan_hash.reduce({}) do |memo, kvp| memo[kvp[0].to_s] = { attribute_value_list: [kvp[1]], # TODO: Provide support for all comparison operators comparison_operator: EQ } memo end if scan_hash.present? Enumerator.new do |y| # Batch loop, pulls multiple requests until done using the start_key loop do results = client.scan(request) results.data[:items].each { |row| y << result_item_to_hash(row) } if((lk = results[:last_evaluated_key]) && batch) request[:exclusive_start_key] = lk else break end end end end |
#truncate(table_name) ⇒ Object
Truncates all records in the given table
532 533 534 535 536 537 538 539 540 541 542 |
# File 'lib/dynamoid/adapter_plugin/aws_sdk_v2.rb', line 532 def truncate(table_name) table = describe_table(table_name) hk = table.hash_key rk = table.range_key scan(table_name, {}, {}).each do |attributes| opts = {} opts[:range_key] = attributes[rk.to_sym] if rk delete_item(table_name, attributes[hk], opts) end end |
#update_item(table_name, key, options = {}) {|iu = ItemUpdater.new(table, key, range_key)| ... } ⇒ Object
Provide support for various options docs.aws.amazon.com/sdkforruby/api/Aws/DynamoDB/Client.html#update_item-instance_method
Edits an existing item’s attributes, or adds a new item to the table if it does not already exist. You can put, delete, or add attribute values
342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 |
# File 'lib/dynamoid/adapter_plugin/aws_sdk_v2.rb', line 342 def update_item(table_name, key, = {}) range_key = .delete(:range_key) conditions = .delete(:conditions) table = describe_table(table_name) yield(iu = ItemUpdater.new(table, key, range_key)) raise "non-empty options: #{}" unless .empty? begin result = client.update_item(table_name: table_name, key: key_stanza(table, key, range_key), attribute_updates: iu.to_h, expected: expected_stanza(conditions), return_values: "ALL_NEW" ) result_item_to_hash(result[:attributes]) rescue Aws::DynamoDB::Errors::ConditionalCheckFailedException => e raise Dynamoid::Errors::ConditionalCheckFailedException, e end end |