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:
2017-04-16 10:11:46 +02:00
parent 43d6eba7b7
commit 44f30aafee
8 changed files with 122 additions and 14 deletions

View File

@@ -195,5 +195,4 @@ public class DataStore {
return result;
}
}

View File

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

View File

@@ -0,0 +1,78 @@
package org.lucares.pdb.datastore;
public class Proposal implements Comparable<Proposal> {
private final String proposedTag;
private final String proposedQuery;
private final long results;
public Proposal(final String proposedTag, final String proposedQuery, final long results) {
super();
this.proposedTag = proposedTag;
this.proposedQuery = proposedQuery;
this.results = results;
}
public String getProposedTag() {
return proposedTag;
}
public String getProposedQuery() {
return proposedQuery;
}
public long getResults() {
return results;
}
@Override
public String toString() {
return "Proposal [proposedTag=" + proposedTag + ", proposedQuery=" + proposedQuery + ", results=" + results
+ "]";
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((proposedQuery == null) ? 0 : proposedQuery.hashCode());
result = prime * result + ((proposedTag == null) ? 0 : proposedTag.hashCode());
result = prime * result + (int) (results ^ (results >>> 32));
return result;
}
@Override
public boolean equals(final Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
final Proposal other = (Proposal) obj;
if (proposedQuery == null) {
if (other.proposedQuery != null)
return false;
} else if (!proposedQuery.equals(other.proposedQuery))
return false;
if (proposedTag == null) {
if (other.proposedTag != null)
return false;
} else if (!proposedTag.equals(other.proposedTag))
return false;
if (results != other.results)
return false;
return true;
}
@Override
public int compareTo(final Proposal o) {
if (results != o.results) {
return results < o.results ? 1 : -1;
}
return proposedTag.compareToIgnoreCase(o.proposedTag);
}
}

View File

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