Search in sources :

Example 1 with Predicate

use of com.google.gerrit.index.query.Predicate 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 2 with Predicate

use of com.google.gerrit.index.query.Predicate in project gerrit by GerritCodeReview.

the class IsWatchedByPredicate method filters.

protected static List<Predicate<ChangeData>> filters(ChangeQueryBuilder.Arguments args) throws QueryParseException {
    List<Predicate<ChangeData>> r = new ArrayList<>();
    ChangeQueryBuilder builder = new ChangeQueryBuilder(args);
    for (ProjectWatchKey w : getWatches(args)) {
        Predicate<ChangeData> f = null;
        if (w.filter() != null) {
            try {
                f = builder.parse(w.filter());
                if (QueryBuilder.find(f, IsWatchedByPredicate.class) != null) {
                    // another user is filtering on. :-)
                    continue;
                }
            } catch (QueryParseException e) {
                continue;
            }
        }
        Predicate<ChangeData> p;
        if (w.project().equals(args.allProjectsName)) {
            p = null;
        } else {
            p = builder.project(w.project().get());
        }
        if (p != null && f != null) {
            r.add(and(p, f));
        } else if (p != null) {
            r.add(p);
        } else if (f != null) {
            r.add(f);
        } else {
            r.add(builder.statusOpen());
        }
    }
    if (r.isEmpty()) {
        return ImmutableList.of(ChangeIndexPredicate.none());
    }
    return ImmutableList.of(or(r));
}
Also used : ProjectWatchKey(com.google.gerrit.server.account.ProjectWatches.ProjectWatchKey) ArrayList(java.util.ArrayList) AndPredicate(com.google.gerrit.index.query.AndPredicate) Predicate(com.google.gerrit.index.query.Predicate) QueryParseException(com.google.gerrit.index.query.QueryParseException)

Example 3 with Predicate

use of com.google.gerrit.index.query.Predicate in project gerrit by GerritCodeReview.

the class ChangeQueryBuilder method getAuthorOrCommitterFullTextPredicate.

private Predicate<ChangeData> getAuthorOrCommitterFullTextPredicate(String who, Function<String, Predicate<ChangeData>> fullPredicateFunc) throws QueryParseException {
    if (isSelf(who)) {
        IdentifiedUser me = args.getIdentifiedUser();
        List<Predicate<ChangeData>> predicates = me.getEmailAddresses().stream().map(fullPredicateFunc).collect(toList());
        return Predicate.or(predicates);
    }
    Set<String> parts = SchemaUtil.getNameParts(who);
    if (parts.isEmpty()) {
        throw error("invalid value");
    }
    List<Predicate<ChangeData>> predicates = parts.stream().map(fullPredicateFunc).collect(toList());
    return Predicate.and(predicates);
}
Also used : IdentifiedUser(com.google.gerrit.server.IdentifiedUser) LimitPredicate(com.google.gerrit.index.query.LimitPredicate) Predicate(com.google.gerrit.index.query.Predicate)

Example 4 with Predicate

use of com.google.gerrit.index.query.Predicate in project gerrit by GerritCodeReview.

the class LabelPredicate method predicates.

