Class LogFile


  • class LogFile
    extends java.lang.Object
    An individual file within a set of log files managed by a Logger.

    The Logger will create an instance of LogFile for each physical file that is configured for the Logger.

    • Field Summary

      Fields 
      Modifier and Type Field Description
      (package private) long bytesWritten
      total number of data written.
      (package private) java.nio.channels.FileChannel channel
      FileChannel associated with this LogFile.
      (package private) java.io.File file  
      (package private) java.lang.String fileMode  
      (package private) int firstBSN
      BSN of first block in the file.
      (package private) long highMark
      log key for the first record in the next file.
      (package private) java.nio.channels.FileLock lock
      FileLock acquired when file is opened.
      (package private) boolean newFile
      indicates the file was created during the call to open()
      (package private) long position
      FileChannel.position() of last read or write.
      (package private) int rewindCounter
      number of times this file position was reset to zero.
      (package private) long tod
      currentTimeMillis when LogFileManager switched to this LogFile.
    • Constructor Summary

      Constructors 
      Constructor Description
      LogFile​(java.io.File file)
      construct an instance of LogFile for a given file name
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      (package private) LogFile close()
      Close the channel associated with this LogFile.
      (package private) void force​(boolean forceMetadata)
      Helper provides access to the FileChannel.force() method for the FileChannel associated with this LogFile.
      (package private) java.lang.String getStats()
      return statistics for this LogFile as an XML string.
      (package private) LogFile open​(java.lang.String fileMode)
      open the file and get the associated nio FileChannel for the file.
      (package private) void write​(LogBuffer lb)
      Helper provides access to the FileChannel.write() method for the FileChannel associated with this LogFile.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • file

        java.io.File file
      • fileMode

        java.lang.String fileMode
        See Also:
        RandomAccessFile(java.lang.String, java.lang.String)
      • channel

        java.nio.channels.FileChannel channel
        FileChannel associated with this LogFile.

        The FileChannel is private to guarantee that all calls to the channel methods come through this LogFile object to allow for statistics collection.

      • rewindCounter

        int rewindCounter
        number of times this file position was reset to zero.
      • bytesWritten

        long bytesWritten
        total number of data written.
      • highMark

        long highMark
        log key for the first record in the next file.

        when a file switch occurs, the LogFileManager stores the mark for the file header record of the next log file into this LogFile object. Effectively, any mark with a value less than the header record for the next log file resides in this or some previous log file.

        Later, when this LogFile object is about to be reused, the value for the active mark is compared with the highMark value. If the active mark is less than highMark, then a LogFileOverflowException is thrown to prevent re-use of a log file that contains active data.

        While this LogFile is the current LogFile of the set, (ie, the LogFile that is currently being written to) the highMark will be set to first record of the subsequent block. For example, while block 1 is being written, highMark will be set to the first record of block 2.

        During replay operations, the end of the active journal can be detected by comparing the BSN of a desired block with the current highMark. If the requested BSN is less than highMark, then the requested block resides within active journal space. If the requested BSN is >= the highMark, then the BSN is invalid.

        Any attempt to add records to the log will cause an exception until Logger.mark() is called to clear prior records from the log.

      • firstBSN

        int firstBSN
        BSN of first block in the file.

        Initialized by LogFileManager and updated as log files are reused.

        Used by LogFileManager.read() to calculate offset into a file to read a specific block.

      • tod

        long tod
        currentTimeMillis when LogFileManager switched to this LogFile.
      • position

        long position
        FileChannel.position() of last read or write.

        May be used to report the file position when IOException occurs.

      • newFile

        boolean newFile
        indicates the file was created during the call to open()
        See Also:
        open(String filemode)
      • lock

        java.nio.channels.FileLock lock
        FileLock acquired when file is opened.
    • Constructor Detail

      • LogFile

        LogFile​(java.io.File file)
        construct an instance of LogFile for a given file name
        Parameters:
        file - filename
    • Method Detail

      • open

        LogFile open​(java.lang.String fileMode)
              throws LogConfigurationException,
                     java.io.FileNotFoundException
        open the file and get the associated nio FileChannel for the file.

        If the file does not exist, then the newFile member is set true.

        Parameters:
        fileMode - value passed to RandomAccessFile constructor.
        Throws:
        java.io.FileNotFoundException - if the parent directory structure does not exist.
        LogConfigurationException
        See Also:
        RandomAccessFile(java.lang.String, java.lang.String)
      • close

        LogFile close()
               throws java.io.IOException
        Close the channel associated with this LogFile.

        Also releases the lock that is held on the file.

        Returns:
        this LogFile
        Throws:
        java.io.IOException
      • write

        void write​(LogBuffer lb)
            throws java.io.IOException
        Helper provides access to the FileChannel.write() method for the FileChannel associated with this LogFile.
        Parameters:
        lb - Reference to a LogBuffer object that is to be written.
        Throws:
        java.io.IOException
      • force

        void force​(boolean forceMetadata)
            throws java.io.IOException
        Helper provides access to the FileChannel.force() method for the FileChannel associated with this LogFile.

        Hides actual FileChannel and allows capture of statistics.

        In theory the force could be eliminated if the file is open with mode "rwd" or "rws" because the access method is supposed to guarantee that the writes do not return until the data is on the media. Unfortunately, testing with Windows XP platforms suggests that system write cache may confuse the Java runtime and the program will actually return before data is on media. Consequently, this method *always* does a force() regardless of the file open mode.

        Parameters:
        forceMetadata - as defined by FileChannel.force()
        Throws:
        java.io.IOException
        See Also:
        FileChannel.force(boolean)
      • getStats

        java.lang.String getStats()
        return statistics for this LogFile as an XML string.
        Returns:
        XML string containing LogFile statistics.