class RR::Committers::BufferedCommitter
This committer periodically commits transactions. It can be used for pre-replication syncs as it
-
updates the activity marker table.
-
switches existing triggers to filter out rubyrep activity
Constants
- DEFAULT_COMMIT_FREQUENCY
Unless overwritten via configuration, transactions are commited after the given number of record changes
Public Class Methods
A new committer is created for each table sync.
-
session: a
Session
object representing the current database session
# File lib/rubyrep/committers/buffered_committer.rb, line 102 def initialize(session) super begin_db_transactions end
Public Instance Methods
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
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
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
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
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
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).
# File lib/rubyrep/committers/buffered_committer.rb, line 134 def delete_record(database, table, values) exclude_rr_activity database, table super commit end
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
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
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.
# File lib/rubyrep/committers/buffered_committer.rb, line 111 def insert_record(database, table, values) exclude_rr_activity database, table super commit end
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
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
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
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
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. Ifnil
, then the primary key must be contained invalues
.
# 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