Search in sources :

Example 1 with QueryOptions

use of com.google.gerrit.index.QueryOptions in project gerrit by GerritCodeReview.

the class QueryProcessor method query.

private List<QueryResult<T>> query(@Nullable List<String> queryStrings, List<Predicate<T>> queries) throws QueryParseException {
    long startNanos = System.nanoTime();
    checkState(!used.getAndSet(true), "%s has already been used", getClass().getSimpleName());
    int cnt = queries.size();
    if (queryStrings != null) {
        int qs = queryStrings.size();
        checkArgument(qs == cnt, "got %s query strings but %s predicates", qs, cnt);
    }
    if (cnt == 0) {
        return ImmutableList.of();
    }
    if (isDisabled()) {
        return disabledResults(queryStrings, queries);
    }
    logger.atFine().log("Executing %d %s index queries for %s", cnt, schemaDef.getName(), callerFinder.findCallerLazy());
    List<QueryResult<T>> out;
    try {
        // 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);
        int queryCount = 0;
        for (Predicate<T> q : queries) {
            checkSupportedForQueries(q);
            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());
            logger.atFine().log("Query options: %s", opts);
            Predicate<T> pred = rewriter.rewrite(q, opts);
            if (enforceVisibility) {
                pred = enforceVisibility(pred);
            }
            predicates.add(pred);
            logger.atFine().log("%s index query[%d]:\n%s", schemaDef.getName(), queryCount++, pred instanceof IndexedQuery ? pred.getChild(0) : 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());
        }
        out = new ArrayList<>(cnt);
        for (int i = 0; i < cnt; i++) {
            ImmutableList<T> matchesList = matches.get(i).toList();
            logger.atFine().log("Matches[%d]:\n%s", i, lazy(() -> matchesList.stream().map(this::formatForLogging).collect(toList())));
            out.add(QueryResult.create(queryStrings != null ? queryStrings.get(i) : null, predicates.get(i), limits.get(i), matchesList));
        }
        // Only measure successful queries that actually touched the index.
        metrics.executionTime.record(schemaDef.getName(), System.nanoTime() - startNanos, TimeUnit.NANOSECONDS);
    } catch (StorageException e) {
        Optional<QueryParseException> qpe = findQueryParseException(e);
        if (qpe.isPresent()) {
            throw new QueryParseException(qpe.get().getMessage(), e);
        }
        throw e;
    }
    return out;
}
Also used : Optional(java.util.Optional) ArrayList(java.util.ArrayList) QueryOptions(com.google.gerrit.index.QueryOptions) StorageException(com.google.gerrit.exceptions.StorageException)

Example 2 with QueryOptions

use of com.google.gerrit.index.QueryOptions in project gerrit by GerritCodeReview.

the class AbstractFakeIndex method getSource.

