Module: OceanDynamo::Tables::ClassMethods

Defined in:
lib/ocean-dynamo/tables.rb

Overview


Class methods

Instance Method Summary collapse

Instance Method Details

#attribute_type(name) ⇒ Object



203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/ocean-dynamo/tables.rb', line 203

def attribute_type(name)
  vals = fields[name][:type]
  case vals
  when :string, :serialized, :reference
    return "S"
  when :integer, :float, :datetime
    return "N"
  when :boolean
    return "B"
  else
    raise "Unknown OceanDynamo type: #{name} - #{vals.inspect}"
  end
end

#create_tableObject



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
# File 'lib/ocean-dynamo/tables.rb', line 104

def create_table
  attrs = table_attribute_definitions  # This already includes secondary indices
  keys = table_key_schema
  options = {
    table_name: table_full_name,
    attribute_definitions: attrs,
    key_schema: keys,
    provisioned_throughput: {
      read_capacity_units: table_read_capacity_units,
      write_capacity_units: table_write_capacity_units
    }
  }
  lsi = local_secondary_indexes.collect { |n| local_secondary_index_declaration n }
  options[:local_secondary_indexes] = lsi unless lsi.blank?
  gsi = global_secondary_indexes.collect { |k, v| global_secondary_index_declaration k, v }
  options[:global_secondary_indexes] = gsi unless gsi.blank?
  dynamo_resource.create_table(options)
  loop do
    ts = fresh_table_status
    break if ts == "ACTIVE"
    sleep 1
  end
  setup_dynamo
  true
end

#delete_tableObject



218
219
220
221
222
# File 'lib/ocean-dynamo/tables.rb', line 218

def delete_table
  return false unless fresh_table_status == "ACTIVE"
  dynamo_table.delete
  true
end

#dynamo_schema(table_hash_key = :id, table_range_key = nil, table_name: compute_table_name, table_name_prefix: nil, table_name_suffix: nil, read_capacity_units: 10, write_capacity_units: 5, connect: :late, create: false, client: nil, **keywords, &block) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/ocean-dynamo/tables.rb', line 17

def dynamo_schema(table_hash_key=:id,
                  table_range_key=nil,
                  table_name: compute_table_name,
                  table_name_prefix: nil,
                  table_name_suffix: nil,
                  read_capacity_units: 10,
                  write_capacity_units: 5,
                  connect: :late,
                  create: false,
                  client: nil,
                  **keywords,
                  &block)
  self.dynamo_client = client
  self.dynamo_resource = nil
  self.dynamo_table = nil
  self.table_connected = false
  self.table_connect_policy = connect
  self.table_create_policy = create
  self.table_hash_key = table_hash_key
  self.table_range_key = table_range_key
  self.table_name = table_name
  self.table_name_prefix = table_name_prefix
  self.table_name_suffix = table_name_suffix
  self.table_read_capacity_units = read_capacity_units
  self.table_write_capacity_units = write_capacity_units
  # Connect if asked to
  establish_db_connection if connect == true
end

#establish_db_connectionObject



47
48
49
50
51
52
53
54
55
56
57
# File 'lib/ocean-dynamo/tables.rb', line 47

def establish_db_connection
  setup_dynamo
  if table_exists?(dynamo_table)
    wait_until_table_is_active
    self.table_connected = true
    update_table_if_required
  else
    raise(TableNotFound, table_full_name) unless table_create_policy
    create_table
  end
end

#fresh_table_statusObject



79
80
81
# File 'lib/ocean-dynamo/tables.rb', line 79

def fresh_table_status
  dynamo_client.describe_table(table_name: table_full_name).table.table_status
end

#global_secondary_index_declaration(index_name, data) ⇒ Object



189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/ocean-dynamo/tables.rb', line 189

