Search in sources :

Example 1 with ResultSet

use of com.google.gwtorm.server.ResultSet in project gerrit by GerritCodeReview.

the class IndexedChangeQuery method read.

@Override
public ResultSet<ChangeData> read() throws OrmException {
    final DataSource<ChangeData> currSource = source;
    final ResultSet<ChangeData> rs = currSource.read();
    return new ResultSet<ChangeData>() {

        @Override
        public Iterator<ChangeData> iterator() {
            return Iterables.transform(rs, cd -> {
                fromSource.put(cd, currSource);
                return cd;
            }).iterator();
        }

        @Override
        public List<ChangeData> toList() {
            List<ChangeData> r = rs.toList();
            for (ChangeData cd : r) {
                fromSource.put(cd, currSource);
            }
            return r;
        }

        @Override
        public void close() {
            rs.close();
        }
    };
}
Also used : Iterables(com.google.common.collect.Iterables) OrmException(com.google.gwtorm.server.OrmException) Change(com.google.gerrit.reviewdb.client.Change) HashMap(java.util.HashMap) IndexedQuery(com.google.gerrit.server.index.IndexedQuery) Matchable(com.google.gerrit.server.query.Matchable) HashSet(java.util.HashSet) IndexConfig(com.google.gerrit.server.index.IndexConfig) CHANGE(com.google.gerrit.server.index.change.ChangeField.CHANGE) ResultSet(com.google.gwtorm.server.ResultSet) DataSource(com.google.gerrit.server.query.DataSource) Map(java.util.Map) ChangeDataSource(com.google.gerrit.server.query.change.ChangeDataSource) ImmutableSet(com.google.common.collect.ImmutableSet) Iterator(java.util.Iterator) Set(java.util.Set) Preconditions.checkState(com.google.common.base.Preconditions.checkState) Predicate(com.google.gerrit.server.query.Predicate) QueryParseException(com.google.gerrit.server.query.QueryParseException) ChangeData(com.google.gerrit.server.query.change.ChangeData) List(java.util.List) PROJECT(com.google.gerrit.server.index.change.ChangeField.PROJECT) QueryOptions(com.google.gerrit.server.index.QueryOptions) VisibleForTesting(com.google.common.annotations.VisibleForTesting) IndexPredicate(com.google.gerrit.server.index.IndexPredicate) ResultSet(com.google.gwtorm.server.ResultSet) ChangeData(com.google.gerrit.server.query.change.ChangeData)

Example 2 with ResultSet

use of com.google.gwtorm.server.ResultSet in project gerrit by GerritCodeReview.

the class QueryProcessor method query.

private List<QueryResult<T>> query(List<String> queryStrings, List<Predicate<T>> queries) throws OrmException, QueryParseException {
    long startNanos = System.nanoTime();
    int cnt = queries.size();
    // Parse and rewrite all queries.
    List<Integer> limits = new ArrayList<>(cnt);
    List<Predicate<T>> predicates = new ArrayList<>(cnt);
    List<DataSource<T>> sources = new ArrayList<>(cnt);
    for (Predicate<T> q : queries) {
        int limit = getEffectiveLimit(q);
        limits.add(limit);
        if (limit == getBackendSupportedLimit()) {
            limit--;
        }
        int page = (start / limit) + 1;
        if (page > indexConfig.maxPages()) {
            throw new QueryParseException("Cannot go beyond page " + indexConfig.maxPages() + " of results");
        }
        // Always bump limit by 1, even if this results in exceeding the permitted
        // max for this user. The only way to see if there are more entities is to
        // ask for one more result from the query.
        QueryOptions opts = createOptions(indexConfig, start, limit + 1, getRequestedFields());
        Predicate<T> pred = rewriter.rewrite(q, opts);
        if (enforceVisibility) {
            pred = enforceVisibility(pred);
        }
        predicates.add(pred);
        @SuppressWarnings("unchecked") DataSource<T> s = (DataSource<T>) pred;
        sources.add(s);
    }
    // Run each query asynchronously, if supported.
    List<ResultSet<T>> matches = new ArrayList<>(cnt);
    for (DataSource<T> s : sources) {
        matches.add(s.read());
    }
    List<QueryResult<T>> out = new ArrayList<>(cnt);
    for (int i = 0; i < cnt; i++) {
        out.add(QueryResult.create(queryStrings != null ? queryStrings.get(i) : null, predicates.get(i), limits.get(i), matches.get(i).toList()));
    }
    // only measure successful queries
    metrics.executionTime.record(schemaDef.getName(), System.nanoTime() - startNanos, TimeUnit.NANOSECONDS);
    return out;
}
Also used : ArrayList(java.util.ArrayList) QueryOptions(com.google.gerrit.server.index.QueryOptions) ResultSet(com.google.gwtorm.server.ResultSet)

Aggregations

QueryOptions (com.google.gerrit.server.index.QueryOptions)2 ResultSet (com.google.gwtorm.server.ResultSet)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 Preconditions.checkState (com.google.common.base.Preconditions.checkState)1 ImmutableSet (com.google.common.collect.ImmutableSet)1 Iterables (com.google.common.collect.Iterables)1 Change (com.google.gerrit.reviewdb.client.Change)1 IndexConfig (com.google.gerrit.server.index.IndexConfig)1 IndexPredicate (com.google.gerrit.server.index.IndexPredicate)1 IndexedQuery (com.google.gerrit.server.index.IndexedQuery)1 CHANGE (com.google.gerrit.server.index.change.ChangeField.CHANGE)1 PROJECT (com.google.gerrit.server.index.change.ChangeField.PROJECT)1 DataSource (com.google.gerrit.server.query.DataSource)1 Matchable (com.google.gerrit.server.query.Matchable)1 Predicate (com.google.gerrit.server.query.Predicate)1 QueryParseException (com.google.gerrit.server.query.QueryParseException)1 ChangeData (com.google.gerrit.server.query.change.ChangeData)1 ChangeDataSource (com.google.gerrit.server.query.change.ChangeDataSource)1 OrmException (com.google.gwtorm.server.OrmException)1 ArrayList (java.util.ArrayList)1