The Unified and Autonomous Development Platform
Limit complexity of literal calculation.
robin shine768971efedd0
diff --git a/gitplex.search/src/main/java/com/pmease/gitplex/search/query/BlobQuery.java b/gitplex.search/src/main/java/com/pmease/gitplex/search/query/BlobQuery.javaindex a0471c939c..045ace0f6f 100644--- a/gitplex.search/src/main/java/com/pmease/gitplex/search/query/BlobQuery.java+++ b/gitplex.search/src/main/java/com/pmease/gitplex/search/query/BlobQuery.java@@ -1,21 +1,18 @@package com.pmease.gitplex.search.query;-import java.io.IOException;-import java.io.StringReader;+import static com.pmease.gitplex.search.IndexConstants.NGRAM_SIZE;+import java.util.List;import javax.annotation.Nullable;-import org.apache.lucene.analysis.ngram.NGramTokenizer;-import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;-import org.apache.lucene.index.Term;import org.apache.lucene.search.BooleanClause.Occur;import org.apache.lucene.search.BooleanQuery;-import org.apache.lucene.search.NGramPhraseQuery;import org.apache.lucene.search.Query;import org.eclipse.jgit.treewalk.TreeWalk;import com.pmease.gitplex.search.hit.QueryHit;+import com.pmease.gitplex.search.query.regex.RegexLiterals;public abstract class BlobQuery {@@ -35,11 +32,9 @@ public abstract class BlobQuery {private final String pathSuffix;- private final int gramSize;-- public BlobQuery(String fieldName, String searchFor, String pathPrefix,- String pathSuffix, boolean wordMatch, boolean caseSensitive,- boolean regex, int count, int gramSize) {+ public BlobQuery(String fieldName, String searchFor, @Nullable String pathPrefix,+ @Nullable String pathSuffix, boolean wordMatch, boolean caseSensitive,+ boolean regex, int count) {this.fieldName = fieldName;this.searchFor = searchFor;this.pathPrefix = pathPrefix;@@ -48,7 +43,6 @@ public abstract class BlobQuery {this.caseSensitive = caseSensitive;this.wordMatch = wordMatch;this.regex = regex;- this.gramSize = gramSize;}public String getFieldName() {@@ -85,48 +79,18 @@ public abstract class BlobQuery {public abstract void collect(TreeWalk treeWalk, List<QueryHit> hits);- @Nullable- private NGramPhraseQuery asNGramQuery(String text) {- if (text != null && text.length() >= gramSize) {- NGramPhraseQuery query = new NGramPhraseQuery(gramSize);- try (NGramTokenizer tokenizer =- new NGramTokenizer(new StringReader(searchFor.toLowerCase()), gramSize, gramSize)) {- tokenizer.reset();- while (tokenizer.incrementToken()) {- query.add(new Term(fieldName,- tokenizer.getAttribute(CharTermAttribute.class).toString()));- }- } catch (IOException e) {- throw new RuntimeException(e);- }- return query;- } else {- return null;- }- }-public Query asLuceneQuery() {BooleanQuery query = new BooleanQuery(true);- /*- if (regex) {- for (String literal: getLiterals(searchFor)) {- NGramPhraseQuery subQuery = asNGramQuery(literal);- if (subQuery != null)- query.add(subQuery, Occur.MUST);- }- } else {- NGramPhraseQuery subQuery = asNGramQuery(searchFor);- if (subQuery != null)- query.add(subQuery, Occur.MUST);- }- */- NGramPhraseQuery subQuery = asNGramQuery(pathPrefix);- if (subQuery != null)- query.add(subQuery, Occur.MUST);-- subQuery = asNGramQuery(pathSuffix);- if (subQuery != null)- query.add(subQuery, Occur.MUST);+ if (regex)+ query.add(new RegexLiterals(searchFor).asLuceneQuery(fieldName), Occur.MUST);+ else if (searchFor.length() >= NGRAM_SIZE)+ query.add(new NGramLuceneQuery(fieldName, searchFor), Occur.MUST);++ if (pathPrefix != null && pathPrefix.length() >= NGRAM_SIZE)+ query.add(new NGramLuceneQuery(fieldName, pathPrefix), Occur.MUST);++ if (pathSuffix != null && pathSuffix.length() >= NGRAM_SIZE)+ query.add(new NGramLuceneQuery(fieldName, pathSuffix), Occur.MUST);return query;}diff --git a/gitplex.search/src/main/java/com/pmease/gitplex/search/query/SymbolQuery.java b/gitplex.search/src/main/java/com/pmease/gitplex/search/query/SymbolQuery.javaindex d852bfa487..7a3123b6e9 100644--- a/gitplex.search/src/main/java/com/pmease/gitplex/search/query/SymbolQuery.java+++ b/gitplex.search/src/main/java/com/pmease/gitplex/search/query/SymbolQuery.java@@ -26,7 +26,7 @@ public class SymbolQuery extends BlobQuery {public SymbolQuery(String searchFor, @Nullable String pathPrefix, @Nullable String pathSuffix,boolean exactMatch, boolean caseSensitive, boolean regex, int count) {super(FieldConstants.BLOB_SYMBOLS.name(), pathPrefix, pathSuffix, searchFor, exactMatch,- caseSensitive, regex, count, IndexConstants.NGRAM_SIZE);+ caseSensitive, regex, count);}public SymbolQuery(String searchFor, boolean exactMatch, boolean caseSensitive, boolean regex, int count) {diff --git a/gitplex.search/src/main/java/com/pmease/gitplex/search/query/TextQuery.java b/gitplex.search/src/main/java/com/pmease/gitplex/search/query/TextQuery.javaindex 41809d7cb9..227458dabf 100644--- a/gitplex.search/src/main/java/com/pmease/gitplex/search/query/TextQuery.java+++ b/gitplex.search/src/main/java/com/pmease/gitplex/search/query/TextQuery.java@@ -22,7 +22,7 @@ public class TextQuery extends BlobQuery {public TextQuery(String searchFor, @Nullable String pathPrefix, @Nullable String pathSuffix,boolean caseSensitive, boolean regex, int count) {super(FieldConstants.BLOB_TEXT.name(), searchFor, pathPrefix, pathSuffix, false,- caseSensitive, regex, count, IndexConstants.NGRAM_SIZE);+ caseSensitive, regex, count);}public TextQuery(String searchFor, boolean caseSensitive, boolean regex, int count) {diff --git a/gitplex.search/src/main/java/com/pmease/gitplex/search/query/regex/AndLiterals.java b/gitplex.search/src/main/java/com/pmease/gitplex/search/query/regex/AndLiterals.javaindex 5e6e6bcdd8..4ae845a8eb 100644--- a/gitplex.search/src/main/java/com/pmease/gitplex/search/query/regex/AndLiterals.java+++ b/gitplex.search/src/main/java/com/pmease/gitplex/search/query/regex/AndLiterals.java@@ -3,10 +3,14 @@ package com.pmease.gitplex.search.query.regex;import java.util.ArrayList;import java.util.List;+import com.google.common.base.Strings;import com.google.common.collect.Lists;public class AndLiterals extends LogicalLiterals {+ // limit complexity of resulting product+ private static final int MAX_ROWS = 64;+public AndLiterals(List<Literals> elements) {super(elements);}@@ -22,21 +26,55 @@ public class AndLiterals extends LogicalLiterals {} else if (getElements().size() == 1) {return getElements().get(0).flattern(outmost);} else {- List<List<LeafLiterals>> rows = new ArrayList<>();-List<List<LeafLiterals>> column1 = getElements().get(0).flattern(false);List<List<LeafLiterals>> column2 = new AndLiterals(getElements().subList(1, getElements().size())).flattern(false);- for (List<LeafLiterals> rowOfColumn1: column1) {- for (List<LeafLiterals> rowOfColumn2: column2) {- List<LeafLiterals> joinedRow = new ArrayList<>(rowOfColumn1);- joinedRow.addAll(rowOfColumn2);- rows.add(LiteralUtils.merge(joinedRow, outmost));++ List<List<LeafLiterals>> rows = new ArrayList<>();+ if (column1.size()*column2.size() <= MAX_ROWS) {+ for (List<LeafLiterals> rowOfColumn1: column1) {+ for (List<LeafLiterals> rowOfColumn2: column2) {+ List<LeafLiterals> joinedRow = new ArrayList<>(rowOfColumn1);+ joinedRow.addAll(rowOfColumn2);+ rows.add(LiteralUtils.merge(joinedRow, outmost));+ }+ }+ } else {+ if (getWeightOfRows(column1)>getWeightOfRows(column2)) {+ for (List<LeafLiterals> row: column1) {+ List<LeafLiterals> joinedRow = new ArrayList<>(row);+ joinedRow.add(new LeafLiterals(null));+ rows.add(LiteralUtils.merge(joinedRow, outmost));+ }+ } else {+ for (List<LeafLiterals> row: column2) {+ List<LeafLiterals> joinedRow = new ArrayList<>();+ joinedRow.add(new LeafLiterals(null));+ joinedRow.addAll(row);+ rows.add(LiteralUtils.merge(joinedRow, outmost));+ }}}- // we must add trim here, otherwise, the resulting production will be increased exponentiallyLiteralUtils.trim(rows, outmost);return rows;}}+ private long getWeightOfRow(List<LeafLiterals> row) {+ long weight = 1;+ for (LeafLiterals leaf: row) {+ if (!Strings.isNullOrEmpty(leaf.getLiteral()))+ weight *= leaf.getLiteral().length();+ }+ return weight;+ }++ private long getWeightOfRows(List<List<LeafLiterals>> rows) {+ long weight = Long.MAX_VALUE;+ for (List<LeafLiterals> row: rows) {+ long rowWeight = getWeightOfRow(row);+ if (rowWeight < weight)+ weight = rowWeight;+ }+ return weight;+ }}diff --git a/gitplex.search/src/main/java/com/pmease/gitplex/search/query/regex/OrLiterals.java b/gitplex.search/src/main/java/com/pmease/gitplex/search/query/regex/OrLiterals.javaindex 1b2a33ca29..c165410b34 100644--- a/gitplex.search/src/main/java/com/pmease/gitplex/search/query/regex/OrLiterals.java+++ b/gitplex.search/src/main/java/com/pmease/gitplex/search/query/regex/OrLiterals.java@@ -18,7 +18,6 @@ public class OrLiterals extends LogicalLiterals {List<List<LeafLiterals>> rows = new ArrayList<>();for (Literals element: getElements())rows.addAll(element.flattern(outmost));- // we must add trim here, otherwise, the resulting production will be increased exponentiallyLiteralUtils.trim(rows, outmost);return rows;}diff --git a/gitplex.search/src/test/java/com/pmease/gitplex/search/IndexAndSearchTest.java b/gitplex.search/src/test/java/com/pmease/gitplex/search/IndexAndSearchTest.javaindex 8804e60fb6..2a3bd62d62 100644--- a/gitplex.search/src/test/java/com/pmease/gitplex/search/IndexAndSearchTest.java+++ b/gitplex.search/src/test/java/com/pmease/gitplex/search/IndexAndSearchTest.java@@ -73,7 +73,7 @@ public class IndexAndSearchTest extends AbstractGitTest {}@Test- public void test() throws InterruptedException {+ public void testBasic() throws InterruptedException {String code = ""+ "public class Dog {\n"+ " public String name;\n"@@ -179,6 +179,28 @@ public class IndexAndSearchTest extends AbstractGitTest {assertEquals(2, hits.size());}+ @Test+ public void testRegex() throws InterruptedException {+ String code = ""+ + "public class Dog {\n"+ + " public String name;\n"+ + "}";+ addFileAndCommit("Dog.java", code, "add dog");++ code = ""+ + "public class Cat {\n"+ + " public String name;\n"+ + "}";+ addFileAndCommit("Cat.java", code, "add cat");++ String commitHash = git.parseRevision("master", true);+ assertEquals(2, indexManager.index(repository, commitHash).getIndexed());++ TextQuery contentQuery = new TextQuery("public", false, false, Integer.MAX_VALUE);+ List<QueryHit> hits = searchManager.search(repository, commitHash, contentQuery);+ assertEquals(4, hits.size());+ }+@Overrideprotected void teardown() {super.teardown();diff --git a/gitplex.search/src/test/java/com/pmease/gitplex/search/query/regex/RegexLiteralsTest.java b/gitplex.search/src/test/java/com/pmease/gitplex/search/query/regex/RegexLiteralsTest.javaindex 3390dd0bd5..df9c0c68d1 100644--- a/gitplex.search/src/test/java/com/pmease/gitplex/search/query/regex/RegexLiteralsTest.java+++ b/gitplex.search/src/test/java/com/pmease/gitplex/search/query/regex/RegexLiteralsTest.java@@ -1,9 +1,12 @@package com.pmease.gitplex.search.query.regex;-import static org.junit.Assert.*;+import static org.junit.Assert.assertEquals;+import static org.junit.Assert.assertTrue;import org.junit.Test;+import com.google.common.base.Splitter;+public class RegexLiteralsTest {@Test@@ -19,6 +22,11 @@ public class RegexLiteralsTest {assertEquals("a&b", new RegexLiterals("a\\d+\\wb").toString());assertEquals("", new RegexLiterals("a*b*c*").toString());assertEquals("abb&bbc", new RegexLiterals("ab{2,}c").toString());++ // should pick the most import part if the regex is too complex to be simplified+ String regex = "[ab1](cd)[pqr](hello)[xyz][ef3][gh4][ij5][kl6][mn7]";+ for (String each: Splitter.on("|").split(new RegexLiterals(regex).toString()))+ assertTrue(each.contains("cd&hello"));}}