Class: FakeDynamo::Table

Inherits:
Object
  • Object
show all
Includes:
Filter, Validation
Defined in:
lib/fake_dynamo/table.rb

Constant Summary

Constants included from Filter

Filter::INF

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Filter

#comparable_types?, #comparison_filter, #contains_filter, def_filter, #in_filter, #not_contains_filter, #not_null_filter, #null_filter, #validate_size, #validate_supported_types

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

#initialize(data) ⇒ Table

Returns a new instance of Table.



10
11
12
13
# File 'lib/fake_dynamo/table.rb', line 10

def initialize(data)
  extract_values(data)
  init
end

Instance Attribute Details

#attribute_definitionsObject

Returns the value of attribute attribute_definitions.



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

def attribute_definitions
  @attribute_definitions
end

#creation_date_timeObject

Returns the value of attribute creation_date_time.



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

def creation_date_time
  @creation_date_time
end

#itemsObject

Returns the value of attribute items.



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

def items
  @items
end

#key_schemaObject

Returns the value of attribute key_schema.



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

def key_schema
  @key_schema
end

#last_decreased_timeObject

Returns the value of attribute last_decreased_time.



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

def last_decreased_time
  @last_decreased_time
end

#last_increased_timeObject

Returns the value of attribute last_increased_time.



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

def last_increased_time
  @last_increased_time
end

#local_secondary_indexesObject

Returns the value of attribute local_secondary_indexes.



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

def local_secondary_indexes
  @local_secondary_indexes
end

#nameObject

Returns the value of attribute name.



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

def name
  @name
end

#read_capacity_unitsObject

Returns the value of attribute read_capacity_units.



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

def read_capacity_units
  @read_capacity_units
end

#size_bytesObject

Returns the value of attribute size_bytes.



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

def size_bytes
  @size_bytes
end

#statusObject

Returns the value of attribute status.



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

def status
  @status
end

#write_capacity_unitsObject

Returns the value of attribute write_capacity_units.



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

def write_capacity_units
  @write_capacity_units
end

Instance Method Details

#activateObject



79
80
81
# File 'lib/fake_dynamo/table.rb', line 79

def activate
  @status = 'ACTIVE'
end

#attributes_to_get(data, index) ⇒ Object



301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
# File 'lib/fake_dynamo/table.rb', line 301

def attributes_to_get(data, index)
  if data['Select'] != 'COUNT'
    if index
      attributes_to_get = projected_attributes(index)
    else
      attributes_to_get = nil # select everything
    end


    if data['AttributesToGet']
      attributes_to_get = data['AttributesToGet']
    elsif data['Select'] == 'ALL_PROJECTED_ATTRIBUTES'
      attributes_to_get = projected_attributes(index)
    elsif data['Select'] == 'ALL_ATTRIBUTES'
      attributes_to_get = nil
    end
  else
    false
  end
end

#batch_delete(key) ⇒ Object



170
171
172
# File 'lib/fake_dynamo/table.rb', line 170

def batch_delete(key)
  @items.delete(key)
end

#batch_delete_request(data) ⇒ Object



166
167
168
# File 'lib/fake_dynamo/table.rb', line 166

def batch_delete_request(data)
  Key.from_data(data['Key'], key_schema)
end

#batch_put(item) ⇒ Object



121
122
123
# File 'lib/fake_dynamo/table.rb', line 121

def batch_put(item)
  @items[item.key] = item
end

#batch_put_request(data) ⇒ Object



117
118
119
# File 'lib/fake_dynamo/table.rb', line 117

def batch_put_request(data)
  Item.from_data(data['Item'], key_schema, attribute_definitions)
end

#check_conditions(old_item, conditions) ⇒ Object



523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
# File 'lib/fake_dynamo/table.rb', line 523

def check_conditions(old_item, conditions)
  return unless conditions

  conditions.each do |name, predicate|
    exist = predicate['Exists']
    value = predicate['Value']

    if not value
      if exist.nil?
        raise ValidationException, "'Exists' is set to null. 'Exists' must be set to false when no Attribute value is specified"
      elsif exist
        raise ValidationException, "'Exists' is set to true. 'Exists' must be set to false when no Attribute value is specified"
      elsif !exist # false
        if old_item and old_item[name]
          raise ConditionalCheckFailedException
        end
      end
    else
      expected_attr = Attribute.from_hash(name, value)

      if exist.nil? or exist
        raise ConditionalCheckFailedException unless (old_item and old_item[name] == expected_attr)
      elsif !exist # false
        raise ValidationException, "Cannot expect an attribute to have a specified value while expecting it to not exist"
      end
    end
  end
