Search in sources :

Example 16 with SirixUsageException

use of org.sirix.exception.SirixUsageException in project sirix by sirixdb.

the class XdmNodeWriterTrxImpl method pi.

/**
 * Processing instruction.
 *
 * @param target target PI
 * @param content content of PI
 * @param insert insertion location
 * @throws SirixException if any unexpected error occurs
 */
private XdmNodeWriteTrx pi(final String target, final String content, final Insert insert) throws SirixException {
    final byte[] targetBytes = getBytes(target);
    if (!XMLToken.isNCName(checkNotNull(targetBytes))) {
        throw new IllegalArgumentException("The target is not valid!");
    }
    if (content.contains("?>-")) {
        throw new SirixUsageException("The content must not contain '?>-'");
    }
    acquireLock();
    try {
        if (getCurrentNode() instanceof StructNode) {
            checkAccessAndCommit();
            // Insert new processing instruction node.
            final byte[] processingContent = getBytes(content);
            long parentKey = 0;
            long leftSibKey = 0;
            long rightSibKey = 0;
            InsertPos pos = InsertPos.ASFIRSTCHILD;
            Optional<SirixDeweyID> id = Optional.<SirixDeweyID>empty();
            switch(insert) {
                case ASFIRSTCHILD:
                    parentKey = getCurrentNode().getNodeKey();
                    leftSibKey = Fixed.NULL_NODE_KEY.getStandardProperty();
                    rightSibKey = ((StructNode) getCurrentNode()).getFirstChildKey();
                    id = newFirstChildID();
                    break;
                case ASRIGHTSIBLING:
                    parentKey = getCurrentNode().getParentKey();
                    leftSibKey = getCurrentNode().getNodeKey();
                    rightSibKey = ((StructNode) getCurrentNode()).getRightSiblingKey();
                    pos = InsertPos.ASRIGHTSIBLING;
                    id = newRightSiblingID();
                    break;
                case ASLEFTSIBLING:
                    parentKey = getCurrentNode().getParentKey();
                    leftSibKey = ((StructNode) getCurrentNode()).getLeftSiblingKey();
                    rightSibKey = getCurrentNode().getNodeKey();
                    pos = InsertPos.ASLEFTSIBLING;
                    id = newLeftSiblingID();
                    break;
                default:
                    throw new IllegalStateException("Insert location not known!");
            }
            final QNm targetName = new QNm(target);
            final long pathNodeKey = mBuildPathSummary ? mPathSummaryWriter.getPathNodeKey(targetName, Kind.PROCESSING_INSTRUCTION) : 0;
            final PINode node = mNodeFactory.createPINode(parentKey, leftSibKey, rightSibKey, targetName, processingContent, mCompression, pathNodeKey, id);
            // Adapt local nodes and hashes.
            mNodeReader.setCurrentNode(node);
            adaptForInsert(node, pos, PageKind.RECORDPAGE);
            mNodeReader.setCurrentNode(node);
            adaptHashesWithAdd();
            return this;
        } else {
            throw new SirixUsageException("Current node must be a structural node!");
        }
    } finally {
        unLock();
    }
}
Also used : QNm(org.brackit.xquery.atomic.QNm) PINode(org.sirix.node.PINode) SirixUsageException(org.sirix.exception.SirixUsageException) SirixDeweyID(org.sirix.node.SirixDeweyID) StructNode(org.sirix.node.interfaces.StructNode)

Example 17 with SirixUsageException

use of org.sirix.exception.SirixUsageException in project sirix by sirixdb.

the class XdmNodeWriterTrxImpl method comment.

/**
 * Comment node.
 *
 * @param value value of comment
 * @param insert insertion location
 * @throws SirixException if any unexpected error occurs
 */
