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:
@@ -10,8 +10,6 @@ import java.util.List;
|
|||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.SortedSet;
|
|
||||||
import java.util.TreeSet;
|
|
||||||
import java.util.concurrent.atomic.AtomicLong;
|
import java.util.concurrent.atomic.AtomicLong;
|
||||||
import java.util.function.Consumer;
|
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) {
|
private PartitionLongList executeQuery(final Query query) {
|
||||||
final long start = System.nanoTime();
|
final long start = System.nanoTime();
|
||||||
synchronized (docIdToDoc) {
|
synchronized (docIdToDoc) {
|
||||||
|
|||||||
@@ -11,12 +11,12 @@ import java.util.List;
|
|||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.SortedSet;
|
import java.util.SortedSet;
|
||||||
|
import java.util.TreeSet;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
import java.util.concurrent.locks.ReentrantLock;
|
import java.util.concurrent.locks.ReentrantLock;
|
||||||
|
|
||||||
import org.apache.commons.lang3.StringUtils;
|
import org.apache.commons.lang3.StringUtils;
|
||||||
import org.lucares.pdb.api.DateTimeRange;
|
import org.lucares.pdb.api.DateTimeRange;
|
||||||
import org.lucares.pdb.api.Query;
|
|
||||||
import org.lucares.pdb.api.QueryWithCaretMarker;
|
import org.lucares.pdb.api.QueryWithCaretMarker;
|
||||||
import org.lucares.pdb.datastore.Proposal;
|
import org.lucares.pdb.datastore.Proposal;
|
||||||
import org.lucares.pdb.plot.api.AxisScale;
|
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,
|
SortedSet<String> fields(@PathVariable(name = "fieldName") final String fieldName,
|
||||||
@RequestParam(name = "query") final String query) {
|
@RequestParam(name = "query") final String query) {
|
||||||
|
|
||||||
final DateTimeRange dateRange = DateTimeRange.max();
|
final String q = String.format("(%s) and %s=", query, fieldName);
|
||||||
final Query q = new Query(query, dateRange);
|
final int caretIndex = q.length() + 1; // the autocomplete methods needs a 1-based index
|
||||||
final SortedSet<String> fields = db.getFieldsValues(q, fieldName);
|
|
||||||
|
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;
|
return fields;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -34,6 +34,17 @@ public class CollectionUtils {
|
|||||||
return Stream.of(input).map(mapper).collect(Collectors.toList());
|
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) {
|
public static <T, V> Map<T, V> createMapFromValues(final Iterable<V> iterable, final Function<V, T> keyMapper) {
|
||||||
final Map<T, V> result = new HashMap<>();
|
final Map<T, V> result = new HashMap<>();
|
||||||
|
|
||||||
|
|||||||
@@ -8,7 +8,6 @@ import java.util.Arrays;
|
|||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
import java.util.SortedSet;
|
|
||||||
import java.util.stream.Stream;
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
import org.lucares.collections.LongList;
|
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.PdbFile;
|
||||||
import org.lucares.pdb.datastore.Proposal;
|
import org.lucares.pdb.datastore.Proposal;
|
||||||
import org.lucares.pdb.datastore.WriteException;
|
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.DataStore;
|
||||||
|
import org.lucares.pdb.datastore.internal.PartitionDiskStore;
|
||||||
import org.lucares.pdb.datastore.lang.SyntaxException;
|
import org.lucares.pdb.datastore.lang.SyntaxException;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
@@ -181,10 +180,6 @@ public class PerformanceDb implements AutoCloseable {
|
|||||||
return fields;
|
return fields;
|
||||||
}
|
}
|
||||||
|
|
||||||
public SortedSet<String> getFieldsValues(final Query query, final String fieldName) {
|
|
||||||
return dataStore.getAvailableValuesForKey(query, fieldName);
|
|
||||||
}
|
|
||||||
|
|
||||||
public PartitionDiskStore getDataStore() {
|
public PartitionDiskStore getDataStore() {
|
||||||
return dataStore.getDiskStorage();
|
return dataStore.getDiskStorage();
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user