Search in sources :

Example 11 with ElementNode

use of org.sirix.node.ElementNode 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 12 with ElementNode

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

the class XdmNodeWriterTrxImpl method moveSubtreeToFirstChild.

@Override
public XdmNodeWriteTrx moveSubtreeToFirstChild(@Nonnegative final long fromKey) throws SirixException, IllegalArgumentException {
    acquireLock();
    try {
        Preconditions.checkArgument(fromKey >= 0 && fromKey <= getMaxNodeKey(), "Argument must be a valid node key!");
        Preconditions.checkArgument(fromKey != getCurrentNode().getNodeKey(), "Can't move itself to right sibling of itself!");
        @SuppressWarnings("unchecked") final Optional<? extends Node> node = (Optional<? extends Node>) getPageTransaction().getRecord(fromKey, PageKind.RECORDPAGE, -1);
        if (!node.isPresent()) {
            throw new IllegalStateException("Node to move must exist!");
        }
        final Node nodeToMove = node.get();
        if (nodeToMove instanceof StructNode && getCurrentNode().getKind() == Kind.ELEMENT) {
            // Safe to cast (because IStructNode is a subtype of INode).
            checkAncestors(nodeToMove);
            checkAccessAndCommit();
            final ElementNode nodeAnchor = (ElementNode) getCurrentNode();
            // Check that it's not already the first child.
            if (nodeAnchor.getFirstChildKey() != nodeToMove.getNodeKey()) {
                final StructNode toMove = (StructNode) nodeToMove;
                // Adapt index-structures (before move).
                adaptSubtreeForMove(toMove, ChangeType.DELETE);
                // Adapt hashes.
                adaptHashesForMove(toMove);
                // Adapt pointers and merge sibling text nodes.
                adaptForMove(toMove, nodeAnchor, InsertPos.ASFIRSTCHILD);
                mNodeReader.moveTo(toMove.getNodeKey());
                adaptHashesWithAdd();
                // Adapt path summary.
                if (mBuildPathSummary && toMove instanceof NameNode) {
                    final NameNode moved = (NameNode) toMove;
                    mPathSummaryWriter.adaptPathForChangedNode(moved, getName(), moved.getURIKey(), moved.getPrefixKey(), moved.getLocalNameKey(), OPType.MOVED);
                }
                // Adapt index-structures (after move).
                adaptSubtreeForMove(toMove, ChangeType.INSERT);
                // Compute and assign new DeweyIDs.
                if (mDeweyIDsStored) {
                    computeNewDeweyIDs();
                }
            }
            return this;
        } else {
            throw new SirixUsageException("Move is not allowed if moved node is not an ElementNode and the node isn't inserted at an element node!");
        }
    } finally {
        unLock();
    }
}
Also used : NameNode(org.sirix.node.interfaces.NameNode) Optional(java.util.Optional) 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) ElementNode(org.sirix.node.ElementNode) SirixUsageException(org.sirix.exception.SirixUsageException) StructNode(org.sirix.node.interfaces.StructNode)

Example 13 with ElementNode

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

the class XdmNodeWriterTrxImpl method insertElementAsRightSibling.

@Override
public XdmNodeWriteTrx insertElementAsRightSibling(final QNm name) throws SirixException {
    if (!XMLToken.isValidQName(checkNotNull(name))) {
        throw new IllegalArgumentException("The QName is not valid!");
    }
    acquireLock();
    try {
        if (getCurrentNode() instanceof StructNode && !isDocumentRoot()) {
            checkAccessAndCommit();
            final long key = getCurrentNode().getNodeKey();
            moveToParent();
            final long pathNodeKey = mBuildPathSummary ? mPathSummaryWriter.getPathNodeKey(name, Kind.ELEMENT) : 0;
            moveTo(key);
            final long parentKey = getCurrentNode().getParentKey();
            final long leftSibKey = getCurrentNode().getNodeKey();
            final long rightSibKey = ((StructNode) getCurrentNode()).getRightSiblingKey();
            final Optional<SirixDeweyID> id = newRightSiblingID();
            final ElementNode node = mNodeFactory.createElementNode(parentKey, leftSibKey, rightSibKey, 0, name, pathNodeKey, id);
            mNodeReader.setCurrentNode(node);
            adaptForInsert(node, InsertPos.ASRIGHTSIBLING, PageKind.RECORDPAGE);
            mNodeReader.setCurrentNode(node);
            adaptHashesWithAdd();
            return this;
        } else {
            throw new SirixUsageException("Insert is not allowed if current node is not an StructuralNode (either Text or Element)!");
        }
    } finally {
        unLock();
    }
}
Also used : SirixDeweyID(org.sirix.node.SirixDeweyID) ElementNode(org.sirix.node.ElementNode) SirixUsageException(org.sirix.exception.SirixUsageException) StructNode(org.sirix.node.interfaces.StructNode)

Example 14 with ElementNode

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

the class XdmNodeWriterTrxImpl method moveSubtreeToRightSibling.

