Search in sources :

Example 1 with NameNode

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

the class XdmNodeWriterTrxImpl method adaptSubtreeForMove.

/**
 * Adapt subtree regarding the index-structures.
 *
 * @param toMove node which is moved (either before the move, or after the move)
 * @param type the type of change (either deleted from the old position or inserted into the new
 *        position)
 * @throws SirixIOException if an I/O exception occurs
 */
private void adaptSubtreeForMove(final Node node, final ChangeType type) throws SirixIOException {
    assert type != null;
    final long beforeNodeKey = getNode().getNodeKey();
    moveTo(node.getNodeKey());
    final Axis axis = new DescendantAxis(this, IncludeSelf.YES);
    while (axis.hasNext()) {
        axis.next();
        for (int i = 0, attCount = getAttributeCount(); i < attCount; i++) {
            moveToAttribute(i);
            final ImmutableAttribute att = (ImmutableAttribute) getNode();
            mIndexController.notifyChange(type, att, att.getPathNodeKey());
            moveToParent();
        }
        for (int i = 0, nspCount = getNamespaceCount(); i < nspCount; i++) {
            moveToAttribute(i);
            final ImmutableNamespace nsp = (ImmutableNamespace) getNode();
            mIndexController.notifyChange(type, nsp, nsp.getPathNodeKey());
            moveToParent();
        }
        long pathNodeKey = -1;
        if (getNode() instanceof ValueNode && getNode().getParentKey() != Fixed.DOCUMENT_NODE_KEY.getStandardProperty()) {
            final long nodeKey = getNode().getNodeKey();
            pathNodeKey = moveToParent().get().getNameNode().getPathNodeKey();
            moveTo(nodeKey);
        } else if (getNode() instanceof NameNode) {
            pathNodeKey = getNameNode().getPathNodeKey();
        }
        mIndexController.notifyChange(type, getNode(), pathNodeKey);
    }
    moveTo(beforeNodeKey);
}
Also used : NameNode(org.sirix.node.interfaces.NameNode) ValueNode(org.sirix.node.interfaces.ValueNode) ImmutableNamespace(org.sirix.node.immutable.ImmutableNamespace) DescendantAxis(org.sirix.axis.DescendantAxis) Axis(org.sirix.api.Axis) LevelOrderAxis(org.sirix.axis.LevelOrderAxis) PostOrderAxis(org.sirix.axis.PostOrderAxis) DescendantAxis(org.sirix.axis.DescendantAxis) ImmutableAttribute(org.sirix.node.immutable.ImmutableAttribute)

Example 2 with NameNode

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

the class XdmNodeWriterTrxImpl method setName.

@Override
public XdmNodeWriteTrx setName(final QNm name) throws SirixException {
    checkNotNull(name);
    acquireLock();
    try {
        if (getCurrentNode() instanceof NameNode) {
            if (!getName().equals(name)) {
                checkAccessAndCommit();
                NameNode node = (NameNode) mNodeReader.getCurrentNode();
                final long oldHash = node.hashCode();
                // Create new keys for mapping.
                final int prefixKey = name.getPrefix() != null && !name.getPrefix().isEmpty() ? getPageTransaction().createNameKey(name.getPrefix(), node.getKind()) : -1;
                final int localNameKey = name.getLocalName() != null && !name.getLocalName().isEmpty() ? getPageTransaction().createNameKey(name.getLocalName(), node.getKind()) : -1;
                final int uriKey = name.getNamespaceURI() != null && !name.getNamespaceURI().isEmpty() ? getPageTransaction().createNameKey(name.getNamespaceURI(), Kind.NAMESPACE) : -1;
                // Adapt path summary.
                if (mBuildPathSummary) {
                    mPathSummaryWriter.adaptPathForChangedNode(node, name, uriKey, prefixKey, localNameKey, OPType.SETNAME);
                }
                // Remove old keys from mapping.
                final Kind nodeKind = node.getKind();
                final int oldPrefixKey = node.getPrefixKey();
                final int oldLocalNameKey = node.getLocalNameKey();
                final int oldUriKey = node.getURIKey();
                final NamePage page = ((NamePage) getPageTransaction().getActualRevisionRootPage().getNamePageReference().getPage());
                page.removeName(oldPrefixKey, nodeKind);
                page.removeName(oldLocalNameKey, nodeKind);
                page.removeName(oldUriKey, Kind.NAMESPACE);
                // Set new keys for current node.
                node = (NameNode) getPageTransaction().prepareEntryForModification(node.getNodeKey(), PageKind.RECORDPAGE, -1, Optional.<UnorderedKeyValuePage>empty());
                node.setLocalNameKey(localNameKey);
                node.setURIKey(uriKey);
                node.setPrefixKey(prefixKey);
                node.setPathNodeKey(mBuildPathSummary ? mPathSummaryWriter.getNodeKey() : 0);
                mNodeReader.setCurrentNode(node);
                adaptHashedWithUpdate(oldHash);
            }
            return this;
        } else {
            throw new SirixUsageException("setName is not allowed if current node is not an INameNode implementation!");
        }
    } finally {
        unLock();
    }
}
Also used : NameNode(org.sirix.node.interfaces.NameNode) PageKind(org.sirix.page.PageKind) Kind(org.sirix.node.Kind) NamePage(org.sirix.page.NamePage) SirixUsageException(org.sirix.exception.SirixUsageException)

