propose for an empty query
This commit is contained in:
@@ -6,13 +6,17 @@ import java.util.List;
|
|||||||
import java.util.SortedSet;
|
import java.util.SortedSet;
|
||||||
|
|
||||||
import org.lucares.pdb.api.Tags;
|
import org.lucares.pdb.api.Tags;
|
||||||
|
import org.lucares.pdb.datastore.internal.DataStore;
|
||||||
|
import org.lucares.pdb.datastore.internal.Proposer;
|
||||||
|
|
||||||
public class PdbDB {
|
public class PdbDB {
|
||||||
|
|
||||||
private final DataStore dataStore;
|
private final DataStore dataStore;
|
||||||
|
private final Proposer proposer;
|
||||||
|
|
||||||
public PdbDB(final Path dataDirectory) throws IOException {
|
public PdbDB(final Path dataDirectory) throws IOException {
|
||||||
dataStore = new DataStore(dataDirectory);
|
dataStore = new DataStore(dataDirectory);
|
||||||
|
proposer = new Proposer(dataStore);
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<Doc> search(final String query) {
|
public List<Doc> search(final String query) {
|
||||||
@@ -31,4 +35,8 @@ public class PdbDB {
|
|||||||
return dataStore.getAvailableValuesForKey(query, fieldName);
|
return dataStore.getAvailableValuesForKey(query, fieldName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public List<Proposal> propose(final String query, final int caretIndex) {
|
||||||
|
return proposer.propose(query, caretIndex);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
package org.lucares.pdb.datastore;
|
package org.lucares.pdb.datastore.internal;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
@@ -16,9 +16,7 @@ import java.util.stream.Stream;
|
|||||||
|
|
||||||
import org.lucares.collections.IntList;
|
import org.lucares.collections.IntList;
|
||||||
import org.lucares.pdb.api.Tags;
|
import org.lucares.pdb.api.Tags;
|
||||||
import org.lucares.pdb.datastore.internal.FolderStorage;
|
import org.lucares.pdb.datastore.Doc;
|
||||||
import org.lucares.pdb.datastore.internal.RadixConverter;
|
|
||||||
import org.lucares.pdb.datastore.internal.StringCompressor;
|
|
||||||
import org.lucares.pdb.datastore.lang.Expression;
|
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;
|
||||||
@@ -153,24 +151,17 @@ public class DataStore {
|
|||||||
|
|
||||||
public List<Doc> search(final String query) {
|
public List<Doc> search(final String query) {
|
||||||
|
|
||||||
final Expression expression = QueryLanguageParser.parse(query);
|
final IntList docIdsList = executeQuery(query);
|
||||||
final ExpressionToDocIdVisitor visitor = new ExpressionToDocIdVisitor(keyToValueToDocId,
|
final List<Doc> result = mapDocIdsToDocs(docIdsList);
|
||||||
new AllDocIds(docIdToDoc));
|
|
||||||
final IntList docIdsList = expression.visit(visitor);
|
|
||||||
|
|
||||||
final List<Doc> result = new ArrayList<>(docIdsList.size());
|
|
||||||
|
|
||||||
final int[] intDocIds = docIdsList.toArray();
|
|
||||||
for (int i = 0; i < intDocIds.length; i++) {
|
|
||||||
final int docId = intDocIds[i];
|
|
||||||
|
|
||||||
final Doc doc = docIdToDoc.get(docId);
|
|
||||||
result.add(doc);
|
|
||||||
}
|
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public int count(final String query) {
|
||||||
|
final IntList docIdsList = executeQuery(query);
|
||||||
|
|
||||||
|
return docIdsList.size();
|
||||||
|
}
|
||||||
|
|
||||||
public List<String> getAvailableFields() {
|
public List<String> getAvailableFields() {
|
||||||
|
|
||||||
final List<String> result = new ArrayList<>();
|
final List<String> result = new ArrayList<>();
|
||||||
@@ -195,4 +186,25 @@ public class DataStore {
|
|||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private IntList executeQuery(final String query) {
|
||||||
|
final Expression expression = QueryLanguageParser.parse(query);
|
||||||
|
final ExpressionToDocIdVisitor visitor = new ExpressionToDocIdVisitor(keyToValueToDocId,
|
||||||
|
new AllDocIds(docIdToDoc));
|
||||||
|
final IntList docIdsList = expression.visit(visitor);
|
||||||
|
return docIdsList;
|
||||||
|
}
|
||||||
|
|
||||||
|
private List<Doc> mapDocIdsToDocs(final IntList docIdsList) {
|
||||||
|
final List<Doc> result = new ArrayList<>(docIdsList.size());
|
||||||
|
|
||||||
|
final int[] intDocIds = docIdsList.toArray();
|
||||||
|
for (int i = 0; i < intDocIds.length; i++) {
|
||||||
|
final int docId = intDocIds[i];
|
||||||
|
|
||||||
|
final Doc doc = docIdToDoc.get(docId);
|
||||||
|
result.add(doc);
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,58 @@
|
|||||||
|
package org.lucares.pdb.datastore.internal;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Map.Entry;
|
||||||
|
|
||||||
|
import org.lucares.pdb.datastore.Proposal;
|
||||||
|
import org.lucares.utils.CollectionUtils;
|
||||||
|
|
||||||
|
public class Proposer {
|
||||||
|
|
||||||
|
private final DataStore dataStore;
|
||||||
|
|
||||||
|
public Proposer(final DataStore dataStore) {
|
||||||
|
this.dataStore = dataStore;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<Proposal> propose(final String query, final int caretIndex) {
|
||||||
|
final List<Proposal> result;
|
||||||
|
|
||||||
|
if (query.isEmpty()) {
|
||||||
|
result = proposeForAllKeys();
|
||||||
|
} else {
|
||||||
|
throw new UnsupportedOperationException();
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
private List<Proposal> proposeForAllKeys() {
|
||||||
|
final List<Proposal> result;
|
||||||
|
final List<String> fields = dataStore.getAvailableFields();
|
||||||
|
|
||||||
|
final Map<String, String> fieldToQuery = CollectionUtils.createMapFromKeys(fields, f -> f + "=*");
|
||||||
|
|
||||||
|
result = computeProposalsForQueries(fieldToQuery);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
private List<Proposal> computeProposalsForQueries(final Map<String, String> keyToQuery) {
|
||||||
|
|
||||||
|
final List<Proposal> result = new ArrayList<>(keyToQuery.size());
|
||||||
|
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);
|
||||||
|
result.add(proposal);
|
||||||
|
}
|
||||||
|
|
||||||
|
Collections.sort(result);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package org.lucares.pdb.datastore;
|
package org.lucares.pdb.datastore.internal;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
@@ -11,6 +11,8 @@ import java.util.Map;
|
|||||||
import java.util.Map.Entry;
|
import java.util.Map.Entry;
|
||||||
|
|
||||||
import org.lucares.pdb.api.Tags;
|
import org.lucares.pdb.api.Tags;
|
||||||
|
import org.lucares.pdb.datastore.Doc;
|
||||||
|
import org.lucares.pdb.datastore.internal.DataStore;
|
||||||
import org.lucares.utils.CollectionUtils;
|
import org.lucares.utils.CollectionUtils;
|
||||||
import org.lucares.utils.file.FileUtils;
|
import org.lucares.utils.file.FileUtils;
|
||||||
import org.testng.Assert;
|
import org.testng.Assert;
|
||||||
@@ -1,22 +1,28 @@
|
|||||||
package org.lucares.pdb.datastore;
|
package org.lucares.pdb.datastore.internal;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.Collections;
|
||||||
import java.util.LinkedHashMap;
|
import java.util.LinkedHashMap;
|
||||||
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
import org.lucares.pdb.api.Tags;
|
import org.lucares.pdb.api.Tags;
|
||||||
|
import org.lucares.pdb.datastore.PdbDB;
|
||||||
|
import org.lucares.pdb.datastore.Proposal;
|
||||||
import org.lucares.utils.file.FileUtils;
|
import org.lucares.utils.file.FileUtils;
|
||||||
|
import org.testng.Assert;
|
||||||
import org.testng.annotations.AfterMethod;
|
import org.testng.annotations.AfterMethod;
|
||||||
import org.testng.annotations.BeforeMethod;
|
import org.testng.annotations.BeforeMethod;
|
||||||
import org.testng.annotations.Test;
|
import org.testng.annotations.Test;
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public class ProposalsTest {
|
public class ProposerTest {
|
||||||
|
|
||||||
private Path dataDirectory;
|
private Path dataDirectory;
|
||||||
private DataStore dataStore;
|
private PdbDB db;
|
||||||
private Map<Tags, Path> tagsToPath;
|
private Map<Tags, Path> tagsToPath;
|
||||||
|
|
||||||
@BeforeMethod
|
@BeforeMethod
|
||||||
@@ -27,11 +33,10 @@ public class ProposalsTest {
|
|||||||
@AfterMethod
|
@AfterMethod
|
||||||
public void afterMethod() throws IOException {
|
public void afterMethod() throws IOException {
|
||||||
FileUtils.delete(dataDirectory);
|
FileUtils.delete(dataDirectory);
|
||||||
dataStore = null;
|
db = null;
|
||||||
tagsToPath = null;
|
tagsToPath = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test(enabled = false)
|
|
||||||
public void testProposals() throws Exception {
|
public void testProposals() throws Exception {
|
||||||
tagsToPath = new LinkedHashMap<>();
|
tagsToPath = new LinkedHashMap<>();
|
||||||
final Tags eagleTim = Tags.create("bird", "eagle", "name", "Tim");
|
final Tags eagleTim = Tags.create("bird", "eagle", "name", "Tim");
|
||||||
@@ -46,10 +51,10 @@ public class ProposalsTest {
|
|||||||
tagsToPath.put(labradorJenny, null);
|
tagsToPath.put(labradorJenny, null);
|
||||||
tagsToPath.put(labradorTim, null);
|
tagsToPath.put(labradorTim, null);
|
||||||
|
|
||||||
dataStore = new DataStore(dataDirectory);
|
db = new PdbDB(dataDirectory);
|
||||||
|
|
||||||
for (final Tags tags : tagsToPath.keySet()) {
|
for (final Tags tags : tagsToPath.keySet()) {
|
||||||
final Path newFile = dataStore.createNewFile(tags);
|
final Path newFile = db.createNewFile(tags);
|
||||||
tagsToPath.put(tags, newFile);
|
tagsToPath.put(tags, newFile);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -63,12 +68,12 @@ public class ProposalsTest {
|
|||||||
private void assertProposals(final String query, final int caretIndex, final Proposal... expected)
|
private void assertProposals(final String query, final int caretIndex, final Proposal... expected)
|
||||||
throws InterruptedException {
|
throws InterruptedException {
|
||||||
|
|
||||||
// final List<Proposal> actual = dataStore.propose(query, caretIndex);
|
final List<Proposal> actual = db.propose(query, caretIndex);
|
||||||
// final List<Proposal> expectedList = Arrays.asList(expected);
|
final List<Proposal> expectedList = Arrays.asList(expected);
|
||||||
// Collections.sort(expectedList);
|
Collections.sort(expectedList);
|
||||||
//
|
|
||||||
// System.out.println("actual: " + actual);
|
System.out.println("actual: " + actual);
|
||||||
// System.out.println("expected: " + expectedList);
|
System.out.println("expected: " + expectedList);
|
||||||
// Assert.assertEquals(expectedList, actual);
|
Assert.assertEquals(expectedList, actual);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -33,7 +33,7 @@ public class CollectionUtils {
|
|||||||
return Stream.of(input).map(mapper).collect(Collectors.toList());
|
return Stream.of(input).map(mapper).collect(Collectors.toList());
|
||||||
}
|
}
|
||||||
|
|
||||||
public static <T, V> Map<T, V> toMap(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<>();
|
||||||
|
|
||||||
for (final V value : iterable) {
|
for (final V value : iterable) {
|
||||||
@@ -45,6 +45,19 @@ public class CollectionUtils {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static <KEY, VALUE> Map<KEY, VALUE> createMapFromKeys(final Iterable<KEY> iterable,
|
||||||
|
final Function<KEY, VALUE> valueMapper) {
|
||||||
|
final Map<KEY, VALUE> result = new HashMap<>();
|
||||||
|
|
||||||
|
for (final KEY key : iterable) {
|
||||||
|
final VALUE value = valueMapper.apply(key);
|
||||||
|
|
||||||
|
result.put(key, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
public static <T> List<T> filter(final Collection<T> collection, final Predicate<T> predicate) {
|
public static <T> List<T> filter(final Collection<T> collection, final Predicate<T> predicate) {
|
||||||
return collection.stream().filter(predicate).collect(Collectors.toList());
|
return collection.stream().filter(predicate).collect(Collectors.toList());
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,7 +14,7 @@ import org.lucares.pdb.api.Entry;
|
|||||||
import org.lucares.pdb.api.GroupResult;
|
import org.lucares.pdb.api.GroupResult;
|
||||||
import org.lucares.pdb.api.Result;
|
import org.lucares.pdb.api.Result;
|
||||||
import org.lucares.pdb.api.Tags;
|
import org.lucares.pdb.api.Tags;
|
||||||
import org.lucares.pdb.datastore.DataStore;
|
import org.lucares.pdb.datastore.internal.DataStore;
|
||||||
import org.lucares.utils.file.FileUtils;
|
import org.lucares.utils.file.FileUtils;
|
||||||
import org.testng.Assert;
|
import org.testng.Assert;
|
||||||
import org.testng.annotations.AfterMethod;
|
import org.testng.annotations.AfterMethod;
|
||||||
|
|||||||
Reference in New Issue
Block a user