end

#consumed_capacity(data) ⇒ Object



515
516
517
518
519
520
521
# File 'lib/fake_dynamo/table.rb', line 515

def consumed_capacity(data)
  if data['ReturnConsumedCapacity'] == 'TOTAL'
    {'ConsumedCapacity' => { 'CapacityUnits' => 1, 'TableName' => @name }}
  else
    {}
  end
end

#create_item?(data) ⇒ Boolean

Returns:

  • (Boolean)


465
466
467
468
469
470
471
472
473
474
# File 'lib/fake_dynamo/table.rb', line 465

def create_item?(data)
  if attribute_updates = data['AttributeUpdates']
    attribute_updates.any? do |name, update_data|
      action = update_data['Action']
      ['PUT', 'ADD', nil].include? action
    end
  else
    true
  end
end

#create_table_dataObject



56
57
58
59
60
61
62
63
64
65
66
# File 'lib/fake_dynamo/table.rb', line 56

def create_table_data
  {
    'TableName' => name,
    'AttributeDefinitions' => attribute_definitions.map(&:description),
    'KeySchema' => key_schema.description,
    'ProvisionedThroughput' => {
      'ReadCapacityUnits' => read_capacity_units,
      'WriteCapacityUnits' => write_capacity_units
    }
  }
end

#deep_copy(x) ⇒ Object



211
212
213
# File 'lib/fake_dynamo/table.rb', line 211

def deep_copy(x)
  Marshal.load(Marshal.dump(x))
end

#deleteObject



83
84
85
86
# File 'lib/fake_dynamo/table.rb', line 83

def delete
  @status = 'DELETING'
  description
end

#delete_item(data) ⇒ Object



152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/fake_dynamo/table.rb', line 152

def delete_item(data)
  key = Key.from_data(data['Key'], key_schema)
  item = @items[key]
  check_conditions(item, data['Expected'])

  @items.delete(key) if item
  if !item
    item = Item.from_key(key)
    consumed_capacity(data).merge(item.collection_metrics(data))
  else
    return_values(data, item).merge(consumed_capacity(data)).merge(item.collection_metrics(data))
  end
end

#describe_tableObject



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

def describe_table
  { 'Table' => description['TableDescription'] }
end

#descriptionObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/fake_dynamo/table.rb', line 15

def description
  {
    'TableDescription' => {
      'AttributeDefinitions' => attribute_definitions.map(&:description),
      'CreationDateTime' => creation_date_time,
      'KeySchema' => key_schema.description,
      'ProvisionedThroughput' => throughput_description,
      'TableName' => name,
      'TableStatus' => status,
      'ItemCount' => items.count,
      'TableSizeBytes' => size_bytes
    }.merge(local_secondary_indexes_description)
  }
end

#drop_till_start(all_items, start_key_hash, forward, schema) ⇒ Object



381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
# File 'lib/fake_dynamo/table.rb', line 381

def drop_till_start(all_items, start_key_hash, forward, schema)
  all_items = all_items.sort_by { |item| item.key }

  unless forward
    all_items = all_items.reverse
  end

  if start_key_hash
    start_key = Key.from_data(start_key_hash, schema)
    all_items.drop_while do |item|
      if forward
        item.key <= start_key
      else
        item.key >= start_key
      end
    end
  else
    all_items
  end
end

#drop_till_start_index(all_items, start_key_hash, forward, schema) ⇒ Object



402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
# File 'lib/fake_dynamo/table.rb', line 402

def drop_till_start_index(all_items, start_key_hash, forward, schema)
  all_items = all_items.sort_by { |item| Key.from_index_item(item, schema) }

  unless forward
    all_items = all_items.reverse
  end

  if start_key_hash
    start_key = Key.from_index_schema(start_key_hash, schema, key_schema)
    all_items.drop_while do |item|
      if forward
        Key.from_index_item(item, schema) <= start_key
      else
        Key.from_index_item(item, schema) >= start_key
      end
    end
  else
    all_items
  end
