Search in sources :

Example 1 with ImmutableNode

use of org.sirix.node.interfaces.immutable.ImmutableNode in project sirix by sirixdb.

the class XdmNodeReadTrxImpl method moveTo.

@Override
public Move<? extends XdmNodeReadTrx> moveTo(final long nodeKey) {
    assertNotClosed();
    // Remember old node and fetch new one.
    final ImmutableNode oldNode = mCurrentNode;
    Optional<? extends Record> newNode;
    try {
        // Immediately return node from item list if node key negative.
        if (nodeKey < 0) {
            if (mItemList.size() > 0) {
                newNode = mItemList.getItem(nodeKey);
            } else {
                newNode = Optional.empty();
            }
        } else {
            final Optional<? extends Record> node = mPageReadTrx.getRecord(nodeKey, PageKind.RECORDPAGE, -1);
            newNode = node;
        }
    } catch (final SirixIOException e) {
        newNode = Optional.empty();
    }
    if (newNode.isPresent()) {
        mCurrentNode = (Node) newNode.get();
        return Move.moved(this);
    } else {
        mCurrentNode = oldNode;
        return Move.notMoved();
    }
}
Also used : ImmutableNode(org.sirix.node.interfaces.immutable.ImmutableNode) SirixIOException(org.sirix.exception.SirixIOException)

Example 2 with ImmutableNode

use of org.sirix.node.interfaces.immutable.ImmutableNode in project sirix by sirixdb.

the class XdmNodeWriterTrxImpl method rollingUpdate.

/**
 * Adapting the structure with a rolling hash for all ancestors only with update.
 *
 * @param oldHash pOldHash to be removed
 * @throws SirixIOException if anything weird happened
 */
private void rollingUpdate(final long oldHash) throws SirixIOException {
    final ImmutableNode newNode = getCurrentNode();
    final long hash = newNode.hashCode();
    final long newNodeHash = hash;
    long resultNew = hash;
    // go the path to the root
    do {
        final Node node = (Node) getPageTransaction().prepareEntryForModification(mNodeReader.getCurrentNode().getNodeKey(), PageKind.RECORDPAGE, -1, Optional.<UnorderedKeyValuePage>empty());
        if (node.getNodeKey() == newNode.getNodeKey()) {
            resultNew = node.getHash() - oldHash;
            resultNew = resultNew + newNodeHash;
        } else {
            resultNew = node.getHash() - oldHash * PRIME;
            resultNew = resultNew + newNodeHash * PRIME;
        }
        node.setHash(resultNew);
    } while (moveTo(mNodeReader.getCurrentNode().getParentKey()).hasMoved());
    mNodeReader.setCurrentNode(newNode);
}
Also used : ImmutableNode(org.sirix.node.interfaces.immutable.ImmutableNode) 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)

Example 3 with ImmutableNode

use of org.sirix.node.interfaces.immutable.ImmutableNode 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 4 with ImmutableNode

use of org.sirix.node.interfaces.immutable.ImmutableNode in project sirix by sirixdb.

the class XdmNodeWriterTrxImpl method addHashAndDescendantCount.

/**
 * Add a hash and the descendant count.
 */
private void addHashAndDescendantCount() throws SirixIOException {
    switch(mHashKind) {
        case ROLLING:
            // Setup.
            final ImmutableNode startNode = getCurrentNode();
            final long oldDescendantCount = mNodeReader.getStructuralNode().getDescendantCount();
            final long descendantCount = oldDescendantCount == 0 ? 1 : oldDescendantCount + 1;
            // Set start node.
            final long hashToAdd = mHash.hashLong(startNode.hashCode()).asLong();
            Node node = (Node) getPageTransaction().prepareEntryForModification(mNodeReader.getCurrentNode().getNodeKey(), PageKind.RECORDPAGE, -1, Optional.<UnorderedKeyValuePage>empty());
            node.setHash(hashToAdd);
            // Set parent node.
            if (startNode.hasParent()) {
                moveToParent();
                node = (Node) getPageTransaction().prepareEntryForModification(mNodeReader.getCurrentNode().getNodeKey(), PageKind.RECORDPAGE, -1, Optional.<UnorderedKeyValuePage>empty());
                node.setHash(node.getHash() + hashToAdd * PRIME);
                setAddDescendants(startNode, node, descendantCount);
            }
            mNodeReader.setCurrentNode(startNode);
            break;
        case POSTORDER:
            postorderAdd();
            break;
        default:
    }
}
Also used : ImmutableNode(org.sirix.node.interfaces.immutable.ImmutableNode) 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)

