Class: Alephant::Sequencer::SequenceTable

Inherits:
Alephant::Support::DynamoDB::Table
  • Object
show all
Includes:
Logger
Defined in:
lib/alephant/sequencer/sequence_table.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(table_name) ⇒ SequenceTable

Returns a new instance of SequenceTable.



15
16
17
18
19
# File 'lib/alephant/sequencer/sequence_table.rb', line 15

def initialize(table_name)
  @mutex      = Mutex.new
  @client     = AWS::DynamoDB::Client::V20120810.new
  @table_name = table_name
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



13
14
15
# File 'lib/alephant/sequencer/sequence_table.rb', line 13

def client
  @client
end

#table_nameObject (readonly)

Returns the value of attribute table_name.



13
14
15
# File 'lib/alephant/sequencer/sequence_table.rb', line 13

def table_name
  @table_name
end

Instance Method Details

#delete_item!(ident) ⇒ Object



76
77
78
79
80
# File 'lib/alephant/sequencer/sequence_table.rb', line 76

def delete_item!(ident)
  client.delete_item(
    item_payload(ident)
  )
end

#sequence_exists(ident) ⇒ Object



21
22
23
24
25
26
27
28
29
# File 'lib/alephant/sequencer/sequence_table.rb', line 21

def sequence_exists(ident)
  if ident.nil?
    return false
  end

  !(client.get_item(
    item_payload(ident)
  ).length == 0)
end

#sequence_for(ident) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/alephant/sequencer/sequence_table.rb', line 31

def sequence_for(ident)
  data = client.get_item(
    item_payload(ident)
  )

  data.length > 0 ? data[:item]["value"][:n].to_i : 0
end

#set_sequence_for(ident, value, last_seen_check = nil) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/alephant/sequencer/sequence_table.rb', line 39

def set_sequence_for(ident, value, last_seen_check = nil)
  begin

    current_sequence = last_seen_check.nil? ? sequence_for(ident) : last_seen_check
    @mutex.synchronize do

      client.put_item({
        :table_name => table_name,
        :item => {
          'key' => {
            'S' => ident
          },
          'value' => {
            'N' => value.to_s
          }
        },
        :expected => {
          'key' => {
            :comparison_operator => 'NULL'
          },
          'value' => {
            :comparison_operator => 'GE',
            :attribute_value_list => [
              { 'N' => current_sequence.to_s }
            ]
          }
        },
        :conditional_operator => 'OR'
      })
    end

    logger.info("SequenceTable#set_sequence_for: #{value} for #{ident} success!")
  rescue AWS::DynamoDB::Errors::ConditionalCheckFailedException => e
    logger.warn("SequenceTable#set_sequence_for: (Value to put: #{value}, existing: #{current_sequence}) #{ident} outdated!")
  end
end