remove the wrapper class PdbDB

It did not serve any purpose and could be replaced by DataStore.
This commit is contained in:
2018-10-04 18:43:27 +02:00
parent 01b93e32ca
commit 8939332004
6 changed files with 41 additions and 102 deletions

View File

@@ -1,60 +0,0 @@
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;
import org.lucares.pdb.datastore.internal.DataStore;
import org.lucares.pdb.datastore.internal.Proposer;
import org.lucares.pdb.diskstorage.DiskStorage;
public class PdbDB implements AutoCloseable {
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) {
return dataStore.search(query);
}
public long 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);
}
public List<Proposal> propose(final String query, final int caretIndex) {
return proposer.propose(query, caretIndex);
}
public List<Doc> getByTags(final Tags tags) {
return dataStore.getByTags(tags);
}
public Path getStorageBasePath() {
return dataStore.getStorageBasePath();
}
@Override
public void close() throws IOException {
dataStore.close();
}
public DiskStorage getDiskStorage() {
return dataStore.getDiskStorage();
}
}

View File

@@ -23,6 +23,7 @@ import org.lucares.collections.IntList;
import org.lucares.pdb.api.StringCompressor; import org.lucares.pdb.api.StringCompressor;
import org.lucares.pdb.api.Tags; import org.lucares.pdb.api.Tags;
import org.lucares.pdb.datastore.Doc; import org.lucares.pdb.datastore.Doc;
import org.lucares.pdb.datastore.Proposal;
import org.lucares.pdb.datastore.lang.Expression; import org.lucares.pdb.datastore.lang.Expression;
import org.lucares.pdb.datastore.lang.ExpressionToDocIdVisitor; import org.lucares.pdb.datastore.lang.ExpressionToDocIdVisitor;
import org.lucares.pdb.datastore.lang.ExpressionToDocIdVisitor.AllDocIds; import org.lucares.pdb.datastore.lang.ExpressionToDocIdVisitor.AllDocIds;
@@ -265,15 +266,15 @@ public class DataStore implements AutoCloseable {
return result; return result;
} }
public Path getStorageBasePath() {
return storageBasePath;
}
@Override @Override
public void close() throws IOException { public void close() throws IOException {
diskStorage.close(); diskStorage.close();
} }
public List<Proposal> propose(final String query, final int caretIndex) {
return new Proposer(this).propose(query, caretIndex);
}
private void initListingFileIfNotExists() throws IOException { private void initListingFileIfNotExists() throws IOException {
if (!Files.exists(listingFilePath)) { if (!Files.exists(listingFilePath)) {

View File

@@ -8,7 +8,6 @@ import java.util.Collections;
import java.util.List; import java.util.List;
import org.lucares.pdb.api.Tags; import org.lucares.pdb.api.Tags;
import org.lucares.pdb.datastore.PdbDB;
import org.lucares.pdb.datastore.Proposal; import org.lucares.pdb.datastore.Proposal;
import org.lucares.utils.CollectionUtils; import org.lucares.utils.CollectionUtils;
import org.lucares.utils.file.FileUtils; import org.lucares.utils.file.FileUtils;
@@ -21,7 +20,7 @@ import org.testng.annotations.Test;
public class ProposerTest { public class ProposerTest {
private Path dataDirectory; private Path dataDirectory;
private PdbDB db; private DataStore dataStore;
@BeforeClass @BeforeClass
public void beforeClass() throws Exception { public void beforeClass() throws Exception {
@@ -32,13 +31,13 @@ public class ProposerTest {
@AfterClass @AfterClass
public void afterClass() throws IOException { public void afterClass() throws IOException {
FileUtils.delete(dataDirectory); FileUtils.delete(dataDirectory);
db.close(); dataStore.close();
db = null; dataStore = null;
Tags.STRING_COMPRESSOR = null; Tags.STRING_COMPRESSOR = null;
} }
private void initDatabase() throws Exception { private void initDatabase() throws Exception {
db = new PdbDB(dataDirectory); dataStore = new DataStore(dataDirectory);
final Tags eagleTim = Tags.create("bird", "eagle", "name", "Tim"); final Tags eagleTim = Tags.create("bird", "eagle", "name", "Tim");
final Tags eagleTimothy = Tags.create("bird", "eagle", "name", "Timothy"); final Tags eagleTimothy = Tags.create("bird", "eagle", "name", "Timothy");
@@ -47,12 +46,12 @@ public class ProposerTest {
final Tags labradorJenny = Tags.create("dog", "labrador", "name", "Jenny"); final Tags labradorJenny = Tags.create("dog", "labrador", "name", "Jenny");
final Tags labradorTim = Tags.create("dog", "labrador", "name", "Tim"); final Tags labradorTim = Tags.create("dog", "labrador", "name", "Tim");
db.createNewFile(eagleTim); dataStore.createNewFile(eagleTim);
db.createNewFile(eagleTimothy); dataStore.createNewFile(eagleTimothy);
db.createNewFile(pigeonJennifer); dataStore.createNewFile(pigeonJennifer);
db.createNewFile(flamingoJennifer); dataStore.createNewFile(flamingoJennifer);
db.createNewFile(labradorJenny); dataStore.createNewFile(labradorJenny);
db.createNewFile(labradorTim); dataStore.createNewFile(labradorTim);
} }
public void testEmptyQuery() throws Exception { public void testEmptyQuery() throws Exception {
@@ -142,7 +141,7 @@ public class ProposerTest {
private void assertProposals(final String query, final int caretIndex, final Proposal... expected) private void assertProposals(final String query, final int caretIndex, final Proposal... expected)
throws InterruptedException { throws InterruptedException {
final List<Proposal> actual = db.propose(query, caretIndex); final List<Proposal> actual = dataStore.propose(query, caretIndex);
final List<Proposal> expectedList = Arrays.asList(expected); final List<Proposal> expectedList = Arrays.asList(expected);
Collections.sort(expectedList); Collections.sort(expectedList);

View File

@@ -16,8 +16,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.PdbDB;
import org.lucares.pdb.datastore.Proposal; import org.lucares.pdb.datastore.Proposal;
import org.lucares.pdb.datastore.internal.DataStore;
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;
@@ -28,13 +28,13 @@ public class PerformanceDb implements AutoCloseable {
private final TagsToFile tagsToFile; private final TagsToFile tagsToFile;
private final PdbDB db; private final DataStore dataStore;
public PerformanceDb(final Path dataDirectory) throws IOException { public PerformanceDb(final Path dataDirectory) throws IOException {
db = new PdbDB(dataDirectory); dataStore = new DataStore(dataDirectory);
tagsToFile = new TagsToFile(db); tagsToFile = new TagsToFile(dataStore);
} }
public void put(final Entry entry) throws WriteException { public void put(final Entry entry) throws WriteException {
@@ -139,7 +139,7 @@ public class PerformanceDb implements AutoCloseable {
private Result toResult(final Grouping grouping) { private Result toResult(final Grouping grouping) {
final List<GroupResult> groupResults = new ArrayList<>(); final List<GroupResult> groupResults = new ArrayList<>();
for (final Group group : grouping.getGroups()) { for (final Group group : grouping.getGroups()) {
final Stream<LongList> stream = PdbFile.toStream(group.getFiles(), db.getDiskStorage()); final Stream<LongList> stream = PdbFile.toStream(group.getFiles(), dataStore.getDiskStorage());
final GroupResult groupResult = new GroupResult(stream, group.getTags()); final GroupResult groupResult = new GroupResult(stream, group.getTags());
groupResults.add(groupResult); groupResults.add(groupResult);
} }
@@ -151,7 +151,7 @@ public class PerformanceDb implements AutoCloseable {
public void close() { public void close() {
tagsToFile.close(); tagsToFile.close();
try { try {
db.close(); dataStore.close();
} catch (final IOException e) { } catch (final IOException e) {
LOGGER.error("failed to close PdbDB", e); LOGGER.error("failed to close PdbDB", e);
} }
@@ -159,17 +159,17 @@ public class PerformanceDb implements AutoCloseable {
public List<Proposal> autocomplete(final String query, final int caretIndex) { public List<Proposal> autocomplete(final String query, final int caretIndex) {
return db.propose(query, caretIndex); return dataStore.propose(query, caretIndex);
} }
public List<String> getFields() { public List<String> getFields() {
final List<String> fields = db.getAvailableFields(); final List<String> fields = dataStore.getAvailableFields();
return fields; return fields;
} }
public SortedSet<String> getFieldsValues(final String query, final String fieldName) { public SortedSet<String> getFieldsValues(final String query, final String fieldName) {
return db.getAvailableValuesForKey(query, fieldName); return dataStore.getAvailableValuesForKey(query, fieldName);
} }
} }

View File

@@ -8,7 +8,7 @@ import java.util.function.Consumer;
import org.lucares.pdb.api.Tags; import org.lucares.pdb.api.Tags;
import org.lucares.pdb.datastore.Doc; import org.lucares.pdb.datastore.Doc;
import org.lucares.pdb.datastore.PdbDB; import org.lucares.pdb.datastore.internal.DataStore;
import org.lucares.performance.db.HotEntryCache.Event; import org.lucares.performance.db.HotEntryCache.Event;
import org.lucares.performance.db.HotEntryCache.EventListener; import org.lucares.performance.db.HotEntryCache.EventListener;
import org.lucares.performance.db.HotEntryCache.EventType; import org.lucares.performance.db.HotEntryCache.EventType;
@@ -64,12 +64,11 @@ public class TagsToFile implements AutoCloseable {
} }
} }
private final PdbDB db;
private final HotEntryCache<CacheKey, PdbWriter> writerCache; private final HotEntryCache<CacheKey, PdbWriter> writerCache;
private final DataStore dataStore;
public TagsToFile(final PdbDB db) { public TagsToFile(final DataStore dataStore) {
this.db = db; this.dataStore = dataStore;
writerCache = new HotEntryCache<>(Duration.ofSeconds(10)); writerCache = new HotEntryCache<>(Duration.ofSeconds(10));
writerCache.addListener(new RemovalListener(), EventType.EVICTED, EventType.REMOVED); writerCache.addListener(new RemovalListener(), EventType.EVICTED, EventType.REMOVED);
@@ -78,7 +77,7 @@ public class TagsToFile implements AutoCloseable {
public List<PdbFile> getFilesForQuery(final String query) { public List<PdbFile> getFilesForQuery(final String query) {
final List<Doc> searchResult = db.search(query); final List<Doc> searchResult = dataStore.search(query);
if (searchResult.size() > 500_000) { if (searchResult.size() > 500_000) {
throw new IllegalStateException("Too many results."); throw new IllegalStateException("Too many results.");
} }
@@ -111,12 +110,12 @@ public class TagsToFile implements AutoCloseable {
if (writer == null) { if (writer == null) {
LOGGER.trace("getByTags({})", tags); LOGGER.trace("getByTags({})", tags);
final List<Doc> docsForTags = db.getByTags(tags); final List<Doc> docsForTags = dataStore.getByTags(tags);
if (docsForTags.size() > 0) { if (docsForTags.size() > 0) {
try { try {
final Doc doc = docsForTags.get(0); final Doc doc = docsForTags.get(0);
final PdbFile pdbFile = new PdbFile(doc.getRootBlockNumber(), tags); final PdbFile pdbFile = new PdbFile(doc.getRootBlockNumber(), tags);
writer = new PdbWriter(pdbFile, db.getDiskStorage()); writer = new PdbWriter(pdbFile, dataStore.getDiskStorage());
} catch (final IOException e) { } catch (final IOException e) {
throw new ReadException(e); throw new ReadException(e);
} }
@@ -134,7 +133,7 @@ public class TagsToFile implements AutoCloseable {
final long start = System.nanoTime(); final long start = System.nanoTime();
try { try {
final PdbFile pdbFile = createNewPdbFile(tags); final PdbFile pdbFile = createNewPdbFile(tags);
final PdbWriter result = new PdbWriter(pdbFile, db.getDiskStorage()); final PdbWriter result = new PdbWriter(pdbFile, dataStore.getDiskStorage());
METRICS_LOGGER_NEW_WRITER.debug("newPdbWriter took {}ms tags: {}", METRICS_LOGGER_NEW_WRITER.debug("newPdbWriter took {}ms tags: {}",
(System.nanoTime() - start) / 1_000_000.0, tags); (System.nanoTime() - start) / 1_000_000.0, tags);
@@ -147,7 +146,7 @@ public class TagsToFile implements AutoCloseable {
private PdbFile createNewPdbFile(final Tags tags) throws IOException { private PdbFile createNewPdbFile(final Tags tags) throws IOException {
final long rootBlockNumber = db.createNewFile(tags); final long rootBlockNumber = dataStore.createNewFile(tags);
final PdbFile result = new PdbFile(rootBlockNumber, tags); final PdbFile result = new PdbFile(rootBlockNumber, tags);
return result; return result;

View File

@@ -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.PdbDB; import org.lucares.pdb.datastore.internal.DataStore;
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,8 +31,8 @@ public class TagsToFilesTest {
public void test() throws Exception { public void test() throws Exception {
try (final PdbDB db = new PdbDB(dataDirectory); // try (final DataStore dataStore = new DataStore(dataDirectory); //
final TagsToFile tagsToFile = new TagsToFile(db)) { final TagsToFile tagsToFile = new TagsToFile(dataStore)) {
final OffsetDateTime date = OffsetDateTime.now(ZoneOffset.UTC); final OffsetDateTime date = OffsetDateTime.now(ZoneOffset.UTC);
final Tags tags = Tags.create("myKey", "myValue"); final Tags tags = Tags.create("myKey", "myValue");
@@ -47,8 +47,8 @@ public class TagsToFilesTest {
public void testAppendingToSameFile() throws Exception { public void testAppendingToSameFile() throws Exception {
try (final PdbDB db = new PdbDB(dataDirectory); // try (final DataStore dataStore = new DataStore(dataDirectory); //
final TagsToFile tagsToFile = new TagsToFile(db);) { final TagsToFile tagsToFile = new TagsToFile(dataStore);) {
// dayC is before dayA and dayB // dayC is before dayA and dayB
final OffsetDateTime dayA = DateUtils.getDate(2016, 1, 2, 1, 1, 1); final OffsetDateTime dayA = DateUtils.getDate(2016, 1, 2, 1, 1, 1);
@@ -72,8 +72,8 @@ public class TagsToFilesTest {
public void testIdenticalDatesGoIntoSameFile() throws Exception { public void testIdenticalDatesGoIntoSameFile() throws Exception {
try (final PdbDB db = new PdbDB(dataDirectory); // try (final DataStore dataStore = new DataStore(dataDirectory); //
final TagsToFile tagsToFile = new TagsToFile(db)) { final TagsToFile tagsToFile = new TagsToFile(dataStore)) {
final OffsetDateTime timestamp = DateUtils.getDate(2016, 1, 1, 13, 1, 1); final OffsetDateTime timestamp = DateUtils.getDate(2016, 1, 1, 13, 1, 1);