Sha256: 246a3ef2bbe9fd5da253d29392f74fb493ca0f7f18038d6650904b96b78028aa

Contents?: true

Size: 1.67 KB

Versions: 11

Compression:

Stored size: 1.67 KB

Contents

module FlydataCore
module Mysql

class BinlogPos
  include Comparable
  def initialize(binlog_str_or_filename_or_hash, binlog_pos = nil)
    arg = binlog_str_or_filename_or_hash
    if binlog_pos
      @pos = binlog_pos
      @filename = arg
    elsif arg.kind_of?(String)
      if arg == "-"
        set_empty
      else
        @filename, @pos = arg.split("\t")
      end
    elsif arg.kind_of?(Hash)
      if arg[:filename] == "-" || arg[:binfile] == "-"
        set_empty
      else
        @pos = arg[:pos]
        @filename = arg[:filename]
        @filename ||= arg[:binfile]
      end
    end
    if @filename.nil? || @pos.nil?
      raise "Invalid initialize argument (#{arg}, #{binlog_pos})"
    end
    @pos = @pos.to_i
  end

  attr_reader :filename, :pos

  def empty?
    @filename && @filename == "-"
  end

  def <=>(other_pos)
    if other_pos.nil?
      raise ArgumentError.new("comparison of BinlosPos with nil failed")
    end

    other_pos = BinlogPos.new(other_pos) unless other_pos.kind_of?(BinlogPos)
    if empty? && other_pos.empty?
      return 0
    elsif empty? || other_pos.empty?
      raise ArgumentError.new("comparison with empty BinlogPos failed")
    else
      (@filename <=> other_pos.filename) == 0 ? (@pos <=> other_pos.pos) : (@filename <=> other_pos.filename)
    end
  end

  def ==(other_pos)
    return false if other_pos.nil?
    return @filename == other_pos.filename if empty? || other_pos.empty?
    self.<=>(other_pos) == 0
  end

  def to_s
    empty? ? "-" : "#{@filename}\t#{@pos}"
  end

  private
  def set_empty
    # @filename == "-" is the definition of `empty`. @pos could be anything but nul
    @filename = "-"
    @pos = -1
  end


end

end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
flydata-0.6.14 flydata-core/lib/flydata-core/mysql/binlog_pos.rb
flydata-0.6.13 flydata-core/lib/flydata-core/mysql/binlog_pos.rb
flydata-0.6.12 flydata-core/lib/flydata-core/mysql/binlog_pos.rb
flydata-0.6.11 flydata-core/lib/flydata-core/mysql/binlog_pos.rb
flydata-0.6.10 flydata-core/lib/flydata-core/mysql/binlog_pos.rb
flydata-0.6.9 flydata-core/lib/flydata-core/mysql/binlog_pos.rb
flydata-0.6.8 flydata-core/lib/flydata-core/mysql/binlog_pos.rb
flydata-0.6.7 flydata-core/lib/flydata-core/mysql/binlog_pos.rb
flydata-0.6.6 flydata-core/lib/flydata-core/mysql/binlog_pos.rb
flydata-0.6.5 flydata-core/lib/flydata-core/mysql/binlog_pos.rb
flydata-0.6.4 flydata-core/lib/flydata-core/mysql/binlog_pos.rb