do not compute counts when proposing all keys

This commit is contained in:
2017-11-18 13:03:45 +01:00
parent 14d1367c4a
commit a6a2236d18
7 changed files with 584 additions and 580 deletions

View File

@@ -5,19 +5,19 @@ public class Proposal implements Comparable<Proposal> {
private final String proposedQuery;
private final long results;
private final boolean hasResults;
public Proposal(final String proposedTag, final String proposedQuery, final long results) {
public Proposal(final String proposedTag, final String proposedQuery, final boolean hasResults) {
super();
this.proposedTag = proposedTag;
this.proposedQuery = proposedQuery;
this.results = results;
this.hasResults = hasResults;
}
public Proposal(final Proposal proposal, final long results) {
public Proposal(final Proposal proposal, final boolean hasResults) {
this.proposedTag = proposal.proposedTag;
this.proposedQuery = proposal.proposedQuery;
this.results = results;
this.hasResults = hasResults;
}
public String getProposedTag() {
@@ -28,35 +28,42 @@ public class Proposal implements Comparable<Proposal> {
return proposedQuery;
}
public long getResults() {
return results;
public boolean hasResults() {
return hasResults;
}
@Override
public String toString() {
return "Proposal [proposedTag:" + proposedTag + ", proposedQuery:" + proposedQuery + ", results=" + results
return "Proposal [proposedTag:" + proposedTag + ", proposedQuery:" + proposedQuery + ", hasResults=" + hasResults
+ "]";
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((proposedQuery == null) ? 0 : proposedQuery.hashCode());
result = prime * result + ((proposedTag == null) ? 0 : proposedTag.hashCode());
result = prime * result + (int) (results ^ (results >>> 32));
result = prime * result + (hasResults ? 1231 : 1237);
result = prime * result
+ ((proposedQuery == null) ? 0 : proposedQuery.hashCode());
result = prime * result
+ ((proposedTag == null) ? 0 : proposedTag.hashCode());
return result;
}
@Override
public boolean equals(final Object obj) {
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
final Proposal other = (Proposal) obj;
Proposal other = (Proposal) obj;
if (hasResults != other.hasResults)
return false;
if (proposedQuery == null) {
if (other.proposedQuery != null)
return false;
@@ -67,18 +74,11 @@ public class Proposal implements Comparable<Proposal> {
return false;
} else if (!proposedTag.equals(other.proposedTag))
return false;
if (results != other.results)
return false;
return true;
}
@Override
public int compareTo(final Proposal o) {
if (results != o.results) {
return results < o.results ? 1 : -1;
}
return proposedTag.compareToIgnoreCase(o.proposedTag);
public int compareTo(Proposal o) {
return proposedTag.compareTo(o.getProposedTag());
}
}

View File

@@ -22,8 +22,11 @@ import org.lucares.pdb.datastore.lang.Expression;
import org.lucares.pdb.datastore.lang.ExpressionToDocIdVisitor;
import org.lucares.pdb.datastore.lang.ExpressionToDocIdVisitor.AllDocIds;
import org.lucares.pdb.datastore.lang.QueryLanguageParser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class DataStore {
private static final Logger EXECUTE_QUERY_LOGGER = LoggerFactory.getLogger("org.lucares.metrics.dataStore.executeQuery");
private static final String SUBDIR_STORAGE = "storage";
private static final String PDB_EXTENSION = ".pdb";
@@ -196,10 +199,12 @@ public class DataStore {
}
private IntList executeQuery(final String query) {
final long start = System.nanoTime();
final Expression expression = QueryLanguageParser.parse(query);
final ExpressionToDocIdVisitor visitor = new ExpressionToDocIdVisitor(keyToValueToDocId,
new AllDocIds(docIdToDoc));
final IntList docIdsList = expression.visit(visitor);
EXECUTE_QUERY_LOGGER.debug("executeQuery({}) took {}ms returned {} results " , query, (System.nanoTime() - start) / 1_000_000.0, docIdsList.size());
return docIdsList;
}

View File

@@ -28,7 +28,7 @@ public class Proposer {
result = ProposerParser.parse(query, dataStore, caretIndex);
}
return CollectionUtils.filter(result, p -> p.getResults() >= 0);
return CollectionUtils.filter(result, p -> p.hasResults() );
}
private SortedSet<Proposal> proposeForAllKeys() {
@@ -37,23 +37,21 @@ public class Proposer {
final Map<String, String> fieldToQuery = CollectionUtils.createMapFromKeys(fields, f -> f + "=*");
result = computeProposalsForQueries(fieldToQuery);
result = toProposalsForQueries(fieldToQuery);
return result;
}
private SortedSet<Proposal> computeProposalsForQueries(final Map<String, String> keyToQuery) {
private SortedSet<Proposal> toProposalsForQueries(final Map<String, String> keyToQuery) {
final SortedSet<Proposal> result = new TreeSet<>();
for (final Entry<String, String> e : keyToQuery.entrySet()) {
final String key = e.getKey();
final String query = e.getValue();
final int count = dataStore.count(query);
final Proposal proposal = new Proposal(key, query, count);
final Proposal proposal = new Proposal(key, query, true);
result.add(proposal);
}
return result;
}
}

View File

@@ -54,13 +54,13 @@ public class QueryCompletionPdbLangParser extends PdbLangParser {
proposedValues.stream()//
.map(v -> {
final StringBuilder newQuery = new StringBuilder(query);
newQuery.replace(start, end + 1, v + " ");
newQuery.replace(start, end + 1, v + " "); // insert the terminal into the query
return new Proposal(v, newQuery.toString(), -1);
return new Proposal(v, newQuery.toString(), false);
}).map(p -> {
final int count = dataStore.count(p.getProposedQuery());
return new Proposal(p, count);
return new Proposal(p, count> 0);
}).forEach(proposals::add);
} else if (_ctx instanceof IdentifierExpressionContext) {
@@ -86,13 +86,13 @@ public class QueryCompletionPdbLangParser extends PdbLangParser {
final StringBuilder newQuery = new StringBuilder(query);
newQuery.replace(charPositionInLine, charPositionInLine + text.length(), " and ");
proposals.add(new Proposal(" and ", newQuery.toString(), 1));
proposals.add(new Proposal(" and ", newQuery.toString(), true));
}
if ("or".startsWith(text)) {
final StringBuilder newQuery = new StringBuilder(query);
newQuery.replace(charPositionInLine, charPositionInLine + text.length(), " or ");
proposals.add(new Proposal(" or ", newQuery.toString(), 1));
proposals.add(new Proposal(" or ", newQuery.toString(), true));
}
}
}
@@ -106,12 +106,12 @@ public class QueryCompletionPdbLangParser extends PdbLangParser {
matchingKeys.stream()//
.map(key -> {
return new Proposal(key, String.format(newQueryPattern, key + "=* "), -1);
return new Proposal(key, String.format(newQueryPattern, key + "=* "), false);
}).map(p -> {
final String proposedQuery = p.getProposedQuery();
final int count = count(proposedQuery);
return new Proposal(p, count);
return new Proposal(p, count > 0);
}).forEach(proposals::add);
}

View File

@@ -65,42 +65,42 @@ public class ProposerTest {
public void testEmptyQuery() throws Exception {
assertProposals("", 0, //
new Proposal("name", "name=*", 6), //
new Proposal("bird", "bird=*", 4), //
new Proposal("dog", "dog=*", 2)//
new Proposal("name", "name=*", true), //
new Proposal("bird", "bird=*", true), //
new Proposal("dog", "dog=*", true)//
);
assertProposals(" ", 1, //
new Proposal("name", "name=*", 6), //
new Proposal("bird", "bird=*", 4), //
new Proposal("dog", "dog=*", 2)//
new Proposal("name", "name=*", true), //
new Proposal("bird", "bird=*", true), //
new Proposal("dog", "dog=*", true)//
);
}
public void testPrefixOfKey() throws Exception {
assertProposals("bi", 2, //
new Proposal("bird", "bird=* ", 4) //
new Proposal("bird", "bird=* ", true) //
);
assertProposals("bird", 4, //
new Proposal("bird", "bird=* ", 4) //
new Proposal("bird", "bird=* ", true) //
);
}
public void testPrefixOfValue() throws Exception {
assertProposals("name =Tim", 9, //
new Proposal("Timothy", "name =Timothy ", 1)
new Proposal("Timothy", "name =Timothy ", true)
);
/*
assertProposals("name =Je", 8, //
new Proposal("Jennifer", "name =Jennifer ", 2), //
new Proposal("Jenny", "name =Jenny ", 1) //
new Proposal("Jennifer", "name =Jennifer ", true), //
new Proposal("Jenny", "name =Jenny ", true) //
);
assertProposals("bird=eagle and n", 16, //
new Proposal("name", "bird=eagle and name=* ", 1) //
new Proposal("name", "bird=eagle and name=* ", true) //
);
/*
*/
}

View File

@@ -101,7 +101,7 @@ public class PdbController implements HardcodedValues {
final int zeroBasedCaretIndex = caretIndex - 1;
final List<Proposal> proposals = db.autocomplete(query, zeroBasedCaretIndex);
final List<Proposal> nonEmptyProposals = CollectionUtils.filter(proposals, p -> p.getResults() > 0);
final List<Proposal> nonEmptyProposals = CollectionUtils.filter(proposals, p -> p.hasResults() );
final List<AutocompleteProposal> autocompleteProposals = toAutocompleteProposals(nonEmptyProposals);
Collections.sort(autocompleteProposals, new AutocompleteProposalByValue());

View File

@@ -21,5 +21,6 @@
<logger name="org.lucares.metrics.proposals" level="DEBUG" />
<logger name="org.lucares.metrics.plotter" level="DEBUG" />
<logger name="org.lucares.metrics.gnuplot" level="DEBUG" />
<logger name="org.lucares.metrics.dataStore" level="DEBUG" />
</Loggers>
</Configuration>