Search in sources :

Example 21 with SirixDeweyID

use of org.sirix.node.SirixDeweyID 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 22 with SirixDeweyID

use of org.sirix.node.SirixDeweyID 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 23 with SirixDeweyID

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

the class XdmNodeWriterTrxImpl method newLeftSiblingID.

/**
 * Get an optional left sibling {@link SirixDeweyID} reference.
 *
 * @return optional left sibling {@link SirixDeweyID} reference
 * @throws SirixException if generating an ID fails
 */
private Optional<SirixDeweyID> newLeftSiblingID() throws SirixException {
    Optional<SirixDeweyID> id = Optional.<SirixDeweyID>empty();
    if (mDeweyIDsStored) {
        final SirixDeweyID currID = mNodeReader.getCurrentNode().getDeweyID().get();
        if (mNodeReader.hasLeftSibling()) {
            mNodeReader.moveToLeftSibling();
            id = Optional.of(SirixDeweyID.newBetween(mNodeReader.getCurrentNode().getDeweyID().get(), currID));
            mNodeReader.moveToRightSibling();
        } else {
            id = Optional.of(SirixDeweyID.newBetween(null, currID));
        }
    }
    return id;
}
Also used : SirixDeweyID(org.sirix.node.SirixDeweyID)

Example 24 with SirixDeweyID

use of org.sirix.node.SirixDeweyID 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 25 with SirixDeweyID

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

the class PageWriteTrxImpl method removeEntry.

@Override
public void removeEntry(final Long recordKey, @Nonnull final PageKind pageKind, final int index, final Optional<UnorderedKeyValuePage> keyValuePage) throws SirixIOException {
    mPageRtx.assertNotClosed();
    final long nodePageKey = mPageRtx.pageKey(recordKey);
    final PageContainer cont = prepareRecordPage(nodePageKey, index, pageKind);
    final Optional<Record> node = getRecord(recordKey, pageKind, index);
    if (node.isPresent()) {
        final Record nodeToDel = node.get();
        final Node delNode = new DeletedNode(new NodeDelegate(nodeToDel.getNodeKey(), -1, -1, -1, Optional.<SirixDeweyID>empty()));
        ((UnorderedKeyValuePage) cont.getModified()).setEntry(delNode.getNodeKey(), delNode);
        ((UnorderedKeyValuePage) cont.getComplete()).setEntry(delNode.getNodeKey(), delNode);
    } else {
        throw new IllegalStateException("Node not found!");
    }
}
Also used : PageContainer(org.sirix.cache.PageContainer) Node(org.sirix.node.interfaces.Node) DeletedNode(org.sirix.node.DeletedNode) Record(org.sirix.node.interfaces.Record) DeletedNode(org.sirix.node.DeletedNode) UnorderedKeyValuePage(org.sirix.page.UnorderedKeyValuePage) SirixDeweyID(org.sirix.node.SirixDeweyID) NodeDelegate(org.sirix.node.delegates.NodeDelegate)

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