com.continuent.tungsten.replicator.thl.log
Class LogRecord

java.lang.Object
  extended by com.continuent.tungsten.replicator.thl.log.LogRecord

public class LogRecord
extends java.lang.Object

Encapsulates a log record from the Tungsten disk log.

Version:
1.0
Author:
Robert Hodges

Field Summary
static byte CRC_TYPE_32
          Record uses conventional CRC-32 computed by Java CRC32 class.
static byte CRC_TYPE_NONE
          Record does not have a CRC computed.
static byte EVENT_REPL
          Denotes record header information.
static byte EVENT_ROTATE
          Denotes a replication event
static int NON_DATA_BYTES
          Number of bytes in length field plus CRC.
 
Constructor Summary
LogRecord(java.io.File file, long offset, boolean truncated)
          Creates an empty record, which is optionally truncated.
LogRecord(java.io.File file, long offset, byte[] bytes, byte crcType, long crc)
          Creates a readable record with indicated content.
 
Method Summary
 boolean checkCrc()
          Computes the CRC value and compares to the CRC stored in the record, returning true only if the CRC values match.
 long computeCrc()
          Compute and return CRC on data.
static long computeCrc32(byte[] bytes)
          Static routine to compute CRC 32.
 void done()
          Deallocate resources and in the case of a writable log record write data.
 boolean equals(java.lang.Object o)
          Return true if two records are equal, which means that offset, byte array, and CRC all match.
 long getCrc()
          Returns the CRC value.
 byte getCrcType()
          Returns the CRC type.
 byte[] getData()
          Returns the underlying byte buffer.
 long getOffset()
          Returns the offset into the source file of this record.
 long getRecordLength()
          Returns the computed length of this record in the file, including length field, data, and CRC.
 boolean isEmpty()
          Returns true if the record is empty.
 boolean isTruncated()
          Returns true if the record is truncated.
 java.io.InputStream read()
          Returns a stream to read record contents.
 void storeCrc(byte crcType)
          Compute and store CRC.
 java.lang.String toString()
          Print log record as string.
 void verifyChecksum()
          Verifies the CRC value.
 java.io.OutputStream write()
          Returns a stream to write record contents.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

NON_DATA_BYTES

public static final int NON_DATA_BYTES
Number of bytes in length field plus CRC. The record length is this number plus the number of bytes of data (currently 4 + 1 + 8).

See Also:
Constant Field Values

EVENT_REPL

public static final byte EVENT_REPL
Denotes record header information.

See Also:
Constant Field Values

EVENT_ROTATE

public static final byte EVENT_ROTATE
Denotes a replication event

See Also:
Constant Field Values

CRC_TYPE_NONE

public static final byte CRC_TYPE_NONE
Record does not have a CRC computed.

See Also:
Constant Field Values

CRC_TYPE_32

public static final byte CRC_TYPE_32
Record uses conventional CRC-32 computed by Java CRC32 class.

See Also:
Constant Field Values
Constructor Detail

LogRecord

public LogRecord(java.io.File file,
                 long offset,
                 boolean truncated)
Creates an empty record, which is optionally truncated.

Parameters:
file - File to which this record belongs
offset - Offset of this record in the file
truncated - If true this record is truncated rather than merely empty

LogRecord

public LogRecord(java.io.File file,
                 long offset,
                 byte[] bytes,
                 byte crcType,
                 long crc)
Creates a readable record with indicated content.

Parameters:
offset - File offset at which this record was read
bytes - Data in record
crcType - Type of CRC check to use
crc - CRC value
Method Detail

getRecordLength

public long getRecordLength()
Returns the computed length of this record in the file, including length field, data, and CRC.


getOffset

public long getOffset()
Returns the offset into the source file of this record.


getData

public byte[] getData()
Returns the underlying byte buffer. Must call done() when writing before calling this method.


getCrcType

public byte getCrcType()
Returns the CRC type.


getCrc

public long getCrc()
Returns the CRC value.


isTruncated

public boolean isTruncated()
Returns true if the record is truncated.


isEmpty

public boolean isEmpty()
Returns true if the record is empty.


computeCrc

public long computeCrc()
                throws java.io.IOException
Compute and return CRC on data.

Throws:
java.io.IOException

storeCrc

public void storeCrc(byte crcType)
              throws java.io.IOException
Compute and store CRC. This is used to populate CRC in a record to which we are writing.

Throws:
java.io.IOException

checkCrc

public boolean checkCrc()
                 throws java.io.IOException
Computes the CRC value and compares to the CRC stored in the record, returning true only if the CRC values match.

Throws:
java.io.IOException

verifyChecksum

public void verifyChecksum()
                    throws LogConsistencyException
Verifies the CRC value. This generates an exception if the exception is bad.

Throws:
LogConsistencyException - Thrown if checksum verification fails

computeCrc32

public static long computeCrc32(byte[] bytes)
                         throws java.io.IOException
Static routine to compute CRC 32.

Throws:
java.io.IOException

read

public java.io.InputStream read()
Returns a stream to read record contents.


write

public java.io.OutputStream write()
Returns a stream to write record contents.


done

public void done()
Deallocate resources and in the case of a writable log record write data.


toString

public java.lang.String toString()
Print log record as string.

Overrides:
toString in class java.lang.Object

equals

public boolean equals(java.lang.Object o)
Return true if two records are equal, which means that offset, byte array, and CRC all match.

Overrides:
equals in class java.lang.Object