simpledb
Class LogFile

java.lang.Object
  extended by simpledb.LogFile

public class LogFile
extends java.lang.Object

LogFile implements the recovery subsystem of SimpleDb. This class is able to write different log records as needed, but it is the responsibility of the caller to ensure that write ahead logging and two-phase locking discipline are followed.

Locking note:

Many of the methods here are synchronized (to prevent concurrent log writes from happening); many of the methods in BufferPool are also synchronized (for similar reasons.) Problem is that BufferPool writes log records (on page flushed) and the log file flushes BufferPool pages (on checkpoints and recovery.) This can lead to deadlock. For that reason, any LogFile operation that needs to access the BufferPool must not be declared synchronized and must begin with a block like:

    synchronized (Database.getBufferPool()) {
       synchronized (this) {

       ..

       }
    }


Field Summary
(package private) static int ABORT_RECORD
           
(package private) static int BEGIN_RECORD
           
(package private) static int CHECKPOINT_RECORD
           
(package private) static int COMMIT_RECORD
           
(package private) static int INT_SIZE
           
(package private)  java.io.File logFile
           
(package private) static int LONG_SIZE
           
(package private) static long NO_CHECKPOINT_ID
           
(package private)  java.io.RandomAccessFile raf
           
(package private) static int UPDATE_RECORD
           
 
Constructor Summary
LogFile(java.io.File f, boolean recover)
          Constructor.
 
Method Summary
 void force()
           
 void logAbort(TransactionId tid)
          Write an abort record to the log for the specified tid, force the log to disk, and perform a rollback
 void logCheckpoint()
          Checkpoint the log and write a checkpoint record.
 void logCommit(TransactionId tid)
          Write a commit record to disk for the specified tid, and force the log to disk.
 void logTruncate()
          Truncate any unneeded portion of the log to reduce its space consumption
 void logWrite(TransactionId tid, Page before, Page after)
          Write an UPDATE record to disk for the specified tid and page (with provided before and after images.)
 void logXactionBegin(TransactionId tid)
          Write a BEGIN record for the specified transaction
 void print()
          Print out a human readable represenation of the log
 void recover()
          Recover the database system by ensuring that the updates of committed transactions are installed and that the updates of uncommitted transactions are not installed.
 void rollback(TransactionId tid)
          Rollback the specified transaction, setting the state of any of pages it updated to their pre-updated state.
 void shutdown()
          Shutdown the logging system, writing out whatever state is necessary so that start up can happen quickly (without extensive recovery.)
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

logFile

java.io.File logFile

raf

java.io.RandomAccessFile raf

ABORT_RECORD

static final int ABORT_RECORD
See Also:
Constant Field Values

COMMIT_RECORD

static final int COMMIT_RECORD
See Also:
Constant Field Values

UPDATE_RECORD

static final int UPDATE_RECORD
See Also:
Constant Field Values

BEGIN_RECORD

static final int BEGIN_RECORD
See Also:
Constant Field Values

CHECKPOINT_RECORD

static final int CHECKPOINT_RECORD
See Also:
Constant Field Values

NO_CHECKPOINT_ID

static final long NO_CHECKPOINT_ID
See Also:
Constant Field Values

INT_SIZE

static int INT_SIZE

LONG_SIZE

static int LONG_SIZE
Constructor Detail

LogFile

public LogFile(java.io.File f,
               boolean recover)
        throws java.io.IOException
Constructor. Initialize and back the log file with the specified file. If recover is true, initiate recovery after loading the log file (requires that the system catalog and buffer pool be fully initialized.

Parameters:
f - The log file
recover - True if recovery should be initiated immediately after loading.
Throws:
java.io.IOException
Method Detail

logAbort

public void logAbort(TransactionId tid)
              throws java.io.IOException
Write an abort record to the log for the specified tid, force the log to disk, and perform a rollback

Parameters:
tid - The aborting transaction.
Throws:
java.io.IOException

logCommit

public void logCommit(TransactionId tid)
               throws java.io.IOException
Write a commit record to disk for the specified tid, and force the log to disk.

Parameters:
tid - The committing transaction.
Throws:
java.io.IOException

logWrite

public void logWrite(TransactionId tid,
                     Page before,
                     Page after)
              throws java.io.IOException
Write an UPDATE record to disk for the specified tid and page (with provided before and after images.)

Parameters:
tid - The transaction performing the write
before - The before image of the page
after - The after image of the page
Throws:
java.io.IOException
See Also:
Page.getBeforeImage()

logXactionBegin

public void logXactionBegin(TransactionId tid)
                     throws java.io.IOException
Write a BEGIN record for the specified transaction

Parameters:
tid - The transaction that is beginning
Throws:
java.io.IOException

logCheckpoint

public void logCheckpoint()
                   throws java.io.IOException
Checkpoint the log and write a checkpoint record.

Throws:
java.io.IOException

logTruncate

public void logTruncate()
                 throws java.io.IOException
Truncate any unneeded portion of the log to reduce its space consumption

Throws:
java.io.IOException

rollback

public void rollback(TransactionId tid)
              throws java.util.NoSuchElementException,
                     java.io.IOException
Rollback the specified transaction, setting the state of any of pages it updated to their pre-updated state. To preserve transaction semantics, this should not be called on transactions that have already committed (though this may not be enforced by this method.)

Parameters:
tid - The transaction to rollback
Throws:
java.util.NoSuchElementException
java.io.IOException

shutdown

public void shutdown()
Shutdown the logging system, writing out whatever state is necessary so that start up can happen quickly (without extensive recovery.)


recover

public void recover()
             throws java.io.IOException
Recover the database system by ensuring that the updates of committed transactions are installed and that the updates of uncommitted transactions are not installed.

Throws:
java.io.IOException

print

public void print()
           throws java.io.IOException
Print out a human readable represenation of the log

Throws:
java.io.IOException

force

public void force()
           throws java.io.IOException
Throws:
java.io.IOException