FileDocCategorySizeDatePackage
LogFile.javaAPI DocGlassfish v2 API12199Fri May 04 22:36:38 BST 2007com.sun.jts.CosTransactions

LogFile

public class LogFile extends Object
The LogFile interface provides operations that control the individual log entries that make up the physical log. It allows writing to the log and reading from the log, along with the capability to close a portion of the log. Different physical logs can be placed on the system with only minor changes to the methods contained in this class.
version
0.01
author
Simon Holdsworth, IBM Corporation
see

Fields Summary
static final int
UNFORCED
Constants for write types.
static final int
FORCED
static final int
NORMAL
Constants for log record types.
static final int
KEYPOINT_START
static final int
KEYPOINT_END
static final int
REWRITE
static Logger
_logger
LogHandle
handle
The handle of the log file.
Constructors Summary
LogFile(LogHandle handle)
LogFile constructor.

param
LogHandle
return
see


              
        

        // Set up the instance variables to those values passed in.

        this.handle = handle;

    
Methods Summary
synchronized booleancheckpoint(LogLSN firstLSN)
Informs the log that all log records older than the one with the given LSN are no longer required.

The checkpoint marks the point where log processing will begin in the event of recovery processing. This will generally correspond to the last record before a successful keypoint.

param
firstLSN
return
see


        boolean result = true;
        LogLSN checkLSN;

        // If the LSN passed in is NULL, assume it means the head.

        if( firstLSN.isNULL() )
            checkLSN = new LogLSN(LogLSN.HEAD_LSN);
        else
            checkLSN = new LogLSN(firstLSN);

        // Checkpoint the log.

        try {
            handle.checkLSN(checkLSN);
            handle.truncate(checkLSN,LogHandle.TAIL_NOT_INCLUSIVE);
        } catch( LogException le ) {
            result = false;
        }

        return result;
    
synchronized booleanclose(boolean deleteFile)
Closes the portion of the log defined by the LogFile object reference.

Deletes the associated logfile if requested.

param
deleteFile
return
see


        boolean result = true;

        // Call to close the physical log.

        try {
            handle.closeFile(deleteFile);
        } catch( LogException le ) {
            result = false;
        }

        return result;
    
voiddump()
Dumps the state of the object.

param
return
see

        //! somtrDump_OBJECT_HEADER;

        // Dump all of the instance variables in the LogFile object, without going
        // any further down object references.

    
synchronized java.util.VectorgetLogRecords()
Returns all of the log records written to the log since the last checkpoint.

The caller is responsible for freeing the sequence storage.

If the log is empty, an empty sequence is returned.

The result is returned in a Vector as we do not know ahead of time how many log records there are.

param
return
The log records.
see

        Vector logRecords = new Vector();
        boolean keypointEndFound = false;
        LogCursor logCursor;

        // Open a cursor for use with the log.

        try {
            logCursor = handle.openCursor(LogLSN.HEAD_LSN,LogLSN.TAIL_LSN);
        } catch( LogException le ) {

            return new Vector();
        }

        // Read each log record from the physical log and place in temporary queue.

        try {
            LogLSN lsn = new LogLSN();
            int[] recordType = new int[1];

            for(;;) {
                byte[] logRecord = logCursor.readCursor(recordType,lsn);

                // Process the log record depending on its type.

                switch( recordType[0] ) {

                    // If the record is a keypoint start, and we have found the end of the
                    // keypoint, then we can stop processing the log.  If the end has not been
                    // found then a failure must have occurred during the keypoint operation,
                    // so we must continue to process the log.
                    // We do not do anything with the contents of the keypoint start record.

                case LogFile.KEYPOINT_START :
                    if( keypointEndFound )
                        throw new LogException(null,LogException.LOG_END_OF_CURSOR,2);
                    break;

                    // If the record is a keypoint end, remember this so that we can stop when
                    // we find the start of the keypoint.
                    // We do not do anything with the contents of the keypoint end record.

                case LogFile.KEYPOINT_END :
                    keypointEndFound = true;
                    break;

                    // For a normal log record, add the records to the list.
                    // For a rewritten record, only add the record to the list if the
                    // keypoint end record has been found.

                case LogFile.NORMAL :
                case LogFile.REWRITE :
                    if( (recordType[0] == LogFile.NORMAL) || keypointEndFound )
                        logRecords.addElement(logRecord);
                    break;

                    // Any other type of log record is ignored.

                default :
                    break;
                }
            }
        } catch( LogException le ) {

            // If any exception other that END_OF_CURSOR was thrown, then return an empty
            // list.

            if( le.errorCode != LogException.LOG_END_OF_CURSOR ) {
                return new Vector();
            }
        }

        // Close the cursor.

        try {
            handle.closeCursor(logCursor); 
        } catch( LogException le ) {
        }

        return logRecords;
    
synchronized byte[]readRestart()
Reads the restart record from the log.

param
return
The restart record.
see

        byte[] result = null;

        // Write the restart information.

        try {
            result = handle.readRestart();
        } catch( LogException le ) {
        }

        return result;
    
synchronized booleanwrite(int writeType, byte[] record, int recordType, LogLSN recordLSN)
Writes a log record to the physical log.

Supports either a force or unforced option with force requiring an immediate write to the log and unforced keeping the data until a force is done somewhere else in the log service.

The LSN of the written log record is an output parameter.

Returns true if the write completed successfully and false if the write did not complete.

param
writeType Forced/non-forced write indicator.
param
record Log record data.
param
recordType Log record type.
param
recordLSN LSN of the written record.
return
see


        boolean result = true;

        // Write the record.
        // Set the result based on return code from log write.

        try {
            LogLSN resultLSN = handle.writeRecord(record,recordType,
                                                  (writeType==LogFile.FORCED ? LogHandle.FORCE : LogHandle.BUFFER));
            if( recordLSN != null )
                recordLSN.copy(resultLSN);
        } catch( LogException le ) {
			_logger.log(Level.SEVERE,"jts.log_error",le.toString());
			 String msg = LogFormatter.getLocalizedMessage(_logger,"jts.log_error",
			 							new java.lang.Object[] {le.toString()});
			throw  new org.omg.CORBA.INTERNAL(msg);
            //if( recordLSN != null )
            //recordLSN.copy(LogLSN.NULL_LSN);
            //result = false;
        }

        return result;
    
synchronized booleanwriteRestart(byte[] record)
Writes the given information in the restart record for the log.

param
record The information to be written.
return
Indicates success of the operation.
see


        boolean result = false;

        // Write the restart information.

        try {
            handle.writeRestart(record);
            result = true;
        } catch( LogException le ) {
            result = false;
        }

        return result;