Search in sources :

Example 31 with Row

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

the class GQL method executeJcrQuery.

private RowIterator executeJcrQuery(String jcrQuery, String jcrQueryLanguage) {
    try {
        QueryManager qm = session.getWorkspace().getQueryManager();
        RowIterator nodes = qm.createQuery(jcrQuery, jcrQueryLanguage).execute().getRows();
        if (filter != null) {
            nodes = new FilteredRowIterator(nodes);
        }
        if (offset > 0) {
            try {
                nodes.skip(offset);
            } catch (NoSuchElementException e) {
                return RowIteratorAdapter.EMPTY;
            }
        }
        if (numResults == Integer.MAX_VALUE) {
            return new RowIterAdapter(nodes, nodes.getSize());
        }
        List<Row> resultRows = new ArrayList<Row>();
        while (numResults-- > 0 && nodes.hasNext()) {
            resultRows.add(nodes.nextRow());
        }
        return new RowIterAdapter(resultRows, resultRows.size());
    } catch (RepositoryException e) {
        // in case of error return empty result
        return RowIteratorAdapter.EMPTY;
    }
}
Also used : RowIterator(javax.jcr.query.RowIterator) QueryManager(javax.jcr.query.QueryManager) ArrayList(java.util.ArrayList) RepositoryException(javax.jcr.RepositoryException) Row(javax.jcr.query.Row) NoSuchElementException(java.util.NoSuchElementException)

Example 32 with Row

use of javax.jcr.query.Row 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 33 with Row

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

the class QueryEngine method sort.

/**
     * Sorts the given query results according to the given QOM orderings. If
     * one or more orderings have been specified, this method will iterate
     * through the entire original result set, order the collected rows, and
     * return a new result set based on the sorted collection of rows.
     * 
     * @param result
     *            original query results
     * @param orderings
     *            QOM orderings
     * @param offset
     *            result offset
     * @param limit
     *            result limit
     * @return sorted query results
     * @throws RepositoryException
     *             if the results can not be sorted
     */
protected static QueryResult sort(QueryResult result, final Ordering[] orderings, OperandEvaluator evaluator, long offset, long limit) throws RepositoryException {
    if ((orderings != null && orderings.length > 0) || offset != 0 || limit >= 0) {
        List<Row> rows = new ArrayList<Row>();
        RowIterator iterator = result.getRows();
        while (iterator.hasNext()) {
            rows.add(iterator.nextRow());
        }
        if (orderings != null && orderings.length > 0) {
            Collections.sort(rows, new RowComparator(orderings, evaluator));
        }
        if (offset > 0) {
            int size = rows.size();
            rows = rows.subList((int) Math.min(offset, size), size);
        }
        if (limit >= 0) {
            int size = rows.size();
            rows = rows.subList(0, (int) Math.min(limit, size));
        }
        return new SimpleQueryResult(result.getColumnNames(), result.getSelectorNames(), new RowIteratorAdapter(rows));
    } else {
        return result;
    }
}
Also used : RowIteratorAdapter(org.apache.jackrabbit.commons.iterator.RowIteratorAdapter) RowIterator(javax.jcr.query.RowIterator) RowComparator(org.apache.jackrabbit.core.query.lucene.sort.RowComparator) ArrayList(java.util.ArrayList) Row(javax.jcr.query.Row) Constraint(javax.jcr.query.qom.Constraint)

Example 34 with Row

use of javax.jcr.query.Row 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 35 with Row

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

the class JsonIndexCommand method runQuery.

private void runQuery(String query, String language, String columnName, boolean quiet, int depth) throws RepositoryException {
    ArrayList<String> list = new ArrayList<String>();
    columnName = query.startsWith("explain") ? "plan" : columnName;
    QueryManager qm = session.getWorkspace().getQueryManager();
    Query q = qm.createQuery(query, language);
    for (String b : q.getBindVariableNames()) {
        ValueFactory vf = session.getValueFactory();
        q.bindValue(b, vf.createValue(data.get("$" + b).toString()));
    }
    QueryResult result = q.execute();
    if (depth != 0) {
        NodeIterator ni = result.getNodes();
        JsopBuilder builder = new JsopBuilder().array();
        while (ni.hasNext()) {
            Node n = ni.nextNode();
            builder.key(n.getPath());
            appendNode(builder, n, depth - 1);
        }
        output.println(JsopBuilder.prettyPrint(builder.endArray().toString()));
        return;
    }
    RowIterator ri = result.getRows();
    while (ri.hasNext()) {
        Row r = ri.nextRow();
        if (columnName != null) {
            String x = r.getValue(columnName).getString();
            list.add(x);
            if (!quiet) {
                output.println(x);
            }
        } else {
            String[] columnNames = result.getColumnNames();
            for (String cn : columnNames) {
                Value v = r.getValue(cn);
                String x = v == null ? null : v.getString();
                if (columnNames.length == 1) {
                    list.add(x);
                    if (!quiet) {
                        output.println(x);
                    }
                } else {
                    list.add(x);
                    if (!quiet) {
                        output.println(cn + ": " + x);
                    }
                }
            }
        }
    }
    data.put("$resultSize", (long) list.size());
    data.put("$result", list.toArray(new String[0]));
}
Also used : NodeIterator(javax.jcr.NodeIterator) Query(javax.jcr.query.Query) Node(javax.jcr.Node) ArrayList(java.util.ArrayList) ValueFactory(javax.jcr.ValueFactory) QueryResult(javax.jcr.query.QueryResult) JsopBuilder(org.apache.jackrabbit.oak.commons.json.JsopBuilder) RowIterator(javax.jcr.query.RowIterator) QueryManager(javax.jcr.query.QueryManager) Value(javax.jcr.Value) Row(javax.jcr.query.Row)

Aggregations

Row (javax.jcr.query.Row)52 RowIterator (javax.jcr.query.RowIterator)27 QueryResult (javax.jcr.query.QueryResult)21 QueryManager (javax.jcr.query.QueryManager)14 ArrayList (java.util.ArrayList)13 Node (javax.jcr.Node)13 Value (javax.jcr.Value)9 Query (javax.jcr.query.Query)9 Test (org.junit.Test)9 Constraint (javax.jcr.query.qom.Constraint)8 RepositoryException (javax.jcr.RepositoryException)7 Session (javax.jcr.Session)7 JackrabbitSession (org.apache.jackrabbit.api.JackrabbitSession)6 AbstractRepositoryTest (org.apache.jackrabbit.oak.jcr.AbstractRepositoryTest)6 ValueFactory (javax.jcr.ValueFactory)5 HashSet (java.util.HashSet)4 TreeSet (java.util.TreeSet)4 HashMap (java.util.HashMap)3 NoSuchElementException (java.util.NoSuchElementException)3 NodeIterator (javax.jcr.NodeIterator)2