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

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

public class LogIndex
extends java.lang.Object

Implements an in-memory index showing the starting sequence number of each index file. Index operations are fully synchronized to ensure there are no issues due to concurrent access across threads.

Version:
1.0
Author:
Stephane Giron

Constructor Summary
LogIndex(java.io.File logDir, java.lang.String filePrefix, long retentionMillis, int bufferSize, boolean isWritable)
          Creates a new in-memory instance on all log files in a particular directory.
 
Method Summary
 void addNewFile(long seqno, java.lang.String fileName)
          Adds a new file to the index.
 boolean fileNameExists(java.lang.String name)
          Returns true if the name of a log file exists in the index.
 long getActiveSeqno()
          Returns the current active sequence number.
 java.lang.String getFile(long seqno)
          Locates and returns the file that contains a given sequence number.
 java.lang.String[] getFileNames()
          Returns an array containing all file names.
 java.lang.String getFirstFile()
          Returns the first index file or null if no such file exists.
 java.util.List<LogIndexEntry> getIndexCopy()
          Returns a copy of the index entries in sorted order.
 java.lang.String getLastFile()
          Returns the last index file or null if no such file exists.
 long getMaxIndexedSeqno()
          Returns the maximum committed sequence number this index knows about.
 long getMinIndexedSeqno()
          Returns the minimum sequence number this index knows about.
 boolean isEmpty()
          Returns true if the index is empty.
 void release()
          Releases resources in the index.
 void removeFile(java.lang.String fileName)
          Remove a file from the index.
 void setActiveSeqno(long activeSeqno)
          Sets the active sequence number, which is the lowest sequence number known to be in use by clients of this log.
 void setMaxIndexedSeqno(long seqno)
          Sets the maximum sequence number this index knows about by updating the last index entry.
 int size()
          Returns the number of files in the index.
 java.lang.String toString()
          
 void validate(java.io.File logDir)
          Validates the index by ensuring that each file exists and that the log entries have matching start and end dates.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

LogIndex

public LogIndex(java.io.File logDir,
                java.lang.String filePrefix,
                long retentionMillis,
                int bufferSize,
                boolean isWritable)
         throws ReplicatorException,
                java.lang.InterruptedException
Creates a new in-memory instance on all log files in a particular directory.

Parameters:
logDir - Log directory
filePrefix - Prefix for log files
retentionMillis - Amount of time to retain log files before auto-deleting
bufferSize - Buffer size for reading log files
isWritable - True if the log is writable and we can clean up
Throws:
ReplicatorException - Thrown in the event of an error constructing the index
java.lang.InterruptedException
Method Detail

getActiveSeqno

public long getActiveSeqno()
Returns the current active sequence number.


setActiveSeqno

public void setActiveSeqno(long activeSeqno)
Sets the active sequence number, which is the lowest sequence number known to be in use by clients of this log. Log files can only be deleted if they are before this sequence number.


isEmpty

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


size

public int size()
Returns the number of files in the index.


release

public void release()
Releases resources in the index.


toString

public java.lang.String toString()

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

setMaxIndexedSeqno

public void setMaxIndexedSeqno(long seqno)
Sets the maximum sequence number this index knows about by updating the last index entry. This routine takes care of patching up the first index entry in a new log, which starts with the default value -1 until we find the correct sequence number.


getMaxIndexedSeqno

public long getMaxIndexedSeqno()
Returns the maximum committed sequence number this index knows about. If the currently open file does not have anything written to it, return the end value from the previous entry.

Returns:
-1 if index is empty, otherwise, return the max value

getMinIndexedSeqno

public long getMinIndexedSeqno()
Returns the minimum sequence number this index knows about. This number might not be committed if we are just writing a new log.


getFile

public java.lang.String getFile(long seqno)
Locates and returns the file that contains a given sequence number. The implementation uses a linear search, so we assume this is a comparatively rare operation on connection startup.


fileNameExists

public boolean fileNameExists(java.lang.String name)
Returns true if the name of a log file exists in the index.


getIndexCopy

public java.util.List<LogIndexEntry> getIndexCopy()
Returns a copy of the index entries in sorted order.


getFirstFile

public java.lang.String getFirstFile()
Returns the first index file or null if no such file exists.


getLastFile

public java.lang.String getLastFile()
Returns the last index file or null if no such file exists.


getFileNames

public java.lang.String[] getFileNames()
Returns an array containing all file names.


addNewFile

public void addNewFile(long seqno,
                       java.lang.String fileName)
Adds a new file to the index.

Parameters:
seqno - Starting sequence number in the file
fileName - Name of the log file

removeFile

public void removeFile(java.lang.String fileName)
Remove a file from the index.


validate

public void validate(java.io.File logDir)
              throws LogConsistencyException
Validates the index by ensuring that each file exists and that the log entries have matching start and end dates.

Throws:
LogConsistencyException