Search in sources :

Example 1 with NameFilter

use of org.sirix.axis.filter.NameFilter in project sirix by sirixdb.

the class PathSummaryWriter method adaptPathForChangedNode.

/**
 * Adapt path summary either for moves or {@code setQName(QName)}.
 *
 * @param node the node for which the path node needs to be adapted
 * @param name the new {@link QName} in case of a new one is set, the old {@link QName} otherwise
 * @param nameKey nameKey of the new node
 * @param uriKey uriKey of the new node
 * @throws SirixException if a Sirix operation fails
 * @throws NullPointerException if {@code pNode} or {@code pQName} is null
 */
public void adaptPathForChangedNode(final ImmutableNameNode node, final QNm name, final int uriKey, final int prefixKey, final int localNameKey, final OPType type) throws SirixException {
    // Possibly either reset a path node or decrement its reference counter
    // and search for the new path node or insert it.
    movePathSummary();
    final long oldPathNodeKey = mPathSummaryReader.getNodeKey();
    // reference-counter would be 0).
    if (type == OPType.SETNAME && mPathSummaryReader.getReferences() == 1) {
        moveSummaryGetLevel(node);
        // Search for new path entry.
        final Axis axis = new FilterAxis(new ChildAxis(mPathSummaryReader), new NameFilter(mPathSummaryReader, Utils.buildName(name)), new PathKindFilter(mPathSummaryReader, node.getKind()));
        if (axis.hasNext()) {
            axis.next();
            // Found node.
            processFoundPathNode(oldPathNodeKey, mPathSummaryReader.getNodeKey(), node.getNodeKey(), uriKey, prefixKey, localNameKey, Remove.YES, type);
        } else {
            if (mPathSummaryReader.getKind() != Kind.DOCUMENT) {
                /* The path summary just needs to be updated for the new renamed node. */
                mPathSummaryReader.moveTo(oldPathNodeKey);
                final PathNode pathNode = (PathNode) mPageWriteTrx.prepareEntryForModification(mPathSummaryReader.getNodeKey(), PageKind.PATHSUMMARYPAGE, 0, Optional.<UnorderedKeyValuePage>empty());
                pathNode.setPrefixKey(prefixKey);
                pathNode.setLocalNameKey(localNameKey);
                pathNode.setURIKey(uriKey);
            }
        }
    } else {
        int level = moveSummaryGetLevel(node);
        // TODO: Johannes: Optimize? (either use this or use the name-mapping,
        // depending on the number of child nodes or nodes with a certain name).
        // Search for new path entry.
        final Axis axis = new FilterAxis(new ChildAxis(mPathSummaryReader), new NameFilter(mPathSummaryReader, Utils.buildName(name)), new PathKindFilter(mPathSummaryReader, node.getKind()));
        if (type == OPType.MOVEDSAMELEVEL || axis.hasNext()) {
            if (type != OPType.MOVEDSAMELEVEL) {
                axis.next();
            }
            // Found node.
            processFoundPathNode(oldPathNodeKey, mPathSummaryReader.getNodeKey(), node.getNodeKey(), uriKey, prefixKey, localNameKey, Remove.NO, type);
        } else {
            long nodeKey = mPathSummaryReader.getNodeKey();
            // Decrement reference count or remove path summary node.
            mNodeRtx.moveTo(node.getNodeKey());
            for (final Axis descendants = new PostOrderAxis(mNodeRtx, IncludeSelf.YES); descendants.hasNext(); ) {
                descendants.next();
                deleteOrDecrement();
                if (mNodeRtx.getKind() == Kind.ELEMENT) {
                    final ElementNode element = (ElementNode) mNodeRtx.getCurrentNode();
                    // Namespaces.
                    for (int i = 0, nsps = element.getNamespaceCount(); i < nsps; i++) {
                        mNodeRtx.moveToNamespace(i);
                        deleteOrDecrement();
                        mNodeRtx.moveToParent();
                    }
                    // Attributes.
                    for (int i = 0, atts = element.getAttributeCount(); i < atts; i++) {
                        mNodeRtx.moveToAttribute(i);
                        deleteOrDecrement();
                        mNodeRtx.moveToParent();
                    }
                }
            }
            mPathSummaryReader.moveTo(nodeKey);
            // Not found => create new path nodes for the whole subtree.
            boolean firstRun = true;
            for (final Axis descendants = new DescendantAxis(mNodeRtx, IncludeSelf.YES); descendants.hasNext(); ) {
                descendants.next();
                if (mNodeRtx.getKind() == Kind.ELEMENT) {
                    // Path Summary : New mapping.
                    if (firstRun) {
                        insertPathAsFirstChild(name, Kind.ELEMENT, ++level);
                        nodeKey = mPathSummaryReader.getNodeKey();
                    } else {
                        insertPathAsFirstChild(mNodeRtx.getName(), Kind.ELEMENT, ++level);
                    }
                    resetPathNodeKey(mNodeRtx.getNodeKey());
                    // Namespaces.
                    for (int i = 0, nsps = mNodeRtx.getNamespaceCount(); i < nsps; i++) {
                        mNodeRtx.moveToNamespace(i);
                        // Path Summary : New mapping.
                        insertPathAsFirstChild(mNodeRtx.getName(), Kind.NAMESPACE, level + 1);
                        resetPathNodeKey(mNodeRtx.getNodeKey());
                        mNodeRtx.moveToParent();
                        mPathSummaryReader.moveToParent();
                    }
                    // Attributes.
                    for (int i = 0, atts = mNodeRtx.getAttributeCount(); i < atts; i++) {
                        mNodeRtx.moveToAttribute(i);
                        // Path Summary : New mapping.
                        insertPathAsFirstChild(mNodeRtx.getName(), Kind.ATTRIBUTE, level + 1);
                        resetPathNodeKey(mNodeRtx.getNodeKey());
                        mNodeRtx.moveToParent();
                        mPathSummaryReader.moveToParent();
                    }
                    if (firstRun) {
                        firstRun = false;
                    } else {
                        mPathSummaryReader.moveToParent();
                        level--;
                    }
                }
            }
            // /*
            // * Remove path nodes with zero node references.
            // *
            // * (TODO: Johannes: might not be necessary, as it's likely that future
            // * updates will reinsert the path).
            // */
            // for (final long key : nodesToDelete) {
            // if (mPathSummaryReader.moveTo(key).hasMoved()) {
            // removePathSummaryNode(Remove.NO);
            // }
            // }
            mPathSummaryReader.moveTo(nodeKey);
        }
    }
}
Also used : ChildAxis(org.sirix.axis.ChildAxis) PostOrderAxis(org.sirix.axis.PostOrderAxis) UnorderedKeyValuePage(org.sirix.page.UnorderedKeyValuePage) ElementNode(org.sirix.node.ElementNode) FilterAxis(org.sirix.axis.filter.FilterAxis) NameFilter(org.sirix.axis.filter.NameFilter) 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 2 with NameFilter

