class RR::ReplicationHelper
Provides helper functionality for replicators. The methods exposed by this class are intended to provide a stable interface for third party replicators.
Attributes
The current Committer
The current ReplicationRun
instance
Public Class Methods
Creates a new SyncHelper
for the given TableSync
instance.
# File lib/rubyrep/replication_helper.rb, line 133 def initialize(replication_run) self.replication_run = replication_run # Creates the committer. Important as it gives the committer the # opportunity to start transactions committer_class = Committers::committers[options[:committer]] @committer = committer_class.new(session) end
Public Instance Methods
Delegates to Session#corresponding_table
# File lib/rubyrep/replication_helper.rb, line 30 def corresponding_table(db_arm, table); session.corresponding_table(db_arm, table); end
Delegates to Committers::BufferedCommitter#delete_record
# File lib/rubyrep/replication_helper.rb, line 49 def delete_record(database, table, values) committer.delete_record(database, table, values) end
Asks the committer (if it exists) to finalize any open transactions success
should be true if there were no problems, false otherwise.
# File lib/rubyrep/replication_helper.rb, line 76 def finalize(success = true) committer.finalize(success) end
Delegates to Committers::BufferedCommitter#insert_record
# File lib/rubyrep/replication_helper.rb, line 39 def insert_record(database, table, values) committer.insert_record(database, table, values) end
Loads the specified record. Returns an according column_name => value hash. Parameters:
-
database
: either :left
or :right
-
table
: name of the table -
key
: A column_name => value hash for all primary key columns.
# File lib/rubyrep/replication_helper.rb, line 58 def load_record(database, table, key) cursor = session.send(database).select_cursor( :table => table, :row_keys => [key], :type_cast => true ) row = nil row = cursor.next_row if cursor.next? cursor.clear row end
Logs the outcome of a replication into the replication log table.
-
diff
: the replicatedReplicationDifference
-
outcome
: string summarizing the outcome of the replication -
details
: string with further details regarding the replication
# File lib/rubyrep/replication_helper.rb, line 104 def log_replication_outcome(diff, outcome, details = nil) table = diff.changes[:left].table key = diff.changes[:left].key if key.size == 1 key = key.values[0] else key_parts = session.left.primary_key_names(table).map do |column_name| %Q("#{column_name}"=>#{key[column_name].to_s.inspect}) end key = key_parts.join(', ') end rep_outcome, rep_details = fit_description_columns(outcome, details) diff_dump = diff.to_yaml[0...ReplicationInitializer::DIFF_DUMP_SIZE] session.left.insert_record "#{options[:rep_prefix]}_logged_events", { :activity => 'replication', :change_table => table, :diff_type => diff.type.to_s, :change_key => key, :left_change_type => (diff.changes[:left] ? diff.changes[:left].type.to_s : nil), :right_change_type => (diff.changes[:right] ? diff.changes[:right].type.to_s : nil), :description => rep_outcome, :long_description => rep_details, :event_time => Time.now, :diff_dump => diff_dump } end
Returns true
if a new transaction was started since the last insert / update / delete.
# File lib/rubyrep/replication_helper.rb, line 34 def new_transaction? committer.new_transaction? end
Current options
# File lib/rubyrep/replication_helper.rb, line 17 def options; @options ||= session.configuration.options; end
Returns the options for the specified table name.
-
table
: name of the table (left database version)
# File lib/rubyrep/replication_helper.rb, line 21 def options_for_table(table) @options_for_table ||= {} unless @options_for_table.include? table @options_for_table[table] = session.configuration.options_for_table(table) end @options_for_table[table] end
The active Session
# File lib/rubyrep/replication_helper.rb, line 14 def session; replication_run.session; end
Converts the row values into their proper types as per table definition.
-
table
: name of the table after whose columns is type-casted. -
row
: A column_name => value hash of the row
Returns a copy of the column_name => value hash (with type-casted values).
# File lib/rubyrep/replication_helper.rb, line 84 def type_cast(table, row) @table_columns ||= {} unless @table_columns.include?(table) column_array = session.left.columns(table) column_hash = {} column_array.each {|column| column_hash[column.name] = column} @table_columns[table] = column_hash end columns = @table_columns[table] type_casted_row = {} row.each_pair do |column_name, value| type_casted_row[column_name] = session.left.connection.fixed_type_cast value, columns[column_name] end type_casted_row end
Delegates to Committers::BufferedCommitter#update_record
# File lib/rubyrep/replication_helper.rb, line 44 def update_record(database, table, values, old_key = nil) committer.update_record(database, table, values, old_key) end