org.apache.activemq.store.kahadb.disk.page
Class Transaction

java.lang.Object
  extended by org.apache.activemq.store.kahadb.disk.page.Transaction
All Implemented Interfaces:
Iterable<Page>

public class Transaction
extends Object
implements Iterable<Page>

The class used to read/update a PageFile object. Using a transaction allows you to do multiple update operations in a single unit of work.


Nested Class Summary
static interface Transaction.CallableClosure<R,T extends Throwable>
          This closure interface is intended for the end user implement callbacks for the Transaction.exectue() method.
static interface Transaction.Closure<T extends Throwable>
          This closure interface is intended for the end user implement callbacks for the Transaction.exectue() method.
 class Transaction.InvalidPageIOException
          The InvalidPageIOException is thrown if try to load/store a a page with an invalid page id.
 class Transaction.PageOverflowIOException
          The PageOverflowIOException occurs when a page write is requested and it's data is larger than what would fit into a single page.
 
Method Summary
<T> Page<T>
allocate()
          Allocates a free page that you can write data to.
<T> Page<T>
allocate(int count)
          Allocates a block of free pages that you can write data to.
 void commit()
          Commits the transaction to the PageFile as a single 'Unit of Work'.
<R,T extends Throwable>
R
execute(Transaction.CallableClosure<R,T> closure)
          Executes a closure and if it does not throw any exceptions, then it commits the transaction.
<T extends Throwable>
void
execute(Transaction.Closure<T> closure)
          Executes a closure and if it does not throw any exceptions, then it commits the transaction.
 void free(long pageId)
          Frees up a previously allocated page so that it can be re-allocated again.
 void free(long pageId, int count)
          Frees up a previously allocated sequence of pages so that it can be re-allocated again.
<T> void
free(Page<T> page)
          Frees up a previously allocated page so that it can be re-allocated again.
<T> void
free(Page<T> page, int count)
          Frees up a previously allocated sequence of pages so that it can be re-allocated again.
 PageFile getPageFile()
           
protected  File getTempFile()
           
 boolean isReadOnly()
           
 Iterator<Page> iterator()
          Allows you to iterate through all active Pages in this object.
 Iterator<Page> iterator(boolean includeFreePages)
          Allows you to iterate through all active Pages in this object.
<T> Page<T>
load(long pageId, Marshaller<T> marshaller)
          Loads a page from disk.
<T> void
load(Page<T> page, Marshaller<T> marshaller)
          Loads a page from disk.
 InputStream openInputStream(Page p)
           
 OutputStream openOutputStream(Page page, boolean overflow)
           
 void rollback()
          Rolls back the transaction.
<T> void
store(Page<T> page, Marshaller<T> marshaller, boolean overflow)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

getPageFile

public PageFile getPageFile()
Returns:
the page file that created this Transaction

allocate

public <T> Page<T> allocate()
                 throws IOException
Allocates a free page that you can write data to.

Returns:
a newly allocated page.
Throws:
Transaction.PageOverflowIOException - If an disk error occurred.
IllegalStateException - if the PageFile is not loaded
IOException

allocate

public <T> Page<T> allocate(int count)
                 throws IOException
Allocates a block of free pages that you can write data to.

Parameters:
count - the number of sequential pages to allocate
Returns:
the first page of the sequential set.
Throws:
Transaction.PageOverflowIOException - If an disk error occurred.
IllegalStateException - if the PageFile is not loaded
IOException

free

public void free(long pageId)
          throws IOException
Frees up a previously allocated page so that it can be re-allocated again.

Parameters:
pageId - the page to free up
Throws:
Transaction.PageOverflowIOException - If an disk error occurred.
IllegalStateException - if the PageFile is not loaded
IOException

free

public void free(long pageId,
                 int count)
          throws IOException
Frees up a previously allocated sequence of pages so that it can be re-allocated again.

Parameters:
pageId - the initial page of the sequence that will be getting freed
count - the number of pages in the sequence
Throws:
Transaction.PageOverflowIOException - If an disk error occurred.
IllegalStateException - if the PageFile is not loaded
IOException

free

public <T> void free(Page<T> page,
                     int count)
          throws IOException
Frees up a previously allocated sequence of pages so that it can be re-allocated again.

Parameters:
page - the initial page of the sequence that will be getting freed
count - the number of pages in the sequence
Throws:
Transaction.PageOverflowIOException - If an disk error occurred.
IllegalStateException - if the PageFile is not loaded
IOException

free

