use of org.sirix.node.DocumentRootNode in project sirix by sirixdb.
the class AVLTreeWriter method index.
/**
* Checks if the specified token is already indexed; if yes, returns its reference. Otherwise,
* creates a new index entry and returns a reference of the indexed token.
*
* @param key token to be indexed
* @param value node key references
* @param move determines if AVLNode cursor must be moved to document root/root node or not
* @return indexed node key references
* @throws SirixIOException if an I/O error occurs
*/
@SuppressWarnings("unchecked")
public V index(final K key, final V value, final MoveCursor move) throws SirixIOException {
if (move == MoveCursor.TO_DOCUMENT_ROOT) {
moveToDocumentRoot();
}
final RevisionRootPage root = mPageWriteTrx.getActualRevisionRootPage();
if (mAVLTreeReader.getAVLNode() == null && ((DocumentRootNode) getNode()).getFirstChildKey() == Fixed.NULL_NODE_KEY.getStandardProperty()) {
// Index is empty.. create root node.
final long nodeKey = getNewNodeKey(root);
final AVLNode<K, V> treeRoot = (AVLNode<K, V>) mPageWriteTrx.createEntry(nodeKey, new AVLNode<>(key, value, new NodeDelegate(nodeKey, Fixed.DOCUMENT_NODE_KEY.getStandardProperty(), 0, 0, Optional.<SirixDeweyID>empty())), mAVLTreeReader.mPageKind, mAVLTreeReader.mIndex, Optional.<UnorderedKeyValuePage>empty());
final DocumentRootNode document = (DocumentRootNode) mPageWriteTrx.prepareEntryForModification(Fixed.DOCUMENT_NODE_KEY.getStandardProperty(), mAVLTreeReader.mPageKind, mAVLTreeReader.mIndex, Optional.<UnorderedKeyValuePage>empty());
document.setFirstChildKey(treeRoot.getNodeKey());
document.incrementChildCount();
document.incrementDescendantCount();
return value;
}
if (move == MoveCursor.TO_DOCUMENT_ROOT || mAVLTreeReader.getAVLNode() == null) {
moveToDocumentRoot();
moveToFirstChild();
}
AVLNode<K, V> node = mAVLTreeReader.getAVLNode();
while (true) {
final int c = key.compareTo(node.getKey());
if (c == 0) {
if (!value.equals(node.getValue())) {
final AVLNode<K, V> avlNode = (AVLNode<K, V>) mPageWriteTrx.prepareEntryForModification(node.getNodeKey(), mAVLTreeReader.mPageKind, mAVLTreeReader.mIndex, Optional.<UnorderedKeyValuePage>empty());
avlNode.setValue(value);
}
return node.getValue();
}
final boolean moved = c < 0 ? moveToFirstChild().hasMoved() : moveToLastChild().hasMoved();
if (moved) {
node = mAVLTreeReader.getAVLNode();
continue;
}
final long nodeKey = getNewNodeKey(root);
final AVLNode<K, V> child = (AVLNode<K, V>) mPageWriteTrx.createEntry(nodeKey, new AVLNode<>(key, value, new NodeDelegate(nodeKey, node.getNodeKey(), 0, 0, Optional.<SirixDeweyID>empty())), mAVLTreeReader.mPageKind, mAVLTreeReader.mIndex, Optional.<UnorderedKeyValuePage>empty());
node = (AVLNode<K, V>) mPageWriteTrx.prepareEntryForModification(node.getNodeKey(), mAVLTreeReader.mPageKind, mAVLTreeReader.mIndex, Optional.<UnorderedKeyValuePage>empty());
if (c < 0) {
node.setLeftChildKey(child.getNodeKey());
adjust(child);
} else {
node.setRightChildKey(child.getNodeKey());
adjust(child);
}
final DocumentRootNode document = (DocumentRootNode) mPageWriteTrx.prepareEntryForModification(Fixed.DOCUMENT_NODE_KEY.getStandardProperty(), mAVLTreeReader.mPageKind, mAVLTreeReader.mIndex, Optional.<UnorderedKeyValuePage>empty());
document.incrementDescendantCount();
return value;
}
}
use of org.sirix.node.DocumentRootNode in project sirix by sirixdb.
the class AVLTreeWriter method rotateRight.
/**
* Right rotation.
*
* @param node node to be rotated
* @throws SirixIOException if an I/O error occurs
*/
@SuppressWarnings({ "unchecked" })
private void rotateRight(AVLNode<K, V> node) throws SirixIOException {
moveTo(node.getNodeKey());
AVLNode<K, V> leftChild = ((AVLTreeReader<K, V>) moveToFirstChild().get()).getAVLNode();
node = (AVLNode<K, V>) mPageWriteTrx.prepareEntryForModification(node.getNodeKey(), mAVLTreeReader.mPageKind, mAVLTreeReader.mIndex, Optional.<UnorderedKeyValuePage>empty());
node.setLeftChildKey(leftChild.getRightChildKey());
if (leftChild.hasRightChild()) {
final Node leftRightChild = (Node) mPageWriteTrx.prepareEntryForModification(leftChild.getRightChildKey(), mAVLTreeReader.mPageKind, mAVLTreeReader.mIndex, Optional.<UnorderedKeyValuePage>empty());
leftRightChild.setParentKey(node.getNodeKey());
}
leftChild = (AVLNode<K, V>) mPageWriteTrx.prepareEntryForModification(leftChild.getNodeKey(), mAVLTreeReader.mPageKind, mAVLTreeReader.mIndex, Optional.<UnorderedKeyValuePage>empty());
leftChild.setParentKey(node.getParentKey());
if (node.getParentKey() == Fixed.DOCUMENT_NODE_KEY.getStandardProperty()) {
final DocumentRootNode parent = (DocumentRootNode) mPageWriteTrx.prepareEntryForModification(Fixed.DOCUMENT_NODE_KEY.getStandardProperty(), mAVLTreeReader.mPageKind, mAVLTreeReader.mIndex, Optional.<UnorderedKeyValuePage>empty());
parent.setFirstChildKey(leftChild.getNodeKey());
} else if (moveTo(node.getParentKey()).hasMoved() && mAVLTreeReader.getAVLNode().getRightChildKey() == node.getNodeKey()) {
final AVLNode<K, V> parent = (AVLNode<K, V>) mPageWriteTrx.prepareEntryForModification(mAVLTreeReader.getNodeKey(), mAVLTreeReader.mPageKind, mAVLTreeReader.mIndex, Optional.<UnorderedKeyValuePage>empty());
parent.setRightChildKey(leftChild.getNodeKey());
} else {
final AVLNode<K, V> parent = (AVLNode<K, V>) mPageWriteTrx.prepareEntryForModification(mAVLTreeReader.getNodeKey(), mAVLTreeReader.mPageKind, mAVLTreeReader.mIndex, Optional.<UnorderedKeyValuePage>empty());
parent.setLeftChildKey(leftChild.getNodeKey());
}
leftChild = (AVLNode<K, V>) mPageWriteTrx.prepareEntryForModification(leftChild.getNodeKey(), mAVLTreeReader.mPageKind, mAVLTreeReader.mIndex, Optional.<UnorderedKeyValuePage>empty());
leftChild.setRightChildKey(node.getNodeKey());
node = (AVLNode<K, V>) mPageWriteTrx.prepareEntryForModification(node.getNodeKey(), mAVLTreeReader.mPageKind, mAVLTreeReader.mIndex, Optional.<UnorderedKeyValuePage>empty());
node.setParentKey(leftChild.getNodeKey());
}
use of org.sirix.node.DocumentRootNode in project sirix by sirixdb.
the class PageUtils method createTree.
/**
* Create the initial tree structure.
*
* @param reference reference from revision root
* @param pageKind the page kind
*/
public static void createTree(@Nonnull PageReference reference, final PageKind pageKind, final int index, final PageReadTrx pageReadTrx, final TransactionIntentLog log) {
Page page = null;
// Level page count exponent from the configuration.
final int[] levelPageCountExp = pageReadTrx.getUberPage().getPageCountExp(pageKind);
// Remaining levels.
for (int i = 0, l = levelPageCountExp.length; i < l; i++) {
page = new IndirectPage();
log.put(reference, new PageContainer(page, page));
reference = page.getReference(0);
}
// Create new record page.
final UnorderedKeyValuePage ndp = new UnorderedKeyValuePage(Fixed.ROOT_PAGE_KEY.getStandardProperty(), pageKind, Constants.NULL_ID_LONG, pageReadTrx);
// Create a {@link DocumentRootNode}.
final Optional<SirixDeweyID> id = pageReadTrx.getResourceManager().getResourceConfig().mDeweyIDsStored ? Optional.of(SirixDeweyID.newRootID()) : Optional.empty();
final NodeDelegate nodeDel = new NodeDelegate(Fixed.DOCUMENT_NODE_KEY.getStandardProperty(), Fixed.NULL_NODE_KEY.getStandardProperty(), Fixed.NULL_NODE_KEY.getStandardProperty(), 0, id);
final StructNodeDelegate strucDel = new StructNodeDelegate(nodeDel, Fixed.NULL_NODE_KEY.getStandardProperty(), Fixed.NULL_NODE_KEY.getStandardProperty(), Fixed.NULL_NODE_KEY.getStandardProperty(), 0, 0);
ndp.setEntry(0L, new DocumentRootNode(nodeDel, strucDel));
log.put(reference, new PageContainer(ndp, ndp));
}
use of org.sirix.node.DocumentRootNode in project sirix by sirixdb.
the class AVLTreeWriter method rotateLeft.
/**
* Left rotation.
*
* @param node node to be rotated
* @throws SirixIOException if an I/O error occurs
*/
@SuppressWarnings({ "unchecked" })
private void rotateLeft(AVLNode<K, V> node) throws SirixIOException {
moveTo(node.getNodeKey());
AVLNode<K, V> right = ((AVLTreeReader<K, V>) moveToLastChild().get()).getAVLNode();
node = (AVLNode<K, V>) mPageWriteTrx.prepareEntryForModification(node.getNodeKey(), mAVLTreeReader.mPageKind, mAVLTreeReader.mIndex, Optional.<UnorderedKeyValuePage>empty());
node.setRightChildKey(right.getLeftChildKey());
if (right.hasLeftChild()) {
final AVLNode<K, V> rightLeftChild = (AVLNode<K, V>) mPageWriteTrx.prepareEntryForModification(right.getLeftChildKey(), mAVLTreeReader.mPageKind, mAVLTreeReader.mIndex, Optional.<UnorderedKeyValuePage>empty());
rightLeftChild.setParentKey(node.getNodeKey());
}
right = (AVLNode<K, V>) mPageWriteTrx.prepareEntryForModification(right.getNodeKey(), mAVLTreeReader.mPageKind, mAVLTreeReader.mIndex, Optional.<UnorderedKeyValuePage>empty());
right.setParentKey(node.getParentKey());
if (node.getParentKey() == Fixed.DOCUMENT_NODE_KEY.getStandardProperty()) {
final DocumentRootNode parent = (DocumentRootNode) mPageWriteTrx.prepareEntryForModification(Fixed.DOCUMENT_NODE_KEY.getStandardProperty(), mAVLTreeReader.mPageKind, mAVLTreeReader.mIndex, Optional.<UnorderedKeyValuePage>empty());
parent.setFirstChildKey(right.getNodeKey());
} else if (moveTo(node.getParentKey()).hasMoved() && mAVLTreeReader.getAVLNode().getLeftChildKey() == node.getNodeKey()) {
final AVLNode<K, V> parent = (AVLNode<K, V>) mPageWriteTrx.prepareEntryForModification(mAVLTreeReader.getNodeKey(), mAVLTreeReader.mPageKind, mAVLTreeReader.mIndex, Optional.<UnorderedKeyValuePage>empty());
parent.setLeftChildKey(right.getNodeKey());
} else {
final AVLNode<K, V> parent = (AVLNode<K, V>) mPageWriteTrx.prepareEntryForModification(mAVLTreeReader.getNodeKey(), mAVLTreeReader.mPageKind, mAVLTreeReader.mIndex, Optional.<UnorderedKeyValuePage>empty());
parent.setRightChildKey(right.getNodeKey());
}
right = (AVLNode<K, V>) mPageWriteTrx.prepareEntryForModification(right.getNodeKey(), mAVLTreeReader.mPageKind, mAVLTreeReader.mIndex, Optional.<UnorderedKeyValuePage>empty());
right.setLeftChildKey(node.getNodeKey());
node = (AVLNode<K, V>) mPageWriteTrx.prepareEntryForModification(node.getNodeKey(), mAVLTreeReader.mPageKind, mAVLTreeReader.mIndex, Optional.<UnorderedKeyValuePage>empty());
node.setParentKey(right.getNodeKey());
}
Aggregations