Search in sources :

Example 1 with CommentNode

use of org.sirix.node.CommentNode 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 2 with CommentNode

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

the class NodeFactoryImpl method createCommentNode.

@Override
public CommentNode createCommentNode(@Nonnegative final long parentKey, @Nonnegative final long leftSibKey, @Nonnegative final long rightSibKey, final byte[] value, final boolean isCompressed, final Optional<SirixDeweyID> id) throws SirixIOException {
    final long revision = mPageWriteTrx.getRevisionNumber();
    final NodeDelegate nodeDel = new NodeDelegate(mPageWriteTrx.getActualRevisionRootPage().getMaxNodeKey() + 1, parentKey, 0, revision, id);
    final boolean compression = isCompressed && value.length > 10;
    final byte[] compressedValue = compression ? Compression.compress(value, Deflater.HUFFMAN_ONLY) : value;
    final ValNodeDelegate valDel = new ValNodeDelegate(nodeDel, compressedValue, compression);
    final StructNodeDelegate structDel = new StructNodeDelegate(nodeDel, Fixed.NULL_NODE_KEY.getStandardProperty(), rightSibKey, leftSibKey, 0, 0);
    return (CommentNode) mPageWriteTrx.createEntry(nodeDel.getNodeKey(), new CommentNode(valDel, structDel), PageKind.RECORDPAGE, -1, Optional.<UnorderedKeyValuePage>empty());
}
Also used : StructNodeDelegate(org.sirix.node.delegates.StructNodeDelegate) ValNodeDelegate(org.sirix.node.delegates.ValNodeDelegate) CommentNode(org.sirix.node.CommentNode) UnorderedKeyValuePage(org.sirix.page.UnorderedKeyValuePage) NodeDelegate(org.sirix.node.delegates.NodeDelegate) StructNodeDelegate(org.sirix.node.delegates.StructNodeDelegate) NameNodeDelegate(org.sirix.node.delegates.NameNodeDelegate) ValNodeDelegate(org.sirix.node.delegates.ValNodeDelegate)

Aggregations

CommentNode (org.sirix.node.CommentNode)2 SirixUsageException (org.sirix.exception.SirixUsageException)1 SirixDeweyID (org.sirix.node.SirixDeweyID)1 NameNodeDelegate (org.sirix.node.delegates.NameNodeDelegate)1 NodeDelegate (org.sirix.node.delegates.NodeDelegate)1 StructNodeDelegate (org.sirix.node.delegates.StructNodeDelegate)1 ValNodeDelegate (org.sirix.node.delegates.ValNodeDelegate)1 StructNode (org.sirix.node.interfaces.StructNode)1 UnorderedKeyValuePage (org.sirix.page.UnorderedKeyValuePage)1