Search in sources :

Example 31 with QueryResult

use of javax.jcr.query.QueryResult in project jackrabbit by apache.

the class QueryEngine method buildLeftRowsJoin.

private Set<Row> buildLeftRowsJoin(ConstraintSplitInfo csi, Comparator<Row> comparator, int printIndentation) throws RepositoryException {
    if (csi.isMultiple()) {
        if (log.isDebugEnabled()) {
            log.debug(genString(printIndentation) + "SQL2 JOIN LEFT SIDE there are multiple inner splits.");
        }
        Set<Row> leftRows = new TreeSet<Row>(comparator);
        leftRows.addAll(buildLeftRowsJoin(csi.getLeftInnerConstraints(), comparator, printIndentation + printIndentStep));
        leftRows.addAll(buildLeftRowsJoin(csi.getRightInnerConstraints(), comparator, printIndentation + printIndentStep));
        return leftRows;
    }
    Set<Row> leftRows = new TreeSet<Row>(comparator);
    QueryResult leftResult = execute(null, csi.getSource().getLeft(), csi.getLeftConstraint(), null, 0, -1, printIndentation);
    for (Row row : JcrUtils.getRows(leftResult)) {
        leftRows.add(row);
    }
    return leftRows;
}
Also used : QueryResult(javax.jcr.query.QueryResult) TreeSet(java.util.TreeSet) Row(javax.jcr.query.Row)

Example 32 with QueryResult

use of javax.jcr.query.QueryResult in project jackrabbit by apache.

the class QueryEngine method execute.

protected QueryResult execute(Column[] columns, Selector selector, Constraint constraint, Ordering[] orderings, long offset, long limit, int printIndentation) throws RepositoryException {
    long time = System.currentTimeMillis();
    Map<String, NodeType> selectorMap = getSelectorNames(selector);
    String[] selectorNames = selectorMap.keySet().toArray(new String[selectorMap.size()]);
    Map<String, PropertyValue> columnMap = getColumnMap(columns, selectorMap);
    String[] columnNames = columnMap.keySet().toArray(new String[columnMap.size()]);
    Sort sort = new Sort();
    if (NATIVE_SORT) {
        sort = new Sort(createSortFields(orderings, session));
    }
    // if true it means that the LuceneQueryFactory should just let the
    // QueryEngine take care of sorting and applying offset and limit
    // constraints
    boolean externalSort = !NATIVE_SORT;
    RowIterator rows = null;
    try {
        rows = new RowIteratorAdapter(lqf.execute(columnMap, selector, constraint, sort, externalSort, offset, limit));
    } catch (IOException e) {
        throw new RepositoryException("Failed to access the query index", e);
    } finally {
        log.debug("{}SQL2 SELECT took {} ms. selector: {}, columns: {}, constraint: {}, offset {}, limit {}", new Object[] { genString(printIndentation), System.currentTimeMillis() - time, selector, Arrays.toString(columnNames), constraint, offset, limit });
    }
    QueryResult result = new SimpleQueryResult(columnNames, selectorNames, rows);
    if (NATIVE_SORT) {
        return result;
    }
    long timeSort = System.currentTimeMillis();
    QueryResult sorted = sort(result, orderings, evaluator, offset, limit);
    log.debug("{}SQL2 SORT took {} ms.", genString(printIndentation), System.currentTimeMillis() - timeSort);
    return sorted;
}
Also used : PropertyValue(javax.jcr.query.qom.PropertyValue) RepositoryException(javax.jcr.RepositoryException) IOException(java.io.IOException) QueryResult(javax.jcr.query.QueryResult) RowIteratorAdapter(org.apache.jackrabbit.commons.iterator.RowIteratorAdapter) NodeType(javax.jcr.nodetype.NodeType) RowIterator(javax.jcr.query.RowIterator) Sort(org.apache.lucene.search.Sort)

Example 33 with QueryResult

use of javax.jcr.query.QueryResult in project jackrabbit by apache.

the class QueryEngine method buildRightRowsJoin.

/**
     * @param csi
     *            contains 'WHERE' constraints and the source information
     * @param rightConstraints
     *            contains 'ON' constraints
     * @param ignoreWhereConstraints
     * @param comparator
     *            used to merge similar rows together
     * @param printIndentation
     *            used in logging
     * @return the right-side dataset of the join operation
     * @throws RepositoryException
     */
