class Moe::Dyna

Attributes

dynamodb[RW]

Public Class Methods

new() click to toggle source
# File lib/moe/dyna.rb, line 6
def initialize
  @dynamodb = Aws.dynamodb
end

Public Instance Methods

batch_write_item(write_tables, items) click to toggle source
# File lib/moe/dyna.rb, line 10
def batch_write_item(write_tables, items)
  explosion = items.map { |item| explode item }

  write_tables.each do |table_name|
    dynamodb.batch_write_item request_items: {
      table_name => batchify(explosion)
    }
  end
end
create_table(name, copies=1, hash_key="hash", range_key=nil, read_capacity=5, write_capacity=10) click to toggle source
# File lib/moe/dyna.rb, line 20
def create_table(name, copies=1, hash_key="hash", range_key=nil, read_capacity=5, write_capacity=10)
  [].tap do |tables|
    1.upto(copies).each do |copy|
      schema = table_schema "#{name}_#{copy}",
                            hash_key,
                            range_key,
                            read_capacity,
                            write_capacity

      table  = dynamodb.create_table schema

      tables << "#{name}_#{copy}"
    end
  end
end
explode(item) click to toggle source
# File lib/moe/dyna.rb, line 36
def explode(item)
  clone = item.clone

  clone.each do |key, value|
    clone[key] = { s: value }
  end
end
find(name) click to toggle source
# File lib/moe/dyna.rb, line 63
def find(name)
  if dynamodb.list_tables.table_names.include? name
    dynamodb.describe_table table_name: name
  else
    false
  end
end
get_item(read_tables, key) click to toggle source
# File lib/moe/dyna.rb, line 44
def get_item(read_tables, key)
  item = nil
  read_tables.each do |table_name|
    item = dynamodb.get_item(table_name: table_name, key: key).item
    break if item
  end
  item
end
implode(item) click to toggle source
# File lib/moe/dyna.rb, line 53
def implode(item)
  item.each_key { |key| item[key] = item[key][:s] }
end
put_item(write_tables, item) click to toggle source
# File lib/moe/dyna.rb, line 57
def put_item(write_tables, item)
  write_tables.each do |table_name|
    dynamodb.put_item table_name: table_name, item: explode(item)
  end
end

Private Instance Methods

batchify(items) click to toggle source
# File lib/moe/dyna.rb, line 73
def batchify(items)
  items.map do |item|
    { put_request:
      { item: item }
    }
  end
end
table_schema(name, hash_key, range_key, read_capacity, write_capacity) click to toggle source
# File lib/moe/dyna.rb, line 81
def table_schema(name, hash_key, range_key, read_capacity, write_capacity)
  { table_name: name,
    key_schema: [
      attribute_name: hash_key,
      key_type: "HASH"
    ],
    attribute_definitions: [
      {
        attribute_name: hash_key,
        attribute_type: "S"
      }
    ],
    provisioned_throughput: {
      read_capacity_units: read_capacity,
      write_capacity_units: write_capacity
    }
  }.tap do |table|
    if range_key
      table[:key_schema]            << { attribute_name: range_key,
                                         key_type: "RANGE" }
      table[:attribute_definitions] << { attribute_name: range_key,
                                         attribute_type: "S" }
    end
  end
end