Search in sources :

Example 1 with QueryOptions

use of com.google.gerrit.server.index.QueryOptions 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)

Example 2 with QueryOptions

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

the class AbstractQueryChangesTest method refStateFields.

@Test
public void refStateFields() throws Exception {
    // This test method manages primary storage manually.
    assume().that(notesMigration.changePrimaryStorage()).isEqualTo(PrimaryStorage.REVIEW_DB);
    Account.Id user = createAccount("user");
    Project.NameKey project = new Project.NameKey("repo");
    TestRepository<Repo> repo = createProject(project.get());
    String path = "file";
    RevCommit commit = repo.parseBody(repo.commit().message("one").add(path, "contents").create());
    Change change = insert(repo, newChangeForCommit(repo, commit));
    Change.Id id = change.getId();
    int c = id.get();
    String changeId = change.getKey().get();
    requestContext.setContext(newRequestContext(user));
    // Ensure one of each type of supported ref is present for the change. If
    // any more refs are added, update this test to reflect them.
    // Edit
    gApi.changes().id(changeId).edit().create();
    // Star
    gApi.accounts().self().starChange(change.getId().toString());
    if (notesMigration.readChanges()) {
        // Robot comment.
        ReviewInput rin = new ReviewInput();
        RobotCommentInput rcin = new RobotCommentInput();
        rcin.robotId = "happyRobot";
        rcin.robotRunId = "1";
        rcin.line = 1;
        rcin.message = "nit: trailing whitespace";
        rcin.path = path;
        rin.robotComments = ImmutableMap.of(path, ImmutableList.of(rcin));
        gApi.changes().id(c).current().review(rin);
    }
    // Draft.
    DraftInput din = new DraftInput();
    din.path = path;
    din.line = 1;
    din.message = "draft";
    gApi.changes().id(c).current().createDraft(din);
    if (notesMigration.readChanges()) {
        // Force NoteDb primary.
        change = ReviewDbUtil.unwrapDb(db).changes().get(id);
        change.setNoteDbState(NoteDbChangeState.NOTE_DB_PRIMARY_STATE);
        ReviewDbUtil.unwrapDb(db).changes().update(Collections.singleton(change));
        indexer.index(db, change);
    }
    QueryOptions opts = IndexedChangeQuery.createOptions(indexConfig, 0, 1, StalenessChecker.FIELDS);
    ChangeData cd = indexes.getSearchIndex().get(id, opts).get();
    String cs = RefNames.shard(c);
    int u = user.get();
    String us = RefNames.shard(u);
    List<String> expectedStates = Lists.newArrayList("repo:refs/users/" + us + "/edit-" + c + "/1", "All-Users:refs/starred-changes/" + cs + "/" + u);
    if (notesMigration.readChanges()) {
        expectedStates.add("repo:refs/changes/" + cs + "/meta");
        expectedStates.add("repo:refs/changes/" + cs + "/robot-comments");
        expectedStates.add("All-Users:refs/draft-comments/" + cs + "/" + u);
    }
    assertThat(cd.getRefStates().stream().map(String::new).map(s -> s.substring(0, s.lastIndexOf(':'))).collect(toList())).containsExactlyElementsIn(expectedStates);
    List<String> expectedPatterns = Lists.newArrayList("repo:refs/users/*/edit-" + c + "/*");
    expectedPatterns.add("All-Users:refs/starred-changes/" + cs + "/*");
    if (notesMigration.readChanges()) {
        expectedPatterns.add("All-Users:refs/draft-comments/" + cs + "/*");
    }
    assertThat(cd.getRefStatePatterns().stream().map(String::new).collect(toList())).containsExactlyElementsIn(expectedPatterns);
}
Also used : AllUsersName(com.google.gerrit.server.config.AllUsersName) Arrays(java.util.Arrays) CommentInfo(com.google.gerrit.extensions.common.CommentInfo) ProjectCache(com.google.gerrit.server.project.ProjectCache) BadRequestException(com.google.gerrit.extensions.restapi.BadRequestException) HashtagsInput(com.google.gerrit.extensions.api.changes.HashtagsInput) Repo(com.google.gerrit.testutil.InMemoryRepositoryManager.Repo) RevWalk(org.eclipse.jgit.revwalk.RevWalk) Config(org.eclipse.jgit.lib.Config) ChangeTriplet(com.google.gerrit.server.change.ChangeTriplet) FluentIterable(com.google.common.collect.FluentIterable) ConfigInput(com.google.gerrit.extensions.api.projects.ConfigInput) Map(java.util.Map) DraftHandling(com.google.gerrit.extensions.api.changes.ReviewInput.DraftHandling) ThrowableSubject(com.google.common.truth.ThrowableSubject) Schema(com.google.gerrit.server.index.Schema) ThreadLocalRequestContext(com.google.gerrit.server.util.ThreadLocalRequestContext) RefUpdate(org.eclipse.jgit.lib.RefUpdate) MILLISECONDS(java.util.concurrent.TimeUnit.MILLISECONDS) NotifyHandling(com.google.gerrit.extensions.api.changes.NotifyHandling) PersonIdent(org.eclipse.jgit.lib.PersonIdent) RefNames(com.google.gerrit.reviewdb.client.RefNames) QueryOptions(com.google.gerrit.server.index.QueryOptions) AuthRequest(com.google.gerrit.server.account.AuthRequest) DisabledReviewDb(com.google.gerrit.testutil.DisabledReviewDb) ExternalIdsUpdate(com.google.gerrit.server.account.externalids.ExternalIdsUpdate) GroupInput(com.google.gerrit.extensions.api.groups.GroupInput) AccountCache(com.google.gerrit.server.account.AccountCache) Iterables(com.google.common.collect.Iterables) ReviewDb(com.google.gerrit.reviewdb.server.ReviewDb) RevCommit(org.eclipse.jgit.revwalk.RevCommit) Change(com.google.gerrit.reviewdb.client.Change) MetaDataUpdate(com.google.gerrit.server.git.MetaDataUpdate) MINUTES(java.util.concurrent.TimeUnit.MINUTES) Sequences(com.google.gerrit.server.Sequences) TestTimeUtil(com.google.gerrit.testutil.TestTimeUtil) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) Lists(com.google.common.collect.Lists) RestApiException(com.google.gerrit.extensions.restapi.RestApiException) Before(org.junit.Before) QueryRequest(com.google.gerrit.extensions.api.changes.Changes.QueryRequest) Project(com.google.gerrit.reviewdb.client.Project) TestRepository(org.eclipse.jgit.junit.TestRepository) RequestContext(com.google.gerrit.server.util.RequestContext) TimeUtil(com.google.gerrit.common.TimeUtil) MoreObjects(com.google.common.base.MoreObjects) ReviewDbUtil(com.google.gerrit.reviewdb.server.ReviewDbUtil) ChangeNotes(com.google.gerrit.server.notedb.ChangeNotes) Test(org.junit.Test) ManualRequestContext(com.google.gerrit.server.util.ManualRequestContext) RobotCommentInput(com.google.gerrit.extensions.api.changes.ReviewInput.RobotCommentInput) SystemReader(org.eclipse.jgit.util.SystemReader) LifecycleManager(com.google.gerrit.lifecycle.LifecycleManager) IdentifiedUser(com.google.gerrit.server.IdentifiedUser) ReviewInput(com.google.gerrit.extensions.api.changes.ReviewInput) HOURS(java.util.concurrent.TimeUnit.HOURS) PatchSet(com.google.gerrit.reviewdb.client.PatchSet) ExternalId(com.google.gerrit.server.account.externalids.ExternalId) FieldDef(com.google.gerrit.server.index.FieldDef) ChangeIndexCollection(com.google.gerrit.server.index.change.ChangeIndexCollection) ObjectReader(org.eclipse.jgit.lib.ObjectReader) AccountManager(com.google.gerrit.server.account.AccountManager) ChangeControl(com.google.gerrit.server.project.ChangeControl) ObjectInserter(org.eclipse.jgit.lib.ObjectInserter) Inject(com.google.inject.Inject) PrimaryStorage(com.google.gerrit.server.notedb.NoteDbChangeState.PrimaryStorage) InheritableBoolean(com.google.gerrit.extensions.client.InheritableBoolean) ChangeIndexer(com.google.gerrit.server.index.change.ChangeIndexer) BatchUpdate(com.google.gerrit.server.update.BatchUpdate) After(org.junit.After) REVIEWED(com.google.gerrit.extensions.client.ListChangesOption.REVIEWED) Assert.fail(org.junit.Assert.fail) OneOffRequestContext(com.google.gerrit.server.util.OneOffRequestContext) ImmutableSet(com.google.common.collect.ImmutableSet) ImmutableMap(com.google.common.collect.ImmutableMap) Timestamp(java.sql.Timestamp) Collection(java.util.Collection) InMemoryRepositoryManager(com.google.gerrit.testutil.InMemoryRepositoryManager) List(java.util.List) Nullable(com.google.gerrit.common.Nullable) AddReviewerInput(com.google.gerrit.extensions.api.changes.AddReviewerInput) Ref(org.eclipse.jgit.lib.Ref) ConfigSuite(com.google.gerrit.testutil.ConfigSuite) Optional(java.util.Optional) Providers(com.google.inject.util.Providers) Branch(com.google.gerrit.reviewdb.client.Branch) DraftInput(com.google.gerrit.extensions.api.changes.DraftInput) InMemoryDatabase(com.google.gerrit.testutil.InMemoryDatabase) ReviewerState(com.google.gerrit.extensions.client.ReviewerState) Patch(com.google.gerrit.reviewdb.client.Patch) SchemaCreator(com.google.gerrit.server.schema.SchemaCreator) HashSet(java.util.HashSet) IndexConfig(com.google.gerrit.server.index.IndexConfig) ImmutableList(com.google.common.collect.ImmutableList) Account(com.google.gerrit.reviewdb.client.Account) ChangeInfo(com.google.gerrit.extensions.common.ChangeInfo) ChangeInserter(com.google.gerrit.server.change.ChangeInserter) TruthJUnit.assume(com.google.common.truth.TruthJUnit.assume) GerritServerTests(com.google.gerrit.testutil.GerritServerTests) CurrentUser(com.google.gerrit.server.CurrentUser) Iterator(java.util.Iterator) ChangeMessageInfo(com.google.gerrit.extensions.common.ChangeMessageInfo) IndexedChangeQuery(com.google.gerrit.server.index.change.IndexedChangeQuery) Truth.assertThat(com.google.common.truth.Truth.assertThat) SchemaFactory(com.google.gwtorm.server.SchemaFactory) StalenessChecker(com.google.gerrit.server.index.change.StalenessChecker) NoteDbChangeState(com.google.gerrit.server.notedb.NoteDbChangeState) ObjectId(org.eclipse.jgit.lib.ObjectId) ChangeField(com.google.gerrit.server.index.change.ChangeField) Injector(com.google.inject.Injector) TimeUnit(java.util.concurrent.TimeUnit) Collectors.toList(java.util.stream.Collectors.toList) PatchSetInserter(com.google.gerrit.server.change.PatchSetInserter) Provider(com.google.inject.Provider) Ignore(org.junit.Ignore) StarsInput(com.google.gerrit.extensions.api.changes.StarsInput) GerritApi(com.google.gerrit.extensions.api.GerritApi) PatchSetUtil(com.google.gerrit.server.PatchSetUtil) Collections(java.util.Collections) SECONDS(java.util.concurrent.TimeUnit.SECONDS) StarredChangesUtil(com.google.gerrit.server.StarredChangesUtil) Account(com.google.gerrit.reviewdb.client.Account) Change(com.google.gerrit.reviewdb.client.Change) ReviewInput(com.google.gerrit.extensions.api.changes.ReviewInput) QueryOptions(com.google.gerrit.server.index.QueryOptions) Project(com.google.gerrit.reviewdb.client.Project) Repo(com.google.gerrit.testutil.InMemoryRepositoryManager.Repo) RobotCommentInput(com.google.gerrit.extensions.api.changes.ReviewInput.RobotCommentInput) DraftInput(com.google.gerrit.extensions.api.changes.DraftInput) RevCommit(org.eclipse.jgit.revwalk.RevCommit) Test(org.junit.Test)

