Class: Dynamoid::AdapterPlugin::AwsSdkV3
- Inherits:
-
Object
- Object
- Dynamoid::AdapterPlugin::AwsSdkV3
- Defined in:
- lib/dynamoid/adapter_plugin/aws_sdk_v3.rb,
lib/dynamoid/adapter_plugin/aws_sdk_v3/scan.rb,
lib/dynamoid/adapter_plugin/aws_sdk_v3/query.rb,
lib/dynamoid/adapter_plugin/aws_sdk_v3/table.rb,
lib/dynamoid/adapter_plugin/aws_sdk_v3/create_table.rb,
lib/dynamoid/adapter_plugin/aws_sdk_v3/item_updater.rb,
lib/dynamoid/adapter_plugin/aws_sdk_v3/batch_get_item.rb,
lib/dynamoid/adapter_plugin/aws_sdk_v3/middleware/limit.rb,
lib/dynamoid/adapter_plugin/aws_sdk_v3/execute_statement.rb,
lib/dynamoid/adapter_plugin/aws_sdk_v3/middleware/backoff.rb,
lib/dynamoid/adapter_plugin/aws_sdk_v3/middleware/start_key.rb,
lib/dynamoid/adapter_plugin/aws_sdk_v3/until_past_table_status.rb
Overview
Note: Don’t use keyword arguments in public methods as far as method calls on adapter are delegated to the plugin.
There are breaking changes in Ruby related to delegating keyword arguments so we have decided just to avoid them when use delegation.
eregon.me/blog/2019/11/10/the-delegation-challenge-of-ruby27.html
Defined Under Namespace
Modules: Middleware Classes: BatchGetItem, CreateTable, ExecuteStatement, ItemUpdater, Query, Scan, Table, UntilPastTableStatus
Constant Summary collapse
- EQ =
'EQ'
- 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' }.freeze
- FIELD_MAP =
{ eq: 'EQ', ne: 'NE', gt: 'GT', lt: 'LT', gte: 'GE', lte: 'LE', begins_with: 'BEGINS_WITH', between: 'BETWEEN', in: 'IN', contains: 'CONTAINS', not_contains: 'NOT_CONTAINS', null: 'NULL', not_null: 'NOT_NULL', }.freeze
- HASH_KEY =
'HASH'
- RANGE_KEY =
'RANGE'
- STRING_TYPE =
'S'
- NUM_TYPE =
'N'
- BINARY_TYPE =
'B'
- TABLE_STATUSES =
{ creating: 'CREATING', updating: 'UPDATING', deleting: 'DELETING', active: 'ACTIVE' }.freeze
- PARSE_TABLE_STATUS =
lambda { |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 }
- BATCH_WRITE_ITEM_REQUESTS_LIMIT =
25
- CONNECTION_CONFIG_OPTIONS =
%i[endpoint region http_continue_timeout http_idle_timeout http_open_timeout http_read_timeout].freeze
Instance Attribute Summary collapse
-
#table_cache ⇒ Object
readonly
Returns the value of attribute table_cache.
Class Method Summary collapse
-
.attribute_value_list(operator, value) ⇒ Object
Build an array of values for Condition Is used in ScanFilter and QueryFilter docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_Condition.html.
Instance Method Summary collapse
-
#batch_delete_item(options) ⇒ Object
Delete many items at once from DynamoDB.
-
#batch_get_item(table_names_with_ids, options = {}, &block) ⇒ Hash
Get many items at once from DynamoDB.
-
#batch_write_item(table_name, objects, options = {}) {|true|false| ... } ⇒ Object
Puts multiple items in one table.
-
#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
-
#execute(statement, parameters = [], options = {}) ⇒ [] | Array[Hash] | Enumerator::Lazy[Hash]
Run PartiQL query.
-
#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, options = {}) ⇒ Enumerable
Query the DynamoDB table.
- #query_count(table_name, options = {}) ⇒ Object
-
#scan(table_name, conditions = {}, options = {}) ⇒ Enumerable
Scan the DynamoDB table.
- #scan_count(table_name, conditions = {}, options = {}) ⇒ Object
-
#truncate(table_name) ⇒ Object
Truncates all records in the given table.
-
#update_item(table_name, key, options = {}) ⇒ Object
Edits an existing item’s attributes, or adds a new item to the table if it does not already exist.
- #update_time_to_live(table_name, attribute) ⇒ Object
Instance Attribute Details
#table_cache ⇒ Object (readonly)
Returns the value of attribute table_cache.
73 74 75 |
# File 'lib/dynamoid/adapter_plugin/aws_sdk_v3.rb', line 73 def table_cache @table_cache end |
Class Method Details
.attribute_value_list(operator, value) ⇒ Object
Build an array of values for Condition Is used in ScanFilter and QueryFilter docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_Condition.html
80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/dynamoid/adapter_plugin/aws_sdk_v3.rb', line 80 def self.attribute_value_list(operator, value) # For BETWEEN and IN operators we should keep value as is (it should be already an array) # NULL and NOT_NULL require absence of attribute list # For all the other operators we wrap the value with array # https://docs.aws.amazon.com/en_us/amazondynamodb/latest/developerguide/LegacyConditionalParameters.Conditions.html if %w[BETWEEN IN].include?(operator) [value].flatten elsif %w[NULL NOT_NULL].include?(operator) nil else [value] end 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::AwsSdkV3.batch_delete_item('table1' => [['hk1', 'rk2'], ['hk1', 'rk2']]]))
See:
-
docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_BatchWriteItem.html
-
docs.aws.amazon.com/sdkforruby/api/Aws/DynamoDB/Client.html#batch_write_item-instance_method
TODO handle rejections because of internal processing failures
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 |
# File 'lib/dynamoid/adapter_plugin/aws_sdk_v3.rb', line 245 def batch_delete_item() requests = [] .each_pair do |table_name, ids| table = describe_table(table_name) ids.each_slice(BATCH_WRITE_ITEM_REQUESTS_LIMIT) do |sliced_ids| delete_requests = sliced_ids.map do |id| { delete_request: { key: key_stanza(table, *id) } } end requests << { table_name => delete_requests } end end requests.each do |items| client.batch_write_item( request_items: items, return_consumed_capacity: 'TOTAL', return_item_collection_metrics: 'SIZE' ) end rescue Aws::DynamoDB::Errors::ConditionalCheckFailedException => e raise Dynamoid::Errors::ConditionalCheckFailedException, e end |
#batch_get_item(table_names_with_ids, options = {}, &block) ⇒ Hash
Get many items at once from DynamoDB. More efficient than getting each item individually.
If optional block is passed ‘nil` will be returned and the block will be called for each read batch of items, meaning once per batch.
Block receives parameters:
-
hash with items like ‘{ table_name: [items]}`
-
and boolean flag is true if there are some unprocessed keys, otherwise false.
@todo: Provide support for passing options to underlying batch_get_item
224 225 226 227 228 229 |
# File 'lib/dynamoid/adapter_plugin/aws_sdk_v3.rb', line 224 def batch_get_item(table_names_with_ids, = {}, &block) tables_with_ids = table_names_with_ids.transform_keys do |name| describe_table(name) end BatchGetItem.new(client, tables_with_ids, ).call(&block) end |
#batch_write_item(table_name, objects, options = {}) {|true|false| ... } ⇒ Object
Puts multiple items in one table
If optional block is passed it will be called for each written batch of items, meaning once per batch. Block receives boolean flag which is true if there are some unprocessed items, otherwise false.
See:
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 |
# File 'lib/dynamoid/adapter_plugin/aws_sdk_v3.rb', line 164 def batch_write_item(table_name, objects, = {}) items = objects.map { |o| sanitize_item(o) } while items.present? batch = items.shift(BATCH_WRITE_ITEM_REQUESTS_LIMIT) requests = batch.map { |item| { put_request: { item: item } } } response = client.batch_write_item( { request_items: { table_name => requests }, return_consumed_capacity: 'TOTAL', return_item_collection_metrics: 'SIZE' }.merge!() ) yield(response.unprocessed_items.present?) if block_given? if response.unprocessed_items.present? items += response.unprocessed_items[table_name].map { |r| r.put_request.item } end end rescue Aws::DynamoDB::Errors::ConditionalCheckFailedException => e raise Dynamoid::Errors::ConditionalCheckFailedException, e end |
#client ⇒ Object
Return the client object.
137 138 139 |
# File 'lib/dynamoid/adapter_plugin/aws_sdk_v3.rb', line 137 def client @client end |
#connect! ⇒ Aws::DynamoDB::Client
Establish the connection to DynamoDB.
97 98 99 100 |
# File 'lib/dynamoid/adapter_plugin/aws_sdk_v3.rb', line 97 def connect! @client = Aws::DynamoDB::Client.new(connection_config) @table_cache = {} end |
#connection_config ⇒ Object
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/dynamoid/adapter_plugin/aws_sdk_v3.rb', line 102 def connection_config @connection_hash = {} (Dynamoid::Config.settings.compact.keys & CONNECTION_CONFIG_OPTIONS).each do |option| @connection_hash[option] = Dynamoid::Config.send(option) end # if credentials are passed, they already contain access key & secret key if Dynamoid::Config.credentials? @connection_hash[:credentials] = Dynamoid::Config.credentials else # otherwise, pass access key & secret key for credentials creation 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 end @connection_hash[:logger] = Dynamoid::Config.logger @connection_hash[:log_level] = :debug # https://github.com/aws/aws-sdk-ruby/blob/master/gems/aws-sdk-core/lib/aws-sdk-core/plugins/logging.rb # https://github.com/aws/aws-sdk-ruby/blob/master/gems/aws-sdk-core/lib/aws-sdk-core/log/formatter.rb if Dynamoid::Config.log_formatter @connection_hash[:log_formatter] = Dynamoid::Config.log_formatter end @connection_hash end |
#count(table_name) ⇒ Object
560 561 562 |
# File 'lib/dynamoid/adapter_plugin/aws_sdk_v3.rb', line 560 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.
281 282 283 284 285 286 287 288 |
# File 'lib/dynamoid/adapter_plugin/aws_sdk_v3.rb', line 281 def create_table(table_name, key = :id, = {}) Dynamoid.logger.info "Creating #{table_name} table. This could take a while." CreateTable.new(client, table_name, key, ).call true rescue Aws::DynamoDB::Errors::ResourceInUseException => e Dynamoid.logger.error "Table #{table_name} cannot be created as it already exists" false 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
320 321 322 |
# File 'lib/dynamoid/adapter_plugin/aws_sdk_v3.rb', line 320 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
333 334 335 336 337 338 339 340 341 342 343 344 345 |
# File 'lib/dynamoid/adapter_plugin/aws_sdk_v3.rb', line 333 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.
353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 |
# File 'lib/dynamoid/adapter_plugin/aws_sdk_v3.rb', line 353 def delete_table(table_name, = {}) resp = client.delete_table(table_name: table_name) if [:sync] status = PARSE_TABLE_STATUS.call(resp, :table_description) if status == TABLE_STATUSES[:deleting] UntilPastTableStatus.new(client, table_name, :deleting).call end end 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
369 370 371 |
# File 'lib/dynamoid/adapter_plugin/aws_sdk_v3.rb', line 369 def delete_table_synchronously(table_name, = {}) delete_table(table_name, .merge(sync: true)) end |
#execute(statement, parameters = [], options = {}) ⇒ [] | Array[Hash] | Enumerator::Lazy[Hash]
574 575 576 577 578 579 580 581 582 583 584 |
# File 'lib/dynamoid/adapter_plugin/aws_sdk_v3.rb', line 574 def execute(statement, parameters = [], = {}) items = ExecuteStatement.new(client, statement, parameters, ).call if items.is_a?(Array) items else items.lazy.flat_map { |array| array } end rescue Aws::DynamoDB::Errors::ConditionalCheckFailedException [] 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.
386 387 388 389 390 391 392 393 394 395 396 397 398 |
# File 'lib/dynamoid/adapter_plugin/aws_sdk_v3.rb', line 386 def get_item(table_name, key, = {}) = .dup ||= {} table = describe_table(table_name) range_key = .delete(:range_key) consistent_read = .delete(:consistent_read) item = client.get_item(table_name: table_name, key: key_stanza(table, key, range_key), consistent_read: consistent_read)[:item] item ? item_to_hash(item) : nil end |
#list_tables ⇒ Object
List all tables on DynamoDB.
434 435 436 437 438 439 440 441 442 443 444 |
# File 'lib/dynamoid/adapter_plugin/aws_sdk_v3.rb', line 434 def list_tables [].tap do |result| start_table_name = nil loop do result_page = client.list_tables exclusive_start_table_name: start_table_name start_table_name = result_page.last_evaluated_table_name result.concat result_page.table_names break unless start_table_name end end 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
454 455 456 457 458 459 460 461 462 463 464 465 466 467 |
# File 'lib/dynamoid/adapter_plugin/aws_sdk_v3.rb', line 454 def put_item(table_name, object, = {}) ||= {} item = sanitize_item(object) 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 |
#query(table_name, options = {}) ⇒ 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.
487 488 489 490 491 492 493 494 495 496 497 498 |
# File 'lib/dynamoid/adapter_plugin/aws_sdk_v3.rb', line 487 def query(table_name, = {}) Enumerator.new do |yielder| table = describe_table(table_name) Query.new(client, table, ).call.each do |page| yielder.yield( page.items.map { |item| item_to_hash(item) }, last_evaluated_key: page.last_evaluated_key ) end end end |
#query_count(table_name, options = {}) ⇒ Object
500 501 502 503 504 505 506 507 |
# File 'lib/dynamoid/adapter_plugin/aws_sdk_v3.rb', line 500 def query_count(table_name, = {}) table = describe_table(table_name) [:select] = 'COUNT' Query.new(client, table, ).call .map(&:count) .reduce(:+) end |
#scan(table_name, conditions = {}, options = {}) ⇒ 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
520 521 522 523 524 525 526 527 528 529 530 531 |
# File 'lib/dynamoid/adapter_plugin/aws_sdk_v3.rb', line 520 def scan(table_name, conditions = {}, = {}) Enumerator.new do |yielder| table = describe_table(table_name) Scan.new(client, table, conditions, ).call.each do |page| yielder.yield( page.items.map { |item| item_to_hash(item) }, last_evaluated_key: page.last_evaluated_key ) end end end |
#scan_count(table_name, conditions = {}, options = {}) ⇒ Object
533 534 535 536 537 538 539 540 |
# File 'lib/dynamoid/adapter_plugin/aws_sdk_v3.rb', line 533 def scan_count(table_name, conditions = {}, = {}) table = describe_table(table_name) [:select] = 'COUNT' Scan.new(client, table, conditions, ).call .map(&:count) .reduce(:+) end |
#truncate(table_name) ⇒ Object
Truncates all records in the given table
548 549 550 551 552 553 554 555 556 557 558 |
# File 'lib/dynamoid/adapter_plugin/aws_sdk_v3.rb', line 548 def truncate(table_name) table = describe_table(table_name) hk = table.hash_key rk = table.range_key ids = scan(table_name, {}, {}).flat_map { |i| i }.map do |attributes| rk ? [attributes[hk], attributes[rk.to_sym]] : attributes[hk] end batch_delete_item(table_name => ids) end |
#update_item(table_name, key, options = {}) ⇒ 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
409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 |
# File 'lib/dynamoid/adapter_plugin/aws_sdk_v3.rb', line 409 def update_item(table_name, key, = {}) = .dup range_key = .delete(:range_key) conditions = .delete(:conditions) table = describe_table(table_name) item_updater = ItemUpdater.new(table, key, range_key) yield(item_updater) raise "non-empty options: #{}" unless .empty? result = client.update_item(table_name: table_name, key: key_stanza(table, key, range_key), attribute_updates: item_updater.attribute_updates, expected: expected_stanza(conditions), return_values: 'ALL_NEW') item_to_hash(result[:attributes]) rescue Aws::DynamoDB::Errors::ConditionalCheckFailedException => e raise Dynamoid::Errors::ConditionalCheckFailedException, e end |
#update_time_to_live(table_name, attribute) ⇒ Object
290 291 292 293 294 295 296 297 298 299 300 |
# File 'lib/dynamoid/adapter_plugin/aws_sdk_v3.rb', line 290 def update_time_to_live(table_name, attribute) request = { table_name: table_name, time_to_live_specification: { attribute_name: attribute, enabled: true, } } client.update_time_to_live(request) end |