All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.google.gerrit.server.query.change.InternalChangeQuery Maven / Gradle / Ivy

There is a newer version: 3.11.1
Show newest version
// Copyright (C) 2014 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.gerrit.server.query.change;

import static com.google.common.base.Preconditions.checkArgument;
import static com.google.gerrit.server.index.change.ChangeField.SUBMISSIONID;
import static com.google.gerrit.server.query.Predicate.and;
import static com.google.gerrit.server.query.Predicate.not;
import static com.google.gerrit.server.query.Predicate.or;
import static com.google.gerrit.server.query.change.ChangeStatusPredicate.open;

import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Function;
import com.google.common.base.Strings;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import com.google.gerrit.reviewdb.client.Account;
import com.google.gerrit.reviewdb.client.Branch;
import com.google.gerrit.reviewdb.client.Change;
import com.google.gerrit.reviewdb.client.Project;
import com.google.gerrit.reviewdb.client.RefNames;
import com.google.gerrit.reviewdb.server.ReviewDb;
import com.google.gerrit.server.index.IndexConfig;
import com.google.gerrit.server.index.change.ChangeIndexCollection;
import com.google.gerrit.server.notedb.ChangeNotes;
import com.google.gerrit.server.query.InternalQuery;
import com.google.gerrit.server.query.Predicate;
import com.google.gwtorm.server.OrmException;
import com.google.inject.Inject;

import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.Ref;
import org.eclipse.jgit.lib.Repository;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Set;

public class InternalChangeQuery extends InternalQuery {
  private static Predicate ref(Branch.NameKey branch) {
    return new RefPredicate(branch.get());
  }

  private static Predicate change(Change.Key key) {
    return new ChangeIdPredicate(key.get());
  }

  private static Predicate project(Project.NameKey project) {
    return new ProjectPredicate(project.get());
  }

  private static Predicate status(Change.Status status) {
    return new ChangeStatusPredicate(status);
  }

  private static Predicate commit(String id) {
    return new CommitPredicate(id);
  }

  private final ChangeData.Factory changeDataFactory;
  private final ChangeNotes.Factory notesFactory;

  @Inject
  InternalChangeQuery(ChangeQueryProcessor queryProcessor,
      ChangeIndexCollection indexes,
      IndexConfig indexConfig,
      ChangeData.Factory changeDataFactory,
      ChangeNotes.Factory notesFactory) {
    super(queryProcessor, indexes, indexConfig);
    this.changeDataFactory = changeDataFactory;
    this.notesFactory = notesFactory;
  }

  @Override
  public InternalChangeQuery setLimit(int n) {
    super.setLimit(n);
    return this;
  }

  @Override
  public InternalChangeQuery enforceVisibility(boolean enforce) {
    super.enforceVisibility(enforce);
    return this;
  }

  @Override
  public InternalChangeQuery setRequestedFields(Set fields) {
    super.setRequestedFields(fields);
    return this;
  }

  @Override
  public InternalChangeQuery noFields() {
    super.noFields();
    return this;
  }

  public List byKey(Change.Key key) throws OrmException {
    return byKeyPrefix(key.get());
  }

  public List byKeyPrefix(String prefix) throws OrmException {
    return query(new ChangeIdPredicate(prefix));
  }

  public List byLegacyChangeId(Change.Id id) throws OrmException {
    return query(new LegacyChangeIdPredicate(id));
  }

  public List byLegacyChangeIds(Collection ids)
      throws OrmException {
    List> preds = new ArrayList<>(ids.size());
    for (Change.Id id : ids) {
      preds.add(new LegacyChangeIdPredicate(id));
    }
    return query(or(preds));
  }

  public List byBranchKey(Branch.NameKey branch, Change.Key key)
      throws OrmException {
    return query(and(
        ref(branch),
        project(branch.getParentKey()),
        change(key)));
  }

  public List byProject(Project.NameKey project)
      throws OrmException {
    return query(project(project));
  }

  public List byBranchOpen(Branch.NameKey branch)
      throws OrmException {
    return query(and(
        ref(branch),
        project(branch.getParentKey()),
        open()));
  }

  public List byBranchNew(Branch.NameKey branch)
      throws OrmException {
    return query(and(
        ref(branch),
        project(branch.getParentKey()),
        status(Change.Status.NEW)));
  }

