Kahadb – Transaction Store


Storing an Object

In the code below, our purpose is to store a Book object to the page file.

Transaction tx = pageFile.tx();
Page page = tx.allocate();
page.set(createBook(name, i));
tx.store(page, bookMarshaller, true);

We first create a transaction and then allocate a page. Next, we create a Book object and then set it to the page. At this point, we don’t know whether one page is enough or it needs more than one to store the Book object.

We also need a marshaller object that knows how to marshall and un-marshall the object into bytes. We pass the Book marshaller along with the page, as we call the store API to store the Book object. Once it is marshaled into a byte array, it is then written into one or more pages depending upon its size.


So a Book object may result into one or more page updates. These updates sit in the transaction cache. Only when a commit is issued, all the page updates are pushed to PageFile’s cache. The writer thread in the PageFile will poll the page updates to write them to the disk.

There are many subtle variations to the above algorithm to improve the performance.

To support writing very large data, that is, when data that exceeds a page, the page updates are added to the queue as the data is being marshaled.  After writing each attribute of the object, the size of the buffer is checked. If it has exceeded the page size, the page is updated with data of page size, added to the queue and a new page is allocated to accommodate the rest.

If the remaining data is still too large for a page, another page will be allocated and the process continues till all the data in buffer is written to the pages.

If the total size of page updates within a transaction exceeds the allowed size, the data is written to a temporary file owned by the transaction.

Any new page updates will be appended to the temporary file.


Leave A Reply