private Set<Row> buildRightRowsJoin(ConstraintSplitInfo csi, List<Constraint> rightConstraints, boolean ignoreWhereConstraints, Comparator<Row> comparator, int printIndentation) throws RepositoryException {
    if (csi.isMultiple()) {
        if (log.isDebugEnabled()) {
            log.debug(genString(printIndentation) + "SQL2 JOIN RIGHT SIDE there are multiple inner splits.");
        }
        Set<Row> rightRows = new TreeSet<Row>(comparator);
        rightRows.addAll(buildRightRowsJoin(csi.getLeftInnerConstraints(), rightConstraints, ignoreWhereConstraints, comparator, printIndentation + printIndentStep));
        rightRows.addAll(buildRightRowsJoin(csi.getRightInnerConstraints(), rightConstraints, ignoreWhereConstraints, comparator, printIndentation + printIndentStep));
        return rightRows;
    }
    if (rightConstraints.size() < 500) {
        Set<Row> rightRows = new TreeSet<Row>(comparator);
        List<Constraint> localRightContraints = rightConstraints;
        Constraint rightConstraint = Constraints.and(qomFactory, Constraints.or(qomFactory, localRightContraints), csi.getRightConstraint());
        if (ignoreWhereConstraints) {
            rightConstraint = Constraints.or(qomFactory, localRightContraints);
        }
        QueryResult rightResult = execute(null, csi.getSource().getRight(), rightConstraint, null, 0, -1, printIndentation);
        for (Row row : JcrUtils.getRows(rightResult)) {
            rightRows.add(row);
        }
        return rightRows;
    }
    // the 'batch by 500' approach
    Set<Row> rightRows = new TreeSet<Row>(comparator);
    for (int i = 0; i < rightConstraints.size(); i += 500) {
        if (log.isDebugEnabled()) {
            log.debug(genString(printIndentation) + "SQL2 JOIN RIGHT SIDE executing batch # " + i + ".");
        }
        List<Constraint> localRightContraints = rightConstraints.subList(i, Math.min(i + 500, rightConstraints.size()));
        Constraint rightConstraint = Constraints.and(qomFactory, Constraints.or(qomFactory, localRightContraints), csi.getRightConstraint());
        if (ignoreWhereConstraints) {
            rightConstraint = Constraints.or(qomFactory, localRightContraints);
        }
        QueryResult rightResult = execute(null, csi.getSource().getRight(), rightConstraint, null, 0, -1, printIndentation);
        for (Row row : JcrUtils.getRows(rightResult)) {
            rightRows.add(row);
        }
    }
    return rightRows;
}
Also used : QueryResult(javax.jcr.query.QueryResult) Constraint(javax.jcr.query.qom.Constraint) TreeSet(java.util.TreeSet) Row(javax.jcr.query.Row) Constraint(javax.jcr.query.qom.Constraint)

Example 34 with QueryResult

use of javax.jcr.query.QueryResult in project jackrabbit by apache.

the class QueryEngine method execute.

public QueryResult execute(Column[] columns, Source source, Constraint constraint, Ordering[] orderings, long offset, long limit) throws RepositoryException {
    long time = System.currentTimeMillis();
    QueryResult qr = execute(columns, source, constraint, orderings, offset, limit, 2);
    log.debug("SQL2 QUERY execute took {} ms. native sort is {}.", System.currentTimeMillis() - time, NATIVE_SORT);
    return qr;
}
Also used : QueryResult(javax.jcr.query.QueryResult)

Example 35 with QueryResult

use of javax.jcr.query.QueryResult in project jackrabbit by apache.

the class SQL2QueryResultTest method executeAndCheckColumns.

private void executeAndCheckColumns(String sql2, int expected, String... cols) throws RepositoryException {
    QueryResult r = executeSQL2Query(sql2);
    assertEquals("Got more columns than expected: " + Arrays.toString(r.getColumnNames()), expected, r.getColumnNames().length);
    if (expected > 0) {
        assertEquals(expected, cols.length);
        List<String> expectedCols = new ArrayList<String>(Arrays.asList(cols));
        expectedCols.removeAll(new ArrayList<String>(Arrays.asList(r.getColumnNames())));
        assertTrue("Got unexpected columns: " + expectedCols, expectedCols.isEmpty());
        for (Row row : JcrUtils.getRows(r)) {
            assertNotNull(row.getValues());
            assertEquals(expected, row.getValues().length);
        }
    }
}
Also used : QueryResult(javax.jcr.query.QueryResult) ArrayList(java.util.ArrayList) Row(javax.jcr.query.Row)

Aggregations

QueryResult (javax.jcr.query.QueryResult)202 Node (javax.jcr.Node)109 Query (javax.jcr.query.Query)98 QueryManager (javax.jcr.query.QueryManager)55 NodeIterator (javax.jcr.NodeIterator)52 RowIterator (javax.jcr.query.RowIterator)46 Session (javax.jcr.Session)36 Test (org.junit.Test)32 Row (javax.jcr.query.Row)21 AbstractRepositoryTest (org.apache.jackrabbit.oak.jcr.AbstractRepositoryTest)21 RepositoryException (javax.jcr.RepositoryException)15 JackrabbitSession (org.apache.jackrabbit.api.JackrabbitSession)15 FacetResult (org.apache.jackrabbit.oak.query.facet.FacetResult)9 Value (javax.jcr.Value)8 NoSuchElementException (java.util.NoSuchElementException)7 ArrayList (java.util.ArrayList)6 ValueFactory (javax.jcr.ValueFactory)6 InvalidItemStateException (javax.jcr.InvalidItemStateException)5 JackrabbitQueryResult (org.apache.jackrabbit.api.query.JackrabbitQueryResult)5 TreeSet (java.util.TreeSet)4