use of org.sirix.axis.filter.NameFilter in project sirix by sirixdb.

the class XPathParser method parseAttributeTest.

/**
 * Parses the the rule AttributeTest according to the following production rule:
 * <p>
 * [59] AttributeTest ::= <"attribute" "("> (AttribNameOrWildcard ("," TypeName)?)? ")" .
 * </p>
 *
 * @return filter
 */
private Filter parseAttributeTest() {
    consume("attribute", true);
    consume(TokenType.OPEN_BR, true);
    Filter filter = new AttributeFilter(getTransaction());
    if (!(mToken.getType() == TokenType.CLOSE_BR)) {
        // add name filter
        final String name = parseAttributeNameOrWildcard();
        if (!name.equals("*")) {
            filter = new NestedFilter(getTransaction(), filter, new NameFilter(getTransaction(), name));
        }
        if (is(TokenType.COMMA, true)) {
            // add type filter
            filter = new NestedFilter(getTransaction(), filter, new TypeFilter(getTransaction(), parseTypeName()));
        }
    }
    consume(TokenType.CLOSE_BR, true);
    return filter;
}
Also used : NestedFilter(org.sirix.axis.filter.NestedFilter) NameFilter(org.sirix.axis.filter.NameFilter) ItemFilter(org.sirix.axis.filter.ItemFilter) AttributeFilter(org.sirix.axis.filter.AttributeFilter) NameFilter(org.sirix.axis.filter.NameFilter) TypeFilter(org.sirix.axis.filter.TypeFilter) NestedFilter(org.sirix.axis.filter.NestedFilter) DocumentRootNodeFilter(org.sirix.axis.filter.DocumentRootNodeFilter) CommentFilter(org.sirix.axis.filter.CommentFilter) WildcardFilter(org.sirix.axis.filter.WildcardFilter) PIFilter(org.sirix.axis.filter.PIFilter) SchemaElementFilter(org.sirix.service.xml.xpath.filter.SchemaElementFilter) Filter(org.sirix.api.Filter) SchemaAttributeFilter(org.sirix.service.xml.xpath.filter.SchemaAttributeFilter) TextFilter(org.sirix.axis.filter.TextFilter) ElementFilter(org.sirix.axis.filter.ElementFilter) NodeFilter(org.sirix.axis.filter.NodeFilter) AttributeFilter(org.sirix.axis.filter.AttributeFilter) SchemaAttributeFilter(org.sirix.service.xml.xpath.filter.SchemaAttributeFilter) TypeFilter(org.sirix.axis.filter.TypeFilter)

