simple auto-completion for the search box

This commit is contained in:
2016-12-23 10:32:51 +01:00
parent bc5d1b0b7b
commit 95e34831d3
21 changed files with 725 additions and 68 deletions

View File

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

View File

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

View File

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

View File

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

View File

@@ -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();

View File

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

View File

@@ -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");