fetch available values for gallery via autocomplete method

We had a method that returned the values of a field
with respect to a query. That method was inefficient,
because it executed the query, fetched all Docs
and collected the values.
The autocomplete method we introduced a while back
can answer the same question but much more efficiently.
This commit is contained in:
2019-08-25 18:52:05 +02:00
parent 4f61d91c79
commit 2f35978184
4 changed files with 21 additions and 34 deletions

View File

@@ -10,8 +10,6 @@ import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;
import java.util.concurrent.atomic.AtomicLong;
import java.util.function.Consumer;
@@ -229,28 +227,6 @@ public class DataStore implements AutoCloseable {
}
public SortedSet<String> getAvailableValuesForKey(final Query query, final String key) {
final SortedSet<String> result = new TreeSet<>();
if (query.getQuery().isEmpty()) {
final PartitionIdSource partitionIdSource = new DatePartitioner(query.getDateRange());
tagToDocsId.visitValues(partitionIdSource, new Tag(key, ""),
(tag, __) -> result.add(tag.getValueAsString()));
} else {
final List<Doc> docs = search(query);
for (final Doc doc : docs) {
final String valueForKey = doc.getTags().getValue(key);
if (valueForKey != null) {
result.add(valueForKey);
}
}
}
return result;
}
private PartitionLongList executeQuery(final Query query) {
final long start = System.nanoTime();
synchronized (docIdToDoc) {

View File

@@ -11,12 +11,12 @@ import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.SortedSet;
import java.util.TreeSet;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;
import org.apache.commons.lang3.StringUtils;
import org.lucares.pdb.api.DateTimeRange;
import org.lucares.pdb.api.Query;
import org.lucares.pdb.api.QueryWithCaretMarker;
import org.lucares.pdb.datastore.Proposal;
import org.lucares.pdb.plot.api.AxisScale;
@@ -252,9 +252,14 @@ public class PdbController implements HardcodedValues, PropertyKeys {
SortedSet<String> fields(@PathVariable(name = "fieldName") final String fieldName,
@RequestParam(name = "query") final String query) {
final DateTimeRange dateRange = DateTimeRange.max();
final Query q = new Query(query, dateRange);
final SortedSet<String> fields = db.getFieldsValues(q, fieldName);
final String q = String.format("(%s) and %s=", query, fieldName);
final int caretIndex = q.length() + 1; // the autocomplete methods needs a 1-based index
final AutocompleteResponse autocompleteResponse = this.autocomplete(q, caretIndex);
final List<AutocompleteProposal> proposals = autocompleteResponse.getProposals();
final SortedSet<String> fields = CollectionUtils.map(proposals, new TreeSet<>(),
AutocompleteProposal::getValue);
return fields;
}

View File

@@ -34,6 +34,17 @@ public class CollectionUtils {
return Stream.of(input).map(mapper).collect(Collectors.toList());
}
public static <O extends Collection<R>, T, R> O map(final Collection<T> input, final O result,
final Function<T, R> mapper) {
for (final T t : input) {
final R e = mapper.apply(t);
result.add(e);
}
return result;
}
public static <T, V> Map<T, V> createMapFromValues(final Iterable<V> iterable, final Function<V, T> keyMapper) {
final Map<T, V> result = new HashMap<>();

View File

@@ -8,7 +8,6 @@ import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Optional;
import java.util.SortedSet;
import java.util.stream.Stream;
import org.lucares.collections.LongList;
@@ -24,8 +23,8 @@ import org.lucares.pdb.datastore.InvalidValueException;
import org.lucares.pdb.datastore.PdbFile;
import org.lucares.pdb.datastore.Proposal;
import org.lucares.pdb.datastore.WriteException;
import org.lucares.pdb.datastore.internal.PartitionDiskStore;
import org.lucares.pdb.datastore.internal.DataStore;
import org.lucares.pdb.datastore.internal.PartitionDiskStore;
import org.lucares.pdb.datastore.lang.SyntaxException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -181,10 +180,6 @@ public class PerformanceDb implements AutoCloseable {
return fields;
}
public SortedSet<String> getFieldsValues(final Query query, final String fieldName) {
return dataStore.getAvailableValuesForKey(query, fieldName);
}
public PartitionDiskStore getDataStore() {
return dataStore.getDiskStorage();
}