Example 3 with NameFilter

use of org.sirix.axis.filter.NameFilter in project sirix by sirixdb.

the class XPathParser method parsePITest.

/**
 * Parses the the rule PITest according to the following production rule:
 * <p>
 * [58] PITest ::= <"processing-instruction" "("> (NCName | StringLiteral)? ")" .
 * </p>
 *
 * @return filter
 */
private Filter parsePITest() {
    consume("processing-instruction", true);
    consume(TokenType.OPEN_BR, true);
    Filter filter = new PIFilter(getTransaction());
    if (!is(TokenType.CLOSE_BR, true)) {
        String stringLiteral;
        if (isQuote()) {
            final byte[] param = ((ValueNode) getTransaction().getItemList().getItem(parseStringLiteral()).get()).getRawValue();
            stringLiteral = Arrays.toString(param);
        } else {
            stringLiteral = parseNCName();
        }
        consume(TokenType.CLOSE_BR, true);
        filter = new NestedFilter(getTransaction(), filter, new NameFilter(getTransaction(), stringLiteral));
    }
    return filter;
}
Also used : NestedFilter(org.sirix.axis.filter.NestedFilter) NameFilter(org.sirix.axis.filter.NameFilter) PIFilter(org.sirix.axis.filter.PIFilter) ItemFilter(org.sirix.axis.filter.ItemFilter) AttributeFilter(org.sirix.axis.filter.AttributeFilter) NameFilter(org.sirix.axis.filter.NameFilter) TypeFilter(org.sirix.axis.filter.TypeFilter) NestedFilter(org.sirix.axis.filter.NestedFilter) DocumentRootNodeFilter(org.sirix.axis.filter.DocumentRootNodeFilter) CommentFilter(org.sirix.axis.filter.CommentFilter) WildcardFilter(org.sirix.axis.filter.WildcardFilter) PIFilter(org.sirix.axis.filter.PIFilter) SchemaElementFilter(org.sirix.service.xml.xpath.filter.SchemaElementFilter) Filter(org.sirix.api.Filter) SchemaAttributeFilter(org.sirix.service.xml.xpath.filter.SchemaAttributeFilter) TextFilter(org.sirix.axis.filter.TextFilter) ElementFilter(org.sirix.axis.filter.ElementFilter) NodeFilter(org.sirix.axis.filter.NodeFilter) ValueNode(org.sirix.node.interfaces.ValueNode)

Example 4 with NameFilter

use of org.sirix.axis.filter.NameFilter in project sirix by sirixdb.

the class ConcurrentAxisTest method testPartConcurrentDescAxis2.

/**
 * Test concurrent.
 *
 * @throws SirixXPathException
 */
// @Bench
@Test
public void testPartConcurrentDescAxis2() throws Exception {
    /* query: //regions/africa//location */
    final int resultNumber = 55;
    final XdmNodeReadTrx firstConcurrRtx = holder.getResourceManager().beginNodeReadTrx();
    final Axis axis = new NestedAxis(new NestedAxis(new FilterAxis(new DescendantAxis(firstConcurrRtx, IncludeSelf.YES), new NameFilter(firstConcurrRtx, "regions")), new FilterAxis(new ChildAxis(firstConcurrRtx), new NameFilter(firstConcurrRtx, "africa"))), new ConcurrentAxis(firstConcurrRtx, new FilterAxis(new DescendantAxis(holder.getReader(), IncludeSelf.YES), new NameFilter(holder.getReader(), "location"))));
    for (int i = 0; i < resultNumber; i++) {
        assertEquals(true, axis.hasNext());
        axis.next();
    }
    assertEquals(axis.hasNext(), false);
}
Also used : ChildAxis(org.sirix.axis.ChildAxis) NameFilter(org.sirix.axis.filter.NameFilter) XdmNodeReadTrx(org.sirix.api.XdmNodeReadTrx) NestedAxis(org.sirix.axis.NestedAxis) DescendantAxis(org.sirix.axis.DescendantAxis) Axis(org.sirix.api.Axis) NestedAxis(org.sirix.axis.NestedAxis) ChildAxis(org.sirix.axis.ChildAxis) FilterAxis(org.sirix.axis.filter.FilterAxis) DescendantAxis(org.sirix.axis.DescendantAxis) XPathAxis(org.sirix.service.xml.xpath.XPathAxis) FilterAxis(org.sirix.axis.filter.FilterAxis) Test(org.junit.Test)

