Class: RR::DirectTableScan

Inherits:
TableScan show all
Includes:
TableScanHelper
Defined in:
lib/rubyrep/direct_table_scan.rb

Overview

Scans two tables for differences. Doesn’t have any reporting functionality by itself. Instead DirectTableScan#run yields all the differences for the caller to do with as it pleases. Usage:

1. Create a new DirectTableScan object and hand it all necessary information
2. Call DirectTableScan#run to do the actual comparison
3. The block handed to DirectTableScan#run receives all differences

Instance Attribute Summary collapse

Attributes inherited from TableScan

#left_table, #primary_key_names, #progress_printer, #right_table, #session

Instance Method Summary collapse

Methods included from TableScanHelper

#rank_rows, scan_class

Methods inherited from TableScan

#scan_options, #update_progress

Constructor Details

#initialize(session, left_table, right_table = nil) ⇒ DirectTableScan

Creates a new DirectTableScan instance

* session: a Session object representing the current database session
* left_table: name of the table in the left database
* right_table: name of the table in the right database. If not given, same like left_table


23
24
25
# File 'lib/rubyrep/direct_table_scan.rb', line 23

def initialize(session, left_table, right_table = nil)
  super
end

Instance Attribute Details

#left_casterObject

The TypeCastingCursor for the left table



14
15
16
# File 'lib/rubyrep/direct_table_scan.rb', line 14

def left_caster
  @left_caster
end

#right_casterObject

The TypeCastingCursor for the right table



17
18
19
# File 'lib/rubyrep/direct_table_scan.rb', line 17

def right_caster
  @right_caster
end

Instance Method Details

#run(&blck) ⇒ Object

Runs the table scan. Calls the block for every found difference. Differences are yielded with 2 parameters

* type: describes the difference, either :left (row only in left table), :right (row only in right table) or :conflict
* row: For :left or :right cases a hash describing the row; for :conflict an array of left and right row.
       A row is a hash of column_name => value pairs.


33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/rubyrep/direct_table_scan.rb', line 33

def run(&blck)
  left_cursor = right_cursor = nil
  left_cursor = session.left.select_cursor(
    :table => left_table,
    :row_buffer_size => scan_options[:row_buffer_size],
    :type_cast => true
  )
  right_cursor = session.right.select_cursor(
    :table => right_table,
    :row_buffer_size => scan_options[:row_buffer_size],
    :type_cast => true
  )
  left_row = right_row = nil
  update_progress 0 # ensures progress bar is printed even if there are no records
  while left_row or right_row or left_cursor.next? or right_cursor.next?
    # if there is no current left row, _try_ to load the next one
    left_row ||= left_cursor.next_row if left_cursor.next?
    # if there is no current right row, _try_ to load the next one
    right_row ||= right_cursor.next_row if right_cursor.next?
    rank = rank_rows left_row, right_row
    case rank
    when -1
      yield :left, left_row
      left_row = nil
      update_progress 1
    when 1
      yield :right, right_row
      right_row = nil
      update_progress 1
    when 0
      update_progress 2
      if not left_row == right_row
        yield :conflict, [left_row, right_row]
      end
      left_row = right_row = nil
    end
    # check for corresponding right rows
  end
ensure
  [left_cursor, right_cursor].each {|cursor| cursor.clear if cursor}
end