do not compute counts when proposing all keys
This commit is contained in:
@@ -5,19 +5,19 @@ public class Proposal implements Comparable<Proposal> {
|
|||||||
|
|
||||||
private final String proposedQuery;
|
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();
|
super();
|
||||||
this.proposedTag = proposedTag;
|
this.proposedTag = proposedTag;
|
||||||
this.proposedQuery = proposedQuery;
|
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.proposedTag = proposal.proposedTag;
|
||||||
this.proposedQuery = proposal.proposedQuery;
|
this.proposedQuery = proposal.proposedQuery;
|
||||||
this.results = results;
|
this.hasResults = hasResults;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getProposedTag() {
|
public String getProposedTag() {
|
||||||
@@ -28,35 +28,42 @@ public class Proposal implements Comparable<Proposal> {
|
|||||||
return proposedQuery;
|
return proposedQuery;
|
||||||
}
|
}
|
||||||
|
|
||||||
public long getResults() {
|
public boolean hasResults() {
|
||||||
return results;
|
return hasResults;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return "Proposal [proposedTag:" + proposedTag + ", proposedQuery:" + proposedQuery + ", results=" + results
|
return "Proposal [proposedTag:" + proposedTag + ", proposedQuery:" + proposedQuery + ", hasResults=" + hasResults
|
||||||
+ "]";
|
+ "]";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int hashCode() {
|
public int hashCode() {
|
||||||
final int prime = 31;
|
final int prime = 31;
|
||||||
int result = 1;
|
int result = 1;
|
||||||
result = prime * result + ((proposedQuery == null) ? 0 : proposedQuery.hashCode());
|
result = prime * result + (hasResults ? 1231 : 1237);
|
||||||
result = prime * result + ((proposedTag == null) ? 0 : proposedTag.hashCode());
|
result = prime * result
|
||||||
result = prime * result + (int) (results ^ (results >>> 32));
|
+ ((proposedQuery == null) ? 0 : proposedQuery.hashCode());
|
||||||
|
result = prime * result
|
||||||
|
+ ((proposedTag == null) ? 0 : proposedTag.hashCode());
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean equals(final Object obj) {
|
public boolean equals(Object obj) {
|
||||||
if (this == obj)
|
if (this == obj)
|
||||||
return true;
|
return true;
|
||||||
if (obj == null)
|
if (obj == null)
|
||||||
return false;
|
return false;
|
||||||
if (getClass() != obj.getClass())
|
if (getClass() != obj.getClass())
|
||||||
return false;
|
return false;
|
||||||
final Proposal other = (Proposal) obj;
|
Proposal other = (Proposal) obj;
|
||||||
|
if (hasResults != other.hasResults)
|
||||||
|
return false;
|
||||||
if (proposedQuery == null) {
|
if (proposedQuery == null) {
|
||||||
if (other.proposedQuery != null)
|
if (other.proposedQuery != null)
|
||||||
return false;
|
return false;
|
||||||
@@ -67,18 +74,11 @@ public class Proposal implements Comparable<Proposal> {
|
|||||||
return false;
|
return false;
|
||||||
} else if (!proposedTag.equals(other.proposedTag))
|
} else if (!proposedTag.equals(other.proposedTag))
|
||||||
return false;
|
return false;
|
||||||
if (results != other.results)
|
|
||||||
return false;
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int compareTo(final Proposal o) {
|
public int compareTo(Proposal o) {
|
||||||
|
return proposedTag.compareTo(o.getProposedTag());
|
||||||
if (results != o.results) {
|
|
||||||
return results < o.results ? 1 : -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
return proposedTag.compareToIgnoreCase(o.proposedTag);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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;
|
||||||
import org.lucares.pdb.datastore.lang.ExpressionToDocIdVisitor.AllDocIds;
|
import org.lucares.pdb.datastore.lang.ExpressionToDocIdVisitor.AllDocIds;
|
||||||
import org.lucares.pdb.datastore.lang.QueryLanguageParser;
|
import org.lucares.pdb.datastore.lang.QueryLanguageParser;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
public class DataStore {
|
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 SUBDIR_STORAGE = "storage";
|
||||||
private static final String PDB_EXTENSION = ".pdb";
|
private static final String PDB_EXTENSION = ".pdb";
|
||||||
@@ -196,10 +199,12 @@ public class DataStore {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private IntList executeQuery(final String query) {
|
private IntList executeQuery(final String query) {
|
||||||
|
final long start = System.nanoTime();
|
||||||
final Expression expression = QueryLanguageParser.parse(query);
|
final Expression expression = QueryLanguageParser.parse(query);
|
||||||
final ExpressionToDocIdVisitor visitor = new ExpressionToDocIdVisitor(keyToValueToDocId,
|
final ExpressionToDocIdVisitor visitor = new ExpressionToDocIdVisitor(keyToValueToDocId,
|
||||||
new AllDocIds(docIdToDoc));
|
new AllDocIds(docIdToDoc));
|
||||||
final IntList docIdsList = expression.visit(visitor);
|
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;
|
return docIdsList;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -28,7 +28,7 @@ public class Proposer {
|
|||||||
result = ProposerParser.parse(query, dataStore, caretIndex);
|
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() {
|
private SortedSet<Proposal> proposeForAllKeys() {
|
||||||
@@ -37,23 +37,21 @@ public class Proposer {
|
|||||||
|
|
||||||
final Map<String, String> fieldToQuery = CollectionUtils.createMapFromKeys(fields, f -> f + "=*");
|
final Map<String, String> fieldToQuery = CollectionUtils.createMapFromKeys(fields, f -> f + "=*");
|
||||||
|
|
||||||
result = computeProposalsForQueries(fieldToQuery);
|
result = toProposalsForQueries(fieldToQuery);
|
||||||
return result;
|
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<>();
|
final SortedSet<Proposal> result = new TreeSet<>();
|
||||||
for (final Entry<String, String> e : keyToQuery.entrySet()) {
|
for (final Entry<String, String> e : keyToQuery.entrySet()) {
|
||||||
final String key = e.getKey();
|
final String key = e.getKey();
|
||||||
final String query = e.getValue();
|
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);
|
result.add(proposal);
|
||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -54,13 +54,13 @@ public class QueryCompletionPdbLangParser extends PdbLangParser {
|
|||||||
proposedValues.stream()//
|
proposedValues.stream()//
|
||||||
.map(v -> {
|
.map(v -> {
|
||||||
final StringBuilder newQuery = new StringBuilder(query);
|
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 -> {
|
}).map(p -> {
|
||||||
|
|
||||||
final int count = dataStore.count(p.getProposedQuery());
|
final int count = dataStore.count(p.getProposedQuery());
|
||||||
return new Proposal(p, count);
|
return new Proposal(p, count> 0);
|
||||||
}).forEach(proposals::add);
|
}).forEach(proposals::add);
|
||||||
|
|
||||||
} else if (_ctx instanceof IdentifierExpressionContext) {
|
} else if (_ctx instanceof IdentifierExpressionContext) {
|
||||||
@@ -86,13 +86,13 @@ public class QueryCompletionPdbLangParser extends PdbLangParser {
|
|||||||
final StringBuilder newQuery = new StringBuilder(query);
|
final StringBuilder newQuery = new StringBuilder(query);
|
||||||
newQuery.replace(charPositionInLine, charPositionInLine + text.length(), " and ");
|
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)) {
|
if ("or".startsWith(text)) {
|
||||||
final StringBuilder newQuery = new StringBuilder(query);
|
final StringBuilder newQuery = new StringBuilder(query);
|
||||||
newQuery.replace(charPositionInLine, charPositionInLine + text.length(), " or ");
|
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()//
|
matchingKeys.stream()//
|
||||||
.map(key -> {
|
.map(key -> {
|
||||||
|
|
||||||
return new Proposal(key, String.format(newQueryPattern, key + "=* "), -1);
|
return new Proposal(key, String.format(newQueryPattern, key + "=* "), false);
|
||||||
}).map(p -> {
|
}).map(p -> {
|
||||||
|
|
||||||
final String proposedQuery = p.getProposedQuery();
|
final String proposedQuery = p.getProposedQuery();
|
||||||
final int count = count(proposedQuery);
|
final int count = count(proposedQuery);
|
||||||
return new Proposal(p, count);
|
return new Proposal(p, count > 0);
|
||||||
}).forEach(proposals::add);
|
}).forEach(proposals::add);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -65,42 +65,42 @@ public class ProposerTest {
|
|||||||
public void testEmptyQuery() throws Exception {
|
public void testEmptyQuery() throws Exception {
|
||||||
|
|
||||||
assertProposals("", 0, //
|
assertProposals("", 0, //
|
||||||
new Proposal("name", "name=*", 6), //
|
new Proposal("name", "name=*", true), //
|
||||||
new Proposal("bird", "bird=*", 4), //
|
new Proposal("bird", "bird=*", true), //
|
||||||
new Proposal("dog", "dog=*", 2)//
|
new Proposal("dog", "dog=*", true)//
|
||||||
);
|
);
|
||||||
|
|
||||||
assertProposals(" ", 1, //
|
assertProposals(" ", 1, //
|
||||||
new Proposal("name", "name=*", 6), //
|
new Proposal("name", "name=*", true), //
|
||||||
new Proposal("bird", "bird=*", 4), //
|
new Proposal("bird", "bird=*", true), //
|
||||||
new Proposal("dog", "dog=*", 2)//
|
new Proposal("dog", "dog=*", true)//
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testPrefixOfKey() throws Exception {
|
public void testPrefixOfKey() throws Exception {
|
||||||
assertProposals("bi", 2, //
|
assertProposals("bi", 2, //
|
||||||
new Proposal("bird", "bird=* ", 4) //
|
new Proposal("bird", "bird=* ", true) //
|
||||||
);
|
);
|
||||||
assertProposals("bird", 4, //
|
assertProposals("bird", 4, //
|
||||||
new Proposal("bird", "bird=* ", 4) //
|
new Proposal("bird", "bird=* ", true) //
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testPrefixOfValue() throws Exception {
|
public void testPrefixOfValue() throws Exception {
|
||||||
assertProposals("name =Tim", 9, //
|
assertProposals("name =Tim", 9, //
|
||||||
new Proposal("Timothy", "name =Timothy ", 1)
|
new Proposal("Timothy", "name =Timothy ", true)
|
||||||
);
|
);
|
||||||
|
|
||||||
/*
|
|
||||||
assertProposals("name =Je", 8, //
|
assertProposals("name =Je", 8, //
|
||||||
new Proposal("Jennifer", "name =Jennifer ", 2), //
|
new Proposal("Jennifer", "name =Jennifer ", true), //
|
||||||
new Proposal("Jenny", "name =Jenny ", 1) //
|
new Proposal("Jenny", "name =Jenny ", true) //
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
||||||
assertProposals("bird=eagle and n", 16, //
|
assertProposals("bird=eagle and n", 16, //
|
||||||
new Proposal("name", "bird=eagle and name=* ", 1) //
|
new Proposal("name", "bird=eagle and name=* ", true) //
|
||||||
);
|
);
|
||||||
|
/*
|
||||||
*/
|
*/
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -101,7 +101,7 @@ public class PdbController implements HardcodedValues {
|
|||||||
final int zeroBasedCaretIndex = caretIndex - 1;
|
final int zeroBasedCaretIndex = caretIndex - 1;
|
||||||
|
|
||||||
final List<Proposal> proposals = db.autocomplete(query, zeroBasedCaretIndex);
|
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);
|
final List<AutocompleteProposal> autocompleteProposals = toAutocompleteProposals(nonEmptyProposals);
|
||||||
Collections.sort(autocompleteProposals, new AutocompleteProposalByValue());
|
Collections.sort(autocompleteProposals, new AutocompleteProposalByValue());
|
||||||
|
|||||||
@@ -21,5 +21,6 @@
|
|||||||
<logger name="org.lucares.metrics.proposals" level="DEBUG" />
|
<logger name="org.lucares.metrics.proposals" level="DEBUG" />
|
||||||
<logger name="org.lucares.metrics.plotter" level="DEBUG" />
|
<logger name="org.lucares.metrics.plotter" level="DEBUG" />
|
||||||
<logger name="org.lucares.metrics.gnuplot" level="DEBUG" />
|
<logger name="org.lucares.metrics.gnuplot" level="DEBUG" />
|
||||||
|
<logger name="org.lucares.metrics.dataStore" level="DEBUG" />
|
||||||
</Loggers>
|
</Loggers>
|
||||||
</Configuration>
|
</Configuration>
|
||||||
Reference in New Issue
Block a user