Module: OceanDynamo::Queries

Included in:
Table
Defined in:
lib/ocean-dynamo/queries.rb

Instance Method Summary collapse

Instance Method Details

#all(consistent: false, **options) ⇒ Object

Returns all records in the table.



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

def all(consistent: false, **options)
  _late_connect?
  records = []
  in_batches :scan, { consistent_read: !!consistent } do |attrs|
    records << new._setup_from_dynamo(attrs)
  end
  records
end

#count(**options) ⇒ Object

The number of records in the table. Updated every 6 hours or so; thus isn’t a reliable real-time measure of the number of table items.



38
39
40
41
# File 'lib/ocean-dynamo/queries.rb', line 38

def count(**options)
  _late_connect?
  dynamo_table.item_count
end

#find(hash, range = nil, consistent: false) ⇒ Object


Class methods



10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/ocean-dynamo/queries.rb', line 10

def find(hash, range=nil, consistent: false)
  return hash.collect {|elem| find elem, range, consistent: consistent } if hash.is_a?(Array)
  _late_connect?
  hash = hash.id if hash.kind_of?(Table)    # TODO: We have (innocuous) leakage, fix!
  range = range.to_i if range.is_a?(Time)
  keys = { table_hash_key.to_s => hash }
  keys[table_range_key] = range if table_range_key && range
  options = { key: keys, consistent_read: consistent }
  item = dynamo_table.get_item(options).item
  unless item
    raise RecordNotFound, "can't find a #{self} with primary key ['#{hash}', #{range.inspect}]" 
  end
  new._setup_from_dynamo(item)
end

#find_by_key(*args) ⇒ Object Also known as: find_by_id



26
27
28
29
30
# File 'lib/ocean-dynamo/queries.rb', line 26

def find_by_key(*args)
  find(*args)
rescue RecordNotFound
  nil
end

#find_each(consistent: false, limit: nil, batch_size: nil) ⇒ Object

Looping through a collection of records from the database (using the all method, for example) is very inefficient since it will try to instantiate all the objects at once. Batch processing methods allow you to work with the records in batches, thereby greatly reducing memory consumption.

TODO: Add support for

index_name: "IndexName",
select: "ALL_ATTRIBUTES", # ALL_ATTRIBUTES, ALL_PROJECTED_ATTRIBUTES, SPECIFIC_ATTRIBUTES, COUNT


89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/ocean-dynamo/queries.rb', line 89

def find_each(consistent: false, limit: nil, batch_size: nil)
  options = { consistent_read: consistent }
  batch_size = limit if limit && batch_size && limit < batch_size
  options[:limit] = batch_size if batch_size   
  in_batches :scan, options do |attrs|
    if limit
      return true if limit <= 0
      limit = limit - 1
    end
    yield new._setup_from_dynamo(attrs)
  end
end

#in_batches(message, options, &block) ⇒ Object

This method takes a block and yields it to every record in a table. message must be either :scan or :query. options is the hash of options to pass to the scan or query operation.

TODO: Add support for

index_name: "IndexName",
select: "ALL_ATTRIBUTES", # ALL_ATTRIBUTES, ALL_PROJECTED_ATTRIBUTES, SPECIFIC_ATTRIBUTES, COUNT


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

def in_batches(message, options, &block)
  _late_connect?
  loop do
    result = dynamo_table.send message, options
    result.items.each do |hash|
      yield hash
    end
    return true unless result.last_evaluated_key
    options[:exclusive_start_key] = result.last_evaluated_key
  end
end