class RR::Committers::BufferedCommitter

This committer periodically commits transactions. It can be used for pre-replication syncs as it

Constants

DEFAULT_COMMIT_FREQUENCY

Unless overwritten via configuration, transactions are commited after the given number of record changes

Public Class Methods

new(session) click to toggle source

A new committer is created for each table sync.

  • session: a Session object representing the current database session

Calls superclass method
# File lib/rubyrep/committers/buffered_committer.rb, line 102
def initialize(session)
  super
  begin_db_transactions
end

Public Instance Methods

activity_marker_table() click to toggle source

Returns the name of the activity marker table

# File lib/rubyrep/committers/buffered_committer.rb, line 31
def activity_marker_table
  @activity_marker_table ||= "#{session.configuration.options[:rep_prefix]}_running_flags"
end
begin_db_transactions() click to toggle source

Begins new transactions in both databases. After starting the transaction, marks the activity of rubyrep.

# File lib/rubyrep/committers/buffered_committer.rb, line 67
def begin_db_transactions
  [:left, :right].each do |database|
    session.send(database).begin_db_transaction
    if maintain_activity_status?
      session.send(database).execute("insert into #{activity_marker_table} values(1)")
    end
  end
end
commit() click to toggle source

Commits the open tranactions and starts new one if the commit_frequency number of record changes have been executed.

# File lib/rubyrep/committers/buffered_committer.rb, line 84
def commit
  @change_counter ||= 0
  @change_counter += 1
  if @change_counter == commit_frequency
    @change_counter = 0
    commit_db_transactions
    begin_db_transactions
  end
end
commit_db_transactions() click to toggle source

Commits the open transactions in both databases. Before committing, clears the rubyrep activity marker.

# File lib/rubyrep/committers/buffered_committer.rb, line 56
def commit_db_transactions
  [:left, :right].each do |database|
    if maintain_activity_status?
      session.send(database).execute("delete from #{activity_marker_table}")
    end
    session.send(database).commit_db_transaction
  end
end
commit_frequency() click to toggle source

Returns the number of changes, after which the open transactions should be committed and new transactions be started.

# File lib/rubyrep/committers/buffered_committer.rb, line 46
def commit_frequency
  unless @commit_frequency
    @commit_frequency = session.configuration.options[:commit_frequency]
    @commit_frequency ||= DEFAULT_COMMIT_FREQUENCY
  end
  @commit_frequency
end
delete_record(database, table, values) click to toggle source

Deletes the specified record in the specified database.

  • database: identifying the database (either :left or :right)

  • table: name of the table

  • values: a hash of column_name => value pairs (must only contain primary key columns).

Calls superclass method
# File lib/rubyrep/committers/buffered_committer.rb, line 134
def delete_record(database, table, values)
  exclude_rr_activity database, table
  super
  commit
end
exclude_rr_activity(database, table) click to toggle source

Switches the trigger mode of the specified table in the specified database to ignore rubyrep activity.

  • database: identifying the database (either :left or :right)

  • table: name of the table

# File lib/rubyrep/committers/buffered_committer.rb, line 21
def exclude_rr_activity(database, table)
  trigger_mode_switcher.exclude_rr_activity database, table
end
finalize(success = true) click to toggle source

Is called after the last insert / update / delete query.

  • success: should be true if there were no problems, false otherwise.

# File lib/rubyrep/committers/buffered_committer.rb, line 142
def finalize(success = true)
  if success
    commit_db_transactions
  else
    rollback_db_transactions
  end
end
insert_record(database, table, values) click to toggle source

Inserts the specified record in the specified database.

  • database: identifying the database (either :left or :right)

  • table: name of the table

  • values: a hash of column_name => value pairs.

Calls superclass method
# File lib/rubyrep/committers/buffered_committer.rb, line 111
def insert_record(database, table, values)
  exclude_rr_activity database, table
  super
  commit
end
maintain_activity_status?() click to toggle source

Returns true if the activity marker table should be maintained.

# File lib/rubyrep/committers/buffered_committer.rb, line 36
def maintain_activity_status?
  unless @activity_status_checked
    @activity_status_checked = true
    @maintain_activity_status = session.left.tables.include?(activity_marker_table)
  end
  @maintain_activity_status
end
new_transaction?() click to toggle source

Returns true if a new transaction was started since the last insert / update / delete.

# File lib/rubyrep/committers/buffered_committer.rb, line 96
def new_transaction?
  @change_counter == 0
end
rollback_db_transactions() click to toggle source

Rolls back the open transactions in both databases.

# File lib/rubyrep/committers/buffered_committer.rb, line 77
def rollback_db_transactions
  session.left.rollback_db_transaction
  session.right.rollback_db_transaction
end
trigger_mode_switcher() click to toggle source

Returns the TriggerModeSwitcher (creates it if necessary)

# File lib/rubyrep/committers/buffered_committer.rb, line 26
def trigger_mode_switcher
  @trigger_mode_switcher ||= TriggerModeSwitcher.new session
end
update_record(database, table, values, old_key = nil) click to toggle source

Updates the specified record in the specified database.

  • database: identifying the database (either :left or :right)

  • table: name of the table

  • values: a hash of column_name => value pairs.

  • old_key: A column_name => value hash identifying original primary key. If nil, then the primary key must be contained in values.

Calls superclass method
# File lib/rubyrep/committers/buffered_committer.rb, line 124
def update_record(database, table, values, old_key = nil)
  exclude_rr_activity database, table
  super
  commit
end