@Override
public DataSource<V> getSource(Predicate<V> p, QueryOptions opts) {
    List<V> results;
    synchronized (indexedDocuments) {
        results = indexedDocuments.values().stream().map(doc -> valueFor(doc)).filter(doc -> p.asMatchable().match(doc)).sorted(sortingComparator()).skip(opts.start()).limit(opts.limit()).collect(toImmutableList());
    }
    return new DataSource<>() {

        @Override
        public int getCardinality() {
            return results.size();
        }

        @Override
        public ResultSet<V> read() {
            return new ListResultSet<>(results);
        }

        @Override
        public ResultSet<FieldBundle> readRaw() {
            ImmutableList.Builder<FieldBundle> fieldBundles = ImmutableList.builder();
            for (V result : results) {
                ImmutableListMultimap.Builder<String, Object> fields = ImmutableListMultimap.builder();
                for (FieldDef<V, ?> field : getSchema().getFields().values()) {
                    if (field.get(result) == null) {
                        continue;
                    }
                    if (field.isRepeatable()) {
                        fields.putAll(field.getName(), (Iterable<?>) field.get(result));
                    } else {
                        fields.put(field.getName(), field.get(result));
                    }
                }
                fieldBundles.add(new FieldBundle(fields.build()));
            }
            return new ListResultSet<>(fieldBundles.build());
        }
    };
}
Also used : InternalGroup(com.google.gerrit.entities.InternalGroup) MergeabilityComputationBehavior(com.google.gerrit.server.change.MergeabilityComputationBehavior) IndexUtils(com.google.gerrit.server.index.IndexUtils) Inject(com.google.inject.Inject) HashMap(java.util.HashMap) ProjectData(com.google.gerrit.index.project.ProjectData) ResultSet(com.google.gerrit.index.query.ResultSet) Assisted(com.google.inject.assistedinject.Assisted) Config(org.eclipse.jgit.lib.Config) ProjectIndex(com.google.gerrit.index.project.ProjectIndex) ListResultSet(com.google.gerrit.index.query.ListResultSet) ImmutableList(com.google.common.collect.ImmutableList) AccountIndex(com.google.gerrit.server.index.account.AccountIndex) Map(java.util.Map) FieldBundle(com.google.gerrit.index.query.FieldBundle) Change(com.google.gerrit.entities.Change) Predicate(com.google.gerrit.index.query.Predicate) AccountGroup(com.google.gerrit.entities.AccountGroup) FieldDef(com.google.gerrit.index.FieldDef) Schema(com.google.gerrit.index.Schema) GerritServerConfig(com.google.gerrit.server.config.GerritServerConfig) ImmutableMap(com.google.common.collect.ImmutableMap) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) Account(com.google.gerrit.entities.Account) QueryOptions(com.google.gerrit.index.QueryOptions) Instant(java.time.Instant) Index(com.google.gerrit.index.Index) ChangeField(com.google.gerrit.server.index.change.ChangeField) ChangeData(com.google.gerrit.server.query.change.ChangeData) List(java.util.List) ImmutableListMultimap(com.google.common.collect.ImmutableListMultimap) SitePaths(com.google.gerrit.server.config.SitePaths) Project(com.google.gerrit.entities.Project) DataSource(com.google.gerrit.index.query.DataSource) GroupIndex(com.google.gerrit.server.index.group.GroupIndex) AccountState(com.google.gerrit.server.account.AccountState) Comparator(java.util.Comparator) ChangeIndex(com.google.gerrit.server.index.change.ChangeIndex) ListResultSet(com.google.gerrit.index.query.ListResultSet) FieldBundle(com.google.gerrit.index.query.FieldBundle) ImmutableList(com.google.common.collect.ImmutableList) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) DataSource(com.google.gerrit.index.query.DataSource) ImmutableListMultimap(com.google.common.collect.ImmutableListMultimap)

Example 3 with QueryOptions

use of com.google.gerrit.index.QueryOptions in project gerrit by GerritCodeReview.

the class InitIT method indexesAllProjectsAndAllUsers.

@Test
public void indexesAllProjectsAndAllUsers() throws Exception {
    initSite();
    try (ServerContext ctx = startServer()) {
        ProjectIndexCollection projectIndex = ctx.getInjector().getInstance(ProjectIndexCollection.class);
        Project.NameKey allProjects = ctx.getInjector().getInstance(AllProjectsName.class);
        Project.NameKey allUsers = ctx.getInjector().getInstance(AllUsersName.class);
        QueryOptions opts = QueryOptions.create(IndexConfig.createDefault(), 0, 1, ImmutableSet.of("name"));
        Optional<ProjectData> allProjectsData = projectIndex.getSearchIndex().get(allProjects, opts);
        assertThat(allProjectsData).isPresent();
        Optional<ProjectData> allUsersData = projectIndex.getSearchIndex().get(allUsers, opts);
        assertThat(allUsersData).isPresent();
    }
}
Also used : Project(com.google.gerrit.entities.Project) ProjectIndexCollection(com.google.gerrit.index.project.ProjectIndexCollection) QueryOptions(com.google.gerrit.index.QueryOptions) ProjectData(com.google.gerrit.index.project.ProjectData) StandaloneSiteTest(com.google.gerrit.acceptance.StandaloneSiteTest) Test(org.junit.Test)

Aggregations

QueryOptions (com.google.gerrit.index.QueryOptions)3 Project (com.google.gerrit.entities.Project)2 ProjectData (com.google.gerrit.index.project.ProjectData)2 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)1 ImmutableListMultimap (com.google.common.collect.ImmutableListMultimap)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 StandaloneSiteTest (com.google.gerrit.acceptance.StandaloneSiteTest)1 Account (com.google.gerrit.entities.Account)1 AccountGroup (com.google.gerrit.entities.AccountGroup)1 Change (com.google.gerrit.entities.Change)1 InternalGroup (com.google.gerrit.entities.InternalGroup)1 StorageException (com.google.gerrit.exceptions.StorageException)1 FieldDef (com.google.gerrit.index.FieldDef)1 Index (com.google.gerrit.index.Index)1 Schema (com.google.gerrit.index.Schema)1 ProjectIndex (com.google.gerrit.index.project.ProjectIndex)1 ProjectIndexCollection (com.google.gerrit.index.project.ProjectIndexCollection)1 DataSource (com.google.gerrit.index.query.DataSource)1 FieldBundle (com.google.gerrit.index.query.FieldBundle)1