Example 5 with ImmutableNode

use of org.sirix.node.interfaces.immutable.ImmutableNode in project sirix by sirixdb.

the class XdmNodeWriterTrxImpl method postorderAdd.

/**
 * Adapting the structure with a rolling hash for all ancestors only with insert.
 *
 * @throws SirixIOException if anything weird happened
 */
private void postorderAdd() throws SirixIOException {
    // start with hash to add
    final ImmutableNode startNode = getCurrentNode();
    // long for adapting the hash of the parent
    long hashCodeForParent = 0;
    // adapting the parent if the current node is no structural one.
    if (!(startNode instanceof StructNode)) {
        final Node node = (Node) getPageTransaction().prepareEntryForModification(mNodeReader.getCurrentNode().getNodeKey(), PageKind.RECORDPAGE, -1, Optional.<UnorderedKeyValuePage>empty());
        node.setHash(mHash.hashLong(mNodeReader.getCurrentNode().hashCode()).asLong());
        moveTo(mNodeReader.getCurrentNode().getParentKey());
    }
    // Cursor to root
    StructNode cursorToRoot;
    do {
        cursorToRoot = (StructNode) getPageTransaction().prepareEntryForModification(mNodeReader.getCurrentNode().getNodeKey(), PageKind.RECORDPAGE, -1, Optional.<UnorderedKeyValuePage>empty());
        hashCodeForParent = mNodeReader.getCurrentNode().hashCode() + hashCodeForParent * PRIME;
        // Caring about attributes and namespaces if node is an element.
        if (cursorToRoot.getKind() == Kind.ELEMENT) {
            final ElementNode currentElement = (ElementNode) cursorToRoot;
            // setting the attributes and namespaces
            final int attCount = ((ElementNode) cursorToRoot).getAttributeCount();
            for (int i = 0; i < attCount; i++) {
                moveTo(currentElement.getAttributeKey(i));
                hashCodeForParent = mNodeReader.getCurrentNode().hashCode() + hashCodeForParent * PRIME;
            }
            final int nspCount = ((ElementNode) cursorToRoot).getNamespaceCount();
            for (int i = 0; i < nspCount; i++) {
                moveTo(currentElement.getNamespaceKey(i));
                hashCodeForParent = mNodeReader.getCurrentNode().hashCode() + hashCodeForParent * PRIME;
            }
            moveTo(cursorToRoot.getNodeKey());
        }
        // Caring about the children of a node
        if (moveTo(mNodeReader.getStructuralNode().getFirstChildKey()).hasMoved()) {
            do {
                hashCodeForParent = mNodeReader.getCurrentNode().getHash() + hashCodeForParent * PRIME;
            } while (moveTo(mNodeReader.getStructuralNode().getRightSiblingKey()).hasMoved());
            moveTo(mNodeReader.getStructuralNode().getParentKey());
        }
        // setting hash and resetting hash
        cursorToRoot.setHash(hashCodeForParent);
        hashCodeForParent = 0;
    } while (moveTo(cursorToRoot.getParentKey()).hasMoved());
    mNodeReader.setCurrentNode(startNode);
}
Also used : ImmutableNode(org.sirix.node.interfaces.immutable.ImmutableNode) 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) StructNode(org.sirix.node.interfaces.StructNode)

Aggregations

ImmutableNode (org.sirix.node.interfaces.immutable.ImmutableNode)11 StructNode (org.sirix.node.interfaces.StructNode)9 ElementNode (org.sirix.node.ElementNode)7 AttributeNode (org.sirix.node.AttributeNode)6 CommentNode (org.sirix.node.CommentNode)6 NamespaceNode (org.sirix.node.NamespaceNode)6 PINode (org.sirix.node.PINode)6 TextNode (org.sirix.node.TextNode)6 NameNode (org.sirix.node.interfaces.NameNode)6 Node (org.sirix.node.interfaces.Node)6 ValueNode (org.sirix.node.interfaces.ValueNode)6 UnorderedKeyValuePage (org.sirix.page.UnorderedKeyValuePage)6 XMLShredder (org.sirix.service.xml.shredder.XMLShredder)2 XMLEventReader (javax.xml.stream.XMLEventReader)1 XMLEvent (javax.xml.stream.events.XMLEvent)1 Axis (org.sirix.api.Axis)1 DescendantAxis (org.sirix.axis.DescendantAxis)1 LevelOrderAxis (org.sirix.axis.LevelOrderAxis)1 PostOrderAxis (org.sirix.axis.PostOrderAxis)1 SirixIOException (org.sirix.exception.SirixIOException)1