Search in sources :

Example 1 with TextNode

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

the class XdmNodeWriterTrxImpl method insertTextAsFirstChild.

@Override
public XdmNodeWriteTrx insertTextAsFirstChild(final String value) throws SirixException {
    checkNotNull(value);
    acquireLock();
    try {
        if (getCurrentNode() instanceof StructNode && !value.isEmpty()) {
            checkAccessAndCommit();
            final long pathNodeKey = getCurrentNode().getNodeKey();
            final long parentKey = getCurrentNode().getNodeKey();
            final long leftSibKey = Fixed.NULL_NODE_KEY.getStandardProperty();
            final long rightSibKey = ((StructNode) getCurrentNode()).getFirstChildKey();
            // Update value in case of adjacent text nodes.
            if (hasNode(rightSibKey)) {
                moveTo(rightSibKey);
                if (getCurrentNode().getKind() == Kind.TEXT) {
                    setValue(new StringBuilder(value).append(getValue()).toString());
                    adaptHashedWithUpdate(getCurrentNode().getHash());
                    return this;
                }
                moveTo(parentKey);
            }
            // Insert new text node if no adjacent text nodes are found.
            final byte[] textValue = getBytes(value);
            final Optional<SirixDeweyID> id = newFirstChildID();
            final TextNode node = mNodeFactory.createTextNode(parentKey, leftSibKey, rightSibKey, textValue, mCompression, id);
            // Adapt local nodes and hashes.
            mNodeReader.setCurrentNode(node);
            adaptForInsert(node, InsertPos.ASFIRSTCHILD, PageKind.RECORDPAGE);
            mNodeReader.setCurrentNode(node);
            adaptHashesWithAdd();
            // Index text value.
            mIndexController.notifyChange(ChangeType.INSERT, node, pathNodeKey);
            return this;
        } else {
            throw new SirixUsageException("Insert is not allowed if current node is not an ElementNode or TextNode!");
        }
    } finally {
        unLock();
    }
}
Also used : TextNode(org.sirix.node.TextNode) SirixDeweyID(org.sirix.node.SirixDeweyID) SirixUsageException(org.sirix.exception.SirixUsageException) StructNode(org.sirix.node.interfaces.StructNode)

Example 2 with TextNode

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

the class XdmNodeWriterTrxImpl method insertTextAsLeftSibling.

@Override
public XdmNodeWriteTrx insertTextAsLeftSibling(final String value) throws SirixException {
    checkNotNull(value);
    acquireLock();
    try {
        if (getCurrentNode() instanceof StructNode && getCurrentNode().getKind() != Kind.DOCUMENT && !value.isEmpty()) {
            checkAccessAndCommit();
            final long parentKey = getCurrentNode().getParentKey();
            final long leftSibKey = ((StructNode) getCurrentNode()).getLeftSiblingKey();
            final long rightSibKey = getCurrentNode().getNodeKey();
            // Update value in case of adjacent text nodes.
            final StringBuilder builder = new StringBuilder();
            if (getCurrentNode().getKind() == Kind.TEXT) {
                builder.append(value);
            }
            builder.append(getValue());
            if (!value.equals(builder.toString())) {
                setValue(builder.toString());
                return this;
            }
            if (hasNode(leftSibKey)) {
                moveTo(leftSibKey);
                final StringBuilder valueBuilder = new StringBuilder();
                if (getCurrentNode().getKind() == Kind.TEXT) {
                    valueBuilder.append(getValue()).append(builder);
                }
                if (!value.equals(valueBuilder.toString())) {
                    setValue(valueBuilder.toString());
                    return this;
                }
            }
            // Insert new text node if no adjacent text nodes are found.
            moveTo(rightSibKey);
            final byte[] textValue = getBytes(builder.toString());
            final Optional<SirixDeweyID> id = newLeftSiblingID();
            final TextNode node = mNodeFactory.createTextNode(parentKey, leftSibKey, rightSibKey, textValue, mCompression, id);
            // Adapt local nodes and hashes.
            mNodeReader.setCurrentNode(node);
            adaptForInsert(node, InsertPos.ASLEFTSIBLING, PageKind.RECORDPAGE);
            mNodeReader.setCurrentNode(node);
            adaptHashesWithAdd();
            // Get the path node key.
            final long pathNodeKey = moveToParent().get().isElement() ? getNameNode().getPathNodeKey() : -1;
            mNodeReader.setCurrentNode(node);
            // Index text value.
            mIndexController.notifyChange(ChangeType.INSERT, node, pathNodeKey);
            return this;
        } else {
            throw new SirixUsageException("Insert is not allowed if current node is not an Element- or Text-node!");
        }
    } finally {
        unLock();
    }
}
Also used : TextNode(org.sirix.node.TextNode) SirixDeweyID(org.sirix.node.SirixDeweyID) SirixUsageException(org.sirix.exception.SirixUsageException) StructNode(org.sirix.node.interfaces.StructNode)

