propose for an empty query

This commit is contained in:
2017-04-16 10:39:17 +02:00
parent 44f30aafee
commit f6a9fc2394
7 changed files with 134 additions and 36 deletions

View File

@@ -6,13 +6,17 @@ import java.util.List;
import java.util.SortedSet;
import org.lucares.pdb.api.Tags;
import org.lucares.pdb.datastore.internal.DataStore;
import org.lucares.pdb.datastore.internal.Proposer;
public class PdbDB {
private final DataStore dataStore;
private final Proposer proposer;
public PdbDB(final Path dataDirectory) throws IOException {
dataStore = new DataStore(dataDirectory);
proposer = new Proposer(dataStore);
}
public List<Doc> search(final String query) {
@@ -31,4 +35,8 @@ public class PdbDB {
return dataStore.getAvailableValuesForKey(query, fieldName);
}
public List<Proposal> propose(final String query, final int caretIndex) {
return proposer.propose(query, caretIndex);
}
}

View File

@@ -1,4 +1,4 @@
package org.lucares.pdb.datastore;
package org.lucares.pdb.datastore.internal;
import java.io.IOException;
import java.nio.file.Path;
@@ -16,9 +16,7 @@ import java.util.stream.Stream;
import org.lucares.collections.IntList;
import org.lucares.pdb.api.Tags;
import org.lucares.pdb.datastore.internal.FolderStorage;
import org.lucares.pdb.datastore.internal.RadixConverter;
import org.lucares.pdb.datastore.internal.StringCompressor;
import org.lucares.pdb.datastore.Doc;
import org.lucares.pdb.datastore.lang.Expression;
import org.lucares.pdb.datastore.lang.ExpressionToDocIdVisitor;
import org.lucares.pdb.datastore.lang.ExpressionToDocIdVisitor.AllDocIds;
@@ -153,24 +151,17 @@ public class DataStore {
public List<Doc> search(final String query) {
final Expression expression = QueryLanguageParser.parse(query);
final ExpressionToDocIdVisitor visitor = new ExpressionToDocIdVisitor(keyToValueToDocId,
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);
}
final IntList docIdsList = executeQuery(query);
final List<Doc> result = mapDocIdsToDocs(docIdsList);
return result;
}
public int count(final String query) {
final IntList docIdsList = executeQuery(query);
return docIdsList.size();
}
public List<String> getAvailableFields() {
final List<String> result = new ArrayList<>();
@@ -195,4 +186,25 @@ public class DataStore {
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;
}
}

View File

@@ -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;
}
}

View File

@@ -1,4 +1,4 @@
package org.lucares.pdb.datastore;
package org.lucares.pdb.datastore.internal;
import java.io.IOException;
import java.nio.file.Files;
@@ -11,6 +11,8 @@ import java.util.Map;
import java.util.Map.Entry;
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.file.FileUtils;
import org.testng.Assert;

View File

@@ -1,22 +1,28 @@
package org.lucares.pdb.datastore;
package org.lucares.pdb.datastore.internal;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
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.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
@Test
public class ProposalsTest {
public class ProposerTest {
private Path dataDirectory;
private DataStore dataStore;
private PdbDB db;
private Map<Tags, Path> tagsToPath;
@BeforeMethod
@@ -27,11 +33,10 @@ public class ProposalsTest {
@AfterMethod
public void afterMethod() throws IOException {
FileUtils.delete(dataDirectory);
dataStore = null;
db = null;
tagsToPath = null;
}
@Test(enabled = false)
public void testProposals() throws Exception {
tagsToPath = new LinkedHashMap<>();
final Tags eagleTim = Tags.create("bird", "eagle", "name", "Tim");
@@ -46,10 +51,10 @@ public class ProposalsTest {
tagsToPath.put(labradorJenny, null);
tagsToPath.put(labradorTim, null);
dataStore = new DataStore(dataDirectory);
db = new PdbDB(dataDirectory);
for (final Tags tags : tagsToPath.keySet()) {
final Path newFile = dataStore.createNewFile(tags);
final Path newFile = db.createNewFile(tags);
tagsToPath.put(tags, newFile);
}
@@ -63,12 +68,12 @@ public class ProposalsTest {
private void assertProposals(final String query, final int caretIndex, final Proposal... expected)
throws InterruptedException {
// final List<Proposal> actual = dataStore.propose(query, caretIndex);
// final List<Proposal> expectedList = Arrays.asList(expected);
// Collections.sort(expectedList);
//
// System.out.println("actual: " + actual);
// System.out.println("expected: " + expectedList);
// Assert.assertEquals(expectedList, actual);
final List<Proposal> actual = db.propose(query, caretIndex);
final List<Proposal> expectedList = Arrays.asList(expected);
Collections.sort(expectedList);
System.out.println("actual: " + actual);
System.out.println("expected: " + expectedList);
Assert.assertEquals(expectedList, actual);
}
}

View File

@@ -33,7 +33,7 @@ public class CollectionUtils {
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<>();
for (final V value : iterable) {
@@ -45,6 +45,19 @@ public class CollectionUtils {
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) {
return collection.stream().filter(predicate).collect(Collectors.toList());
}

View File

@@ -14,7 +14,7 @@ import org.lucares.pdb.api.Entry;
import org.lucares.pdb.api.GroupResult;
import org.lucares.pdb.api.Result;
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.testng.Assert;
import org.testng.annotations.AfterMethod;