Example 3 with NameNode

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

the class PathSummaryWriter method processFoundPathNode.

/**
 * Process a found path node.
 *
 * @param oldPathNodeKey key of old path node
 * @param newPathNodeKey key of new path node
 * @param oldNodeKey key of old node
 * @param uriKey key of URI
 * @param prefixKey key of prefix
 * @param localNameKey key of local name
 * @param remove determines if a {@link PathNode} must be removed or not
 * @param type type of operation
 * @throws SirixException if Sirix fails to do so
 */
private void processFoundPathNode(@Nonnegative final long oldPathNodeKey, @Nonnegative final long newPathNodeKey, @Nonnegative final long oldNodeKey, final int uriKey, final int prefixKey, final int localNameKey, final Remove remove, final OPType type) throws SirixException {
    final PathSummaryReader cloned = PathSummaryReader.getInstance(mPageWriteTrx, mNodeRtx.getResourceManager());
    boolean moved = cloned.moveTo(oldPathNodeKey).hasMoved();
    assert moved;
    // Set new reference count of the root.
    if (type != OPType.MOVEDSAMELEVEL) {
        final PathNode currNode = (PathNode) mPageWriteTrx.prepareEntryForModification(mPathSummaryReader.getNodeKey(), PageKind.PATHSUMMARYPAGE, 0, Optional.<UnorderedKeyValuePage>empty());
        currNode.setReferenceCount(currNode.getReferences() + cloned.getReferences());
        currNode.setLocalNameKey(localNameKey);
        currNode.setPrefixKey(prefixKey);
        currNode.setURIKey(uriKey);
    }
    // For all old path nodes: Merge paths and adapt reference counts.
    mPathSummaryReader.moveToFirstChild();
    final int oldLevel = cloned.getLevel();
    for (final Axis oldDescendants = new DescendantAxis(cloned); oldDescendants.hasNext(); ) {
        oldDescendants.next();
        // Search for new path entry.
        final Axis axis = new FilterAxis(new LevelOrderAxis.Builder(mPathSummaryReader).filterLevel(cloned.getLevel() - oldLevel).includeSelf().build(), new NameFilter(mPathSummaryReader, Utils.buildName(cloned.getName())), new PathKindFilter(mPathSummaryReader, cloned.getPathKind()), new PathLevelFilter(mPathSummaryReader, cloned.getLevel()));
        if (axis.hasNext()) {
            axis.next();
            // Set new reference count.
            if (type != OPType.MOVEDSAMELEVEL) {
                final PathNode currNode = (PathNode) mPageWriteTrx.prepareEntryForModification(mPathSummaryReader.getNodeKey(), PageKind.PATHSUMMARYPAGE, 0, Optional.<UnorderedKeyValuePage>empty());
                currNode.setReferenceCount(currNode.getReferences() + cloned.getReferences());
            }
        } else {
            // Insert new node.
            insertPathAsFirstChild(cloned.getName(), cloned.getPathKind(), mPathSummaryReader.getLevel() + 1);
            // Set new reference count.
            final PathNode currNode = (PathNode) mPageWriteTrx.prepareEntryForModification(mPathSummaryReader.getNodeKey(), PageKind.PATHSUMMARYPAGE, 0, Optional.<UnorderedKeyValuePage>empty());
            currNode.setReferenceCount(cloned.getReferences());
        }
        mPathSummaryReader.moveTo(newPathNodeKey);
    }
    // Set new path nodes of the changed nodes, that is set their PCR
    // references.
    mPathSummaryReader.moveTo(newPathNodeKey);
    mNodeRtx.moveTo(oldNodeKey);
    boolean first = true;
    for (final Axis axis = new DescendantAxis(mNodeRtx, IncludeSelf.YES); axis.hasNext(); ) {
        axis.next();
        if (first && type == OPType.SETNAME) {
            first = false;
        } else if (mNodeRtx.getNode() instanceof ImmutableNameNode) {
            cloned.moveTo(((NameNode) mNodeRtx.getCurrentNode()).getPathNodeKey());
            resetPath(newPathNodeKey, cloned.getLevel());
            if (mNodeRtx.getNode().getKind() == Kind.ELEMENT) {
                final ElementNode element = (ElementNode) mNodeRtx.getCurrentNode();
                for (int i = 0, nspCount = element.getNamespaceCount(); i < nspCount; i++) {
                    mNodeRtx.moveToNamespace(i);
                    cloned.moveTo(((NameNode) mNodeRtx.getCurrentNode()).getPathNodeKey());
                    resetPath(newPathNodeKey, cloned.getLevel());
                    mNodeRtx.moveToParent();
                }
                for (int i = 0, attCount = element.getAttributeCount(); i < attCount; i++) {
                    mNodeRtx.moveToAttribute(i);
                    cloned.moveTo(((NameNode) mNodeRtx.getCurrentNode()).getPathNodeKey());
                    resetPath(newPathNodeKey, cloned.getLevel());
                    mNodeRtx.moveToParent();
                }
            }
        }
    }
    // Then: Remove old nodes.
    if (remove == Remove.YES) {
        mPathSummaryReader.moveTo(oldPathNodeKey);
        removePathSummaryNode(remove);
    }
}
Also used : ImmutableNameNode(org.sirix.node.interfaces.immutable.ImmutableNameNode) NameNode(org.sirix.node.interfaces.NameNode) PathLevelFilter(org.sirix.axis.filter.PathLevelFilter) UnorderedKeyValuePage(org.sirix.page.UnorderedKeyValuePage) ElementNode(org.sirix.node.ElementNode) FilterAxis(org.sirix.axis.filter.FilterAxis) NameFilter(org.sirix.axis.filter.NameFilter) ImmutableNameNode(org.sirix.node.interfaces.immutable.ImmutableNameNode) DescendantAxis(org.sirix.axis.DescendantAxis) Axis(org.sirix.api.Axis) ChildAxis(org.sirix.axis.ChildAxis) PostOrderAxis(org.sirix.axis.PostOrderAxis) FilterAxis(org.sirix.axis.filter.FilterAxis) DescendantAxis(org.sirix.axis.DescendantAxis) LevelOrderAxis(org.sirix.axis.LevelOrderAxis) PathKindFilter(org.sirix.axis.filter.PathKindFilter)

