use of org.apache.hyracks.storage.common.buffercache.ICachedPage in project asterixdb by apache.
the class LinkedMetaDataPageManager method releasePage.
@Override
public void releasePage(ITreeIndexMetadataFrame metaFrame, int freePageNum) throws HyracksDataException {
// Get the metadata node
ICachedPage metaPage = bufferCache.pin(BufferedFileHandle.getDiskPageId(fileId, getMetadataPageId()), false);
metaPage.acquireWriteLatch();
try {
metaFrame.setPage(metaPage);
if (metaFrame.getSpace() > Integer.BYTES) {
metaFrame.addFreePage(freePageNum);
} else {
// allocate a new page in the chain of meta pages
int newPageNum = metaFrame.getFreePage();
if (newPageNum < 0) {
throw new HyracksDataException("Inconsistent Meta Page State. It has no space, but it also has no entries.");
}
ICachedPage newNode = bufferCache.pin(BufferedFileHandle.getDiskPageId(fileId, newPageNum), false);
newNode.acquireWriteLatch();
try {
int metaMaxPage = metaFrame.getMaxPage();
// copy metaDataPage to newNode
System.arraycopy(metaPage.getBuffer().array(), 0, newNode.getBuffer().array(), 0, metaPage.getBuffer().capacity());
metaFrame.init();
metaFrame.setNextMetadataPage(newPageNum);
metaFrame.setMaxPage(metaMaxPage);
metaFrame.addFreePage(freePageNum);
} finally {
newNode.releaseWriteLatch(true);
bufferCache.unpin(newNode);
}
}
} finally {
metaPage.releaseWriteLatch(true);
bufferCache.unpin(metaPage);
}
}
use of org.apache.hyracks.storage.common.buffercache.ICachedPage in project asterixdb by apache.
the class LinkedMetaDataPageManager method getFileOffset.
@Override
public long getFileOffset(ITreeIndexMetadataFrame frame, IValueReference key) throws HyracksDataException {
int metadataPageNum = getMetadataPageId();
if (metadataPageNum != IBufferCache.INVALID_PAGEID) {
ICachedPage metaNode = bufferCache.pin(BufferedFileHandle.getDiskPageId(fileId, getMetadataPageId()), false);
metaNode.acquireReadLatch();
try {
frame.setPage(metaNode);
return ((long) metadataPageNum * bufferCache.getPageSizeWithHeader()) + frame.getOffset(key);
} finally {
metaNode.releaseReadLatch();
bufferCache.unpin(metaNode);
}
}
return -1;
}
use of org.apache.hyracks.storage.common.buffercache.ICachedPage in project asterixdb by apache.
the class LinkedMetaDataPageManager method getRootPageId.
@Override
public int getRootPageId() throws HyracksDataException {
ICachedPage metaNode;
metaNode = bufferCache.pin(BufferedFileHandle.getDiskPageId(fileId, getMetadataPageId()), false);
ITreeIndexMetadataFrame metaFrame = frameFactory.createFrame();
metaNode.acquireReadLatch();
try {
metaFrame.setPage(metaNode);
return metaFrame.getRootPageId();
} finally {
metaNode.releaseReadLatch();
bufferCache.unpin(metaNode);
}
}
use of org.apache.hyracks.storage.common.buffercache.ICachedPage in project asterixdb by apache.
the class LinkedMetaDataPageManager method takePage.
@Override
public int takePage(ITreeIndexMetadataFrame metaFrame) throws HyracksDataException {
ICachedPage metaNode = bufferCache.pin(BufferedFileHandle.getDiskPageId(fileId, getMetadataPageId()), false);
metaNode.acquireWriteLatch();
int freePage = IBufferCache.INVALID_PAGEID;
try {
metaFrame.setPage(metaNode);
freePage = metaFrame.getFreePage();
if (freePage < 0) {
// no free page entry on this page
int nextPage = metaFrame.getNextMetadataPage();
if (nextPage > 0) {
// sibling may have free pages
ICachedPage nextNode = bufferCache.pin(BufferedFileHandle.getDiskPageId(fileId, nextPage), false);
nextNode.acquireWriteLatch();
// of nextpage
try {
// remember entries that remain unchanged
int maxPage = metaFrame.getMaxPage();
// copy entire page (including sibling pointer, free
// page entries, and all other info)
// after this copy nextPage is considered a free page
System.arraycopy(nextNode.getBuffer().array(), 0, metaNode.getBuffer().array(), 0, nextNode.getBuffer().capacity());
// reset unchanged entry
metaFrame.setMaxPage(maxPage);
freePage = metaFrame.getFreePage();
// happen, but we deal with it anyway just to be safe
if (freePage < 0) {
freePage = nextPage;
} else {
metaFrame.addFreePage(nextPage);
}
} finally {
nextNode.releaseWriteLatch(true);
bufferCache.unpin(nextNode);
}
} else {
freePage = metaFrame.getMaxPage();
freePage++;
metaFrame.setMaxPage(freePage);
}
}
} finally {
metaNode.releaseWriteLatch(true);
bufferCache.unpin(metaNode);
}
return freePage;
}
use of org.apache.hyracks.storage.common.buffercache.ICachedPage in project asterixdb by apache.
the class BufferCacheTest method simpleOpenPinCloseTest.
@Test
public void simpleOpenPinCloseTest() throws HyracksException {
TestStorageManagerComponentHolder.init(PAGE_SIZE, NUM_PAGES, MAX_OPEN_FILES);
IBufferCache bufferCache = TestStorageManagerComponentHolder.getBufferCache(ctx.getJobletContext().getServiceContext());
IFileMapProvider fmp = TestStorageManagerComponentHolder.getFileMapProvider();
IIOManager ioManager = TestStorageManagerComponentHolder.getIOManager();
String fileName = getFileName();
FileReference file = ioManager.resolve(fileName);
bufferCache.createFile(file);
int fileId = fmp.lookupFileId(file);
int num = 10;
int testPageId = 0;
bufferCache.openFile(fileId);
ICachedPage page = null;
// tryPin should fail
page = bufferCache.tryPin(BufferedFileHandle.getDiskPageId(fileId, testPageId));
Assert.assertNull(page);
// pin page should succeed
page = bufferCache.pin(BufferedFileHandle.getDiskPageId(fileId, testPageId), true);
page.acquireWriteLatch();
try {
for (int i = 0; i < num; i++) {
page.getBuffer().putInt(i * 4, i);
}
// try pin should succeed
ICachedPage page2 = bufferCache.tryPin(BufferedFileHandle.getDiskPageId(fileId, testPageId));
Assert.assertNotNull(page2);
bufferCache.unpin(page2);
} finally {
page.releaseWriteLatch(true);
bufferCache.unpin(page);
}
bufferCache.closeFile(fileId);
// This code is commented because the method pinSanityCheck in the BufferCache is commented.
/*boolean exceptionThrown = false;
// tryPin should fail since file is not open
try {
page = bufferCache.tryPin(BufferedFileHandle.getDiskPageId(fileId, testPageId));
} catch (HyracksDataException e) {
exceptionThrown = true;
}
Assert.assertTrue(exceptionThrown);
// pin should fail since file is not open
exceptionThrown = false;
try {
page = bufferCache.pin(BufferedFileHandle.getDiskPageId(fileId, testPageId), false);
} catch (HyracksDataException e) {
exceptionThrown = true;
}
Assert.assertTrue(exceptionThrown);*/
// open file again
bufferCache.openFile(fileId);
// tryPin should succeed because page should still be cached
page = bufferCache.tryPin(BufferedFileHandle.getDiskPageId(fileId, testPageId));
Assert.assertNotNull(page);
page.acquireReadLatch();
try {
// verify contents of page
for (int i = 0; i < num; i++) {
Assert.assertEquals(page.getBuffer().getInt(i * 4), i);
}
} finally {
page.releaseReadLatch();
bufferCache.unpin(page);
}
bufferCache.closeFile(fileId);
bufferCache.close();
}
Aggregations