Search in sources :

Example 11 with StructNode

use of org.sirix.node.interfaces.StructNode in project sirix by sirixdb.

the class XdmNodeWriterTrxImpl method addParentHash.

/**
 * Add a hash.
 *
 * @param startNode start node
 */
private void addParentHash(final ImmutableNode startNode) throws SirixIOException {
    switch(mHashKind) {
        case ROLLING:
            final long hashToAdd = mHash.hashLong(startNode.hashCode()).asLong();
            final Node node = (Node) getPageTransaction().prepareEntryForModification(mNodeReader.getCurrentNode().getNodeKey(), PageKind.RECORDPAGE, -1, Optional.empty());
            node.setHash(node.getHash() + hashToAdd * PRIME);
            if (startNode instanceof StructNode) {
                ((StructNode) node).setDescendantCount(((StructNode) node).getDescendantCount() + ((StructNode) startNode).getDescendantCount() + 1);
            }
            break;
        case POSTORDER:
            break;
        default:
    }
}
Also used : 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) StructNode(org.sirix.node.interfaces.StructNode)

Example 12 with StructNode

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

use of org.sirix.node.interfaces.StructNode in project sirix by sirixdb.

the class XdmNodeWriterTrxImpl method setAddDescendants.

/**
 * Set new descendant count of ancestor after an add-operation.
 *
 * @param startNode the node which has been removed
 * @param nodeToModify node to modify
 * @param descendantCount the descendantCount to add
 */
private void setAddDescendants(final ImmutableNode startNode, final Node nodeToModifiy, @Nonnegative final long descendantCount) {
    assert startNode != null;
    assert descendantCount >= 0;
    assert nodeToModifiy != null;
    if (startNode instanceof StructNode) {
        final StructNode node = (StructNode) nodeToModifiy;
        final long oldDescendantCount = node.getDescendantCount();
        node.setDescendantCount(oldDescendantCount + descendantCount);
    }
}
Also used : StructNode(org.sirix.node.interfaces.StructNode)

Example 14 with StructNode

use of org.sirix.node.interfaces.StructNode 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 15 with StructNode

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

Aggregations

StructNode (org.sirix.node.interfaces.StructNode)41 SirixDeweyID (org.sirix.node.SirixDeweyID)12 SirixUsageException (org.sirix.exception.SirixUsageException)11 ElementNode (org.sirix.node.ElementNode)10 TextNode (org.sirix.node.TextNode)8 ImmutableNode (org.sirix.node.interfaces.immutable.ImmutableNode)8 UnorderedKeyValuePage (org.sirix.page.UnorderedKeyValuePage)8 CommentNode (org.sirix.node.CommentNode)6 PINode (org.sirix.node.PINode)6 AttributeNode (org.sirix.node.AttributeNode)5 Kind (org.sirix.node.Kind)5 NamespaceNode (org.sirix.node.NamespaceNode)5 NameNode (org.sirix.node.interfaces.NameNode)5 Node (org.sirix.node.interfaces.Node)5 ValueNode (org.sirix.node.interfaces.ValueNode)5 PageKind (org.sirix.page.PageKind)5 Optional (java.util.Optional)3 PostOrderAxis (org.sirix.axis.PostOrderAxis)3 Axis (org.sirix.api.Axis)2 DescendantAxis (org.sirix.axis.DescendantAxis)2