Example 3 with TextNode

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

the class XdmNodeWriterTrxImpl method insertTextAsRightSibling.

@Override
public XdmNodeWriteTrx insertTextAsRightSibling(final String value) throws SirixException {
    checkNotNull(value);
    acquireLock();
    try {
        if (getCurrentNode() instanceof StructNode && getCurrentNode().getKind() != Kind.DOCUMENT && !value.isEmpty()) {
            checkAccessAndCommit();
            final long parentKey = getCurrentNode().getParentKey();
            final long leftSibKey = getCurrentNode().getNodeKey();
            final long rightSibKey = ((StructNode) getCurrentNode()).getRightSiblingKey();
            // Update value in case of adjacent text nodes.
            final StringBuilder builder = new StringBuilder();
            if (getCurrentNode().getKind() == Kind.TEXT) {
                builder.append(getValue());
            }
            builder.append(value);
            if (!value.equals(builder.toString())) {
                setValue(builder.toString());
                return this;
            }
            if (hasNode(rightSibKey)) {
                moveTo(rightSibKey);
                if (getCurrentNode().getKind() == Kind.TEXT) {
                    builder.append(getValue());
                }
                if (!value.equals(builder.toString())) {
                    setValue(builder.toString());
                    return this;
                }
            }
            // Insert new text node if no adjacent text nodes are found.
            moveTo(leftSibKey);
            final byte[] textValue = getBytes(builder.toString());
            final Optional<SirixDeweyID> id = newRightSiblingID();
            final TextNode node = mNodeFactory.createTextNode(parentKey, leftSibKey, rightSibKey, textValue, mCompression, id);
            // Adapt local nodes and hashes.
            mNodeReader.setCurrentNode(node);
            adaptForInsert(node, InsertPos.ASRIGHTSIBLING, PageKind.RECORDPAGE);
            mNodeReader.setCurrentNode(node);
            adaptHashesWithAdd();
            // Get the path node key.
            final long pathNodeKey = moveToParent().get().isElement() ? getNameNode().getPathNodeKey() : -1;
            mNodeReader.setCurrentNode(node);
            // Index text value.
            mIndexController.notifyChange(ChangeType.INSERT, node, pathNodeKey);
            return this;
        } else {
            throw new SirixUsageException("Insert is not allowed if current node is not an Element- or Text-node or value is empty!");
        }
    } finally {
        unLock();
    }
}
Also used : TextNode(org.sirix.node.TextNode) SirixDeweyID(org.sirix.node.SirixDeweyID) SirixUsageException(org.sirix.exception.SirixUsageException) StructNode(org.sirix.node.interfaces.StructNode)

Example 4 with TextNode

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

the class NodeFactoryImpl method createTextNode.

@Override
public TextNode createTextNode(@Nonnegative final long parentKey, @Nonnegative final long leftSibKey, @Nonnegative final long rightSibKey, @Nonnull 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 (TextNode) mPageWriteTrx.createEntry(nodeDel.getNodeKey(), new TextNode(valDel, structDel), PageKind.RECORDPAGE, -1, Optional.<UnorderedKeyValuePage>empty());
}
Also used : StructNodeDelegate(org.sirix.node.delegates.StructNodeDelegate) ValNodeDelegate(org.sirix.node.delegates.ValNodeDelegate) TextNode(org.sirix.node.TextNode) 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)

Example 5 with TextNode

use of org.sirix.node.TextNode 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)

Aggregations

TextNode (org.sirix.node.TextNode)5 SirixUsageException (org.sirix.exception.SirixUsageException)4 StructNode (org.sirix.node.interfaces.StructNode)4 SirixDeweyID (org.sirix.node.SirixDeweyID)3 Optional (java.util.Optional)1 OPType (org.sirix.index.path.summary.PathSummaryWriter.OPType)1 AttributeNode (org.sirix.node.AttributeNode)1 CommentNode (org.sirix.node.CommentNode)1 ElementNode (org.sirix.node.ElementNode)1 NamespaceNode (org.sirix.node.NamespaceNode)1 PINode (org.sirix.node.PINode)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 NameNode (org.sirix.node.interfaces.NameNode)1 Node (org.sirix.node.interfaces.Node)1 ValueNode (org.sirix.node.interfaces.ValueNode)1 ImmutableNode (org.sirix.node.interfaces.immutable.ImmutableNode)1 UnorderedKeyValuePage (org.sirix.page.UnorderedKeyValuePage)1