Search in sources :

Example 11 with SirixDeweyID

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

the class XdmNodeWriterTrxImpl method insertElementAsLeftSibling.

@Override
public XdmNodeWriteTrx insertElementAsLeftSibling(final QNm name) throws SirixException {
    if (!XMLToken.isValidQName(checkNotNull(name))) {
        throw new IllegalArgumentException("The QName is not valid!");
    }
    acquireLock();
    try {
        if (getCurrentNode() instanceof StructNode && getCurrentNode().getKind() != Kind.DOCUMENT) {
            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 = ((StructNode) getCurrentNode()).getLeftSiblingKey();
            final long rightSibKey = getCurrentNode().getNodeKey();
            final Optional<SirixDeweyID> id = newLeftSiblingID();
            final ElementNode node = mNodeFactory.createElementNode(parentKey, leftSibKey, rightSibKey, 0, name, pathNodeKey, id);
            mNodeReader.setCurrentNode(node);
            adaptForInsert(node, InsertPos.ASLEFTSIBLING, 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 12 with SirixDeweyID

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

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

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

the class XdmNodeWriterTrxImpl method newRightSiblingID.

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

Example 15 with SirixDeweyID

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

the class XdmNodeWriterTrxImpl method insertAttribute.

@Override
public XdmNodeWriteTrx insertAttribute(final QNm name, final String value, final Movement move) throws SirixException {
    checkNotNull(value);
    if (!XMLToken.isValidQName(checkNotNull(name))) {
        throw new IllegalArgumentException("The QName is not valid!");
    }
    acquireLock();
    try {
        if (getCurrentNode().getKind() == Kind.ELEMENT) {
            checkAccessAndCommit();
            /*
         * Update value in case of the same attribute name is found but the attribute to insert has
         * a different value (otherwise an exception is thrown because of a duplicate attribute
         * which would otherwise be inserted!).
         */
            final ElementNode element = (ElementNode) getCurrentNode();
            final Optional<Long> attKey = element.getAttributeKeyByName(name);
            if (attKey.isPresent()) {
                moveTo(attKey.get());
                final QNm qName = getName();
                if (name.equals(qName)) {
                    if (getValue().equals(value)) {
                        throw new SirixUsageException("Duplicate attribute!");
                    } else {
                        setValue(value);
                    }
                }
                moveToParent();
            }
            // Get the path node key.
            final long pathNodeKey = mNodeReader.mResourceManager.getResourceConfig().mPathSummary ? mPathSummaryWriter.getPathNodeKey(name, Kind.ATTRIBUTE) : 0;
            final byte[] attValue = getBytes(value);
            final Optional<SirixDeweyID> id = newAttributeID();
            final long elementKey = getCurrentNode().getNodeKey();
            final AttributeNode node = mNodeFactory.createAttributeNode(elementKey, name, attValue, pathNodeKey, id);
            final Node parentNode = (Node) getPageTransaction().prepareEntryForModification(node.getParentKey(), PageKind.RECORDPAGE, -1, Optional.<UnorderedKeyValuePage>empty());
            ((ElementNode) parentNode).insertAttribute(node.getNodeKey(), node.getPrefixKey() + node.getLocalNameKey());
            mNodeReader.setCurrentNode(node);
            adaptHashesWithAdd();
            // Index text value.
            mIndexController.notifyChange(ChangeType.INSERT, node, pathNodeKey);
            if (move == Movement.TOPARENT) {
                moveToParent();
            }
            return this;
        } else {
            throw new SirixUsageException("Insert is not allowed if current node is not an ElementNode!");
        }
    } finally {
        unLock();
    }
}
Also used : AttributeNode(org.sirix.node.AttributeNode) 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) UnorderedKeyValuePage(org.sirix.page.UnorderedKeyValuePage) ElementNode(org.sirix.node.ElementNode) SirixUsageException(org.sirix.exception.SirixUsageException) SirixDeweyID(org.sirix.node.SirixDeweyID) QNm(org.brackit.xquery.atomic.QNm)

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