simple auto-completion for the search box
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
|
||||
dependencies {
|
||||
compile project(':pdb-api')
|
||||
compile 'org.lucares:ludb:1.0.20161217184921'
|
||||
compile 'org.lucares:ludb:1.0.20161223090600'
|
||||
compile 'com.fasterxml.jackson.core:jackson-databind:2.8.5'
|
||||
compile 'org.mapdb:mapdb:3.0.2'
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package org.lucares.performance.db;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@@ -21,7 +22,7 @@ public interface CollectionUtils {
|
||||
return result;
|
||||
}
|
||||
|
||||
public default <T> List<T> filter(final List<T> list, final Predicate<T> predicate) {
|
||||
public default <T> List<T> filter(final Collection<T> list, final Predicate<T> predicate) {
|
||||
return list.stream().filter(predicate).collect(Collectors.toList());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package org.lucares.performance.db;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Path;
|
||||
import java.time.OffsetDateTime;
|
||||
@@ -15,6 +16,10 @@ import java.util.logging.Logger;
|
||||
import java.util.stream.Stream;
|
||||
import java.util.stream.StreamSupport;
|
||||
|
||||
import org.lucares.ludb.Field;
|
||||
import org.lucares.ludb.FieldType;
|
||||
import org.lucares.ludb.H2DB;
|
||||
import org.lucares.ludb.Proposal;
|
||||
import org.lucares.pdb.api.Entry;
|
||||
import org.lucares.pdb.api.GroupResult;
|
||||
import org.lucares.pdb.api.Result;
|
||||
@@ -26,8 +31,18 @@ public class PerformanceDb implements AutoCloseable {
|
||||
|
||||
private final TagsToFile tagsToFile;
|
||||
|
||||
private final H2DB db;
|
||||
|
||||
public PerformanceDb(final Path dataDirectory) {
|
||||
tagsToFile = new TagsToFile(dataDirectory);
|
||||
|
||||
db = new H2DB(new File(dataDirectory.toFile(), "lu.db"));
|
||||
|
||||
final Field dateOffsetField = db.getField(Fields.DATE_OFFSET);
|
||||
if (dateOffsetField == null) {
|
||||
db.createField(Fields.DATE_OFFSET, FieldType.STRING);
|
||||
}
|
||||
|
||||
tagsToFile = new TagsToFile(dataDirectory, db);
|
||||
}
|
||||
|
||||
public void put(final Entry entry) throws WriteException {
|
||||
@@ -166,6 +181,15 @@ public class PerformanceDb implements AutoCloseable {
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
tagsToFile.close();
|
||||
try {
|
||||
db.close();
|
||||
} catch (final Exception e) {
|
||||
// H2 doesn't actually do anything in close
|
||||
throw new IllegalStateException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public List<Proposal> autocomplete(final String query, final int caretIndex) {
|
||||
return db.proposeTagForQuery(query, caretIndex);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,26 +14,18 @@ import java.util.UUID;
|
||||
|
||||
import org.lucares.ludb.Document;
|
||||
import org.lucares.ludb.Field;
|
||||
import org.lucares.ludb.FieldExistsException;
|
||||
import org.lucares.ludb.FieldType;
|
||||
import org.lucares.ludb.H2DB;
|
||||
import org.lucares.pdb.api.Tags;
|
||||
|
||||
public class TagsToFile implements AutoCloseable, CollectionUtils {
|
||||
public class TagsToFile implements CollectionUtils {
|
||||
|
||||
private final H2DB db;
|
||||
private final Path dataDirectory;
|
||||
|
||||
public TagsToFile(final Path dataDirectory) {
|
||||
super();
|
||||
public TagsToFile(final Path dataDirectory, final H2DB db) {
|
||||
this.dataDirectory = dataDirectory;
|
||||
db = new H2DB(new File(dataDirectory.toFile(), "lu.db"));
|
||||
try {
|
||||
db.createField(Fields.DATE_OFFSET, FieldType.STRING);
|
||||
} catch (final FieldExistsException e) {
|
||||
// TODO @ahr ludb needs a hasField method, or a
|
||||
// createIfNotExists
|
||||
}
|
||||
this.db = db;
|
||||
}
|
||||
|
||||
private List<PdbFile> getFilesMatchingTagsExactly(final Tags tags) {
|
||||
@@ -59,8 +51,7 @@ public class TagsToFile implements AutoCloseable, CollectionUtils {
|
||||
result.add(pdbFile);
|
||||
}
|
||||
} catch (final NullPointerException e) {
|
||||
// TODO @ahr ludb should handle unknown fields better
|
||||
e.printStackTrace();
|
||||
// TODO @ahr unknown fields in searches must be handled better
|
||||
}
|
||||
|
||||
Collections.sort(result, PdbFileByTimeAsc.INSTANCE);
|
||||
@@ -129,18 +120,18 @@ public class TagsToFile implements AutoCloseable, CollectionUtils {
|
||||
|
||||
ensureFieldsExist(tags);
|
||||
|
||||
tags.forEach((key, value) -> {
|
||||
db.setProperty(file, key, value);
|
||||
tags.forEach((fieldName, value) -> {
|
||||
TagsUtils.setProperty(db, file, fieldName, value);
|
||||
});
|
||||
|
||||
db.setProperty(file, Fields.DATE_OFFSET, day.serialize());
|
||||
TagsUtils.setProperty(db, file, Fields.DATE_OFFSET, day.serialize());
|
||||
|
||||
result = new PdbFile(day, file, tags);
|
||||
return result;
|
||||
}
|
||||
|
||||
private void ensureFieldsExist(final Tags tags) {
|
||||
final List<Field> fields = db.getAvailableFields();
|
||||
final List<Field> fields = db.getAvailableFields("");
|
||||
final Map<String, Field> fieldsMap = toMap(fields, Field::getName);
|
||||
|
||||
tags.forEach((key, value) -> {
|
||||
@@ -166,15 +157,4 @@ public class TagsToFile implements AutoCloseable, CollectionUtils {
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
try {
|
||||
db.close();
|
||||
} catch (final Exception e) {
|
||||
// H2 doesn't actually do anything in close
|
||||
throw new IllegalStateException(e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,10 @@
|
||||
package org.lucares.performance.db;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
import org.lucares.ludb.FieldNotExistsException;
|
||||
import org.lucares.ludb.FieldType;
|
||||
import org.lucares.ludb.H2DB;
|
||||
import org.lucares.pdb.api.Tags;
|
||||
|
||||
class TagsUtils {
|
||||
@@ -10,4 +15,17 @@ class TagsUtils {
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
static void setProperty(final H2DB db, final File file, final String fieldName, final String value) {
|
||||
try {
|
||||
db.setProperty(file, fieldName, value);
|
||||
} catch (final FieldNotExistsException e) {
|
||||
db.createField(fieldName, FieldType.STRING);
|
||||
try {
|
||||
db.setProperty(file, fieldName, value);
|
||||
} catch (final FieldNotExistsException e1) {
|
||||
throw new IllegalStateException(e1);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -32,7 +32,6 @@ public class TimeRange {
|
||||
}
|
||||
|
||||
public boolean inRange(final long epochMilli) {
|
||||
// TODO @ahr cache the epoch millis
|
||||
final long fromEpochMilli = from.toInstant().toEpochMilli();
|
||||
final long toEpochMilli = to.toInstant().toEpochMilli();
|
||||
|
||||
|
||||
@@ -19,7 +19,6 @@ import java.util.concurrent.TimeUnit;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
import org.lucares.ludb.FieldType;
|
||||
import org.lucares.ludb.H2DB;
|
||||
import org.mapdb.DB;
|
||||
import org.mapdb.DBMaker;
|
||||
@@ -127,12 +126,7 @@ public class ObjectMapperTest {
|
||||
db.addDocument(file);
|
||||
|
||||
tagEntry.getTags().forEach((k, v) -> {
|
||||
try {
|
||||
db.setProperty(file, k, v);
|
||||
} catch (final NullPointerException e) {
|
||||
db.createField(k, FieldType.STRING);
|
||||
db.setProperty(file, k, v);
|
||||
}
|
||||
TagsUtils.setProperty(db, file, k, v);
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
@@ -1,11 +1,14 @@
|
||||
package org.lucares.performance.db;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.time.OffsetDateTime;
|
||||
import java.time.ZoneOffset;
|
||||
|
||||
import org.lucares.ludb.FieldType;
|
||||
import org.lucares.ludb.H2DB;
|
||||
import org.lucares.pdb.api.Tags;
|
||||
import org.testng.Assert;
|
||||
import org.testng.annotations.AfterMethod;
|
||||
@@ -28,7 +31,12 @@ public class TagsToFilesTest {
|
||||
}
|
||||
|
||||
public void test() throws Exception {
|
||||
try (final TagsToFile tagsToFile = new TagsToFile(dataDirectory)) {
|
||||
|
||||
try (H2DB db = new H2DB(new File(dataDirectory.toFile(), "lu.db"))) {
|
||||
|
||||
db.createField(Fields.DATE_OFFSET, FieldType.STRING);
|
||||
|
||||
final TagsToFile tagsToFile = new TagsToFile(dataDirectory, db);
|
||||
|
||||
final OffsetDateTime date = OffsetDateTime.now(ZoneOffset.UTC);
|
||||
final Tags tags = Tags.create("myKey", "myValue");
|
||||
|
||||
Reference in New Issue
Block a user