Search in sources :

Example 16 with Constraint

use of javax.jcr.query.qom.Constraint in project jackrabbit-oak by apache.

the class QomTest method and.

@Test
public void and() throws RepositoryException {
    Constraint c0 = f.propertyExistence("x", "c0");
    Constraint c1 = f.propertyExistence("x", "c1");
    And and = f.and(c0, c1);
    assertEquals(and.getConstraint1(), c0);
    assertEquals(and.getConstraint2(), c1);
    assertEquals("([x].[c0] IS NOT NULL) AND ([x].[c1] IS NOT NULL)", and.toString());
}
Also used : Constraint(javax.jcr.query.qom.Constraint) And(javax.jcr.query.qom.And) Test(org.junit.Test) AbstractRepositoryTest(org.apache.jackrabbit.oak.jcr.AbstractRepositoryTest)

Example 17 with Constraint

use of javax.jcr.query.qom.Constraint in project jackrabbit-oak by apache.

the class QomTest method or.

@Test
public void or() throws RepositoryException {
    Constraint c0 = f.propertyExistence("x", "c0");
    Constraint c1 = f.propertyExistence("x", "c1");
    Or or = f.or(c0, c1);
    assertEquals(or.getConstraint1(), c0);
    assertEquals(or.getConstraint2(), c1);
    assertEquals("([x].[c0] IS NOT NULL) OR ([x].[c1] IS NOT NULL)", or.toString());
}
Also used : Or(javax.jcr.query.qom.Or) Constraint(javax.jcr.query.qom.Constraint) Test(org.junit.Test) AbstractRepositoryTest(org.apache.jackrabbit.oak.jcr.AbstractRepositoryTest)

Example 18 with Constraint

use of javax.jcr.query.qom.Constraint in project jackrabbit by apache.

the class QueryEngine method execute.

protected QueryResult execute(JoinMerger merger, ConstraintSplitInfo csInfo, boolean isOuterJoin, int printIndentation) throws RepositoryException {
    Comparator<Row> leftCo = new RowPathComparator(merger.getLeftSelectors());
    long timeJoinLeftSide = System.currentTimeMillis();
    if (csInfo.isMultiple()) {
        log.debug("{} SQL2 JOIN execute: there are multiple inner splits.", genString(printIndentation));
        // first branch
        long bTime = System.currentTimeMillis();
        QueryResult branch1 = execute(merger, csInfo.getLeftInnerConstraints(), isOuterJoin, printIndentation + printIndentStep);
        Set<Row> allRows = new TreeSet<Row>(new RowPathComparator(Arrays.asList(merger.getSelectorNames())));
        RowIterator ri1 = branch1.getRows();
        while (ri1.hasNext()) {
            Row r = ri1.nextRow();
            allRows.add(r);
        }
        log.debug("{} SQL2 JOIN executed first branch, took {} ms.", genString(printIndentation), System.currentTimeMillis() - bTime);
        // second branch
        bTime = System.currentTimeMillis();
        QueryResult branch2 = execute(merger, csInfo.getRightInnerConstraints(), isOuterJoin, printIndentation + printIndentStep);
        RowIterator ri2 = branch2.getRows();
        while (ri2.hasNext()) {
            Row r = ri2.nextRow();
            allRows.add(r);
        }
        log.debug("{} SQL2 JOIN executed second branch, took {} ms.", genString(printIndentation), System.currentTimeMillis() - bTime);
        return new SimpleQueryResult(merger.getColumnNames(), merger.getSelectorNames(), new RowIteratorAdapter(allRows));
    }
    Set<Row> leftRows = buildLeftRowsJoin(csInfo, leftCo, printIndentation + printIndentStep);
    if (log.isDebugEnabled()) {
        timeJoinLeftSide = System.currentTimeMillis() - timeJoinLeftSide;
        log.debug(genString(printIndentation) + "SQL2 JOIN LEFT SIDE took " + timeJoinLeftSide + " ms. fetched " + leftRows.size() + " rows.");
    }
    // The join constraint information is split into:
    // - rightConstraints selects just the 'ON' constraints
    // - csInfo has the 'WHERE' constraints
    //
    // So, in the case of an OUTER JOIN we'll run 2 queries, one with
    // 'ON'
    // and one with 'ON' + 'WHERE' conditions
    // this way, at merge time in case of an outer join we can tell if
    // it's a 'null' row, or a bad row -> one that must not be returned.
    // This way at the end we'll have:
    // - rightRowsSet containing the 'ON' dataset
    // - excludingOuterJoinRowsSet: the 'ON' + 'WHERE' condition
    // dataset, or
    // NULL if there is no 'WHERE' condition
    long timeJoinRightSide = System.currentTimeMillis();
    List<Constraint> rightConstraints = merger.getRightJoinConstraints(leftRows);
    Comparator<Row> rightCo = new RowPathComparator(merger.getRightSelectors());
    if (leftRows == null || leftRows.isEmpty()) {
        return merger.merge(new RowIteratorAdapter((leftRows == null) ? Collections.emptySet() : leftRows), new RowIteratorAdapter(new TreeSet<Row>()), null, rightCo);
    }
    Set<Row> rightRows = buildRightRowsJoin(csInfo, rightConstraints, isOuterJoin, rightCo, printIndentation + printIndentStep);
    // this has to be initialized as null
    Set<Row> excludingOuterJoinRowsSet = null;
    if (isOuterJoin && csInfo.getRightConstraint() != null) {
        excludingOuterJoinRowsSet = buildRightRowsJoin(csInfo, rightConstraints, false, rightCo, printIndentation + printIndentStep);
    }
    if (log.isDebugEnabled()) {
        timeJoinRightSide = System.currentTimeMillis() - timeJoinRightSide;
        log.debug(genString(printIndentation) + "SQL2 JOIN RIGHT SIDE took " + timeJoinRightSide + " ms. fetched " + rightRows.size() + " rows.");
    }
    // merge left with right datasets
    return merger.merge(new RowIteratorAdapter(leftRows), new RowIteratorAdapter(rightRows), excludingOuterJoinRowsSet, rightCo);
}
Also used : QueryResult(javax.jcr.query.QueryResult) RowIteratorAdapter(org.apache.jackrabbit.commons.iterator.RowIteratorAdapter) Constraint(javax.jcr.query.qom.Constraint) TreeSet(java.util.TreeSet) RowIterator(javax.jcr.query.RowIterator) Row(javax.jcr.query.Row)