public <T> void free(Page<T> page)
          throws IOException
Frees up a previously allocated page so that it can be re-allocated again.

Parameters:
page - the page to free up
Throws:
Transaction.PageOverflowIOException - If an disk error occurred.
IllegalStateException - if the PageFile is not loaded
IOException

store

public <T> void store(Page<T> page,
                      Marshaller<T> marshaller,
                      boolean overflow)
           throws IOException
Parameters:
page - the page to write. The Page object must be fully populated with a valid pageId, type, and data.
marshaller - the marshaler to use to load the data portion of the Page, may be null if you do not wish to write the data.
overflow - If true, then if the page data marshalls to a bigger size than can fit in one page, then additional overflow pages are automatically allocated and chained to this page to store all the data. If false, and the overflow condition would occur, then the PageOverflowIOException is thrown.
Throws:
Transaction.PageOverflowIOException - If an disk error occurred.
Transaction.PageOverflowIOException - If the page data marshalls to size larger than maximum page size and overflow was false.
IllegalStateException - if the PageFile is not loaded
IOException

openOutputStream

public OutputStream openOutputStream(Page page,
                                     boolean overflow)
                              throws IOException
Throws:
Transaction.PageOverflowIOException
IOException

load

public <T> Page<T> load(long pageId,
                        Marshaller<T> marshaller)
             throws IOException
Loads a page from disk.

Parameters:
pageId - the id of the page to load
marshaller - the marshaler to use to load the data portion of the Page, may be null if you do not wish to load the data.
Returns:
The page with the given id
Throws:
Transaction.PageOverflowIOException - If an disk error occurred.
IllegalStateException - if the PageFile is not loaded
IOException

load

public <T> void load(Page<T> page,
                     Marshaller<T> marshaller)
          throws IOException
Loads a page from disk.

Parameters:
page - - The pageId field must be properly set
marshaller - the marshaler to use to load the data portion of the Page, may be null if you do not wish to load the data.
Throws:
Transaction.PageOverflowIOException - If an disk error occurred.
Transaction.InvalidPageIOException - If the page is is not valid.
IllegalStateException - if the PageFile is not loaded
IOException

openInputStream

public InputStream openInputStream(Page p)
                            throws IOException
Throws:
IOException
See Also:
load(org.apache.activemq.store.kahadb.disk.page.Page, org.apache.activemq.store.kahadb.disk.util.Marshaller)

iterator

public Iterator<Page> iterator()
Allows you to iterate through all active Pages in this object. Pages with type Page.FREE_TYPE are not included in this iteration. Pages removed with Iterator.remove() will not actually get removed until the transaction commits.

Specified by:
iterator in interface Iterable<Page>
Throws:
IllegalStateException - if the PageFile is not loaded

iterator

public Iterator<Page> iterator(boolean includeFreePages)
Allows you to iterate through all active Pages in this object. You can optionally include free pages in the pages iterated.

Parameters:
includeFreePages - - if true, free pages are included in the iteration
Throws:
IllegalStateException - if the PageFile is not loaded

commit

public void commit()
            throws IOException
Commits the transaction to the PageFile as a single 'Unit of Work'. Either all page updates associated with the transaction are written to disk or none will.

Throws:
IOException

rollback

public void rollback()
              throws IOException
Rolls back the transaction.

Throws:
IOException

getTempFile

protected File getTempFile()

isReadOnly

public boolean isReadOnly()
Returns:
true if there are no uncommitted page file updates associated with this transaction.

execute

public <T extends Throwable> void execute(Transaction.Closure<T> closure)
             throws T extends Throwable,
                    IOException
Executes a closure and if it does not throw any exceptions, then it commits the transaction. If the closure throws an Exception, then the transaction is rolled back.

Type Parameters:
T -
Parameters:
closure - - the work to get exectued.
Throws:
T - if the closure throws it
Transaction.PageOverflowIOException - If the commit fails.
T extends Throwable
IOException

execute

public <R,T extends Throwable> R execute(Transaction.CallableClosure<R,T> closure)
          throws T extends Throwable,
                 IOException
Executes a closure and if it does not throw any exceptions, then it commits the transaction. If the closure throws an Exception, then the transaction is rolled back.

Type Parameters:
T -
Parameters:
closure - - the work to get exectued.
Throws:
T - if the closure throws it
Transaction.PageOverflowIOException - If the commit fails.
T extends Throwable
IOException


Copyright © 2005–2013 The Apache Software Foundation. All rights reserved.