Add IndexedAccountQuery
This change extracts the generic parts from IndexedChangeQuery into a base class that can also be used as base for IndexedAccountQuery. IndexedAccountQuery will be needed to implement AccountIndexRewriter. Change-Id: I865868a866ca3cda84ef28bad0733b41f776dff4 Signed-off-by: Edwin Kempin <ekempin@google.com>
This commit is contained in:
@@ -0,0 +1,183 @@
|
||||
// Copyright (C) 2016 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.index;
|
||||
|
||||
import com.google.common.base.Function;
|
||||
import com.google.common.base.MoreObjects;
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import com.google.common.collect.Iterables;
|
||||
import com.google.gerrit.server.query.DataSource;
|
||||
import com.google.gerrit.server.query.Paginated;
|
||||
import com.google.gerrit.server.query.Predicate;
|
||||
import com.google.gerrit.server.query.QueryParseException;
|
||||
import com.google.gwtorm.server.OrmException;
|
||||
import com.google.gwtorm.server.ResultSet;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Wrapper combining an {@link IndexPredicate} together with a
|
||||
* {@link DataSource} that returns matching results from the index.
|
||||
* <p>
|
||||
* Appropriate to return as the rootmost predicate that can be processed using
|
||||
* the secondary index; such predicates must also implement {@link DataSource}
|
||||
* to be chosen by the query processor.
|
||||
*
|
||||
* @param <I> The type of the IDs by which the entities are stored in the index.
|
||||
* @param <T> The type of the entities that are stored in the index.
|
||||
*/
|
||||
public class IndexedQuery<I, T> extends Predicate<T>
|
||||
implements DataSource<T>, Paginated<T> {
|
||||
protected final Index<I, T> index;
|
||||
|
||||
private QueryOptions opts;
|
||||
private final Predicate<T> pred;
|
||||
private DataSource<T> source;
|
||||
private final Map<T, DataSource<T>> fromSource;
|
||||
|
||||
public IndexedQuery(Index<I, T> index, Predicate<T> pred,
|
||||
QueryOptions opts) throws QueryParseException {
|
||||
this.index = index;
|
||||
this.opts = opts;
|
||||
this.pred = pred;
|
||||
this.source = index.getSource(pred, this.opts);
|
||||
this.fromSource = new HashMap<>();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getChildCount() {
|
||||
return 1;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Predicate<T> getChild(int i) {
|
||||
if (i == 0) {
|
||||
return pred;
|
||||
}
|
||||
throw new ArrayIndexOutOfBoundsException(i);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Predicate<T>> getChildren() {
|
||||
return ImmutableList.of(pred);
|
||||
}
|
||||
|
||||
@Override
|
||||
public QueryOptions getOptions() {
|
||||
return opts;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getCardinality() {
|
||||
return source != null ? source.getCardinality() : opts.limit();
|
||||
}
|
||||
|
||||
@Override
|
||||
public ResultSet<T> read() throws OrmException {
|
||||
final DataSource<T> currSource = source;
|
||||
final ResultSet<T> rs = currSource.read();
|
||||
|
||||
return new ResultSet<T>() {
|
||||
@Override
|
||||
public Iterator<T> iterator() {
|
||||
return Iterables.transform(
|
||||
rs,
|
||||
new Function<T, T>() {
|
||||
@Override
|
||||
public
|
||||
T apply(T t) {
|
||||
fromSource.put(t, currSource);
|
||||
return t;
|
||||
}
|
||||
}).iterator();
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<T> toList() {
|
||||
List<T> r = rs.toList();
|
||||
for (T t : r) {
|
||||
fromSource.put(t, currSource);
|
||||
}
|
||||
return r;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
rs.close();
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
public ResultSet<T> restart(int start) throws OrmException {
|
||||
opts = opts.withStart(start);
|
||||
try {
|
||||
source = index.getSource(pred, opts);
|
||||
} catch (QueryParseException e) {
|
||||
// Don't need to show this exception to the user; the only thing that
|
||||
// changed about pred was its start, and any other QPEs that might happen
|
||||
// should have already thrown from the constructor.
|
||||
throw new OrmException(e);
|
||||
}
|
||||
// Don't convert start to a limit, since the caller of this method (see
|
||||
// AndSource) has calculated the actual number to skip.
|
||||
return read();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Predicate<T> copy(Collection<? extends Predicate<T>> children) {
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean match(T t) throws OrmException {
|
||||
return (source != null && fromSource.get(t) == source) || pred.match(t);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getCost() {
|
||||
// Index queries are assumed to be cheaper than any other type of query, so
|
||||
// so try to make sure they get picked. Note that pred's cost may be higher
|
||||
// because it doesn't know whether it's being used in an index query or not.
|
||||
return 1;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return pred.hashCode();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object other) {
|
||||
if (other == null || getClass() != other.getClass()) {
|
||||
return false;
|
||||
}
|
||||
IndexedQuery<?, ?> o = (IndexedQuery<?, ?>) other;
|
||||
return pred.equals(o.pred)
|
||||
&& opts.equals(o.opts);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return MoreObjects.toStringHelper("index")
|
||||
.add("p", pred)
|
||||
.add("opts", opts)
|
||||
.toString();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
// Copyright (C) 2016 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.index.account;
|
||||
|
||||
import com.google.gerrit.reviewdb.client.Account;
|
||||
import com.google.gerrit.server.account.AccountState;
|
||||
import com.google.gerrit.server.index.Index;
|
||||
import com.google.gerrit.server.index.IndexedQuery;
|
||||
import com.google.gerrit.server.index.QueryOptions;
|
||||
import com.google.gerrit.server.query.DataSource;
|
||||
import com.google.gerrit.server.query.Predicate;
|
||||
import com.google.gerrit.server.query.QueryParseException;
|
||||
|
||||
public class IndexedAccountQuery extends IndexedQuery<Account.Id, AccountState>
|
||||
implements DataSource<AccountState> {
|
||||
|
||||
public IndexedAccountQuery(Index<Account.Id, AccountState> index,
|
||||
Predicate<AccountState> pred, QueryOptions opts)
|
||||
throws QueryParseException {
|
||||
super(index, pred, opts);
|
||||
}
|
||||
}
|
||||
@@ -18,28 +18,19 @@ import static com.google.gerrit.server.index.change.ChangeField.CHANGE;
|
||||
import static com.google.gerrit.server.index.change.ChangeField.PROJECT;
|
||||
|
||||
import com.google.common.annotations.VisibleForTesting;
|
||||
import com.google.common.base.Function;
|
||||
import com.google.common.base.MoreObjects;
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import com.google.common.collect.ImmutableSet;
|
||||
import com.google.common.collect.Iterables;
|
||||
import com.google.common.primitives.Ints;
|
||||
import com.google.gerrit.reviewdb.client.Change;
|
||||
import com.google.gerrit.server.index.IndexConfig;
|
||||
import com.google.gerrit.server.index.IndexPredicate;
|
||||
import com.google.gerrit.server.index.IndexedQuery;
|
||||
import com.google.gerrit.server.index.QueryOptions;
|
||||
import com.google.gerrit.server.query.DataSource;
|
||||
import com.google.gerrit.server.query.Predicate;
|
||||
import com.google.gerrit.server.query.QueryParseException;
|
||||
import com.google.gerrit.server.query.change.ChangeData;
|
||||
import com.google.gerrit.server.query.change.ChangeDataSource;
|
||||
import com.google.gerrit.server.query.change.Paginated;
|
||||
import com.google.gwtorm.server.OrmException;
|
||||
import com.google.gwtorm.server.ResultSet;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.HashSet;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
@@ -50,8 +41,8 @@ import java.util.Set;
|
||||
* the secondary index; such predicates must also implement
|
||||
* {@link ChangeDataSource} to be chosen by the query processor.
|
||||
*/
|
||||
public class IndexedChangeQuery extends Predicate<ChangeData>
|
||||
implements ChangeDataSource, Paginated {
|
||||
public class IndexedChangeQuery extends IndexedQuery<Change.Id, ChangeData>
|
||||
implements ChangeDataSource {
|
||||
public static QueryOptions oneResult() {
|
||||
return createOptions(IndexConfig.createDefault(), 0, 1,
|
||||
ImmutableSet.<String> of());
|
||||
@@ -78,144 +69,13 @@ public class IndexedChangeQuery extends Predicate<ChangeData>
|
||||
return IndexedChangeQuery.createOptions(opts.config(), 0, limit, opts.fields());
|
||||
}
|
||||
|
||||
private final ChangeIndex index;
|
||||
|
||||
private QueryOptions opts;
|
||||
private Predicate<ChangeData> pred;
|
||||
private DataSource<ChangeData> source;
|
||||
|
||||
public IndexedChangeQuery(ChangeIndex index, Predicate<ChangeData> pred,
|
||||
QueryOptions opts) throws QueryParseException {
|
||||
this.index = index;
|
||||
this.opts = convertOptions(opts);
|
||||
this.pred = pred;
|
||||
this.source = index.getSource(pred, this.opts);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getChildCount() {
|
||||
return 1;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Predicate<ChangeData> getChild(int i) {
|
||||
if (i == 0) {
|
||||
return pred;
|
||||
}
|
||||
throw new ArrayIndexOutOfBoundsException(i);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Predicate<ChangeData>> getChildren() {
|
||||
return ImmutableList.of(pred);
|
||||
}
|
||||
|
||||
@Override
|
||||
public QueryOptions getOptions() {
|
||||
return opts;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getCardinality() {
|
||||
return source != null ? source.getCardinality() : opts.limit();
|
||||
super(index, pred, convertOptions(opts));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasChange() {
|
||||
return index.getSchema().hasField(ChangeField.CHANGE);
|
||||
}
|
||||
|
||||
@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,
|
||||
new Function<ChangeData, ChangeData>() {
|
||||
@Override
|
||||
public
|
||||
ChangeData apply(ChangeData input) {
|
||||
input.cacheFromSource(currSource);
|
||||
return input;
|
||||
}
|
||||
}).iterator();
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<ChangeData> toList() {
|
||||
List<ChangeData> r = rs.toList();
|
||||
for (ChangeData cd : r) {
|
||||
cd.cacheFromSource(currSource);
|
||||
}
|
||||
return r;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
rs.close();
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
public ResultSet<ChangeData> restart(int start) throws OrmException {
|
||||
opts = opts.withStart(start);
|
||||
try {
|
||||
source = index.getSource(pred, opts);
|
||||
} catch (QueryParseException e) {
|
||||
// Don't need to show this exception to the user; the only thing that
|
||||
// changed about pred was its start, and any other QPEs that might happen
|
||||
// should have already thrown from the constructor.
|
||||
throw new OrmException(e);
|
||||
}
|
||||
// Don't convert start to a limit, since the caller of this method (see
|
||||
// AndSource) has calculated the actual number to skip.
|
||||
return read();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Predicate<ChangeData> copy(
|
||||
Collection<? extends Predicate<ChangeData>> children) {
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean match(ChangeData cd) throws OrmException {
|
||||
return (source != null && cd.isFromSource(source)) || pred.match(cd);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getCost() {
|
||||
// Index queries are assumed to be cheaper than any other type of query, so
|
||||
// so try to make sure they get picked. Note that pred's cost may be higher
|
||||
// because it doesn't know whether it's being used in an index query or not.
|
||||
return 1;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return pred.hashCode();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object other) {
|
||||
if (other == null || getClass() != other.getClass()) {
|
||||
return false;
|
||||
}
|
||||
IndexedChangeQuery o = (IndexedChangeQuery) other;
|
||||
return pred.equals(o.pred)
|
||||
&& opts.equals(o.opts);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return MoreObjects.toStringHelper("index")
|
||||
.add("p", pred)
|
||||
.add("opts", opts)
|
||||
.toString();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,14 +12,14 @@
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package com.google.gerrit.server.query.change;
|
||||
package com.google.gerrit.server.query;
|
||||
|
||||
import com.google.gerrit.server.index.QueryOptions;
|
||||
import com.google.gwtorm.server.OrmException;
|
||||
import com.google.gwtorm.server.ResultSet;
|
||||
|
||||
public interface Paginated {
|
||||
public interface Paginated<T> {
|
||||
QueryOptions getOptions();
|
||||
|
||||
ResultSet<ChangeData> restart(int start) throws OrmException;
|
||||
ResultSet<T> restart(int start) throws OrmException;
|
||||
}
|
||||
@@ -22,6 +22,7 @@ import com.google.common.collect.FluentIterable;
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import com.google.common.collect.Iterables;
|
||||
import com.google.gerrit.server.query.AndPredicate;
|
||||
import com.google.gerrit.server.query.Paginated;
|
||||
import com.google.gerrit.server.query.Predicate;
|
||||
import com.google.gwtorm.server.ListResultSet;
|
||||
import com.google.gwtorm.server.OrmException;
|
||||
@@ -126,7 +127,8 @@ public class AndSource extends AndPredicate<ChangeData>
|
||||
// least one of its results, we may not have filled the full
|
||||
// limit the caller wants. Restart the source and continue.
|
||||
//
|
||||
Paginated p = (Paginated) source;
|
||||
@SuppressWarnings("unchecked")
|
||||
Paginated<ChangeData> p = (Paginated<ChangeData>) source;
|
||||
while (skipped && r.size() < p.getOptions().limit() + start) {
|
||||
skipped = false;
|
||||
ResultSet<ChangeData> next = p.restart(nextStart);
|
||||
|
||||
@@ -64,7 +64,6 @@ import com.google.gerrit.server.project.ChangeControl;
|
||||
import com.google.gerrit.server.project.NoSuchChangeException;
|
||||
import com.google.gerrit.server.project.ProjectCache;
|
||||
import com.google.gerrit.server.project.SubmitRuleEvaluator;
|
||||
import com.google.gerrit.server.query.DataSource;
|
||||
import com.google.gwtorm.server.OrmException;
|
||||
import com.google.gwtorm.server.ResultSet;
|
||||
import com.google.inject.assistedinject.Assisted;
|
||||
@@ -323,7 +322,6 @@ public class ChangeData {
|
||||
private final MergeabilityCache mergeabilityCache;
|
||||
private final StarredChangesUtil starredChangesUtil;
|
||||
private final Change.Id legacyId;
|
||||
private DataSource<ChangeData> returnedBySource;
|
||||
private Project.NameKey project;
|
||||
private Change change;
|
||||
private ChangeNotes notes;
|
||||
@@ -551,14 +549,6 @@ public class ChangeData {
|
||||
return db;
|
||||
}
|
||||
|
||||
public boolean isFromSource(DataSource<ChangeData> s) {
|
||||
return s == returnedBySource;
|
||||
}
|
||||
|
||||
public void cacheFromSource(DataSource<ChangeData> s) {
|
||||
returnedBySource = s;
|
||||
}
|
||||
|
||||
public void setCurrentFilePaths(List<String> filePaths) throws OrmException {
|
||||
PatchSet ps = currentPatchSet();
|
||||
if (ps != null) {
|
||||
|
||||
Reference in New Issue
Block a user