Sha256: a5ced315aab2159db64cd7b9c6562412163297ea16290f34dca9367a51918551

Contents?: true

Size: 1.77 KB

Versions: 5

Compression:

Stored size: 1.77 KB

Contents

require 'json'

module Sdbport
  class Domain
    class Import

      def initialize(args)
        @name       = args[:name]
        @logger     = args[:logger]
        @access_key = args[:access_key]
        @secret_key = args[:secret_key]
        @region     = args[:region]
        @buffer     = {}
      end

      def import(input)
        create_domain

        return false unless ensure_domain_empty

        @logger.info "Importing #{@name} in #{@region} from #{input}."

        file = File.open(input, 'r')
        while (line = file.gets)
          add_line_to_buffer line
          write_buffer if buffer_full? 
        end
        write_buffer unless buffer_empty?
        true
      end

      private

      def add_line_to_buffer(line)
        line.chomp!
        data       = JSON.parse line
        id         = data.first
        attributes = data.last

        @logger.debug "Adding #{id} with attributes #{attributes.to_s}."
        @buffer.merge!({ id => attributes })
      end

      def buffer_full?
        @buffer.count > 24
      end

      def buffer_empty?
        @buffer.count.zero?
      end

      def write_buffer
        @logger.debug "Writing #{@buffer.count} entries to SimpleDB."
        sdb.batch_put_attributes @name, @buffer
        @buffer.clear
      end

      def ensure_domain_empty
        if sdb.domain_empty? @name
          true
        else
          @logger.error "Domain #{@name} in #{@region} not empty."
          false
        end
      end

      def create_domain
        sdb.create_domain_unless_present @name
      end

      def sdb
        @sdb ||= AWS::SimpleDB.new :access_key => @access_key,
                                   :secret_key => @secret_key,
                                   :region     => @region
      end

    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
sdbport-0.4.1 lib/sdbport/domain/import.rb
sdbport-0.4.0 lib/sdbport/domain/import.rb
sdbport-0.3.0 lib/sdbport/domain/import.rb
sdbport-0.2.1 lib/sdbport/domain/import.rb
sdbport-0.2.0 lib/sdbport/domain/import.rb