Class: DynamoDbTableManager

Inherits:
Object
  • Object
show all
Defined in:
lib/dynamodb_framework/dynamodb_table_manager.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeDynamoDbTableManager

Returns a new instance of DynamoDbTableManager.



5
6
7
# File 'lib/dynamodb_framework/dynamodb_table_manager.rb', line 5

def initialize
  @dynamodb = DynamoDbStore.new
end

Instance Attribute Details

#dynamodbObject (readonly)

Returns the value of attribute dynamodb.



3
4
5
# File 'lib/dynamodb_framework/dynamodb_table_manager.rb', line 3

def dynamodb
  @dynamodb
end

Instance Method Details

#add_index(table_name, attributes, global_index) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/dynamodb_framework/dynamodb_table_manager.rb', line 68

def add_index(table_name, attributes, global_index)

  attribute_definitions = []

  attributes.each do |a|
    attribute_definitions.push({ :attribute_name => a[:name], :attribute_type => a[:type] })
  end

  table = {
      :table_name => table_name,
      :attribute_definitions => attribute_definitions,
      :global_secondary_index_updates => [
          :create => global_index
      ]
  }

  dynamodb.client.update_table(table)

  # wait for table to be updated
  puts "Adding global index: #{global_index[:index_name]}..."
  wait_until_index_active(table_name, global_index[:index_name])
  puts "Index added!"
end

#create(table_name, attributes, partition_key, range_key = nil, read_capacity = 20, write_capacity = 10, global_indexes = nil) ⇒ Object



208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
# File 'lib/dynamodb_framework/dynamodb_table_manager.rb', line 208

def create(table_name, attributes, partition_key, range_key = nil, read_capacity = 20, write_capacity = 10, global_indexes = nil)

  if exists?(table_name)
    return
  end

  attribute_definitions = []

  attributes.each do |a|
    attribute_definitions.push({ :attribute_name => a[:name], :attribute_type => a[:type] })
  end

  key_schema = []
  key_schema.push({ :attribute_name => partition_key, :key_type => :HASH })
  if range_key != nil
    key_schema.push({ :attribute_name => range_key, :key_type => :RANGE })
  end

  table = {
      :table_name => table_name,
      :attribute_definitions => attribute_definitions,
      :key_schema => key_schema,
      :provisioned_throughput => {
          :read_capacity_units => read_capacity,
          :write_capacity_units => write_capacity
      }
  }

  if global_indexes != nil
    table[:global_secondary_indexes] = global_indexes
  end

  dynamodb.client.create_table(table)

  # wait for table to be created
  puts "waiting for table: [#{table_name}] to be created..."
  dynamodb.client.wait_until(:table_exists, table_name: table_name)
  puts "table: [#{table_name}] created!"
end

#create_global_index(name, partition_key, range_key = nil, read_capacity = 20, write_capacity = 10) ⇒ Object



248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
# File 'lib/dynamodb_framework/dynamodb_table_manager.rb', line 248

def create_global_index(name, partition_key, range_key = nil, read_capacity = 20, write_capacity = 10)

  key_schema = []

  key_schema.push({ :attribute_name => partition_key, :key_type => :HASH })
  if range_key != nil
    key_schema.push({ :attribute_name => range_key, :key_type => :RANGE })
  end

  index = {
      :index_name => name,
      :key_schema => key_schema,
      :projection => {
          :projection_type => :ALL
      },
      :provisioned_throughput => {
          :read_capacity_units => read_capacity,
          :write_capacity_units => write_capacity,
      }
  }

  return index
end

#drop(table_name) ⇒ Object



272
273
274
275
276
277
278
279
280
281
# File 'lib/dynamodb_framework/dynamodb_table_manager.rb', line 272

def drop(table_name)

  if !exists?(table_name)
    return
  end

  puts "dropping table: [#{table_name}] ..."
  dynamodb.client.delete_table({ table_name: table_name })
  puts "table: [#{table_name}] dropped!"
end

#drop_index(table_name, index_name) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/dynamodb_framework/dynamodb_table_manager.rb', line 115

def drop_index(table_name, index_name)
  table = {
      :table_name => table_name,
      :global_secondary_index_updates => [
          :delete => {
              :index_name => index_name
          }
      ]
  }

  dynamodb.client.update_table(table)

  # wait for table to be updated
  puts "Deleting global index: #{index_name}..."
  wait_until_index_dropped(table_name, index_name)
  puts "Index: [#{index_name}] dropped!"
end

#exists?(table_name) ⇒ Boolean

Returns:

  • (Boolean)