@Override
public XdmNodeWriteTrx moveSubtreeToRightSibling(@Nonnegative final long fromKey) throws SirixException {
    acquireLock();
    try {
        if (fromKey < 0 || fromKey > getMaxNodeKey()) {
            throw new IllegalArgumentException("Argument must be a valid node key!");
        }
        if (fromKey == getCurrentNode().getNodeKey()) {
            throw new IllegalArgumentException("Can't move itself to first child of itself!");
        }
        // Save: Every node in the "usual" node page is of type Node.
        @SuppressWarnings("unchecked") final Optional<? extends Node> node = (Optional<? extends Node>) getPageTransaction().getRecord(fromKey, PageKind.RECORDPAGE, -1);
        if (!node.isPresent()) {
            throw new IllegalStateException("Node to move must exist!");
        }
        final Node nodeToMove = node.get();
        if (nodeToMove instanceof StructNode && getCurrentNode() instanceof StructNode) {
            final StructNode toMove = (StructNode) nodeToMove;
            checkAncestors(toMove);
            checkAccessAndCommit();
            final StructNode nodeAnchor = (StructNode) getCurrentNode();
            if (nodeAnchor.getRightSiblingKey() != nodeToMove.getNodeKey()) {
                final long parentKey = toMove.getParentKey();
                // Adapt hashes.
                adaptHashesForMove(toMove);
                // Adapt pointers and merge sibling text nodes.
                adaptForMove(toMove, nodeAnchor, InsertPos.ASRIGHTSIBLING);
                mNodeReader.moveTo(toMove.getNodeKey());
                adaptHashesWithAdd();
                // Adapt path summary.
                if (mBuildPathSummary && toMove instanceof NameNode) {
                    final NameNode moved = (NameNode) toMove;
                    final OPType type = moved.getParentKey() == parentKey ? OPType.MOVEDSAMELEVEL : OPType.MOVED;
                    mPathSummaryWriter.adaptPathForChangedNode(moved, getName(), moved.getURIKey(), moved.getPrefixKey(), moved.getLocalNameKey(), type);
                }
                // Recompute DeweyIDs if they are used.
                if (mDeweyIDsStored) {
                    computeNewDeweyIDs();
                }
            }
            return this;
        } else {
            throw new SirixUsageException("Move is not allowed if moved node is not an ElementNode or TextNode and the node isn't inserted at an ElementNode or TextNode!");
        }
    } finally {
        unLock();
    }
}
Also used : NameNode(org.sirix.node.interfaces.NameNode) Optional(java.util.Optional) OPType(org.sirix.index.path.summary.PathSummaryWriter.OPType) 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) SirixUsageException(org.sirix.exception.SirixUsageException) StructNode(org.sirix.node.interfaces.StructNode)

Example 15 with ElementNode

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

the class NodeFactoryImpl method createElementNode.

@Override
public ElementNode createElementNode(@Nonnegative final long parentKey, @Nonnegative final long leftSibKey, @Nonnegative final long rightSibKey, final long hash, @Nonnull final QNm name, @Nonnegative final long pathNodeKey, final Optional<SirixDeweyID> id) throws SirixIOException {
    final int uriKey = name.getNamespaceURI() != null && !name.getNamespaceURI().isEmpty() ? mPageWriteTrx.createNameKey(name.getNamespaceURI(), Kind.NAMESPACE) : -1;
    final int prefixKey = name.getPrefix() != null && !name.getPrefix().isEmpty() ? mPageWriteTrx.createNameKey(name.getPrefix(), Kind.ELEMENT) : -1;
    final int localNameKey = mPageWriteTrx.createNameKey(name.getLocalName(), Kind.ELEMENT);
    final long revision = mPageWriteTrx.getRevisionNumber();
    final NodeDelegate nodeDel = new NodeDelegate(mPageWriteTrx.getActualRevisionRootPage().getMaxNodeKey() + 1, parentKey, 0, revision, id);
    final StructNodeDelegate structDel = new StructNodeDelegate(nodeDel, Fixed.NULL_NODE_KEY.getStandardProperty(), rightSibKey, leftSibKey, 0, 0);
    final NameNodeDelegate nameDel = new NameNodeDelegate(nodeDel, uriKey, prefixKey, localNameKey, pathNodeKey);
    return (ElementNode) mPageWriteTrx.createEntry(nodeDel.getNodeKey(), new ElementNode(structDel, nameDel, new ArrayList<Long>(), HashBiMap.<Long, Long>create(), new ArrayList<Long>(), name), PageKind.RECORDPAGE, -1, Optional.<UnorderedKeyValuePage>empty());
}
Also used : StructNodeDelegate(org.sirix.node.delegates.StructNodeDelegate) UnorderedKeyValuePage(org.sirix.page.UnorderedKeyValuePage) ElementNode(org.sirix.node.ElementNode) NodeDelegate(org.sirix.node.delegates.NodeDelegate) StructNodeDelegate(org.sirix.node.delegates.StructNodeDelegate) NameNodeDelegate(org.sirix.node.delegates.NameNodeDelegate) ValNodeDelegate(org.sirix.node.delegates.ValNodeDelegate) NameNodeDelegate(org.sirix.node.delegates.NameNodeDelegate)

Aggregations

ElementNode (org.sirix.node.ElementNode)15 StructNode (org.sirix.node.interfaces.StructNode)10 SirixUsageException (org.sirix.exception.SirixUsageException)8 UnorderedKeyValuePage (org.sirix.page.UnorderedKeyValuePage)7 NameNode (org.sirix.node.interfaces.NameNode)6 ImmutableNode (org.sirix.node.interfaces.immutable.ImmutableNode)6 AttributeNode (org.sirix.node.AttributeNode)5 CommentNode (org.sirix.node.CommentNode)5 NamespaceNode (org.sirix.node.NamespaceNode)5 PINode (org.sirix.node.PINode)5 SirixDeweyID (org.sirix.node.SirixDeweyID)5 TextNode (org.sirix.node.TextNode)5 Node (org.sirix.node.interfaces.Node)5 ValueNode (org.sirix.node.interfaces.ValueNode)5 PostOrderAxis (org.sirix.axis.PostOrderAxis)4 QNm (org.brackit.xquery.atomic.QNm)3 Axis (org.sirix.api.Axis)3 DescendantAxis (org.sirix.axis.DescendantAxis)3 LevelOrderAxis (org.sirix.axis.LevelOrderAxis)3 Optional (java.util.Optional)2