class Soar::Registry::Directory::Provider::DynamoDb
Constants
- LIMIT
Attributes
Public Class Methods
@param [Hash{String => String}] credentials @option credentials [String] username @option credentials [String] password @param [Hash{String => String}] table @option table [String] name @option table [Hash{String => String}] index
* partition_key (String) * sort_key (String) * global_secondary_indexes (Array<String>)
@param [Hash{String => String}] configuration @option configuration [String] region @option configuration [String] endpoint
# File lib/soar/registry/directory/provider/dynamo_db.rb, line 28 def initialize(credentials: , table: , configuration: ) @table_name = table['name'] index = table['index'] @partition_key = index['partition_key'] @sort_key = index['sort_key'] if index.key?('sort_key') @global_secondary_indexes = index['global_secondary_index'] if index.key?('global_secondary_index') configuration['credentials'] = Aws::Credentials.new(credentials['username'], credentials['password']) @client = Aws::DynamoDB::Client.new(Hashie.symbolize_keys(configuration)) end
Public Instance Methods
@param [Hash{String => String}] primary_key @option primary_key [String] partition_key required @option primary_key [String] sort_key required only when defined in schema @return [Hash{String => Hash, String, Number, Array }] a single matching entry @raise [Soar::Registry::Directory::Error::NoEntriesFoundError] if primary key not found
# File lib/soar/registry/directory/provider/dynamo_db.rb, line 67 def fetch(primary_key) adapt_exceptions do options = { table_name: @table_name, key: primary_key } identity = @client.get_item(options) raise Soar::Registry::Directory::Error::NoEntriesFoundError, "No entries found for primary_key = #{primary_key}" if identity.item.nil? identity.item end end
@return [Array<String>] a list of primary keys and global secondary indexes
# File lib/soar/registry/directory/provider/dynamo_db.rb, line 108 def index adapt_exceptions do resp = @client.describe_table({ table_name: @table_name }) indexed_attributes = [] resp['table']['key_schema'].each { |key_schema| indexed_attributes << key_schema['attribute_name'] } if resp['table'].key?('key_schema') resp['table']['global_secondary_indexes'].each { |index| index['key_schema'].each { |key_schema| indexed_attributes << key_schema['attribute_name'] } } if resp['table'].key?('global_secondary_indexes') indexed_attributes end end
@param [Hash{String => String, Hash, Number, Array}] entry @return [Boolean] @raise [Soar::Registry::Directory::Error::DuplicateEntryError]
# File lib/soar/registry/directory/provider/dynamo_db.rb, line 43 def put(entry) condition_expression = "attribute_not_exists(#{@partition_key})" condition_expression += " AND attribute_not_exists(#{@sort_key})" if @sort_key #@global_secondary_indexes.each { |global_secondary_index| # condition_expression += " AND attribute_not_exists(#{global_secondary_index})" #} if @global_secondary_indexes adapt_exceptions do @client.put_item({ table_name: @table_name, item: entry, condition_expression: condition_expression }) return true end end
Deletes existing table and creates a new one in its place @param [String] name the table name @param [Hash{String => Hash, Array, String, Number]] structure table structure @return [Boolean]
# File lib/soar/registry/directory/provider/dynamo_db.rb, line 134 def recreate_table(name:, structure:) adapt_exceptions do if @client.list_tables.table_names.include?(name) result = @client.delete_table({ table_name: name }) end @client.create_table(Hashie.symbolize_keys(structure)) return true end end
@param [String] key @param [String] value @return [Array<Hash{String => Hash, String, Number, Array}>] list of matching entries @raise [ArgumentError] if query or index is not specified
# File lib/soar/registry/directory/provider/dynamo_db.rb, line 85 def search(key, value) adapt_exceptions do options = { table_name: @table_name, select: 'ALL_ATTRIBUTES', limit: LIMIT, key_condition_expression: "#{key} = :value", expression_attribute_values: { ":value": value } } options.merge!({index_name: "#{key}-index"}) if @global_secondary_indexes and @global_secondary_indexes.include?(key) identity = @client.query(options) identity.items.map { |item| Hashie.stringify_keys(item) } end end
Private Instance Methods
# File lib/soar/registry/directory/provider/dynamo_db.rb, line 149 def adapt_exceptions begin yield rescue Seahorse::Client::NetworkingError => e raise Soar::Registry::Directory::Error::NetworkingError, e.message rescue Aws::DynamoDB::Errors::ConditionalCheckFailedException => e raise Soar::Registry::Directory::Error::DuplicateEntryError, e.message rescue StandardError => e raise e, e.message end end