Search in sources :

Example 16 with SirixDeweyID

use of org.sirix.node.SirixDeweyID in project sirix by sirixdb.

the class XdmNodeWriterTrxImpl method insertNamespace.

@Override
public XdmNodeWriteTrx insertNamespace(final QNm name, final Movement move) throws SirixException {
    if (!XMLToken.isValidQName(checkNotNull(name))) {
        throw new IllegalArgumentException("The QName is not valid!");
    }
    acquireLock();
    try {
        if (getCurrentNode().getKind() == Kind.ELEMENT) {
            checkAccessAndCommit();
            for (int i = 0, namespCount = ((ElementNode) getCurrentNode()).getNamespaceCount(); i < namespCount; i++) {
                moveToNamespace(i);
                final QNm qName = getName();
                if (name.getPrefix().equals(qName.getPrefix())) {
                    throw new SirixUsageException("Duplicate namespace!");
                }
                moveToParent();
            }
            final long pathNodeKey = mBuildPathSummary ? mPathSummaryWriter.getPathNodeKey(name, Kind.NAMESPACE) : 0;
            final long elementKey = getCurrentNode().getNodeKey();
            final Optional<SirixDeweyID> id = newNamespaceID();
            final NamespaceNode node = mNodeFactory.createNamespaceNode(elementKey, name, pathNodeKey, id);
            final Node parentNode = (Node) getPageTransaction().prepareEntryForModification(node.getParentKey(), PageKind.RECORDPAGE, -1, Optional.<UnorderedKeyValuePage>empty());
            ((ElementNode) parentNode).insertNamespace(node.getNodeKey());
            mNodeReader.setCurrentNode(node);
            adaptHashesWithAdd();
            if (move == Movement.TOPARENT) {
                moveToParent();
            }
            return this;
        } else {
            throw new SirixUsageException("Insert is not allowed if current node is not an ElementNode!");
        }
    } finally {
        unLock();
    }
}
Also used : QNm(org.brackit.xquery.atomic.QNm) TextNode(org.sirix.node.TextNode) CommentNode(org.sirix.node.CommentNode) PINode(org.sirix.node.PINode) Node(org.sirix.node.interfaces.Node) ValueNode(org.sirix.node.interfaces.ValueNode) AttributeNode(org.sirix.node.AttributeNode) ImmutableNode(org.sirix.node.interfaces.immutable.ImmutableNode) StructNode(org.sirix.node.interfaces.StructNode) ElementNode(org.sirix.node.ElementNode) NamespaceNode(org.sirix.node.NamespaceNode) NameNode(org.sirix.node.interfaces.NameNode) NamespaceNode(org.sirix.node.NamespaceNode) UnorderedKeyValuePage(org.sirix.page.UnorderedKeyValuePage) ElementNode(org.sirix.node.ElementNode) SirixUsageException(org.sirix.exception.SirixUsageException) SirixDeweyID(org.sirix.node.SirixDeweyID)

Example 17 with SirixDeweyID

use of org.sirix.node.SirixDeweyID 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));
}
Also used : PageContainer(org.sirix.cache.PageContainer) DocumentRootNode(org.sirix.node.DocumentRootNode) StructNodeDelegate(org.sirix.node.delegates.StructNodeDelegate) Page(org.sirix.page.interfaces.Page) SirixDeweyID(org.sirix.node.SirixDeweyID) NodeDelegate(org.sirix.node.delegates.NodeDelegate) StructNodeDelegate(org.sirix.node.delegates.StructNodeDelegate)

Example 18 with SirixDeweyID

use of org.sirix.node.SirixDeweyID in project sirix by sirixdb.

the class XdmNodeReadTrxImpl method getRightSiblingDeweyID.

@Override
public Optional<SirixDeweyID> getRightSiblingDeweyID() {
    if (mResourceManager.getResourceConfig().mDeweyIDsStored) {
        final StructNode node = getStructuralNode();
        final long nodeKey = node.getNodeKey();
        Optional<SirixDeweyID> deweyID = Optional.<SirixDeweyID>empty();
        if (node.hasRightSibling()) {
            // Right sibling node.
            deweyID = moveTo(node.getRightSiblingKey()).get().getDeweyID();
        }
        moveTo(nodeKey);
        return deweyID;
    }
    return Optional.<SirixDeweyID>empty();
}
Also used : SirixDeweyID(org.sirix.node.SirixDeweyID) StructNode(org.sirix.node.interfaces.StructNode)

Example 19 with SirixDeweyID

use of org.sirix.node.SirixDeweyID in project sirix by sirixdb.

the class XdmNodeWriterTrxImpl method insertElementAsFirstChild.

@Override
public XdmNodeWriteTrx insertElementAsFirstChild(final QNm name) throws SirixException {
    if (!XMLToken.isValidQName(checkNotNull(name))) {
        throw new IllegalArgumentException("The QName is not valid!");
    }
    acquireLock();
    try {
        final Kind kind = mNodeReader.getCurrentNode().getKind();
        if (kind == Kind.ELEMENT || kind == Kind.DOCUMENT) {
            checkAccessAndCommit();
            final long parentKey = mNodeReader.getCurrentNode().getNodeKey();
            final long leftSibKey = Fixed.NULL_NODE_KEY.getStandardProperty();
            final long rightSibKey = ((StructNode) mNodeReader.getCurrentNode()).getFirstChildKey();
            final long pathNodeKey = mBuildPathSummary ? mPathSummaryWriter.getPathNodeKey(name, Kind.ELEMENT) : 0;
            final Optional<SirixDeweyID> id = newFirstChildID();
            final ElementNode node = mNodeFactory.createElementNode(parentKey, leftSibKey, rightSibKey, 0, name, pathNodeKey, id);
            mNodeReader.setCurrentNode(node);
            adaptForInsert(node, InsertPos.ASFIRSTCHILD, PageKind.RECORDPAGE);
            mNodeReader.setCurrentNode(node);
            adaptHashesWithAdd();
            return this;
        } else {
            throw new SirixUsageException("Insert is not allowed if current node is not an ElementNode!");
        }
    } finally {
        unLock();
    }
}
Also used : PageKind(org.sirix.page.PageKind) Kind(org.sirix.node.Kind) SirixDeweyID(org.sirix.node.SirixDeweyID) ElementNode(org.sirix.node.ElementNode) SirixUsageException(org.sirix.exception.SirixUsageException) StructNode(org.sirix.node.interfaces.StructNode)

Example 20 with SirixDeweyID

use of org.sirix.node.SirixDeweyID in project sirix by sirixdb.

the class XdmNodeWriterTrxImpl method computeNewDeweyIDs.

/**
 * Compute the new DeweyIDs.
 *
 * @throws SirixException if anything went wrong
 */