  public Iterable byCommitsOnBranchNotMerged(Repository repo,
      ReviewDb db, Branch.NameKey branch, List hashes)
      throws OrmException, IOException {
    return byCommitsOnBranchNotMerged(repo, db, branch, hashes,
        // Account for all commit predicates plus ref, project, status.
        indexConfig.maxTerms() - 3);
  }

  @VisibleForTesting
  Iterable byCommitsOnBranchNotMerged(Repository repo, ReviewDb db,
      Branch.NameKey branch, List hashes, int indexLimit)
      throws OrmException, IOException {
    if (hashes.size() > indexLimit) {
      return byCommitsOnBranchNotMergedFromDatabase(repo, db, branch, hashes);
    }
    return byCommitsOnBranchNotMergedFromIndex(branch, hashes);
  }

  private Iterable byCommitsOnBranchNotMergedFromDatabase(
      Repository repo, final ReviewDb db, final Branch.NameKey branch,
      List hashes) throws OrmException, IOException {
    Set changeIds = Sets.newHashSetWithExpectedSize(hashes.size());
    String lastPrefix = null;
    for (Ref ref :
        repo.getRefDatabase().getRefs(RefNames.REFS_CHANGES).values()) {
      String r = ref.getName();
      if ((lastPrefix != null && r.startsWith(lastPrefix))
          || !hashes.contains(ref.getObjectId().name())) {
        continue;
      }
      Change.Id id = Change.Id.fromRef(r);
      if (id == null) {
        continue;
      }
      if (changeIds.add(id)) {
        lastPrefix = r.substring(0, r.lastIndexOf('/'));
      }
    }

    return Lists.transform(notesFactory.create(db, branch.getParentKey(),
        changeIds, new com.google.common.base.Predicate() {
          @Override
          public boolean apply(ChangeNotes notes) {
            Change c = notes.getChange();
            return c.getDest().equals(branch)
                && c.getStatus() != Change.Status.MERGED;
          }
        }), new Function() {
          @Override
          public ChangeData apply(ChangeNotes notes) {
            return changeDataFactory.create(db, notes);
          }
        });
  }

  private Iterable byCommitsOnBranchNotMergedFromIndex(
      Branch.NameKey branch, List hashes) throws OrmException {
    return query(and(
        ref(branch),
        project(branch.getParentKey()),
        not(status(Change.Status.MERGED)),
        or(commits(hashes))));
  }

  private static List> commits(List hashes) {
    List> commits = new ArrayList<>(hashes.size());
    for (String s : hashes) {
      commits.add(commit(s));
    }
    return commits;
  }

  public List byProjectOpen(Project.NameKey project)
      throws OrmException {
    return query(and(project(project), open()));
  }

  public List byTopicOpen(String topic)
      throws OrmException {
    return query(and(new ExactTopicPredicate(topic), open()));
  }

  public List byCommit(ObjectId id) throws OrmException {
    return byCommit(id.name());
  }

  public List byCommit(String hash) throws OrmException {
    return query(commit(hash));
  }

  public List byProjectCommit(Project.NameKey project,
      String hash) throws OrmException {
    return query(and(project(project), commit(hash)));
  }

  public List byProjectCommits(Project.NameKey project,
      List hashes) throws OrmException {
    int n = indexConfig.maxTerms() - 1;
    checkArgument(hashes.size() <= n, "cannot exceed %s commits", n);
    return query(and(project(project), or(commits(hashes))));
  }

  public List byBranchCommit(String project, String branch,
      String hash) throws OrmException {
    return query(and(
        new ProjectPredicate(project),
        new RefPredicate(branch),
        commit(hash)));
  }

  public List byBranchCommit(Branch.NameKey branch, String hash)
      throws OrmException {
    return byBranchCommit(branch.getParentKey().get(), branch.get(), hash);
  }

  public List bySubmissionId(String cs) throws OrmException {
    if (Strings.isNullOrEmpty(cs) || !schema().hasField(SUBMISSIONID)) {
      return Collections.emptyList();
    }
    return query(new SubmissionIdPredicate(cs));
  }

  public List byProjectGroups(Project.NameKey project,
      Collection groups) throws OrmException {
    List groupPredicates = new ArrayList<>(groups.size());
    for (String g : groups) {
      groupPredicates.add(new GroupPredicate(g));
    }
    return query(and(project(project), or(groupPredicates)));
  }

  @SuppressWarnings("deprecation")
  public List byIsStarred(Account.Id id) throws OrmException {
    return query(new IsStarredByPredicate(id));
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy