Transaction

0

Page Allocation 

In order to read/update a PageFile object, we need a Transaction Object. Using a transaction we can do multiple update operations in a single unit of work. Transaction would need a block of free pages to write data to. Transaction.allocate(count) allocates a block of free pages that one can write data to.

Transaction
Transaction

Load

Transaction looks into its writes cache to see if a page write already exists. If yes, it will make a copy of the page and will use it, else it will request the PageFile object to look into its cache. PageFile has its own writes cache. If it finds the page there it will return it else will look into its page cache.
Transaction
Transaction

Write

Page header is skipped as it will be written later. A page header is of 21 bytes. It is made up of
  1. Page type
  2. Transaction ID
  3. Next page (in case of overflow)
  4. and checksum
There are three page types – Part, End and Free.
After each write to the buffer, it is checked whether the page size allocated has been exceeded.
If the data in buffer exceeds the page size, then the exceeded data is written to a new page. If the page is still not big enough for the data, then another page is allocated. An overflow may result in a link of pages to accommodate the data. If a page is linked to another page, then the page type is “Part”. If a page is free for  allocation it is of type “Free”. A page which is contains the last part of the data is of type “End”. Each page type is assigned an integer value to know its type. 
If the data spans to more than one page, the “next” attribute will know which page ID to link to.

Transaction
Transaction

If the write content is more than the page size, the data spans to more than one page. After each page write, the page is queued up and then prepares itself to write to the next page. The page header is skipped initially as the page type is known only later, when the data is marshalled. If the data exceeds the page size, a new page is allocated to write the remaining data. The current page becomes ‘Part’ page type with the next page ID in its header. The data is copied to a page write and queued up. If the data exceeds the transaction size, it will be written to a temp file and then the page write will be queued up. Once the current page is written, the buffer will be re-used for the next page. The header is skipped. The remaining data which we skipped will be written to the  buffer. If the buffer still exceeds the page size, a new page will be allocated and the same process will be followed again.

Transaction
Transaction

After we call commit, the ‘page writes’ buffer would be written to PageFile’s batch. Once the PageFile’s buffer reaches batch size, the entire batch of writes will be written to the disk.

Home

Share.

Leave A Reply