protected static List<Predicate<ChangeData>> predicates(Args args) {
    String v = args.value;
    List<Integer> counts = getCounts(args.count, args.countOp);
    try {
        MagicLabelVote mlv = MagicLabelVote.parseWithEquals(v);
        List<Predicate<ChangeData>> result = Lists.newArrayListWithCapacity(counts.size());
        if (counts.isEmpty()) {
            result.add(magicLabelPredicate(args, mlv, /* count= */
            null));
        } else {
            counts.forEach(count -> result.add(magicLabelPredicate(args, mlv, count)));
        }
        return result;
    } catch (IllegalArgumentException e) {
    // Try next format.
    }
    Parsed parsed = null;
    try {
        LabelVote lv = LabelVote.parse(v);
        parsed = new Parsed(lv.label(), "=", lv.value());
    } catch (IllegalArgumentException e) {
    // Try next format.
    }
    try {
        LabelVote lv = LabelVote.parseWithEquals(v);
        parsed = new Parsed(lv.label(), "=", lv.value());
    } catch (IllegalArgumentException e) {
    // Try next format.
    }
    Range range;
    if (parsed == null) {
        range = RangeUtil.getRange(v, -MAX_LABEL_VALUE, MAX_LABEL_VALUE);
        if (range == null) {
            range = new Range(v, 1, 1);
        }
    } else {
        range = RangeUtil.getRange(parsed.label, parsed.test, parsed.numericValue, -MAX_LABEL_VALUE, MAX_LABEL_VALUE);
    }
    String prefix = range.prefix;
    int min = range.min;
    int max = range.max;
    List<Predicate<ChangeData>> r = Lists.newArrayListWithCapacity((counts.isEmpty() ? 1 : counts.size()) * (max - min + 1));
    for (int i = min; i <= max; i++) {
        if (counts.isEmpty()) {
            r.add(onePredicate(args, prefix, i, /* count= */
            null));
        } else {
            for (int count : counts) {
                r.add(onePredicate(args, prefix, i, count));
            }
        }
    }
    return r;
}
Also used : LabelVote(com.google.gerrit.server.util.LabelVote) Range(com.google.gerrit.index.query.RangeUtil.Range) Predicate(com.google.gerrit.index.query.Predicate) OrPredicate(com.google.gerrit.index.query.OrPredicate)

Example 5 with Predicate

use of com.google.gerrit.index.query.Predicate in project gerrit by GerritCodeReview.

the class ChangeQueryBuilder method ownerin.

@Operator
public Predicate<ChangeData> ownerin(String group) throws QueryParseException, IOException {
    GroupReference g = GroupBackends.findBestSuggestion(args.groupBackend, group);
    if (g == null) {
        throw error("Group " + group + " not found");
    }
    AccountGroup.UUID groupId = g.getUUID();
    GroupDescription.Basic groupDescription = args.groupBackend.get(groupId);
    if (!(groupDescription instanceof GroupDescription.Internal)) {
        return new OwnerinPredicate(args.userFactory, groupId);
    }
    Set<Account.Id> accounts = getMembers(groupId);
    List<Predicate<ChangeData>> p = Lists.newArrayListWithCapacity(accounts.size());
    for (Account.Id id : accounts) {
        p.add(ChangePredicates.owner(id));
    }
    return Predicate.or(p);
}
Also used : Account(com.google.gerrit.entities.Account) ReviewerStateInternal(com.google.gerrit.server.notedb.ReviewerStateInternal) LimitPredicate(com.google.gerrit.index.query.LimitPredicate) Predicate(com.google.gerrit.index.query.Predicate) GroupDescription(com.google.gerrit.entities.GroupDescription) AccountGroup(com.google.gerrit.entities.AccountGroup) GroupReference(com.google.gerrit.entities.GroupReference)

Aggregations

Predicate (com.google.gerrit.index.query.Predicate)14 LimitPredicate (com.google.gerrit.index.query.LimitPredicate)6 ImmutableList (com.google.common.collect.ImmutableList)5 Account (com.google.gerrit.entities.Account)5 Change (com.google.gerrit.entities.Change)5 Project (com.google.gerrit.entities.Project)5 QueryParseException (com.google.gerrit.index.query.QueryParseException)5 ChangeData (com.google.gerrit.server.query.change.ChangeData)5 ArrayList (java.util.ArrayList)5 AccountGroup (com.google.gerrit.entities.AccountGroup)4 GroupReference (com.google.gerrit.entities.GroupReference)4 Inject (com.google.inject.Inject)4 IOException (java.io.IOException)4 HashSet (java.util.HashSet)4 List (java.util.List)4 Set (java.util.Set)4 Repository (org.eclipse.jgit.lib.Repository)4 GroupDescription (com.google.gerrit.entities.GroupDescription)3 RefNames (com.google.gerrit.entities.RefNames)3 StorageException (com.google.gerrit.exceptions.StorageException)3