Example 5 with NameFilter

use of org.sirix.axis.filter.NameFilter in project sirix by sirixdb.

the class ConcurrentAxisTest method testConcurrent.

/**
 * Test concurrent.
 *
 * @throws SirixException
 *
 * @throws SirixXPathException
 */
// @Bench
@Test
public void testConcurrent() throws Exception {
    /* query: //regions/africa//location */
    final int resultNumber = 55;
    final XdmNodeReadTrx firstConcurrRtx = holder.getResourceManager().beginNodeReadTrx();
    final XdmNodeReadTrx secondConcurrRtx = holder.getResourceManager().beginNodeReadTrx();
    final XdmNodeReadTrx thirdConcurrRtx = holder.getResourceManager().beginNodeReadTrx();
    final XdmNodeReadTrx firstRtx = holder.getResourceManager().beginNodeReadTrx();
    final XdmNodeReadTrx secondRtx = holder.getResourceManager().beginNodeReadTrx();
    final XdmNodeReadTrx thirdRtx = holder.getResourceManager().beginNodeReadTrx();
    final Axis axis = new NestedAxis(new NestedAxis(new ConcurrentAxis(firstConcurrRtx, new FilterAxis(new DescendantAxis(firstRtx, IncludeSelf.YES), new NameFilter(firstRtx, "regions"))), new ConcurrentAxis(secondConcurrRtx, new FilterAxis(new ChildAxis(secondRtx), new NameFilter(secondRtx, "africa")))), new ConcurrentAxis(thirdConcurrRtx, new FilterAxis(new DescendantAxis(thirdRtx, IncludeSelf.YES), new NameFilter(thirdRtx, "location"))));
    for (int i = 0; i < resultNumber; i++) {
        assertEquals(true, axis.hasNext());
        axis.next();
    }
    assertEquals(false, axis.hasNext());
}
Also used : ChildAxis(org.sirix.axis.ChildAxis) NameFilter(org.sirix.axis.filter.NameFilter) XdmNodeReadTrx(org.sirix.api.XdmNodeReadTrx) NestedAxis(org.sirix.axis.NestedAxis) DescendantAxis(org.sirix.axis.DescendantAxis) Axis(org.sirix.api.Axis) NestedAxis(org.sirix.axis.NestedAxis) ChildAxis(org.sirix.axis.ChildAxis) FilterAxis(org.sirix.axis.filter.FilterAxis) DescendantAxis(org.sirix.axis.DescendantAxis) XPathAxis(org.sirix.service.xml.xpath.XPathAxis) FilterAxis(org.sirix.axis.filter.FilterAxis) Test(org.junit.Test)

Aggregations

NameFilter (org.sirix.axis.filter.NameFilter)16 FilterAxis (org.sirix.axis.filter.FilterAxis)12 Axis (org.sirix.api.Axis)11 DescendantAxis (org.sirix.axis.DescendantAxis)9 Test (org.junit.Test)8 ChildAxis (org.sirix.axis.ChildAxis)8 XdmNodeReadTrx (org.sirix.api.XdmNodeReadTrx)6 NodeFilter (org.sirix.axis.filter.NodeFilter)5 TextFilter (org.sirix.axis.filter.TextFilter)5 LevelOrderAxis (org.sirix.axis.LevelOrderAxis)4 NestedAxis (org.sirix.axis.NestedAxis)4 PostOrderAxis (org.sirix.axis.PostOrderAxis)4 AttributeFilter (org.sirix.axis.filter.AttributeFilter)4 ElementFilter (org.sirix.axis.filter.ElementFilter)4 ItemFilter (org.sirix.axis.filter.ItemFilter)4 NestedFilter (org.sirix.axis.filter.NestedFilter)4 PathKindFilter (org.sirix.axis.filter.PathKindFilter)4 UnorderedKeyValuePage (org.sirix.page.UnorderedKeyValuePage)4 XPathAxis (org.sirix.service.xml.xpath.XPathAxis)4 Filter (org.sirix.api.Filter)3