private void computeNewDeweyIDs() throws SirixException {
    SirixDeweyID id = null;
    if (hasLeftSibling() && hasRightSibling()) {
        id = SirixDeweyID.newBetween(getLeftSiblingDeweyID().get(), getRightSiblingDeweyID().get());
    } else if (hasLeftSibling()) {
        id = SirixDeweyID.newBetween(getLeftSiblingDeweyID().get(), null);
    } else if (hasRightSibling()) {
        id = SirixDeweyID.newBetween(null, getRightSiblingDeweyID().get());
    } else {
        id = mNodeReader.getParentDeweyID().get().getNewChildID();
    }
    assert id != null;
    final long nodeKey = mNodeReader.getCurrentNode().getNodeKey();
    final StructNode root = (StructNode) getPageTransaction().prepareEntryForModification(nodeKey, PageKind.RECORDPAGE, -1, Optional.<UnorderedKeyValuePage>empty());
    root.setDeweyID(Optional.of(id));
    if (root.hasFirstChild()) {
        final Node firstChild = (Node) getPageTransaction().prepareEntryForModification(root.getFirstChildKey(), PageKind.RECORDPAGE, -1, Optional.<UnorderedKeyValuePage>empty());
        firstChild.setDeweyID(Optional.of(id.getNewChildID()));
        int previousLevel = getDeweyID().get().getLevel();
        mNodeReader.moveTo(firstChild.getNodeKey());
        int attributeNr = 0;
        int nspNr = 0;
        for (@SuppressWarnings("unused") final long key : LevelOrderAxis.newBuilder(this).includeNonStructuralNodes().build()) {
            Optional<SirixDeweyID> deweyID = Optional.<SirixDeweyID>empty();
            if (isAttribute()) {
                final long attNodeKey = mNodeReader.getNodeKey();
                if (attributeNr == 0) {
                    deweyID = Optional.of(mNodeReader.getParentDeweyID().get().getNewAttributeID());
                } else {
                    mNodeReader.moveTo(attributeNr - 1);
                    deweyID = Optional.of(SirixDeweyID.newBetween(mNodeReader.getNode().getDeweyID().get(), null));
                }
                mNodeReader.moveTo(attNodeKey);
                attributeNr++;
            } else if (isNamespace()) {
                final long nspNodeKey = mNodeReader.getNodeKey();
                if (nspNr == 0) {
                    deweyID = Optional.of(mNodeReader.getParentDeweyID().get().getNewNamespaceID());
                } else {
                    mNodeReader.moveTo(nspNr - 1);
                    deweyID = Optional.of(SirixDeweyID.newBetween(mNodeReader.getNode().getDeweyID().get(), null));
                }
                mNodeReader.moveTo(nspNodeKey);
                nspNr++;
            } else {
                attributeNr = 0;
                nspNr = 0;
                if (previousLevel + 1 == getDeweyID().get().getLevel()) {
                    if (mNodeReader.hasLeftSibling()) {
                        deweyID = Optional.of(SirixDeweyID.newBetween(getLeftSiblingDeweyID().get(), null));
                    } else {
                        deweyID = Optional.of(getParentDeweyID().get().getNewChildID());
                    }
                } else {
                    previousLevel++;
                    deweyID = Optional.of(getParentDeweyID().get().getNewChildID());
                }
            }
            final Node node = (Node) getPageTransaction().prepareEntryForModification(mNodeReader.getCurrentNode().getNodeKey(), PageKind.RECORDPAGE, -1, Optional.<UnorderedKeyValuePage>empty());
            node.setDeweyID(deweyID);
        }
        mNodeReader.moveTo(nodeKey);
    }
}
Also used : TextNode(org.sirix.node.TextNode) CommentNode(org.sirix.node.CommentNode) PINode(org.sirix.node.PINode) Node(org.sirix.node.interfaces.Node) ValueNode(org.sirix.node.interfaces.ValueNode) AttributeNode(org.sirix.node.AttributeNode) ImmutableNode(org.sirix.node.interfaces.immutable.ImmutableNode) StructNode(org.sirix.node.interfaces.StructNode) ElementNode(org.sirix.node.ElementNode) NamespaceNode(org.sirix.node.NamespaceNode) NameNode(org.sirix.node.interfaces.NameNode) UnorderedKeyValuePage(org.sirix.page.UnorderedKeyValuePage) SirixDeweyID(org.sirix.node.SirixDeweyID) StructNode(org.sirix.node.interfaces.StructNode)

Aggregations

SirixDeweyID (org.sirix.node.SirixDeweyID)25 StructNode (org.sirix.node.interfaces.StructNode)14 SirixUsageException (org.sirix.exception.SirixUsageException)10 ElementNode (org.sirix.node.ElementNode)6 TextNode (org.sirix.node.TextNode)6 Test (org.junit.Test)5 XdmNodeReadTrx (org.sirix.api.XdmNodeReadTrx)5 XdmNodeWriteTrx (org.sirix.api.XdmNodeWriteTrx)5 DescendantAxis (org.sirix.axis.DescendantAxis)5 NonStructuralWrapperAxis (org.sirix.axis.NonStructuralWrapperAxis)5 UnorderedKeyValuePage (org.sirix.page.UnorderedKeyValuePage)5 CommentNode (org.sirix.node.CommentNode)4 PINode (org.sirix.node.PINode)4 Node (org.sirix.node.interfaces.Node)4 QNm (org.brackit.xquery.atomic.QNm)3 AttributeNode (org.sirix.node.AttributeNode)3 NamespaceNode (org.sirix.node.NamespaceNode)3 NodeDelegate (org.sirix.node.delegates.NodeDelegate)3 NameNode (org.sirix.node.interfaces.NameNode)3 ValueNode (org.sirix.node.interfaces.ValueNode)3