Sha256: d3ca00d7e77b5984b5acd717490f31c3a7148ed9ffec3b156ac2fa4d25a504a2

Contents?: true

Size: 1.95 KB

Versions: 2

Compression:

Stored size: 1.95 KB

Contents

require "crimp"
require "alephant/lookup/lookup_location"
require "alephant/logger"

module Alephant
  module Lookup
    class LookupQuery
      include Logger
      attr_reader :table_name, :lookup_location

      def initialize(table_name, component_id, opts, batch_version)
        options = {}
        options[:endpoint] = ENV['AWS_DYNAMO_DB_ENDPOINT'] if ENV['AWS_DYNAMO_DB_ENDPOINT']
        @client            = Aws::DynamoDB::Client.new(options)
        @table_name        = table_name
        @lookup_location   = LookupLocation.new(component_id, opts, batch_version)

        logger.info(
          "event"        => "LookupQueryInitialized",
          "tableName"    => table_name,
          "componentId"  => component_id,
          "location"     => lookup_location,
          "batchVersion" => batch_version,
          "method"       => "#{self.class}#initialize"
        )
      end

      def run!
        lookup_location.tap do |l|
          l.location = s3_location_from(
            @client.query(to_q)
          ).tap do |loc|
            logger.info(
              "event"    => "S3LocationRetrieved",
              "location" => loc,
              "method"   => "#{self.class}#run!"
            )
          end
        end
      end

      private

      def s3_location_from(result)
        result.count == 1 ? result.items.first['location'] : nil
      end

      def to_q
        {
          :table_name => table_name,
          :consistent_read => true,
          :projection_expression => '#loc',
          :expression_attribute_names => {
            '#loc' => 'location'
          },
          :key_condition_expression => 'component_key = :component_key AND batch_version = :batch_version',
          :expression_attribute_values => {
            ':component_key' => @lookup_location.component_key,
            ':batch_version' => @lookup_location.batch_version.to_i # @TODO: Verify if this is nil as this would be 0
          }
        }
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
alephant-lookup-2.2.0 lib/alephant/lookup/lookup_query.rb
alephant-lookup-2.1.0 lib/alephant/lookup/lookup_query.rb