Class: FakeDynamo::DB

Inherits:
Object
  • Object
show all
Includes:
Validation
Defined in:
lib/fake_dynamo/db.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Validation

#add_errors, #api_config, #api_config_path, #api_input_spec, #available_operations, #key_schema_mismatch, #param, #validate!, #validate_hash_condition, #validate_hash_key, #validate_index_names, #validate_input, #validate_key_data, #validate_key_schema, #validate_operation, #validate_payload, #validate_projection, #validate_range_condition, #validate_range_key, #validate_request_size, #validate_spec, #validate_type

Constructor Details

#initializeDB

Returns a new instance of DB.



14
15
16
# File 'lib/fake_dynamo/db.rb', line 14

def initialize
  @tables = {}
end

Instance Attribute Details

#tablesObject

Returns the value of attribute tables.



6
7
8
# File 'lib/fake_dynamo/db.rb', line 6

def tables
  @tables
end

Class Method Details

.delegate_to_table(*methods) ⇒ Object



80
81
82
83
84
85
86
# File 'lib/fake_dynamo/db.rb', line 80

def self.delegate_to_table(*methods)
  methods.each do |method|
    define_method(method) do |data|
      find_table(data['TableName']).send(method, data)
    end
  end
end

.instanceObject



9
10
11
# File 'lib/fake_dynamo/db.rb', line 9

def instance
  @db ||= DB.new
end

Instance Method Details

#batch_get_item(data) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/fake_dynamo/db.rb', line 91

def batch_get_item(data)
  response = {}
  consumed_capacity = {}
  unprocessed_keys = {}
  sack = Sack.new

  data['RequestItems'].each do |table_name, table_data|
    table = find_table(table_name)

    unless response[table_name]
      response[table_name] = []
      set_consumed_capacity(consumed_capacity, table, data)
    end

    table_data['Keys'].each do |key|
      if sack.has_space?
        if item_hash = table.get_raw_item(key, table_data['AttributesToGet'])
          response[table_name] << item_hash
          sack.add(item_hash)
        end
      else
        unless unprocessed_keys[table_name]
          unprocessed_keys[table_name] = {'Keys' => []}
          unprocessed_keys[table_name]['AttributesToGet'] = table_data['AttributesToGet'] if table_data['AttributesToGet']
        end

        unprocessed_keys[table_name]['Keys'] << key
      end
    end
  end

  response = { 'Responses' => response, 'UnprocessedKeys' => unprocessed_keys }
  merge_consumed_capacity(consumed_capacity, response)
end

#batch_write_item(data) ⇒ Object



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/fake_dynamo/db.rb', line 126

def batch_write_item(data)
  response = {}
  consumed_capacity = {}
  item_collection_metrics = {}
  merge_metrics = false
  items = {}
  request_count = 0

  # validation
  data['RequestItems'].each do |table_name, requests|
    table = find_table(table_name)

    items[table.name] ||= {}
    item_collection_metrics[table.name] ||= []

    requests.each do |request|
      if request['PutRequest']
        item = table.batch_put_request(request['PutRequest'])
        check_item_conflict(items, table.name, item.key)
        items[table.name][item.key] = item
      else
        key = table.batch_delete_request(request['DeleteRequest'])
        check_item_conflict(items, table.name, key)
        items[table.name][key] = :delete
      end

      request_count += 1
    end
  end

  check_max_request(request_count)

  # real modification
  items.each do |table_name, requests|
    table = find_table(table_name)
    item_collection_metrics[table.name] ||= []

    requests.each do |key, value|
      if value == :delete
        table.batch_delete(key)
      else
        table.batch_put(value)
      end

      unless (metrics = Item.from_key(key).collection_metrics(data)).empty?
        merge_metrics = true
        item_collection_metrics[table.name] << metrics['ItemCollectionMetrics']
      end

    end
    set_consumed_capacity(consumed_capacity, table, data)
  end

  response = { 'UnprocessedItems' => {} }
  response = merge_consumed_capacity(consumed_capacity, response)
  if merge_metrics
    response.merge!({'ItemCollectionMetrics' => item_collection_metrics})
  end
  response
end

#create_table(data) ⇒ Object



28
29
30
31
32
33
34
35
36
37
# File 'lib/fake_dynamo/db.rb', line 28

def create_table(data)
  table_name = data['TableName']
  raise ResourceInUseException, "Duplicate table name: #{table_name}" if tables[table_name]

  table = Table.new(data)
  tables[table_name] = table
  response = table.description
  table.activate
  response
end

#delete_table(data) ⇒ Object



44
45
46
47
48
49
# File 'lib/fake_dynamo/db.rb', line 44

def delete_table(data)
  table_name = data['TableName']
  table = find_table(table_name)
  tables.delete(table_name)
  table.delete
end

#describe_table(data) ⇒ Object



39
40
41
42
# File 'lib/fake_dynamo/db.rb', line 39

def describe_table(data)
  table = find_table(data['TableName'])
  table.describe_table
end

#list_tables(data) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/fake_dynamo/db.rb', line 51

def list_tables(data)
  start_table = data['ExclusiveStartTableName']
  limit = data['Limit']

  all_tables = tables.keys
  start = 0

  if start_table
    if i = all_tables.index(start_table)
      start = i + 1
    end
  end

  limit ||= all_tables.size
  result_tables = all_tables[start, limit]
  response = { 'TableNames' => result_tables }

  if (start + limit ) < all_tables.size
    last_table = all_tables[start + limit -1]
    response.merge!({ 'LastEvaluatedTableName' => last_table })
  end
  response
end

#process(operation, data) ⇒ Object



22
23
24
25
26
# File 'lib/fake_dynamo/db.rb', line 22

def process(operation, data)
  validate_payload(operation, data)
  operation = operation.underscore
  self.send operation, data
end

#resetObject



18
19
20
# File 'lib/fake_dynamo/db.rb', line 18

def reset
  @tables = {}
end

#update_table(data) ⇒ Object



75
76
77
78
# File 'lib/fake_dynamo/db.rb', line 75

def update_table(data)
  table = find_table(data['TableName'])
  table.update(data['ProvisionedThroughput']['ReadCapacityUnits'], data['ProvisionedThroughput']['WriteCapacityUnits'])
end