public class IndexPageCache
extends java.lang.Object
| Modifier and Type | Class and Description |
|---|---|
private static class |
IndexPageCache.CacheDataPage
IndexPageCache implementation of an Index
IndexData.DataPage. |
private static class |
IndexPageCache.DataPageExtra
Keeps track of the extra info for an index page.
|
private class |
IndexPageCache.DataPageMain
Keeps track of the main info for an index page.
|
private static class |
IndexPageCache.EntryListView
A view of an index page's entries which combines the normal entries and
tail entry into one collection.
|
private static class |
IndexPageCache.UpdateType |
| Modifier and Type | Field and Description |
|---|---|
private java.util.Map<java.lang.Integer,IndexPageCache.DataPageMain> |
_dataPages
the currently loaded pages for this index, pageNumber -> page
|
private IndexData |
_indexData
the index whose pages this cache is managing
|
private java.util.List<IndexPageCache.CacheDataPage> |
_modifiedPages
the currently modified index pages
|
private IndexPageCache.DataPageMain |
_rootPage
the root page for the index
|
private static int |
MAX_CACHE_SIZE
max number of pages to cache (unless a write operation is in
progress)
|
| Constructor and Description |
|---|
IndexPageCache(IndexData indexData) |
| Modifier and Type | Method and Description |
|---|---|
private void |
addEntry(IndexPageCache.CacheDataPage cacheDataPage,
int entryIdx,
IndexData.Entry newEntry)
Adds the entry to the given page at the given index.
|
private void |
addParentEntry(IndexPageCache.CacheDataPage parentDataPage,
IndexPageCache.CacheDataPage childDataPage)
Adds an entry for the given child page to the given parent page.
|
private void |
addToPeersBefore(IndexPageCache.CacheDataPage newDataPage,
IndexPageCache.CacheDataPage origDataPage)
Inserts the new page as a peer between the given original page and any
previous peer page.
|
private IndexPageCache.CacheDataPage |
allocateNewCacheDataPage(java.lang.Integer parentPageNumber,
boolean isLeaf)
Allocates a new index page with the given parent page and type.
|
private java.util.List<java.lang.Object> |
collectPages(java.util.List<java.lang.Object> pages,
IndexPageCache.DataPageMain dpMain)
Collects all the cache pages in the cache.
|
private void |
deleteDataPage(IndexPageCache.CacheDataPage cacheDataPage)
Deletes the given index page from the file (clears the page).
|
private void |
demoteTail(IndexPageCache.CacheDataPage cacheDataPage)
Makes the tail entry of the given page a normal entry on that page, done
when there is only one entry left on a page, and it is the tail.
|
IndexPageCache.CacheDataPage |
findCacheDataPage(IndexData.Entry e)
Finds the index page on which the given entry does or should reside.
|
private static byte[] |
findCommonPrefix(IndexData.Entry e1,
IndexData.Entry e2)
Finds the valid entry prefix given the first/last entries on an index
page.
|
IndexPageCache.CacheDataPage |
getCacheDataPage(java.lang.Integer pageNumber)
Returns a CacheDataPage for the given page number, may be
null if
the given page number is invalid. |
private IndexPageCache.DataPageMain |
getDataPage(java.lang.Integer pageNumber)
Returns a DataPageMain for the given page number, may be
null if
the given page number is invalid. |
IndexData |
getIndexData() |
PageChannel |
getPageChannel() |
private void |
handleEmptyPages()
Handles any modified pages which are empty as the first pass during a
write() call. |
private IndexPageCache.CacheDataPage |
nestRootDataPage(IndexPageCache.CacheDataPage rootDataPage)
Copies the current root page info into a new page and nests this page
under the root page.
|
private void |
preparePagesForWriting()
Prepares any non-empty modified pages for writing as the second pass
during a
write() call. |
private void |
promoteTail(IndexPageCache.CacheDataPage cacheDataPage)
Makes the last normal entry of the given page the tail entry on that
page, done when there are multiple entries on a page and no tail entry.
|
private void |
purgeOldPages()
Trims the size of the _dataPages cache appropriately (assuming caller has
already verified that the cache needs trimming).
|
private IndexPageCache.CacheDataPage |
readDataPage(java.lang.Integer pageNumber)
Reads the given index page from the file.
|
private void |
removeDataPage(IndexPageCache.CacheDataPage parentDataPage,
IndexPageCache.CacheDataPage cacheDataPage,
IndexData.Entry oldLastEntry)
Removes an index page which has become empty.
|
private IndexData.Entry |
removeEntry(IndexPageCache.CacheDataPage cacheDataPage,
int entryIdx)
Removes the entry with the given index from the given page.
|
private void |
removeFromPeers(IndexPageCache.CacheDataPage cacheDataPage)
Removes a now empty index page from its next and previous peers.
|
private void |
reparentChildren(IndexPageCache.CacheDataPage cacheDataPage)
Sets the parent info for the children of the given page to the given
page.
|
private void |
replaceParentEntry(IndexPageCache.CacheDataPage parentDataPage,
IndexPageCache.CacheDataPage childDataPage,
IndexData.Entry oldEntry)
Replaces the entry for the given child page in the given parent page.
|
private void |
separateFromNextPeer(IndexPageCache.CacheDataPage cacheDataPage)
Separates the given index page from any next peer page.
|
private void |
setModified(IndexPageCache.CacheDataPage cacheDataPage)
Marks the given index page as modified and saves it for writing, if
necessary (if the page is already marked, does nothing).
|
void |
setRootPageNumber(int pageNumber)
Sets the root page for this index, must be called before normal usage.
|
private void |
splitDataPage(IndexPageCache.CacheDataPage origDataPage)
Splits an index page which has too many entries on it.
|
java.lang.String |
toString() |
private IndexData.Entry |
updateEntry(IndexPageCache.CacheDataPage cacheDataPage,
int entryIdx,
IndexData.Entry newEntry,
IndexPageCache.UpdateType upType)
Updates the entries on the given page according to the given updateType.
|
private void |
updateParentEntry(IndexPageCache.CacheDataPage parentDataPage,
IndexPageCache.CacheDataPage childDataPage,
IndexData.Entry oldEntry,
IndexData.Entry newEntry,
IndexPageCache.UpdateType upType)
Updates the entry for the given child page in the given parent page
according to the given updateType.
|
private void |
updateParentTail(IndexPageCache.CacheDataPage parentDataPage,
IndexPageCache.CacheDataPage childDataPage,
IndexPageCache.UpdateType upType)
Updates the child tail info in the given parent page according to the
given updateType.
|
(package private) void |
validate()
Used by unit tests to validate the internal status of the index.
|
private void |
validateChildren(IndexPageCache.DataPageMain dpMain,
IndexPageCache.DataPageExtra dpExtra)
Validates the children for an index page
|
private void |
validateEntries(IndexPageCache.DataPageExtra dpExtra)
Validates the entries for an index page
|
private void |
validateEntryForPage(IndexPageCache.DataPageMain dpMain,
IndexData.Entry entry)
Verifies that the given entry type (node/leaf) is valid for the given
page (node/leaf).
|
private void |
validatePeers(IndexPageCache.DataPageMain dpMain)
Validates the peer pages for an index page.
|
private void |
validatePeerStatus(IndexPageCache.DataPageMain dpMain,
IndexPageCache.DataPageMain peerMain)
Validates the given peer page against the given index page
|
private java.lang.String |
withErrorContext(java.lang.String msg) |
void |
write()
Writes any outstanding changes for this index to the file.
|
private void |
writeDataPage(IndexPageCache.CacheDataPage cacheDataPage)
Writes the given index page to the file.
|
private void |
writeDataPages()
Writes any non-empty modified pages as the last pass during a
write() call. |
private static final int MAX_CACHE_SIZE
private final IndexData _indexData
private IndexPageCache.DataPageMain _rootPage
private final java.util.Map<java.lang.Integer,IndexPageCache.DataPageMain> _dataPages
private final java.util.List<IndexPageCache.CacheDataPage> _modifiedPages
public IndexPageCache(IndexData indexData)
public IndexData getIndexData()
public PageChannel getPageChannel()
public void setRootPageNumber(int pageNumber)
throws java.io.IOException
pageNumber - the root page numberjava.io.IOExceptionpublic void write()
throws java.io.IOException
java.io.IOExceptionprivate void handleEmptyPages()
throws java.io.IOException
write() call. All empty pages are removed from the _modifiedPages
collection by this method.java.io.IOExceptionprivate void preparePagesForWriting()
throws java.io.IOException
write() call. Updates entry prefixes, promotes/demotes
tail pages, and splits pages as needed.java.io.IOExceptionprivate void writeDataPages()
throws java.io.IOException
write() call. Clears the _modifiedPages collection when finised.java.io.IOExceptionpublic IndexPageCache.CacheDataPage getCacheDataPage(java.lang.Integer pageNumber) throws java.io.IOException
null if
the given page number is invalid. Loads the given page if necessary.java.io.IOExceptionprivate IndexPageCache.DataPageMain getDataPage(java.lang.Integer pageNumber) throws java.io.IOException
null if
the given page number is invalid. Loads the given page if necessary.java.io.IOExceptionprivate void writeDataPage(IndexPageCache.CacheDataPage cacheDataPage) throws java.io.IOException
java.io.IOExceptionprivate void deleteDataPage(IndexPageCache.CacheDataPage cacheDataPage) throws java.io.IOException
java.io.IOExceptionprivate IndexPageCache.CacheDataPage readDataPage(java.lang.Integer pageNumber) throws java.io.IOException
java.io.IOExceptionprivate IndexData.Entry removeEntry(IndexPageCache.CacheDataPage cacheDataPage, int entryIdx) throws java.io.IOException
cacheDataPage - the page from which to remove the entryentryIdx - the index of the entry to removejava.io.IOExceptionprivate void addEntry(IndexPageCache.CacheDataPage cacheDataPage, int entryIdx, IndexData.Entry newEntry) throws java.io.IOException
cacheDataPage - the page to which to add the entryentryIdx - the index at which to add the entrynewEntry - the entry to addjava.io.IOExceptionprivate IndexData.Entry updateEntry(IndexPageCache.CacheDataPage cacheDataPage, int entryIdx, IndexData.Entry newEntry, IndexPageCache.UpdateType upType) throws java.io.IOException
cacheDataPage - the page to updateentryIdx - the index at which to add/remove/replace the entrynewEntry - the entry to add/replaceupType - the type of update to makejava.io.IOExceptionprivate void removeDataPage(IndexPageCache.CacheDataPage parentDataPage, IndexPageCache.CacheDataPage cacheDataPage, IndexData.Entry oldLastEntry) throws java.io.IOException
parentDataPage - the parent of the removed pagecacheDataPage - the page to removeoldLastEntry - the last entry for this page (before it was removed)java.io.IOExceptionprivate void removeFromPeers(IndexPageCache.CacheDataPage cacheDataPage) throws java.io.IOException
cacheDataPage - the page to removejava.io.IOExceptionprivate void addParentEntry(IndexPageCache.CacheDataPage parentDataPage, IndexPageCache.CacheDataPage childDataPage) throws java.io.IOException
parentDataPage - the parent page to which to add the entrychildDataPage - the child from which to get the entry to addjava.io.IOExceptionprivate void replaceParentEntry(IndexPageCache.CacheDataPage parentDataPage, IndexPageCache.CacheDataPage childDataPage, IndexData.Entry oldEntry) throws java.io.IOException
parentDataPage - the parent page in which to replace the entrychildDataPage - the child for which the entry is being replacedoldEntry - the old child entry for the child pagejava.io.IOExceptionprivate void updateParentEntry(IndexPageCache.CacheDataPage parentDataPage, IndexPageCache.CacheDataPage childDataPage, IndexData.Entry oldEntry, IndexData.Entry newEntry, IndexPageCache.UpdateType upType) throws java.io.IOException
parentDataPage - the parent page in which to update the entrychildDataPage - the child for which the entry is being updatedoldEntry - the old child entry to remove/replacenewEntry - the new child entry to replace/addupType - the type of update to makejava.io.IOExceptionprivate void updateParentTail(IndexPageCache.CacheDataPage parentDataPage, IndexPageCache.CacheDataPage childDataPage, IndexPageCache.UpdateType upType) throws java.io.IOException
parentDataPage - the parent page in which to update the child tailchildDataPage - the child to add/replaceupType - the type of update to makejava.io.IOExceptionprivate void validateEntryForPage(IndexPageCache.DataPageMain dpMain, IndexData.Entry entry)
dpMain - the page to which the entry will be addedentry - the entry being addedjava.lang.IllegalStateException - if the entry type does not match the page
typeprivate void splitDataPage(IndexPageCache.CacheDataPage origDataPage) throws java.io.IOException
origDataPage - the page to splitjava.io.IOExceptionprivate IndexPageCache.CacheDataPage nestRootDataPage(IndexPageCache.CacheDataPage rootDataPage) throws java.io.IOException
rootDataPage - the root data pagejava.io.IOExceptionprivate IndexPageCache.CacheDataPage allocateNewCacheDataPage(java.lang.Integer parentPageNumber, boolean isLeaf) throws java.io.IOException
parentPageNumber - the parent page for the new pageisLeaf - whether or not the new page is a leaf pagejava.io.IOExceptionprivate void addToPeersBefore(IndexPageCache.CacheDataPage newDataPage, IndexPageCache.CacheDataPage origDataPage) throws java.io.IOException
newDataPage - the new index pageorigDataPage - the current index pagejava.io.IOExceptionprivate void separateFromNextPeer(IndexPageCache.CacheDataPage cacheDataPage) throws java.io.IOException
cacheDataPage - the index page to be separatedjava.io.IOExceptionprivate void reparentChildren(IndexPageCache.CacheDataPage cacheDataPage) throws java.io.IOException
cacheDataPage - the page whose children need to be updatedjava.io.IOExceptionprivate void demoteTail(IndexPageCache.CacheDataPage cacheDataPage) throws java.io.IOException
cacheDataPage - the page whose tail must be updatedjava.io.IOExceptionprivate void promoteTail(IndexPageCache.CacheDataPage cacheDataPage) throws java.io.IOException
cacheDataPage - the page whose tail must be updatedjava.io.IOExceptionpublic IndexPageCache.CacheDataPage findCacheDataPage(IndexData.Entry e) throws java.io.IOException
e - the entry to findjava.io.IOExceptionprivate void setModified(IndexPageCache.CacheDataPage cacheDataPage)
cacheDataPage - the modified index pageprivate static byte[] findCommonPrefix(IndexData.Entry e1, IndexData.Entry e2)
e1 - the first entry on the pagee2 - the last entry on the pagevoid validate()
throws java.io.IOException
java.io.IOExceptionprivate void validateEntries(IndexPageCache.DataPageExtra dpExtra) throws java.io.IOException
dpExtra - the entries to validatejava.io.IOExceptionprivate void validateChildren(IndexPageCache.DataPageMain dpMain, IndexPageCache.DataPageExtra dpExtra) throws java.io.IOException
dpMain - the index pagedpExtra - the child entries to validatejava.io.IOExceptionprivate void validatePeers(IndexPageCache.DataPageMain dpMain) throws java.io.IOException
dpMain - the index pagejava.io.IOExceptionprivate void validatePeerStatus(IndexPageCache.DataPageMain dpMain, IndexPageCache.DataPageMain peerMain) throws java.io.IOException
dpMain - the index pagepeerMain - the peer index pagejava.io.IOExceptionprivate java.util.List<java.lang.Object> collectPages(java.util.List<java.lang.Object> pages,
IndexPageCache.DataPageMain dpMain)
pages - the List to updatedpMain - the index page to collectprivate void purgeOldPages()
public java.lang.String toString()
toString in class java.lang.Objectprivate java.lang.String withErrorContext(java.lang.String msg)