def global_secondary_index_declaration(index_name, data)
  hash_key, range_key = data["keys"]
  key_schema = table_key_schema(hash_key: hash_key, range_key: range_key)
  { index_name: index_name,
    key_schema: key_schema,
    projection: { projection_type: data["projection_type"] },
    provisioned_throughput: {
      read_capacity_units: data["read_capacity_units"],
      write_capacity_units: data["write_capacity_units"]
    }
  }
end

#local_secondary_index_declaration(name) ⇒ Object



181
182
183
184
185
186
# File 'lib/ocean-dynamo/tables.rb', line 181

def local_secondary_index_declaration(name)
  { index_name: name,
    key_schema: table_key_schema(range_key: name),
    projection: { projection_type: "KEYS_ONLY" }
  }
end

#local_secondary_indexesObject



170
171
172
173
174
175
176
177
178
# File 'lib/ocean-dynamo/tables.rb', line 170

def local_secondary_indexes
  @local_secondary_indexes ||= begin
    result = []
    fields.each do |name, |
      (result << name) if ["local_secondary_index"]
    end
    result
  end
end

#setup_dynamoObject



60
61
62
63
64
# File 'lib/ocean-dynamo/tables.rb', line 60

def setup_dynamo
  self.dynamo_client ||= Aws::DynamoDB::Client.new
  self.dynamo_resource ||= Aws::DynamoDB::Resource.new(client: dynamo_client)
  self.dynamo_table = dynamo_resource.table(table_full_name)
end

#table_attribute_definitionsObject



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/ocean-dynamo/tables.rb', line 145

def table_attribute_definitions
  attrs = []
  attrs << { attribute_name: table_hash_key.to_s, attribute_type: attribute_type(table_hash_key) }
  attrs << { attribute_name: table_range_key.to_s, attribute_type: attribute_type(table_range_key) }  if table_range_key
  local_secondary_indexes.each do |name|
    attrs << { attribute_name: name, attribute_type: attribute_type(name) }
  end
  global_secondary_indexes.each do |index_name, data|
    data["keys"].each do |name|
      next if attrs.any? { |a| a[:attribute_name] == name }
      attrs << { attribute_name: name, attribute_type: attribute_type(name) }
    end
  end
  attrs
end

#table_exists?(table) ⇒ Boolean

Returns:

  • (Boolean)


67
68
69
70
71
72
73
74
75
76
# File 'lib/ocean-dynamo/tables.rb', line 67

def table_exists?(table)
  begin
    fresh_table_status
    return true if table.data_loaded?
    table.load
  rescue Aws::DynamoDB::Errors::ResourceNotFoundException
    return false
  end
  true
end

#table_key_schema(hash_key: table_hash_key, range_key: table_range_key) ⇒ Object



162
163
164
165
166
167
# File 'lib/ocean-dynamo/tables.rb', line 162

def table_key_schema(hash_key: table_hash_key, range_key: table_range_key)
  keys = []
  keys << { attribute_name: hash_key.to_s, key_type: "HASH" }
  keys << { attribute_name: range_key.to_s, key_type: "RANGE" } if range_key
  keys
end

#update_table_if_requiredObject



131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/ocean-dynamo/tables.rb', line 131

def update_table_if_required
  #puts "Updating table #{table_full_name}"
  # attrs = table_attribute_definitions
  # active_attrs = []
  # dynamo_table.attribute_definitions.each do |k|
  #   active_attrs << { attribute_name: k.attribute_name, attribute_type: k.attribute_type }
  # end
  # return false if active_attrs == attrs
  # options = { attribute_definitions: attrs }
  # dynamo_table.update(options)
  true
end

#wait_until_table_is_activeObject



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/ocean-dynamo/tables.rb', line 84

def wait_until_table_is_active
  loop do
    case st = fresh_table_status
    when "ACTIVE"
      update_table_if_required
      return
    when "UPDATING", "CREATING"
      sleep 1
      next
    when "DELETING"
      sleep 1 while table_exists?(dynamo_table) && fresh_table_status == "DELETING"
      create_table
      return
    else
      raise UnknownTableStatus.new("Unknown DynamoDB table status '#{st}'")
    end
  end
end