Example 4 with NameNode

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

the class PathSummaryWriter method resetPath.

/**
 * Reset the path node key of a node.
 *
 * @param newPathNodeKey path node key of new path node
 * @param oldLevel old level of node
 * @throws SirixIOException if an I/O error occurs
 */
private void resetPath(@Nonnegative final long newPathNodeKey, @Nonnegative final int oldLevel) throws SirixIOException {
    // Search for new path entry.
    mPathSummaryReader.moveTo(newPathNodeKey);
    final Axis filterAxis = new FilterAxis(new LevelOrderAxis.Builder(mPathSummaryReader).includeSelf().build(), new NameFilter(mPathSummaryReader, Utils.buildName(mNodeRtx.getName())), new PathKindFilter(mPathSummaryReader, mNodeRtx.getKind()), new PathLevelFilter(mPathSummaryReader, oldLevel));
    if (filterAxis.hasNext()) {
        filterAxis.next();
        // Set new path node.
        final NameNode node = (NameNode) mPageWriteTrx.prepareEntryForModification(mNodeRtx.getNodeKey(), PageKind.RECORDPAGE, -1, Optional.<UnorderedKeyValuePage>empty());
        node.setPathNodeKey(mPathSummaryReader.getNodeKey());
    } else {
        throw new IllegalStateException();
    }
}
Also used : ImmutableNameNode(org.sirix.node.interfaces.immutable.ImmutableNameNode) NameNode(org.sirix.node.interfaces.NameNode) NameFilter(org.sirix.axis.filter.NameFilter) PathLevelFilter(org.sirix.axis.filter.PathLevelFilter) UnorderedKeyValuePage(org.sirix.page.UnorderedKeyValuePage) Axis(org.sirix.api.Axis) ChildAxis(org.sirix.axis.ChildAxis) PostOrderAxis(org.sirix.axis.PostOrderAxis) FilterAxis(org.sirix.axis.filter.FilterAxis) DescendantAxis(org.sirix.axis.DescendantAxis) LevelOrderAxis(org.sirix.axis.LevelOrderAxis) FilterAxis(org.sirix.axis.filter.FilterAxis) PathKindFilter(org.sirix.axis.filter.PathKindFilter) LevelOrderAxis(org.sirix.axis.LevelOrderAxis)

