Support regexp in file operator if Lucene indexing is enabled

Change-Id: Id68206412e1aa88a42adfdd34b63faaffd77ee40
Signed-off-by: Edwin Kempin <edwin.kempin@sap.com>
This commit is contained in:
Edwin Kempin
2013-06-26 02:41:03 +02:00
parent b2eaee406a
commit 83ec5d48a2
4 changed files with 47 additions and 10 deletions

View File

@@ -34,6 +34,7 @@ import com.google.gerrit.server.index.FieldDef;
import com.google.gerrit.server.index.FieldDef.FillArgs;
import com.google.gerrit.server.index.FieldType;
import com.google.gerrit.server.index.IndexPredicate;
import com.google.gerrit.server.index.RegexPredicate;
import com.google.gerrit.server.index.TimestampRangePredicate;
import com.google.gerrit.server.query.AndPredicate;
import com.google.gerrit.server.query.NotPredicate;
@@ -61,6 +62,7 @@ import org.apache.lucene.search.BooleanQuery;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.NumericRangeQuery;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.RegexpQuery;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.SearcherManager;
import org.apache.lucene.search.Sort;
@@ -307,8 +309,24 @@ public class LuceneChangeIndex implements ChangeIndex, LifecycleListener {
}
private Query exactQuery(IndexPredicate<ChangeData> p) {
if (p instanceof RegexPredicate<?>) {
return regexQuery(p);
} else {
return new TermQuery(new Term(p.getOperator(), p.getValue()));
}
}
private Query regexQuery(IndexPredicate<ChangeData> p) {
String re = p.getValue();
if (re.startsWith("^")) {
re = re.substring(1);
}
if (re.endsWith("$") && !re.endsWith("\\$")) {
re = re.substring(0, re.length() - 1);
}
return new RegexpQuery(new Term(p.getOperator(), re));
}
private class QuerySource implements ChangeDataSource {
// TODO(dborowitz): Push limit down from predicate tree.

View File

@@ -0,0 +1,21 @@
// Copyright (C) 2013 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;
public abstract class RegexPredicate<I> extends IndexPredicate<I> {
protected RegexPredicate(FieldDef<I, ?> def, String value) {
super(def, value);
}
}

View File

@@ -305,16 +305,13 @@ public class ChangeQueryBuilder extends QueryBuilder<ChangeData> {
@Operator
public Predicate<ChangeData> file(String file) throws QueryParseException {
if (allowFileRegex) {
if (file.startsWith("^")) {
if (allowFileRegex || args.index != ChangeIndex.DISABLED) {
return new RegexFilePredicate(args.dbProvider, args.patchListCache, file);
} else {
throw new IllegalArgumentException();
throw error("secondary index must be enabled for file:" + file);
}
} else {
if (file.startsWith("^")) {
throw error("regular expression not permitted here: file:" + file);
}
if (args.index == ChangeIndex.DISABLED) {
throw error("secondary index must be enabled for file:" + file);
}

View File

@@ -15,8 +15,9 @@
package com.google.gerrit.server.query.change;
import com.google.gerrit.reviewdb.server.ReviewDb;
import com.google.gerrit.server.index.ChangeField;
import com.google.gerrit.server.index.RegexPredicate;
import com.google.gerrit.server.patch.PatchListCache;
import com.google.gerrit.server.query.OperatorPredicate;
import com.google.gwtorm.server.OrmException;
import com.google.inject.Provider;
@@ -27,7 +28,7 @@ import dk.brics.automaton.RunAutomaton;
import java.util.Collections;
import java.util.List;
class RegexFilePredicate extends OperatorPredicate<ChangeData> {
class RegexFilePredicate extends RegexPredicate<ChangeData> {
private final Provider<ReviewDb> db;
private final PatchListCache cache;
private final RunAutomaton pattern;
@@ -38,7 +39,7 @@ class RegexFilePredicate extends OperatorPredicate<ChangeData> {
private final boolean prefixOnly;
RegexFilePredicate(Provider<ReviewDb> db, PatchListCache plc, String re) {
super(ChangeQueryBuilder.FIELD_FILE, re);
super(ChangeField.FILE, re);
this.db = db;
this.cache = plc;