end

#filter(items, conditions, limit, fail_on_type_mismatch, sack_attributes = nil) ⇒ Object



423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
# File 'lib/fake_dynamo/table.rb', line 423

def filter(items, conditions, limit, fail_on_type_mismatch, sack_attributes = nil)
  limit ||= -1
  result = []
  sack = Sack.new
  last_evaluated_item = nil
  scaned_count = 0
  items.each do |item|
    select = true
    conditions.each do |attribute_name, condition|
      value = condition['AttributeValueList']
      comparison_op = condition['ComparisonOperator']
      unless self.send("#{comparison_op.downcase}_filter", value, item[attribute_name], fail_on_type_mismatch)
        select = false
        break
      end
    end

    scaned_count += 1

    if select
      result << item
      if sack_attributes
        sack.add(filter_attributes(item, sack_attributes))
      else
        sack.add(item)
      end

      if (limit -= 1) == 0 || (!sack.has_space?)
        last_evaluated_item = item
        break
      end
    end
  end
  [result, last_evaluated_item, scaned_count]
end

#filter_attributes(item, attributes_to_get) ⇒ Object



142
143
144
145
146
147
148
149
150
# File 'lib/fake_dynamo/table.rb', line 142

def filter_attributes(item, attributes_to_get)
  hash = item.as_hash
  if attributes_to_get
    hash.select! do |attribute, value|
      attributes_to_get.include? attribute
    end
  end
  hash
end

#get_item(data) ⇒ Object



125
126
127
128
129
130
131
# File 'lib/fake_dynamo/table.rb', line 125

def get_item(data)
  response = consumed_capacity(data)
  if item_hash = get_raw_item(data['Key'], data['AttributesToGet'])
    response.merge!('Item' => item_hash)
  end
  response
end

#get_items_by_hash_key(hash_key) ⇒ Object



459
460
461
462
463
# File 'lib/fake_dynamo/table.rb', line 459

def get_items_by_hash_key(hash_key)
  items.values.select do |i|
    i.key.primary == hash_key
  end
end

#get_raw_item(key_data, attributes_to_get) ⇒ Object



133
134
135
136
137
138
139
140
# File 'lib/fake_dynamo/table.rb', line 133

def get_raw_item(key_data, attributes_to_get)
  key = Key.from_data(key_data, key_schema)
  item = @items[key]

  if item
    filter_attributes(item, attributes_to_get)
  end
end

#local_secondary_indexes_descriptionObject



48
49
50
51
52
53
54
# File 'lib/fake_dynamo/table.rb', line 48

def local_secondary_indexes_description
  if local_secondary_indexes
    { 'LocalSecondaryIndexes' => local_secondary_indexes.map(&:description) }
  else
    {}
  end
end

#merge_items(response, data, results, index = nil) ⇒ Object



294
295
296
297
298
299
# File 'lib/fake_dynamo/table.rb', line 294

def merge_items(response, data, results, index = nil)
  if (attrs = attributes_to_get(data, index)) != false
    response['Items'] = results.map { |r| filter_attributes(r, attrs) }
  end
  response
end

#projected_attributes(index) ⇒ Object



336
337
338
339
340
341
342
343
344
345
346
347
348
349
# File 'lib/fake_dynamo/table.rb', line 336

def projected_attributes(index)
  if !index
    raise ValidationException, "ALL_PROJECTED_ATTRIBUTES can be used only when Querying using an IndexName"
  else
    case index.projection.type
    when 'ALL'
      nil
    when 'KEYS_ONLY'
      (key_schema.keys + index.key_schema.keys).uniq
    when 'INCLUDE'
      (key_schema.keys + index.key_schema.keys + index.projection.non_key_attributes).uniq
    end
  end
end

#put_item(data) ⇒ Object



108
109
110
111
112
113
114
115
# File 'lib/fake_dynamo/table.rb', line 108

def put_item(data)
  item = Item.from_data(data['Item'], key_schema, attribute_definitions)
  old_item = @items[item.key]
  check_conditions(old_item, data['Expected'])
  @items[item.key] = item

  return_values(data, old_item).merge(item.collection_metrics(data))
end