Example 5 with NameNode

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

the class PathSummaryWriter method resetPathNodeKey.

/**
 * Reset a path node key.
 *
 * @param nodeKey the nodeKey of the node to adapt
 * @throws SirixException if anything fails
 */
private void resetPathNodeKey(@Nonnegative final long nodeKey) throws SirixException {
    final NameNode currNode = (NameNode) mPageWriteTrx.prepareEntryForModification(nodeKey, PageKind.RECORDPAGE, -1, Optional.<UnorderedKeyValuePage>empty());
    currNode.setPathNodeKey(mPathSummaryReader.getNodeKey());
}
Also used : ImmutableNameNode(org.sirix.node.interfaces.immutable.ImmutableNameNode) NameNode(org.sirix.node.interfaces.NameNode) UnorderedKeyValuePage(org.sirix.page.UnorderedKeyValuePage)

Aggregations

NameNode (org.sirix.node.interfaces.NameNode)9 Axis (org.sirix.api.Axis)3 DescendantAxis (org.sirix.axis.DescendantAxis)3 LevelOrderAxis (org.sirix.axis.LevelOrderAxis)3 PostOrderAxis (org.sirix.axis.PostOrderAxis)3 SirixUsageException (org.sirix.exception.SirixUsageException)3 ElementNode (org.sirix.node.ElementNode)3 ValueNode (org.sirix.node.interfaces.ValueNode)3 ImmutableNameNode (org.sirix.node.interfaces.immutable.ImmutableNameNode)3 UnorderedKeyValuePage (org.sirix.page.UnorderedKeyValuePage)3 Optional (java.util.Optional)2 ChildAxis (org.sirix.axis.ChildAxis)2 FilterAxis (org.sirix.axis.filter.FilterAxis)2 NameFilter (org.sirix.axis.filter.NameFilter)2 PathKindFilter (org.sirix.axis.filter.PathKindFilter)2 PathLevelFilter (org.sirix.axis.filter.PathLevelFilter)2 AttributeNode (org.sirix.node.AttributeNode)2 CommentNode (org.sirix.node.CommentNode)2 Kind (org.sirix.node.Kind)2 NamespaceNode (org.sirix.node.NamespaceNode)2