add a new facade in front of DataStore
This is done in preparation for the proposal API. In order to compute proposals we need to consume the API of the DataStore, but the code does not need to be in the DataStore. Extracting the API allows us to separate these concerns.
This commit is contained in:
@@ -195,5 +195,4 @@ public class DataStore {
|
|||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,34 @@
|
|||||||
|
package org.lucares.pdb.datastore;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.nio.file.Path;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.SortedSet;
|
||||||
|
|
||||||
|
import org.lucares.pdb.api.Tags;
|
||||||
|
|
||||||
|
public class PdbDB {
|
||||||
|
|
||||||
|
private final DataStore dataStore;
|
||||||
|
|
||||||
|
public PdbDB(final Path dataDirectory) throws IOException {
|
||||||
|
dataStore = new DataStore(dataDirectory);
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<Doc> search(final String query) {
|
||||||
|
return dataStore.search(query);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Path createNewFile(final Tags tags) throws IOException {
|
||||||
|
return dataStore.createNewFile(tags);
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<String> getAvailableFields() {
|
||||||
|
return dataStore.getAvailableFields();
|
||||||
|
}
|
||||||
|
|
||||||
|
public SortedSet<String> getAvailableValuesForKey(final String query, final String fieldName) {
|
||||||
|
return dataStore.getAvailableValuesForKey(query, fieldName);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package org.lucares.performance.db;
|
package org.lucares.pdb.datastore;
|
||||||
|
|
||||||
public class Proposal implements Comparable<Proposal> {
|
public class Proposal implements Comparable<Proposal> {
|
||||||
private final String proposedTag;
|
private final String proposedTag;
|
||||||
@@ -0,0 +1,74 @@
|
|||||||
|
package org.lucares.pdb.datastore;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.Path;
|
||||||
|
import java.util.LinkedHashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import org.lucares.pdb.api.Tags;
|
||||||
|
import org.lucares.utils.file.FileUtils;
|
||||||
|
import org.testng.annotations.AfterMethod;
|
||||||
|
import org.testng.annotations.BeforeMethod;
|
||||||
|
import org.testng.annotations.Test;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public class ProposalsTest {
|
||||||
|
|
||||||
|
private Path dataDirectory;
|
||||||
|
private DataStore dataStore;
|
||||||
|
private Map<Tags, Path> tagsToPath;
|
||||||
|
|
||||||
|
@BeforeMethod
|
||||||
|
public void beforeMethod() throws IOException {
|
||||||
|
dataDirectory = Files.createTempDirectory("pdb");
|
||||||
|
}
|
||||||
|
|
||||||
|
@AfterMethod
|
||||||
|
public void afterMethod() throws IOException {
|
||||||
|
FileUtils.delete(dataDirectory);
|
||||||
|
dataStore = null;
|
||||||
|
tagsToPath = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test(enabled = false)
|
||||||
|
public void testProposals() throws Exception {
|
||||||
|
tagsToPath = new LinkedHashMap<>();
|
||||||
|
final Tags eagleTim = Tags.create("bird", "eagle", "name", "Tim");
|
||||||
|
final Tags pigeonJennifer = Tags.create("bird", "pigeon", "name", "Jennifer");
|
||||||
|
final Tags flamingoJennifer = Tags.create("bird", "flamingo", "name", "Jennifer");
|
||||||
|
final Tags labradorJenny = Tags.create("dog", "labrador", "name", "Jenny");
|
||||||
|
final Tags labradorTim = Tags.create("dog", "labrador", "name", "Tim");
|
||||||
|
|
||||||
|
tagsToPath.put(eagleTim, null);
|
||||||
|
tagsToPath.put(pigeonJennifer, null);
|
||||||
|
tagsToPath.put(flamingoJennifer, null);
|
||||||
|
tagsToPath.put(labradorJenny, null);
|
||||||
|
tagsToPath.put(labradorTim, null);
|
||||||
|
|
||||||
|
dataStore = new DataStore(dataDirectory);
|
||||||
|
|
||||||
|
for (final Tags tags : tagsToPath.keySet()) {
|
||||||
|
final Path newFile = dataStore.createNewFile(tags);
|
||||||
|
tagsToPath.put(tags, newFile);
|
||||||
|
}
|
||||||
|
|
||||||
|
assertProposals("", 0, //
|
||||||
|
new Proposal("name", "name=*", 5), //
|
||||||
|
new Proposal("bird", "bird=*", 3), //
|
||||||
|
new Proposal("dog", "dog=*", 2)//
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -11,6 +11,7 @@ import java.util.Locale;
|
|||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.SortedSet;
|
import java.util.SortedSet;
|
||||||
|
|
||||||
|
import org.lucares.pdb.datastore.Proposal;
|
||||||
import org.lucares.pdb.plot.api.PlotSettings;
|
import org.lucares.pdb.plot.api.PlotSettings;
|
||||||
import org.lucares.pdbui.domain.AutocompleteProposal;
|
import org.lucares.pdbui.domain.AutocompleteProposal;
|
||||||
import org.lucares.pdbui.domain.AutocompleteProposalByValue;
|
import org.lucares.pdbui.domain.AutocompleteProposalByValue;
|
||||||
@@ -18,7 +19,6 @@ import org.lucares.pdbui.domain.AutocompleteResponse;
|
|||||||
import org.lucares.pdbui.domain.PlotRequest;
|
import org.lucares.pdbui.domain.PlotRequest;
|
||||||
import org.lucares.pdbui.domain.PlotResponse;
|
import org.lucares.pdbui.domain.PlotResponse;
|
||||||
import org.lucares.performance.db.PerformanceDb;
|
import org.lucares.performance.db.PerformanceDb;
|
||||||
import org.lucares.performance.db.Proposal;
|
|
||||||
import org.lucares.recommind.logs.DataSeries;
|
import org.lucares.recommind.logs.DataSeries;
|
||||||
import org.lucares.recommind.logs.InternalPlottingException;
|
import org.lucares.recommind.logs.InternalPlottingException;
|
||||||
import org.lucares.recommind.logs.NoDataPointsException;
|
import org.lucares.recommind.logs.NoDataPointsException;
|
||||||
|
|||||||
@@ -22,7 +22,8 @@ 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.PdbDB;
|
||||||
|
import org.lucares.pdb.datastore.Proposal;
|
||||||
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;
|
||||||
@@ -33,11 +34,11 @@ public class PerformanceDb implements AutoCloseable {
|
|||||||
|
|
||||||
private final TagsToFile tagsToFile;
|
private final TagsToFile tagsToFile;
|
||||||
|
|
||||||
private final DataStore db;
|
private final PdbDB db;
|
||||||
|
|
||||||
public PerformanceDb(final Path dataDirectory) throws IOException {
|
public PerformanceDb(final Path dataDirectory) throws IOException {
|
||||||
|
|
||||||
db = new DataStore(dataDirectory);
|
db = new PdbDB(dataDirectory);
|
||||||
|
|
||||||
tagsToFile = new TagsToFile(db);
|
tagsToFile = new TagsToFile(db);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,8 +17,8 @@ import java.util.function.Consumer;
|
|||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
import org.lucares.pdb.api.Tags;
|
import org.lucares.pdb.api.Tags;
|
||||||
import org.lucares.pdb.datastore.DataStore;
|
|
||||||
import org.lucares.pdb.datastore.Doc;
|
import org.lucares.pdb.datastore.Doc;
|
||||||
|
import org.lucares.pdb.datastore.PdbDB;
|
||||||
import org.lucares.utils.CollectionUtils;
|
import org.lucares.utils.CollectionUtils;
|
||||||
import org.lucares.utils.file.FileUtils;
|
import org.lucares.utils.file.FileUtils;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
@@ -64,11 +64,11 @@ public class TagsToFile implements AutoCloseable {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private final DataStore db;
|
private final PdbDB db;
|
||||||
|
|
||||||
private final Map<Tags, WriterCache> cachedWriters = new HashMap<>();
|
private final Map<Tags, WriterCache> cachedWriters = new HashMap<>();
|
||||||
|
|
||||||
public TagsToFile(final DataStore db) {
|
public TagsToFile(final PdbDB db) {
|
||||||
this.db = db;
|
this.db = db;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ import java.time.ZoneOffset;
|
|||||||
|
|
||||||
import org.lucares.pdb.api.Entry;
|
import org.lucares.pdb.api.Entry;
|
||||||
import org.lucares.pdb.api.Tags;
|
import org.lucares.pdb.api.Tags;
|
||||||
import org.lucares.pdb.datastore.DataStore;
|
import org.lucares.pdb.datastore.PdbDB;
|
||||||
import org.testng.Assert;
|
import org.testng.Assert;
|
||||||
import org.testng.annotations.AfterMethod;
|
import org.testng.annotations.AfterMethod;
|
||||||
import org.testng.annotations.BeforeMethod;
|
import org.testng.annotations.BeforeMethod;
|
||||||
@@ -31,7 +31,7 @@ public class TagsToFilesTest {
|
|||||||
|
|
||||||
public void test() throws Exception {
|
public void test() throws Exception {
|
||||||
|
|
||||||
final DataStore db = new DataStore(dataDirectory);
|
final PdbDB db = new PdbDB(dataDirectory);
|
||||||
try (final TagsToFile tagsToFile = new TagsToFile(db)) {
|
try (final TagsToFile tagsToFile = new TagsToFile(db)) {
|
||||||
|
|
||||||
final OffsetDateTime date = OffsetDateTime.now(ZoneOffset.UTC);
|
final OffsetDateTime date = OffsetDateTime.now(ZoneOffset.UTC);
|
||||||
@@ -46,7 +46,7 @@ public class TagsToFilesTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void testAppendingToSameFileIfNewDateIsAfter() throws Exception {
|
public void testAppendingToSameFileIfNewDateIsAfter() throws Exception {
|
||||||
final DataStore db = new DataStore(dataDirectory);
|
final PdbDB db = new PdbDB(dataDirectory);
|
||||||
try (final TagsToFile tagsToFile = new TagsToFile(db);) {
|
try (final TagsToFile tagsToFile = new TagsToFile(db);) {
|
||||||
|
|
||||||
final OffsetDateTime day1 = DateUtils.getDate(2016, 1, 1, 1, 1, 1);
|
final OffsetDateTime day1 = DateUtils.getDate(2016, 1, 1, 1, 1, 1);
|
||||||
@@ -66,7 +66,7 @@ public class TagsToFilesTest {
|
|||||||
@Test(invocationCount = 1)
|
@Test(invocationCount = 1)
|
||||||
public void testNewFileIfDateIsTooOld() throws Exception {
|
public void testNewFileIfDateIsTooOld() throws Exception {
|
||||||
|
|
||||||
final DataStore db = new DataStore(dataDirectory);
|
final PdbDB db = new PdbDB(dataDirectory);
|
||||||
try (final TagsToFile tagsToFile = new TagsToFile(db);) {
|
try (final TagsToFile tagsToFile = new TagsToFile(db);) {
|
||||||
|
|
||||||
final OffsetDateTime afternoon = DateUtils.getDate(2016, 1, 1, 13, 1, 1);
|
final OffsetDateTime afternoon = DateUtils.getDate(2016, 1, 1, 13, 1, 1);
|
||||||
@@ -104,7 +104,7 @@ public class TagsToFilesTest {
|
|||||||
|
|
||||||
public void testIdenticalDatesGoIntoSameFile() throws Exception {
|
public void testIdenticalDatesGoIntoSameFile() throws Exception {
|
||||||
|
|
||||||
final DataStore db = new DataStore(dataDirectory);
|
final PdbDB db = new PdbDB(dataDirectory);
|
||||||
try (final TagsToFile tagsToFile = new TagsToFile(db)) {
|
try (final TagsToFile tagsToFile = new TagsToFile(db)) {
|
||||||
|
|
||||||
final OffsetDateTime timestamp = DateUtils.getDate(2016, 1, 1, 13, 1, 1);
|
final OffsetDateTime timestamp = DateUtils.getDate(2016, 1, 1, 13, 1, 1);
|
||||||
|
|||||||
Reference in New Issue
Block a user