use of org.apache.hyracks.storage.common.buffercache.ICachedPage in project asterixdb by apache.
the class AbstractLSMIndexFileManager method isValidTreeIndex.
protected TreeIndexState isValidTreeIndex(ITreeIndex treeIndex) throws HyracksDataException {
IBufferCache bufferCache = treeIndex.getBufferCache();
treeIndex.activate();
try {
int metadataPage = treeIndex.getPageManager().getMetadataPageId();
if (metadataPage < 0) {
return TreeIndexState.INVALID;
}
ITreeIndexMetadataFrame metadataFrame = treeIndex.getPageManager().createMetadataFrame();
ICachedPage page = bufferCache.pin(BufferedFileHandle.getDiskPageId(treeIndex.getFileId(), metadataPage), false);
page.acquireReadLatch();
try {
metadataFrame.setPage(page);
if (!metadataFrame.isValid()) {
return TreeIndexState.INVALID;
} else if (metadataFrame.getVersion() != ITreeIndexFrame.Constants.VERSION) {
return TreeIndexState.VERSION_MISMATCH;
} else {
return TreeIndexState.VALID;
}
} finally {
page.releaseReadLatch();
bufferCache.unpin(page);
}
} finally {
treeIndex.deactivate();
}
}
use of org.apache.hyracks.storage.common.buffercache.ICachedPage in project asterixdb by apache.
the class RTree method findTupleToDelete.
private int findTupleToDelete(RTreeOpContext ctx) throws HyracksDataException {
boolean writeLatched = false;
boolean readLatched = false;
boolean succeeded = false;
ICachedPage node = null;
ctx.getPathList().add(rootPage, -1, -1);
try {
while (!ctx.getPathList().isEmpty()) {
int pageId = ctx.getPathList().getLastPageId();
long parentLsn = ctx.getPathList().getLastPageLsn();
ctx.getPathList().moveLast();
node = bufferCache.pin(BufferedFileHandle.getDiskPageId(fileId, pageId), false);
node.acquireReadLatch();
readLatched = true;
ctx.getInteriorFrame().setPage(node);
boolean isLeaf = ctx.getInteriorFrame().isLeaf();
long pageLsn = ctx.getInteriorFrame().getPageLsn();
if (pageId != rootPage && parentLsn < ctx.getInteriorFrame().getPageNsn()) {
// Concurrent split detected, we need to visit the right
// page
int rightPage = ctx.getInteriorFrame().getRightPage();
if (rightPage != -1) {
ctx.getPathList().add(rightPage, parentLsn, -1);
}
}
if (!isLeaf) {
for (int i = 0; i < ctx.getInteriorFrame().getTupleCount(); i++) {
int childPageId = ctx.getInteriorFrame().getChildPageIdIfIntersect(ctx.getTuple(), i, ctx.getCmp());
if (childPageId != -1) {
ctx.getPathList().add(childPageId, pageLsn, -1);
}
}
} else {
ctx.getLeafFrame().setPage(node);
int tupleIndex = ctx.getLeafFrame().findTupleIndex(ctx.getTuple(), ctx.getCmp());
if (tupleIndex != -1) {
node.releaseReadLatch();
readLatched = false;
bufferCache.unpin(node);
node = bufferCache.pin(BufferedFileHandle.getDiskPageId(fileId, pageId), false);
node.acquireWriteLatch();
writeLatched = true;
ctx.getLeafFrame().setPage(node);
// leaf page. Simply we restart the search.
if (!ctx.getLeafFrame().isLeaf()) {
ctx.getPathList().add(pageId, -1, -1);
node.releaseWriteLatch(true);
writeLatched = false;
bufferCache.unpin(node);
continue;
}
if (ctx.getLeafFrame().getPageLsn() != pageLsn) {
// The page was changed while we unlocked it
tupleIndex = ctx.getLeafFrame().findTupleIndex(ctx.getTuple(), ctx.getCmp());
if (tupleIndex == -1) {
ctx.getPathList().add(pageId, parentLsn, -1);
node.releaseWriteLatch(true);
writeLatched = false;
bufferCache.unpin(node);
continue;
} else {
succeeded = true;
return tupleIndex;
}
} else {
succeeded = true;
return tupleIndex;
}
}
}
node.releaseReadLatch();
readLatched = false;
bufferCache.unpin(node);
}
} finally {
if (!succeeded) {
if (readLatched) {
node.releaseReadLatch();
readLatched = false;
bufferCache.unpin(node);
} else if (writeLatched) {
node.releaseWriteLatch(true);
writeLatched = false;
bufferCache.unpin(node);
}
}
}
return -1;
}
use of org.apache.hyracks.storage.common.buffercache.ICachedPage in project asterixdb by apache.
the class RTree method insertTuple.
private void insertTuple(ICachedPage node, int pageId, ITupleReference tuple, RTreeOpContext ctx, boolean isLeaf) throws HyracksDataException {
boolean succeeded = false;
FrameOpSpaceStatus spaceStatus;
if (!isLeaf) {
spaceStatus = ctx.getInteriorFrame().hasSpaceInsert(tuple);
} else {
spaceStatus = ctx.getLeafFrame().hasSpaceInsert(tuple);
}
switch(spaceStatus) {
case SUFFICIENT_CONTIGUOUS_SPACE:
{
try {
if (!isLeaf) {
ctx.getInteriorFrame().insert(tuple, -1);
} else {
ctx.getModificationCallback().found(null, tuple);
ctx.getLeafFrame().insert(tuple, -1);
}
succeeded = true;
} finally {
if (succeeded) {
ctx.getLSNUpdates().add(node);
ctx.getSplitKey().reset();
} else if (isLeaf) {
// In case of a crash, we un-latch the interior node
// inside updateParentForInsert.
node.releaseWriteLatch(true);
bufferCache.unpin(node);
}
}
break;
}
case SUFFICIENT_SPACE:
{
try {
if (!isLeaf) {
ctx.getInteriorFrame().compact();
ctx.getInteriorFrame().insert(tuple, -1);
} else {
ctx.getLeafFrame().compact();
ctx.getModificationCallback().found(null, tuple);
ctx.getLeafFrame().insert(tuple, -1);
}
succeeded = true;
} finally {
if (succeeded) {
ctx.getLSNUpdates().add(node);
ctx.getSplitKey().reset();
} else if (isLeaf) {
// In case of a crash, we un-latch the interior node
// inside updateParentForInsert.
node.releaseWriteLatch(true);
bufferCache.unpin(node);
}
}
break;
}
case INSUFFICIENT_SPACE:
{
int rightPageId = freePageManager.takePage(ctx.getMetaFrame());
ICachedPage rightNode = bufferCache.pin(BufferedFileHandle.getDiskPageId(fileId, rightPageId), true);
rightNode.acquireWriteLatch();
try {
IRTreeFrame rightFrame;
if (!isLeaf) {
rightFrame = (IRTreeFrame) interiorFrameFactory.createFrame();
rightFrame.setPage(rightNode);
rightFrame.initBuffer(ctx.getInteriorFrame().getLevel());
rightFrame.setRightPage(ctx.getInteriorFrame().getRightPage());
ctx.getInteriorFrame().split(rightFrame, tuple, ctx.getSplitKey(), ctx, bufferCache);
ctx.getInteriorFrame().setRightPage(rightPageId);
} else {
rightFrame = (IRTreeFrame) leafFrameFactory.createFrame();
rightFrame.setPage(rightNode);
rightFrame.initBuffer((byte) 0);
rightFrame.setRightPage(ctx.getInteriorFrame().getRightPage());
ctx.getModificationCallback().found(null, tuple);
ctx.getLeafFrame().split(rightFrame, tuple, ctx.getSplitKey(), ctx, bufferCache);
ctx.getLeafFrame().setRightPage(rightPageId);
}
succeeded = true;
} finally {
if (succeeded) {
ctx.getNSNUpdates().add(rightNode);
ctx.getLSNUpdates().add(rightNode);
ctx.getNSNUpdates().add(node);
ctx.getLSNUpdates().add(node);
} else if (isLeaf) {
// In case of a crash, we un-latch the interior node
// inside updateParentForInsert.
node.releaseWriteLatch(true);
bufferCache.unpin(node);
rightNode.releaseWriteLatch(true);
bufferCache.unpin(rightNode);
} else {
rightNode.releaseWriteLatch(true);
bufferCache.unpin(rightNode);
}
}
ctx.getSplitKey().setPages(pageId, rightPageId);
if (pageId == rootPage) {
int newLeftId = freePageManager.takePage(ctx.getMetaFrame());
ICachedPage newLeftNode = bufferCache.pin(BufferedFileHandle.getDiskPageId(fileId, newLeftId), true);
newLeftNode.acquireWriteLatch();
succeeded = false;
try {
// copy left child to new left child
System.arraycopy(node.getBuffer().array(), 0, newLeftNode.getBuffer().array(), 0, newLeftNode.getBuffer().capacity());
// initialize new root (leftNode becomes new root)
ctx.getInteriorFrame().setPage(node);
ctx.getInteriorFrame().initBuffer((byte) (ctx.getInteriorFrame().getLevel() + 1));
ctx.getSplitKey().setLeftPage(newLeftId);
ctx.getInteriorFrame().insert(ctx.getSplitKey().getLeftTuple(), -1);
ctx.getInteriorFrame().insert(ctx.getSplitKey().getRightTuple(), -1);
succeeded = true;
} finally {
if (succeeded) {
ctx.getNSNUpdates().remove(ctx.getNSNUpdates().size() - 1);
ctx.getLSNUpdates().remove(ctx.getLSNUpdates().size() - 1);
ctx.getNSNUpdates().add(newLeftNode);
ctx.getLSNUpdates().add(newLeftNode);
ctx.getNSNUpdates().add(node);
ctx.getLSNUpdates().add(node);
ctx.getSplitKey().reset();
} else if (isLeaf) {
// In case of a crash, we un-latch the interior node
// inside updateParentForInsert.
node.releaseWriteLatch(true);
bufferCache.unpin(node);
rightNode.releaseWriteLatch(true);
bufferCache.unpin(rightNode);
newLeftNode.releaseWriteLatch(true);
bufferCache.unpin(newLeftNode);
} else {
rightNode.releaseWriteLatch(true);
bufferCache.unpin(rightNode);
newLeftNode.releaseWriteLatch(true);
bufferCache.unpin(newLeftNode);
}
}
}
break;
}
default:
{
throw new IllegalStateException("NYI: " + spaceStatus);
}
}
}
use of org.apache.hyracks.storage.common.buffercache.ICachedPage in project asterixdb by apache.
the class RTree method printTree.
@SuppressWarnings("rawtypes")
public void printTree(int pageId, ICachedPage parent, boolean unpin, IRTreeLeafFrame leafFrame, IRTreeInteriorFrame interiorFrame, byte treeHeight, ISerializerDeserializer[] keySerdes, StringBuilder strBuilder, MultiComparator cmp) throws Exception {
ICachedPage node = bufferCache.pin(BufferedFileHandle.getDiskPageId(fileId, pageId), false);
node.acquireReadLatch();
try {
if (parent != null && unpin == true) {
parent.releaseReadLatch();
bufferCache.unpin(parent);
}
interiorFrame.setPage(node);
int level = interiorFrame.getLevel();
strBuilder.append(String.format("%1d ", level));
strBuilder.append(String.format("%3d ", pageId) + ": ");
for (int i = 0; i < treeHeight - level; i++) {
strBuilder.append(" ");
}
String keyString;
long LSN, NSN;
int rightPage;
if (interiorFrame.isLeaf()) {
leafFrame.setPage(node);
keyString = TreeIndexUtils.printFrameTuples(leafFrame, keySerdes);
LSN = leafFrame.getPageLsn();
NSN = leafFrame.getPageNsn();
rightPage = leafFrame.getRightPage();
} else {
keyString = TreeIndexUtils.printFrameTuples(interiorFrame, keySerdes);
LSN = interiorFrame.getPageLsn();
NSN = interiorFrame.getPageNsn();
rightPage = interiorFrame.getRightPage();
}
strBuilder.append(keyString + "\n" + "pageId: " + pageId + " LSN: " + LSN + " NSN: " + NSN + " rightPage: " + rightPage + "\n");
if (!interiorFrame.isLeaf()) {
ArrayList<Integer> children = ((RTreeNSMInteriorFrame) (interiorFrame)).getChildren(cmp);
for (int i = 0; i < children.size(); i++) {
printTree(children.get(i), node, i == children.size() - 1, leafFrame, interiorFrame, treeHeight, keySerdes, strBuilder, cmp);
}
} else {
node.releaseReadLatch();
bufferCache.unpin(node);
}
} catch (Exception e) {
node.releaseReadLatch();
bufferCache.unpin(node);
e.printStackTrace();
}
}
use of org.apache.hyracks.storage.common.buffercache.ICachedPage in project asterixdb by apache.
the class TreeTupleSorter method hasNext.
@Override
public boolean hasNext() throws HyracksDataException {
if (numTuples <= currentTupleIndex) {
return false;
}
// We don't latch pages since this code is only used by flush () before
// bulk-loading the r-tree to disk and flush is not concurrent.
//
ICachedPage node1 = bufferCache.pin(BufferedFileHandle.getDiskPageId(fileId, tPointers[currentTupleIndex * 2]), false);
try {
leafFrame1.setPage(node1);
frameTuple1.resetByTupleOffset(leafFrame1.getBuffer().array(), tPointers[currentTupleIndex * 2 + 1]);
} finally {
bufferCache.unpin(node1);
}
return true;
}
Aggregations