Example 19 with Constraint

use of javax.jcr.query.qom.Constraint in project jackrabbit by apache.

the class ChildNodeJoinMerger method getRightJoinConstraints.

@Override
public List<Constraint> getRightJoinConstraints(Collection<Row> leftRows) throws RepositoryException {
    Set<String> paths = new HashSet<String>();
    for (Row row : leftRows) {
        paths.addAll(getLeftValues(row));
    }
    List<Constraint> constraints = new ArrayList<Constraint>();
    for (String path : paths) {
        if (rightSelectors.contains(childSelector)) {
            constraints.add(factory.childNode(childSelector, path));
        } else {
            constraints.add(factory.sameNode(parentSelector, path));
        }
    }
    return constraints;
}
Also used : Constraint(javax.jcr.query.qom.Constraint) ArrayList(java.util.ArrayList) Row(javax.jcr.query.Row) HashSet(java.util.HashSet)

Example 20 with Constraint

use of javax.jcr.query.qom.Constraint in project jackrabbit by apache.

the class ConstraintSplitter method splitNot.

private void splitNot(ConstraintSplitInfo constraintSplitInfo, Not not) throws RepositoryException {
    Constraint constraint = not.getConstraint();
    if (constraint instanceof Not) {
        split(constraintSplitInfo, ((Not) constraint).getConstraint());
    } else if (constraint instanceof And) {
        And and = (And) constraint;
        split(constraintSplitInfo, factory.or(factory.not(and.getConstraint1()), factory.not(and.getConstraint2())));
    } else if (constraint instanceof Or) {
        Or or = (Or) constraint;
        split(constraintSplitInfo, factory.and(factory.not(or.getConstraint1()), factory.not(or.getConstraint2())));
    } else {
        splitBySelectors(constraintSplitInfo, not, getSelectorNames(constraint));
    }
}
Also used : Not(javax.jcr.query.qom.Not) Or(javax.jcr.query.qom.Or) Constraint(javax.jcr.query.qom.Constraint) And(javax.jcr.query.qom.And)

Aggregations

Constraint (javax.jcr.query.qom.Constraint)25 Row (javax.jcr.query.Row)7 Column (javax.jcr.query.qom.Column)6 ArrayList (java.util.ArrayList)5 AbstractRepositoryTest (org.apache.jackrabbit.oak.jcr.AbstractRepositoryTest)5 Test (org.junit.Test)5 Or (javax.jcr.query.qom.Or)4 Ordering (javax.jcr.query.qom.Ordering)4 QueryObjectModel (javax.jcr.query.qom.QueryObjectModel)4 Source (javax.jcr.query.qom.Source)4 HashSet (java.util.HashSet)3 And (javax.jcr.query.qom.And)3 Not (javax.jcr.query.qom.Not)3 PropertyValue (javax.jcr.query.qom.PropertyValue)3 TreeSet (java.util.TreeSet)2 RepositoryException (javax.jcr.RepositoryException)2 QueryResult (javax.jcr.query.QueryResult)2 BindVariableValue (javax.jcr.query.qom.BindVariableValue)2 QueryObjectModelFactory (javax.jcr.query.qom.QueryObjectModelFactory)2 Selector (javax.jcr.query.qom.Selector)2