Class: RR::Committers::BufferedCommitter
- Inherits:
-
DefaultCommitter
- Object
- DefaultCommitter
- RR::Committers::BufferedCommitter
- Defined in:
- lib/rubyrep/committers/buffered_committer.rb
Overview
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
Constant Summary collapse
- DEFAULT_COMMIT_FREQUENCY =
Unless overwritten via configuration, transactions are commited after the given number of record changes
1000
Instance Attribute Summary
Attributes inherited from DefaultCommitter
Instance Method Summary collapse
-
#activity_marker_table ⇒ Object
Returns the name of the activity marker table.
-
#begin_db_transactions ⇒ Object
Begins new transactions in both databases.
-
#commit ⇒ Object
Commits the open tranactions and starts new one if the #commit_frequency number of record changes have been executed.
-
#commit_db_transactions ⇒ Object
Commits the open transactions in both databases.
-
#commit_frequency ⇒ Object
Returns the number of changes, after which the open transactions should be committed and new transactions be started.
-
#delete_record(database, table, values) ⇒ Object
Deletes the specified record in the specified database.
-
#exclude_rr_activity(database, table) ⇒ Object
Switches the trigger mode of the specified
table
in the specifieddatabase
to ignore rubyrep activity. -
#finalize(success = true) ⇒ Object
Is called after the last insert / update / delete query.
-
#initialize(session) ⇒ BufferedCommitter
constructor
A new committer is created for each table sync.
-
#insert_record(database, table, values) ⇒ Object
Inserts the specified record in the specified database.
-
#maintain_activity_status? ⇒ Boolean
Returns
true
if the activity marker table should be maintained. -
#new_transaction? ⇒ Boolean
Returns
true
if a new transaction was started since the last insert / update / delete. -
#rollback_db_transactions ⇒ Object
Rolls back the open transactions in both databases.
-
#trigger_mode_switcher ⇒ Object
Returns the TriggerModeSwitcher (creates it if necessary).
-
#update_record(database, table, values, old_key = nil) ⇒ Object
Updates the specified record in the specified database.
Constructor Details
#initialize(session) ⇒ BufferedCommitter
A new committer is created for each table sync.
-
session: a Session object representing the current database session
102 103 104 105 |
# File 'lib/rubyrep/committers/buffered_committer.rb', line 102 def initialize(session) super begin_db_transactions end |
Instance Method Details
#activity_marker_table ⇒ Object
Returns the name of the activity marker table
31 32 33 |
# File 'lib/rubyrep/committers/buffered_committer.rb', line 31 def activity_marker_table @activity_marker_table ||= "#{session.configuration.[:rep_prefix]}_running_flags" end |
#begin_db_transactions ⇒ Object
Begins new transactions in both databases. After starting the transaction, marks the activity of rubyrep.
67 68 69 70 71 72 73 74 |
# 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 ⇒ Object
Commits the open tranactions and starts new one if the #commit_frequency number of record changes have been executed.
84 85 86 87 88 89 90 91 92 |
# 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 ⇒ Object
Commits the open transactions in both databases. Before committing, clears the rubyrep activity marker.
56 57 58 59 60 61 62 63 |
# 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 ⇒ Object
Returns the number of changes, after which the open transactions should be committed and new transactions be started.
46 47 48 49 50 51 52 |
# File 'lib/rubyrep/committers/buffered_committer.rb', line 46 def commit_frequency unless @commit_frequency @commit_frequency = session.configuration.[:commit_frequency] @commit_frequency ||= DEFAULT_COMMIT_FREQUENCY end @commit_frequency end |
#delete_record(database, table, values) ⇒ Object
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).
134 135 136 137 138 |
# 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) ⇒ Object
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
21 22 23 |
# 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) ⇒ Object
Is called after the last insert / update / delete query.
-
success
: should be true if there were no problems, false otherwise.
142 143 144 145 146 147 148 |
# 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) ⇒ Object
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.
111 112 113 114 115 |
# 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? ⇒ Boolean
Returns true
if the activity marker table should be maintained.
36 37 38 39 40 41 42 |
# 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? ⇒ Boolean
Returns true
if a new transaction was started since the last insert / update / delete.
96 97 98 |
# File 'lib/rubyrep/committers/buffered_committer.rb', line 96 def new_transaction? @change_counter == 0 end |
#rollback_db_transactions ⇒ Object
Rolls back the open transactions in both databases.
77 78 79 80 |
# 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 ⇒ Object
Returns the TriggerModeSwitcher (creates it if necessary)
26 27 28 |
# 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) ⇒ Object
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
.
124 125 126 127 128 |
# 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 |