#put_item_data(item) ⇒ Object



68
69
70
71
72
73
# File 'lib/fake_dynamo/table.rb', line 68

def put_item_data(item)
  {
    'TableName' => name,
    'Item' => item.as_hash
  }
end

#query(data) ⇒ Object



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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
# File 'lib/fake_dynamo/table.rb', line 215

def query(data)
  range_key_present
  select_and_attributes_to_get_present?(data)
  validate_limit(data)

  index = nil
  if index_name = data['IndexName']
    index = local_secondary_indexes.find { |i| i.name == index_name }
    raise ValidationException, "The provided starting key is invalid" unless index
    schema = index.key_schema
  else
    schema = key_schema
  end

  hash_condition = data['KeyConditions'][schema.hash_key.name]
  validate_hash_condition(hash_condition)

  hash_attribute = Attribute.from_hash(schema.hash_key.name, hash_condition['AttributeValueList'].first)
  matched_items = get_items_by_hash_key(hash_attribute)

  forward = data.has_key?('ScanIndexForward') ? data['ScanIndexForward'] : true
  if index
    matched_items = drop_till_start_index(matched_items, data['ExclusiveStartKey'], forward, schema)
  else
    matched_items = drop_till_start(matched_items, data['ExclusiveStartKey'], forward, schema)
  end

  if !(range_condition = data['KeyConditions'].clone.tap { |h| h.delete(schema.hash_key.name) }).empty?
    validate_range_condition(range_condition, schema)
    conditions = range_condition
  else
    conditions = {}
  end

  results, last_evaluated_item, _ = filter(matched_items, conditions, data['Limit'], true, sack_attributes(data, index))

  response = {'Count' => results.size}.merge(consumed_capacity(data))
  merge_items(response, data, results, index)

  if last_evaluated_item
    if index
      response['LastEvaluatedKey'] = Key.from_index_item(last_evaluated_item, schema).as_hash
    else
      response['LastEvaluatedKey'] = last_evaluated_item.key.as_hash
    end
  end
  response
end

#return_values(data, old_item, new_item = {}) ⇒ Object



480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
# File 'lib/fake_dynamo/table.rb', line 480

def return_values(data, old_item, new_item={})
  old_item ||= {}
  old_hash = old_item.kind_of?(Item) ? old_item.as_hash : old_item

  new_item ||= {}
  new_hash = new_item.kind_of?(Item) ? new_item.as_hash : new_item


  return_value = data['ReturnValues']
  result = case return_value
           when 'ALL_OLD'
             old_hash
           when 'ALL_NEW'
             new_hash
           when 'UPDATED_OLD'
             updated = updated_attributes(data)
             old_hash.select { |name, _| updated.include? name }
           when 'UPDATED_NEW'
             updated = updated_attributes(data)
             new_hash.select { |name, _| updated.include? name }
           when 'NONE', nil
             {}
           else
             raise 'unknown return value'
           end

  result = unless result.empty?
             { 'Attributes' => result }
           else
             {}
           end

  result.merge(consumed_capacity(data))
end

#sack_attributes(data, index) ⇒ Object



322
323
324
325
326
327
328
329
330
331
332
333
334
# File 'lib/fake_dynamo/table.rb', line 322

def sack_attributes(data, index)
  return if !index || index.projection.type == 'ALL'

  if data['Select'] == 'COUNT'
    return projected_attributes(index)
  end

  if attrs = attributes_to_get(data, index)
    if (attrs - (projected_attributes(index))).empty?
      return projected_attributes(index)
    end
  end
end

#scan(data) ⇒ Object



264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
# File 'lib/fake_dynamo/table.rb', line 264

def scan(data)
  select_and_attributes_to_get_present?(data)
  total_segments_and_segment_present?(data)
  validate_limit(data)

  conditions = data['ScanFilter'] || {}


  if (segment = data['Segment']) && (total_segments = data['TotalSegments'])
    chunk_size = (items.values.size / total_segments.to_f).ceil
    current_segment = items.values.slice(segment * chunk_size, chunk_size) || []
  else
    current_segment = items.values
  end

  all_items = drop_till_start(current_segment, data['ExclusiveStartKey'], true, key_schema)
  results, last_evaluated_item, scaned_count = filter(all_items, conditions, data['Limit'], false)
  response = {
    'Count' => results.size,
    'ScannedCount' => scaned_count}.merge(consumed_capacity(data))

  merge_items(response, data, results)

  if last_evaluated_item
    response['LastEvaluatedKey'] = last_evaluated_item.key.as_hash
  end

  response