private XdmNodeWriteTrx comment(final String value, final Insert insert) throws SirixException {
    // Produces a NPE if value is null (what we want).
    if (value.contains("--")) {
        throw new SirixUsageException("Character sequence \"--\" is not allowed in comment content!");
    }
    if (value.endsWith("-")) {
        throw new SirixUsageException("Comment content must not end with \"-\"!");
    }
    acquireLock();
    try {
        if (getCurrentNode() instanceof StructNode && (getCurrentNode().getKind() != Kind.DOCUMENT || (getCurrentNode().getKind() == Kind.DOCUMENT && insert == Insert.ASFIRSTCHILD))) {
            checkAccessAndCommit();
            // Insert new comment node.
            final byte[] commentValue = getBytes(value);
            long parentKey = 0;
            long leftSibKey = 0;
            long rightSibKey = 0;
            InsertPos pos = InsertPos.ASFIRSTCHILD;
            Optional<SirixDeweyID> id = Optional.<SirixDeweyID>empty();
            switch(insert) {
                case ASFIRSTCHILD:
                    parentKey = getCurrentNode().getNodeKey();
                    leftSibKey = Fixed.NULL_NODE_KEY.getStandardProperty();
                    rightSibKey = ((StructNode) getCurrentNode()).getFirstChildKey();
                    id = newFirstChildID();
                    break;
                case ASRIGHTSIBLING:
                    parentKey = getCurrentNode().getParentKey();
                    leftSibKey = getCurrentNode().getNodeKey();
                    rightSibKey = ((StructNode) getCurrentNode()).getRightSiblingKey();
                    pos = InsertPos.ASRIGHTSIBLING;
                    id = newRightSiblingID();
                    break;
                case ASLEFTSIBLING:
                    parentKey = getCurrentNode().getParentKey();
                    leftSibKey = ((StructNode) getCurrentNode()).getLeftSiblingKey();
                    rightSibKey = getCurrentNode().getNodeKey();
                    pos = InsertPos.ASLEFTSIBLING;
                    id = newLeftSiblingID();
                    break;
                default:
                    throw new IllegalStateException("Insert location not known!");
            }
            final CommentNode node = mNodeFactory.createCommentNode(parentKey, leftSibKey, rightSibKey, commentValue, mCompression, id);
            // Adapt local nodes and hashes.
            mNodeReader.setCurrentNode(node);
            adaptForInsert(node, pos, PageKind.RECORDPAGE);
            mNodeReader.setCurrentNode(node);
            adaptHashesWithAdd();
            return this;
        } else {
            throw new SirixUsageException("Current node must be a structural node!");
        }
    } finally {
        unLock();
    }
}
Also used : CommentNode(org.sirix.node.CommentNode) SirixUsageException(org.sirix.exception.SirixUsageException) SirixDeweyID(org.sirix.node.SirixDeweyID) StructNode(org.sirix.node.interfaces.StructNode)

Example 18 with SirixUsageException

use of org.sirix.exception.SirixUsageException 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 19 with SirixUsageException

use of org.sirix.exception.SirixUsageException 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 20 with SirixUsageException

use of org.sirix.exception.SirixUsageException in project sirix by sirixdb.

the class XdmNodeWriterTrxImpl method close.

@Override
public void close() {
    acquireLock();
    try {
        if (!isClosed()) {
            // Make sure to commit all dirty data.
            if (mModificationCount > 0) {
                throw new SirixUsageException("Must commit/rollback transaction first!");
            }
            // Release all state immediately.
            mNodeReader.mResourceManager.closeWriteTransaction(getId());
            mNodeReader.close();
            removeCommitFile();
            mPathSummaryWriter = null;
            mNodeFactory = null;
            // Shutdown pool.
            mPool.shutdown();
            try {
                mPool.awaitTermination(2, TimeUnit.SECONDS);
            } catch (final InterruptedException e) {
                throw new SirixThreadedException(e);
            }
        }
    } finally {
        unLock();
    }
}
Also used : SirixThreadedException(org.sirix.exception.SirixThreadedException) SirixUsageException(org.sirix.exception.SirixUsageException)

Aggregations

SirixUsageException (org.sirix.exception.SirixUsageException)22 StructNode (org.sirix.node.interfaces.StructNode)13 SirixDeweyID (org.sirix.node.SirixDeweyID)10 ElementNode (org.sirix.node.ElementNode)8 TextNode (org.sirix.node.TextNode)7 Node (org.sirix.node.interfaces.Node)6 UnorderedKeyValuePage (org.sirix.page.UnorderedKeyValuePage)6 CommentNode (org.sirix.node.CommentNode)5 PINode (org.sirix.node.PINode)5 NameNode (org.sirix.node.interfaces.NameNode)5 ValueNode (org.sirix.node.interfaces.ValueNode)5 ImmutableNode (org.sirix.node.interfaces.immutable.ImmutableNode)5 QNm (org.brackit.xquery.atomic.QNm)4 SirixThreadedException (org.sirix.exception.SirixThreadedException)4 AttributeNode (org.sirix.node.AttributeNode)4 NamespaceNode (org.sirix.node.NamespaceNode)4 Optional (java.util.Optional)2 AtomicLong (java.util.concurrent.atomic.AtomicLong)2 Axis (org.sirix.api.Axis)2 XdmNodeReadTrx (org.sirix.api.XdmNodeReadTrx)2