Class: Alephant::Sequencer::SequenceTable
- Inherits:
-
Object
- Object
- Alephant::Sequencer::SequenceTable
- Defined in:
- lib/alephant/models/sequence_table.rb
Constant Summary collapse
- TIMEOUT =
120- DEFAULT_CONFIG =
{ :write_units => 5, :read_units => 10, }
- SCHEMA =
{ :hash_key => { :key => :string, :value => :string } }
Instance Attribute Summary collapse
-
#table_name ⇒ Object
readonly
Returns the value of attribute table_name.
Instance Method Summary collapse
- #create ⇒ Object
- #delete_item!(ident) ⇒ Object
-
#initialize(table_name, config = DEFAULT_CONFIG) ⇒ SequenceTable
constructor
A new instance of SequenceTable.
- #sequence_for(ident) ⇒ Object
- #set_sequence_for(ident, value) ⇒ Object
- #table ⇒ Object
Constructor Details
#initialize(table_name, config = DEFAULT_CONFIG) ⇒ SequenceTable
Returns a new instance of SequenceTable.
22 23 24 25 26 27 |
# File 'lib/alephant/models/sequence_table.rb', line 22 def initialize(table_name, config = DEFAULT_CONFIG) @mutex = Mutex.new @dynamo_db = AWS::DynamoDB.new @table_name = table_name @config = config end |
Instance Attribute Details
#table_name ⇒ Object (readonly)
Returns the value of attribute table_name.
8 9 10 |
# File 'lib/alephant/models/sequence_table.rb', line 8 def table_name @table_name end |
Instance Method Details
#create ⇒ Object
29 30 31 32 33 34 |
# File 'lib/alephant/models/sequence_table.rb', line 29 def create @mutex.synchronize do ensure_table_exists ensure_table_active end end |
#delete_item!(ident) ⇒ Object
56 57 58 |
# File 'lib/alephant/models/sequence_table.rb', line 56 def delete_item!(ident) table.items[ident].delete end |
#sequence_for(ident) ⇒ Object
40 41 42 43 |
# File 'lib/alephant/models/sequence_table.rb', line 40 def sequence_for(ident) rows = batch_get_value_for(ident) rows.count >= 1 ? rows.first['value'].to_i : 0 end |
#set_sequence_for(ident, value) ⇒ Object
45 46 47 48 49 50 51 52 53 54 |
# File 'lib/alephant/models/sequence_table.rb', line 45 def set_sequence_for(ident,value) @mutex.synchronize do AWS::DynamoDB::BatchWrite.new.tap { |batch| batch.put( table_name, [:key => ident,:value => value] ) }.process! end end |
#table ⇒ Object
36 37 38 |
# File 'lib/alephant/models/sequence_table.rb', line 36 def table @table ||= @dynamo_db.tables[@table_name] end |