com.continuent.tungsten.replicator.extractor.mysql
Class BinlogReader

java.lang.Object
  extended by com.continuent.tungsten.replicator.extractor.mysql.BinlogReader
All Implemented Interfaces:
java.io.FilenameFilter, java.lang.Cloneable

public class BinlogReader
extends java.lang.Object
implements java.io.FilenameFilter, java.lang.Cloneable

Encapsulates logic to open, read data, and clone positions on MySQL binlog files. This file takes care of validating the header and also eliminates much of the tangled logic of validating headers and cloning positions present in former implementations. It also leans on the BufferedFileDataInput class to ensure reads are buffered and minimize use of disk metadata calls that kill performance on network-attached storage.

The new implementation replaces the old BinlogPosition class authored by Seppo Jaakola; a few fields are copied into this implementation but the logic is quite different.

Author:
Robert Hodges

Constructor Summary
BinlogReader(long start, java.lang.String fileName, java.lang.String directory, java.lang.String baseName, int bufferSize)
          Defines all possible binlog parameters .
BinlogReader(java.lang.String directory, java.lang.String baseName, int bufferSize)
          Defines only binlog directory and binlog file base name.
 
Method Summary
 boolean accept(java.io.File dir, java.lang.String name)
           
 long available()
          Returns the number of bytes available in the current log file.
 BinlogReader clone()
          Clones the current reader position.
 void close()
          Closes the stream but leaves directory and base name intact
 java.lang.String getBaseName()
           
 java.lang.String getDirectory()
           
 int getEventID()
           
 java.lang.String getFileName()
           
 long getPosition()
           
 int getVersion()
           
 boolean isOpen()
          Returns true if a binlog log file is currently open.
 void mark(int readLimit)
          Mark binlog to read up to limit.
 void read(byte[] buf)
          Read bytes fully into a byte buffer.
 void read(byte[] buf, int offset, int len)
          Read bytes fully into a byte buffer.
 byte readByte()
          Read a single byte from binlog.
 int readInt()
          Read a 4-byte int from binlog.
 long readLong()
          Read an 8-byte long from binlog.
 void reset()
          Reset binlog back to last mark.
 void setBaseName(java.lang.String baseName)
           
 void setDirectory(java.lang.String directory)
           
 void setEventID(int eventID)
           
 void setFileName(java.lang.String fileName)
           
 void setStartPosition(long newPosition)
           
 void setVersion(int version)
           
 long skip(long bytes)
          Skips a given number of bytes.
 java.lang.String toString()
           
 long waitAvailable(int requested, int waitMillis)
          Wait for a specific number of bytes to be available or until we time out
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

BinlogReader

public BinlogReader(java.lang.String directory,
                    java.lang.String baseName,
                    int bufferSize)
Defines only binlog directory and binlog file base name.

Parameters:
directory - Directory path where binlog files should reside
baseName - File name pattern for binlog files: basenName001.bin

BinlogReader

public BinlogReader(long start,
                    java.lang.String fileName,
                    java.lang.String directory,
                    java.lang.String baseName,
                    int bufferSize)
Defines all possible binlog parameters .

Parameters:
start - Start location in the file, will skip until that
fileName - Full file path name to open
directory - Directory path where binlog files should reside
baseName - File name pattern for binlog files: baseName-000001.bin
bufferSize - Size of buffer to read
Method Detail

clone

public BinlogReader clone()
Clones the current reader position. Clients call open() on the resulting file to create an alternate read stream.

Overrides:
clone in class java.lang.Object
See Also:
Object.clone()

isOpen

public boolean isOpen()
Returns true if a binlog log file is currently open.


close

public void close()
           throws ReplicatorException
Closes the stream but leaves directory and base name intact

Throws:
ReplicatorException

available

public long available()
               throws java.io.IOException,
                      java.lang.InterruptedException
Returns the number of bytes available in the current log file. WARNING: this is a potentially expensive call as it will fetch disk metadata over a network if necessary.

Throws:
java.io.IOException - Thrown if call fails
java.lang.InterruptedException

waitAvailable

public long waitAvailable(int requested,
                          int waitMillis)
                   throws java.io.IOException,
                          java.lang.InterruptedException
Wait for a specific number of bytes to be available or until we time out

Parameters:
requested - Number of bytes requested
waitMillis - Timeout in milliseconds
Returns:
Number of bytes available for non-blocking read
Throws:
java.io.IOException - Thrown if wait fails
java.lang.InterruptedException - Thrown if interrupted during wait

skip

public long skip(long bytes)
          throws java.io.IOException
Skips a given number of bytes.

Returns:
Actual number of bytes skipped
Throws:
java.io.IOException - Thrown if bytes cannot be skipped

mark

public void mark(int readLimit)
Mark binlog to read up to limit.

Parameters:
readLimit - Number of bytes that may be read before resetting

reset

public void reset()
           throws java.io.IOException,
                  java.lang.InterruptedException
Reset binlog back to last mark.

Throws:
java.io.IOException - Thrown if mark has been invalidated or not set
java.lang.InterruptedException - Thrown if we are interrupted

read

public void read(byte[] buf)
          throws java.io.IOException
Read bytes fully into a byte buffer. This call blocks until data are available.

Parameters:
buf - Buffer into which to read bytes
Throws:
java.io.IOException - Thrown if read fails.

read

public void read(byte[] buf,
                 int offset,
                 int len)
          throws java.io.IOException
Read bytes fully into a byte buffer. This call blocks until data are available.

Parameters:
buf - Buffer into which to transfer bytes
offset - Point at which to write bytes in array
len - Number of bytes to read
Throws:
java.io.IOException - Thrown if read fails.

readLong

public long readLong()
              throws java.io.IOException
Read an 8-byte long from binlog.

Throws:
java.io.IOException - Thrown if read fails.

readInt

public int readInt()
            throws java.io.IOException
Read a 4-byte int from binlog.

Throws:
java.io.IOException - Thrown if read fails.

readByte

public byte readByte()
              throws java.io.IOException
Read a single byte from binlog.

Throws:
java.io.IOException - Thrown if read fails.

setStartPosition

public void setStartPosition(long newPosition)

getPosition

public long getPosition()

setFileName

public void setFileName(java.lang.String fileName)

getFileName

public java.lang.String getFileName()

setDirectory

public void setDirectory(java.lang.String directory)

getDirectory

public java.lang.String getDirectory()

accept

public boolean accept(java.io.File dir,
                      java.lang.String name)
Specified by:
accept in interface java.io.FilenameFilter

getBaseName

public java.lang.String getBaseName()

setBaseName

public void setBaseName(java.lang.String baseName)

getEventID

public int getEventID()

setEventID

public void setEventID(int eventID)

getVersion

public int getVersion()

setVersion

public void setVersion(int version)

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object