Aggregations

MoreObjects (com.google.common.base.MoreObjects)1 FluentIterable (com.google.common.collect.FluentIterable)1 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 ImmutableSet (com.google.common.collect.ImmutableSet)1 Iterables (com.google.common.collect.Iterables)1 Lists (com.google.common.collect.Lists)1 ThrowableSubject (com.google.common.truth.ThrowableSubject)1 Truth.assertThat (com.google.common.truth.Truth.assertThat)1 TruthJUnit.assume (com.google.common.truth.TruthJUnit.assume)1 Nullable (com.google.gerrit.common.Nullable)1 TimeUtil (com.google.gerrit.common.TimeUtil)1 GerritApi (com.google.gerrit.extensions.api.GerritApi)1 AddReviewerInput (com.google.gerrit.extensions.api.changes.AddReviewerInput)1 QueryRequest (com.google.gerrit.extensions.api.changes.Changes.QueryRequest)1 DraftInput (com.google.gerrit.extensions.api.changes.DraftInput)1 HashtagsInput (com.google.gerrit.extensions.api.changes.HashtagsInput)1 NotifyHandling (com.google.gerrit.extensions.api.changes.NotifyHandling)1 ReviewInput (com.google.gerrit.extensions.api.changes.ReviewInput)1 DraftHandling (com.google.gerrit.extensions.api.changes.ReviewInput.DraftHandling)1