9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/dynamodb_framework/dynamodb_table_manager.rb', line 9

def exists?(table_name)

  exists = true

  begin
    dynamodb.client.describe_table(:table_name => table_name)
  rescue Aws::DynamoDB::Errors::ResourceNotFoundException
    exists = false
  end

  return exists

end

#get_index_status(table_name, index_name) ⇒ Object



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/dynamodb_framework/dynamodb_table_manager.rb', line 138

def get_index_status(table_name, index_name)
  result = dynamodb.client.describe_table(:table_name => table_name)

  if result.table[:global_secondary_indexes] == nil
    return nil
  end

  index = result.table[:global_secondary_indexes].select { |i| i[:index_name] == index_name }

  if index.length > 0
    return index[0][:index_status]
  end

  return nil
end

#get_status(table_name) ⇒ Object



133
134
135
136
# File 'lib/dynamodb_framework/dynamodb_table_manager.rb', line 133

def get_status(table_name)
  result = dynamodb.client.describe_table(:table_name => table_name)
  return result.table[:table_status]
end

#has_index?(table_name, index_name) ⇒ Boolean

Returns:

  • (Boolean)


23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/dynamodb_framework/dynamodb_table_manager.rb', line 23

def has_index?(table_name, index_name)
  exists = true

  begin
    result = dynamodb.client.describe_table(:table_name => table_name)

    if result.table[:global_secondary_indexes] == nil
      return false
    end

    if result.table[:global_secondary_indexes].select { |i| i[:index_name] == index_name }.length > 0
      exists = true
    else
      exists = false
    end
  rescue Aws::DynamoDB::Errors::ResourceNotFoundException
    exists = false
  end

  return exists
end

#update_index_throughput(table_name, index_name, read_capacity, write_capacity) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/dynamodb_framework/dynamodb_table_manager.rb', line 92

def update_index_throughput(table_name, index_name, read_capacity, write_capacity)
  table = {
      :table_name => table_name,
      :global_secondary_index_updates => [
          :update => {
              :index_name => index_name,
              :provisioned_throughput => {
                  :read_capacity_units => read_capacity,
                  :write_capacity_units => write_capacity
              }
          }
      ]
  }

  dynamodb.client.update_table(table)

  # wait for table to be updated
  puts "Updating throughput for global index: #{index_name}..."
  puts "waiting for table: [#{table_name}] to be updated..."
  wait_until_active(table_name)
  puts "table: [#{table_name}] updated!"
end

#update_throughput(table_name, read_capacity, write_capacity) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/dynamodb_framework/dynamodb_table_manager.rb', line 45

def update_throughput(table_name, read_capacity, write_capacity)

  if !exists?(table_name)
    raise "table: #{table_name}, does not exist."
  end

  table = {
      :table_name => table_name,
      :provisioned_throughput => {
          :read_capacity_units => read_capacity,
          :write_capacity_units => write_capacity
      }
  }

  dynamodb.client.update_table(table)

  # wait for table to be updated
  puts "waiting for table: [#{table_name}] to be updated..."
  wait_until_active(table_name)
  puts "table: [#{table_name}] updated!"

end

#wait_until_active(table_name) ⇒ Object



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/dynamodb_framework/dynamodb_table_manager.rb', line 154

def wait_until_active(table_name)

  end_time = Time.now + 5.minutes
  while Time.now < end_time do

    status = get_status(table_name)

    if status == 'ACTIVE'
      return
    end

    sleep(5.seconds)
  end

  raise "Timeout occured while waiting for table: #{table_name}, to become active."

end

#wait_until_index_active(table_name, index_name) ⇒ Object



172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/dynamodb_framework/dynamodb_table_manager.rb', line 172

def wait_until_index_active(table_name, index_name)

  end_time = Time.now + 5.minutes
  while Time.now < end_time do

    status = get_index_status(table_name, index_name)

    if status == 'ACTIVE'
      return
    end

    sleep(5.seconds)
  end

  raise "Timeout occured while waiting for table: #{table_name}, index: #{index_name}, to become active."

end

#wait_until_index_dropped(table_name, index_name) ⇒ Object



190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'lib/dynamodb_framework/dynamodb_table_manager.rb', line 190

def wait_until_index_dropped(table_name, index_name)

  end_time = Time.now + 5.minutes
  while Time.now < end_time do

    status = get_index_status(table_name, index_name)

    if status == nil
      return
    end

    sleep(5.seconds)
  end

  raise "Timeout occured while waiting for table: #{table_name}, index: #{index_name}, to be dropped."

end