Search in sources :

Example 1 with RevisionRootPage

use of org.sirix.page.RevisionRootPage 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;
    }
}
Also used : DocumentRootNode(org.sirix.node.DocumentRootNode) UnorderedKeyValuePage(org.sirix.page.UnorderedKeyValuePage) SirixDeweyID(org.sirix.node.SirixDeweyID) NodeDelegate(org.sirix.node.delegates.NodeDelegate) RevisionRootPage(org.sirix.page.RevisionRootPage)

Example 2 with RevisionRootPage

use of org.sirix.page.RevisionRootPage in project sirix by sirixdb.

the class PageReadTrxImpl method loadRevRoot.

/**
 * Get revision root page belonging to revision key.
 *
 * @param revisionKey key of revision to find revision root page for
 * @return revision root page of this revision key
 *
 * @throws SirixIOException if something odd happens within the creation process
 */
@Override
public RevisionRootPage loadRevRoot(@Nonnegative final int revisionKey) throws SirixIOException {
    checkArgument(revisionKey >= 0 && revisionKey <= mResourceManager.getMostRecentRevisionNumber(), "%s must be >= 0 and <= last stored revision (%s)!", revisionKey, mResourceManager.getMostRecentRevisionNumber());
    // The indirect page reference either fails horribly or returns a non null instance.
    final PageReference reference = getPageReferenceForPage(mUberPage.getIndirectPageReference(), revisionKey, -1, PageKind.UBERPAGE);
    try {
        RevisionRootPage page = null;
        if (mTrxIntentLog != null) {
            // Try to get it from the transaction log if it's present.
            final PageContainer cont = mTrxIntentLog.get(reference);
            page = cont == null ? null : (RevisionRootPage) cont.getComplete();
        }
        if (page == null) {
            assert reference.getKey() != Constants.NULL_ID_LONG || reference.getLogKey() != Constants.NULL_ID_INT || reference.getPersistentLogKey() != Constants.NULL_ID_LONG;
            page = (RevisionRootPage) mPageCache.get(reference);
        }
        return page;
    } catch (final ExecutionException | UncheckedExecutionException e) {
        throw new SirixIOException(e.getCause());
    }
}
Also used : PageReference(org.sirix.page.PageReference) PageContainer(org.sirix.cache.PageContainer) UncheckedExecutionException(com.google.common.util.concurrent.UncheckedExecutionException) UncheckedExecutionException(com.google.common.util.concurrent.UncheckedExecutionException) ExecutionException(java.util.concurrent.ExecutionException) SirixIOException(org.sirix.exception.SirixIOException) RevisionRootPage(org.sirix.page.RevisionRootPage)

Example 3 with RevisionRootPage

use of org.sirix.page.RevisionRootPage in project sirix by sirixdb.

the class PageWriteTrxImpl method preparePreviousRevisionRootPage.

/**
 * Prepare the previous revision root page and retrieve the next {@link RevisionRootPage}.
 *
 * @param baseRevision base revision
 * @param representRevision the revision to represent
 * @return new {@link RevisionRootPage} instance
 * @throws SirixIOException if an I/O error occurs
 */
private RevisionRootPage preparePreviousRevisionRootPage(@Nonnegative final int baseRevision, @Nonnegative final int representRevision) throws SirixIOException {
    if (getUberPage().isBootstrap()) {
        final RevisionRootPage revisionRootPage = mPageRtx.loadRevRoot(baseRevision);
        // appendLogRecord(new PageContainer(revisionRootPage, revisionRootPage));
        return revisionRootPage;
    } else {
        // Prepare revision root nodePageReference.
        final RevisionRootPage revisionRootPage = new RevisionRootPage(mPageRtx.loadRevRoot(baseRevision), representRevision + 1);
        // Prepare indirect tree to hold reference to prepared revision root nodePageReference.
        final PageReference revisionRootPageReference = prepareLeafOfTree(getUberPage().getIndirectPageReference(), getUberPage().getRevisionNumber(), -1, PageKind.UBERPAGE);
        // Link the prepared revision root nodePageReference with the prepared indirect tree.
        appendLogRecord(revisionRootPageReference, new PageContainer(revisionRootPage, revisionRootPage));
        // Return prepared revision root nodePageReference.
        return revisionRootPage;
    }
}
Also used : PageReference(org.sirix.page.PageReference) PageContainer(org.sirix.cache.PageContainer) RevisionRootPage(org.sirix.page.RevisionRootPage)

Aggregations

RevisionRootPage (org.sirix.page.RevisionRootPage)3 PageContainer (org.sirix.cache.PageContainer)2 PageReference (org.sirix.page.PageReference)2 UncheckedExecutionException (com.google.common.util.concurrent.UncheckedExecutionException)1 ExecutionException (java.util.concurrent.ExecutionException)1 SirixIOException (org.sirix.exception.SirixIOException)1 DocumentRootNode (org.sirix.node.DocumentRootNode)1 SirixDeweyID (org.sirix.node.SirixDeweyID)1 NodeDelegate (org.sirix.node.delegates.NodeDelegate)1 UnorderedKeyValuePage (org.sirix.page.UnorderedKeyValuePage)1