end

#select_and_attributes_to_get_present?(data) ⇒ Boolean

Returns:

  • (Boolean)


351
352
353
354
355
356
# File 'lib/fake_dynamo/table.rb', line 351

def select_and_attributes_to_get_present?(data)
  select = data['Select']
  if select and data['AttributesToGet'] and (select != 'SPECIFIC_ATTRIBUTES')
    raise ValidationException, "Cannot specify the AttributesToGet when choosing to get only the #{select}"
  end
end

#throughput_descriptionObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/fake_dynamo/table.rb', line 30

def throughput_description
  result = {
    'NumberOfDecreasesToday' => 0,
    'ReadCapacityUnits' => read_capacity_units,
    'WriteCapacityUnits' => write_capacity_units
  }

  if last_increased_time
    result['LastIncreaseDateTime'] = @last_increased_time
  end

  if last_decreased_time
    result['LastDecreaseDateTime'] = @last_decreased_time
  end

  result
end

#total_segments_and_segment_present?(data) ⇒ Boolean

Returns:

  • (Boolean)


358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
# File 'lib/fake_dynamo/table.rb', line 358

def total_segments_and_segment_present?(data)
  segment, total_segments = data['Segment'], data['TotalSegments']

  if (total_segments && !segment)
    raise ValidationException, "The Segment parameter is required but was not present in the request when parameter TotalSegments is present"
  end

  if (segment && !total_segments)
    raise ValidationException, "The TotalSegments parameter is required but was not present in the request when Segment parameter is present"
  end

  if (segment && total_segments) &&
      (segment >= total_segments)
    raise ValidationException, "The Segment parameter is zero-based and must be less than parameter TotalSegments: Segment: #{segment} is not less than TotalSegments: #{total_segments}"
  end
end

#update(read_capacity_units, write_capacity_units) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/fake_dynamo/table.rb', line 88

def update(read_capacity_units, write_capacity_units)
  if @read_capacity_units > read_capacity_units
    @last_decreased_time = Time.now.to_i
  elsif @read_capacity_units < read_capacity_units
    @last_increased_time = Time.now.to_i
  end

  if @write_capacity_units > write_capacity_units
    @last_decreased_time = Time.now.to_i
  elsif @write_capacity_units < write_capacity_units
    @last_increased_time = Time.now.to_i
  end

  @read_capacity_units, @write_capacity_units = read_capacity_units, write_capacity_units

  response = description
  response['TableDescription']['TableStatus'] = 'UPDATING'
  response
end

#update_item(data) ⇒ Object



174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/fake_dynamo/table.rb', line 174

def update_item(data)
  key = Key.from_data(data['Key'], key_schema)
  item = @items[key]
  check_conditions(item, data['Expected'])

  unless item
    item = Item.from_key(key)
    if create_item?(data)
      @items[key] = item
    else
      return consumed_capacity(data).merge(item.collection_metrics(data))
    end
    item_created = true
  end

  old_item = deep_copy(item)
  begin
    old_hash = item.as_hash
    if attribute_updates = data['AttributeUpdates']
      attribute_updates.each do |name, update_data|
        item.update(name, update_data)
      end

      item.validate_attribute_types(attribute_definitions)
    end
  rescue => e
    if item_created
      @items.delete(key)
    else
      @items[key] = old_item
    end
    raise e
  end

  return_values(data, old_hash, item).merge(item.collection_metrics(data))
end

#updated_attributes(data) ⇒ Object



476
477
478
# File 'lib/fake_dynamo/table.rb', line 476

def updated_attributes(data)
  data['AttributeUpdates'].map { |name, _| name }
end

#validate_limit(data) ⇒ Object



375
376
377
378
379
# File 'lib/fake_dynamo/table.rb', line 375

def validate_limit(data)
  if data['Limit'] and data['Limit'] <= 0
    raise ValidationException, "Limit failed to satisfy constraint: Member must have value greater than or equal to 1"
  end
end