Skip to content
Permalink
Browse files

push name into CollectionBuilder constructor

  • Loading branch information...
jeremydeanlakey committed May 14, 2019
1 parent dd243eb commit fb81db66a43a82263844f492946fd217c3d61185
@@ -6,7 +6,6 @@
import org.bluedb.api.exceptions.BlueDbException;

public interface BlueCollectionBuilder<T extends Serializable> {
public BlueCollectionBuilder<T> withName(String name);
public BlueCollectionBuilder<T> usingClasses(Collection<Class<? extends Serializable>> classesToRegister);
public BlueCollectionBuilder<T> withOptimizedClasses(Collection<Class<? extends Serializable>> classesToRegister);
public BlueCollection<T> build() throws BlueDbException;
}
@@ -9,7 +9,7 @@

public <T extends Serializable> BlueCollection<T> initializeCollection(String name, Class<? extends BlueKey> keyType, Class<T> valueType, @SuppressWarnings("unchecked") Class<? extends Serializable>... additionalClassesToRegister) throws BlueDbException;

public <T extends Serializable, K extends BlueKey> BlueCollectionBuilder<T> collectionBuilder(Class<K> keyType, Class<T> valueType);
public <T extends Serializable, K extends BlueKey> BlueCollectionBuilder<T> collectionBuilder(String name, Class<K> keyType, Class<T> valueType);

public <T extends Serializable> BlueCollection<T> getCollection(String name, Class<T> valueType) throws BlueDbException;

@@ -14,23 +14,18 @@
private final BlueDbOnDisk db;
private final Class<T> valueType;
private final Class<? extends BlueKey> requestedKeyType;
String name;
private final String name;
ArrayList<Class<? extends Serializable>> registeredClasses = new ArrayList<>();

protected BlueCollectionOnDiskBuilder(BlueDbOnDisk db, Class<? extends BlueKey> keyType, Class<T> valueType) {
protected BlueCollectionOnDiskBuilder(BlueDbOnDisk db, String name, Class<? extends BlueKey> keyType, Class<T> valueType) {
this.db = db;
this.name = name;
this.requestedKeyType = keyType;
this.valueType = valueType;
}

@Override
public BlueCollectionOnDiskBuilder<T> withName(String name) {
this.name = name;
return this;
}

@Override
public BlueCollectionOnDiskBuilder<T> usingClasses(Collection<Class<? extends Serializable>> classesToRegister) {
public BlueCollectionOnDiskBuilder<T> withOptimizedClasses(Collection<Class<? extends Serializable>> classesToRegister) {
registeredClasses.addAll(classesToRegister);
return this;
}
@@ -46,8 +46,8 @@
}

@Override
public <T extends Serializable, K extends BlueKey> BlueCollectionOnDiskBuilder<T> collectionBuilder(Class <K> keyType, Class<T> valueType) {
return new BlueCollectionOnDiskBuilder<T>(this, keyType, valueType);
public <T extends Serializable, K extends BlueKey> BlueCollectionOnDiskBuilder<T> collectionBuilder(String name, Class <K> keyType, Class<T> valueType) {
return new BlueCollectionOnDiskBuilder<T>(this, name, keyType, valueType);
}

@Override
@@ -61,10 +61,10 @@ protected void setUp() throws Exception {
filesToDelete = new ArrayList<>();
dbPath = createTempFolder().toPath();
db = new BlueDbOnDiskBuilder().setPath(dbPath).build();
timeCollection = db.collectionBuilder(TimeKey.class, TestValue.class).withName(TIME_COLLECTION_NAME).build();
hashGroupedCollection = db.collectionBuilder(HashGroupedKey.class, TestValue.class).withName(HASH_GROUPED_COLLECTION_NAME).build();
longCollection = db.collectionBuilder(LongKey.class, TestValue.class).withName(LONG_COLLECTION_NAME).build();
intCollection = db.collectionBuilder(IntegerKey.class, TestValue.class).withName(INT_COLLECTION_NAME).build();
timeCollection = db.collectionBuilder(TIME_COLLECTION_NAME, TimeKey.class, TestValue.class).build();
hashGroupedCollection = db.collectionBuilder(HASH_GROUPED_COLLECTION_NAME, HashGroupedKey.class, TestValue.class).build();
longCollection = db.collectionBuilder(LONG_COLLECTION_NAME, LongKey.class, TestValue.class).build();
intCollection = db.collectionBuilder(INT_COLLECTION_NAME, IntegerKey.class, TestValue.class).build();
dbPath = db.getPath();
lockManager = timeCollection.getFileManager().getLockManager();
rollupScheduler = new RollupScheduler(timeCollection);
@@ -30,7 +30,7 @@ public void test_shutdown() throws Exception{
@Test
public void test_getUntypedCollectionForBackup() throws Exception {
String timeCollectionName = getTimeCollectionName();
BlueCollectionOnDisk<String> newCollection = db.collectionBuilder(TimeKey.class, String.class).withName("new_collection").build();
BlueCollectionOnDisk<String> newCollection = db.collectionBuilder("new_collection", TimeKey.class, String.class).build();
assertNotNull(db.getUntypedCollectionForBackup(timeCollectionName));
assertNotNull(db.getUntypedCollectionForBackup("new_collection"));

@@ -51,7 +51,7 @@ public void test_getUntypedCollectionForBackup() throws Exception {

@Test
public void test_getCollection() throws Exception {
db.collectionBuilder(TimeKey.class, TestValue.class).withName(getTimeCollectionName()).build();
db.collectionBuilder(getTimeCollectionName(), TimeKey.class, TestValue.class).build();

BlueCollection<TestValue> collection = db.getCollection(getTimeCollectionName(), TestValue.class);
assertNotNull(collection);
@@ -61,7 +61,7 @@ public void test_getCollection() throws Exception {

@Test
public void test_getCollection_wrong_type() throws Exception {
BlueCollection<TestValue> valueCollection = db.collectionBuilder(TimeKey.class, TestValue.class).withName(getTimeCollectionName()).build();
BlueCollection<TestValue> valueCollection = db.collectionBuilder(getTimeCollectionName(), TimeKey.class, TestValue.class).build();
TimeKey testValueKey = new TimeKey(1, 1);
valueCollection.insert(testValueKey, new TestValue("Bob"));
try {
@@ -73,8 +73,8 @@ public void test_getCollection_wrong_type() throws Exception {

@Test
public void test_initializeCollection_existing_correct_type() throws Exception {
db.collectionBuilder(TimeKey.class, TestValue.class).withName(getTimeCollectionName()).build();
assertNotNull(db.collectionBuilder(TimeKey.class, TestValue.class).withName(getTimeCollectionName()).build()); // make sure it works the second time as well
db.collectionBuilder(getTimeCollectionName(), TimeKey.class, TestValue.class).build();
assertNotNull(db.collectionBuilder(getTimeCollectionName(), TimeKey.class, TestValue.class).build()); // make sure it works the second time as well
}

@SuppressWarnings("unchecked")
@@ -589,8 +589,8 @@ public void test_getAllCollectionsFromDisk() throws Exception {
getTimeCollection();
List<BlueCollectionOnDisk<?>> allCollections = db().getAllCollectionsFromDisk();
assertEquals(4, allCollections.size());
db().collectionBuilder(HashGroupedKey.class, String.class).withName("string").build();
db().collectionBuilder(HashGroupedKey.class, Long.class).withName("long").build();
db().collectionBuilder("string", HashGroupedKey.class, String.class).build();
db().collectionBuilder("long", HashGroupedKey.class, Long.class).build();
allCollections = db().getAllCollectionsFromDisk();
assertEquals(6, allCollections.size());
}
@@ -601,7 +601,7 @@ public void test_backup() throws Exception {
TestValue value1 = createValue("Anna");
getTimeCollection().insert(key1At1, value1);

BlueCollectionOnDisk<TestValue2> secondCollection = db.collectionBuilder(TimeKey.class, TestValue2.class).withName("testing_2").build();
BlueCollectionOnDisk<TestValue2> secondCollection = db.collectionBuilder("testing_2", TimeKey.class, TestValue2.class).build();
TestValue2 valueInSecondCollection = new TestValue2("Joe", 3);
secondCollection.insert(key1At1, valueInSecondCollection);

@@ -615,12 +615,12 @@ public void test_backup() throws Exception {
Path restoredBlueDbPath = Paths.get(restoredPath.toString(), "bluedb");

BlueDbOnDisk restoredDb = new BlueDbOnDiskBuilder().setPath(restoredBlueDbPath).build();
BlueCollectionOnDisk<TestValue> restoredCollection = restoredDb.collectionBuilder(TimeKey.class, TestValue.class).withName(getTimeCollectionName()).build();
BlueCollectionOnDisk<TestValue> restoredCollection = restoredDb.collectionBuilder(getTimeCollectionName(), TimeKey.class, TestValue.class).build();
// BlueCollectionOnDisk<TestValue> restoredCollection = (BlueCollectionOnDisk<TestValue>) restoredDb.initializeCollection(getTimeCollectionName(), TimeKey.class, TestValue.class);
assertTrue(restoredCollection.contains(key1At1));
assertEquals(value1, restoredCollection.get(key1At1));

BlueCollectionOnDisk<TestValue2> secondCollectionRestored = restoredDb.collectionBuilder(TimeKey.class, TestValue2.class).withName("testing_2").build();
BlueCollectionOnDisk<TestValue2> secondCollectionRestored = restoredDb.collectionBuilder("testing_2", TimeKey.class, TestValue2.class).build();
assertTrue(secondCollectionRestored.contains(key1At1));
assertEquals(valueInSecondCollection, secondCollectionRestored.get(key1At1));
}
@@ -43,7 +43,7 @@ public void test_backupToTempDirectory_simple() throws Exception {
backupTask.backupToTempDirectory(collectionsToBackup, backedUpPath);

BlueDbOnDisk restoredDb = new BlueDbOnDiskBuilder().setPath(backedUpPath).build();
BlueCollectionOnDisk<TestValue> restoredCollection = restoredDb.collectionBuilder(TimeKey.class, TestValue.class).withName(getTimeCollectionName()).build();
BlueCollectionOnDisk<TestValue> restoredCollection = restoredDb.collectionBuilder(getTimeCollectionName(), TimeKey.class, TestValue.class).build();
assertTrue(restoredCollection.contains(key1At1));
assertEquals(value1, restoredCollection.get(key1At1));
}
@@ -64,7 +64,7 @@ public void test_backupToTempDirectory_change_pending() throws Exception {
backupTask.backupToTempDirectory(collectionsToBackup, backedUpPath);

BlueDbOnDisk restoredDb = new BlueDbOnDiskBuilder().setPath(backedUpPath).build();
BlueCollectionOnDisk<TestValue> restoredCollection = restoredDb.collectionBuilder(TimeKey.class, TestValue.class).withName(getTimeCollectionName()).build();
BlueCollectionOnDisk<TestValue> restoredCollection = restoredDb.collectionBuilder(getTimeCollectionName(), TimeKey.class, TestValue.class).build();
assertTrue(restoredCollection.contains(key1At1));
assertTrue(restoredCollection.contains(key2At2));
assertEquals(value1, restoredCollection.get(key1At1));
@@ -87,7 +87,7 @@ public void test_backupToTempDirectory_rollup_pending() throws Exception {
backupTask.backupToTempDirectory(collectionsToBackup, backedUpPath);

BlueDbOnDisk restoredDb = new BlueDbOnDiskBuilder().setPath(backedUpPath).build();
BlueCollectionOnDisk<TestValue> restoredCollection = restoredDb.collectionBuilder(TimeKey.class, TestValue.class).withName(getTimeCollectionName()).build();
BlueCollectionOnDisk<TestValue> restoredCollection = restoredDb.collectionBuilder(getTimeCollectionName(), TimeKey.class, TestValue.class).build();
Path segmentPath = restoredCollection.getSegmentManager().getSegment(1).getPath();
File[] filesInSegment = segmentPath.toFile().listFiles();
assertEquals(1, filesInSegment.length);
@@ -112,7 +112,7 @@ public void test_restore() throws Exception {
List<TestValue> valuesInNewCollection = newCollection.query().getList();

BlueDbOnDisk restoredDb = getRestoredDatabase(zipPath);
BlueCollectionOnDisk<TestValue> restoredCollection = restoredDb.collectionBuilder(TimeKey.class, TestValue.class).withName(collectionName).build();
BlueCollectionOnDisk<TestValue> restoredCollection = restoredDb.collectionBuilder(collectionName, TimeKey.class, TestValue.class).build();
List<TestValue> valuesInRestoredCollection = restoredCollection.query().getList();

assertEquals(valuesInNewCollection, valuesInRestoredCollection);
@@ -134,7 +134,7 @@ public void test_restoreOfOldDomainBackup() throws Exception {
List<TestValue> valuesInNewCollection = newCollection.query().getList();

BlueDbOnDisk restoredDb = getRestoredDatabase(zipPath);
BlueCollectionOnDisk<TestValue> restoredCollection = restoredDb.collectionBuilder(TimeKey.class, TestValue.class).withName(collectionName).build();
BlueCollectionOnDisk<TestValue> restoredCollection = restoredDb.collectionBuilder(collectionName, TimeKey.class, TestValue.class).build();
List<TestValue> valuesInRestoredCollection = restoredCollection.query().getList();

assertEquals(valuesInNewCollection, valuesInRestoredCollection);
@@ -151,8 +151,8 @@ private BlueDbOnDisk getRestoredDatabase(Path zipPath) throws BlueDbException, U
private BlueDbOnDisk createTestRestoreDatabase() throws BlueDbException {
Path newDbPath = createTempFolder().toPath();
BlueDbOnDisk newDb = new BlueDbOnDiskBuilder().setPath(newDbPath).build();
BlueCollectionOnDisk<TestValue> newCollection = newDb.collectionBuilder(TimeKey.class, TestValue.class)
.withName("time_collection").usingClasses(Arrays.asList(TestValue2.class, TestValueSub.class)).build();
BlueCollectionOnDisk<TestValue> newCollection = newDb.collectionBuilder("time_collection", TimeKey.class, TestValue.class)
.withOptimizedClasses(Arrays.asList(TestValue2.class, TestValueSub.class)).build();

TestValue value1 = new TestValue("Anna");
TestValue value2 = new TestValueSub("Bob");
@@ -141,7 +141,7 @@ public void test_batchInsert_index() throws Exception {

@Test
public void test_insert_times() throws Exception {
BlueCollectionOnDisk<String> stringCollection = db().collectionBuilder(TimeKey.class, String.class).withName("test_strings").build();
BlueCollectionOnDisk<String> stringCollection = db().collectionBuilder("test_strings", TimeKey.class, String.class).build();
String value = "string";
int n = 100;
for (int i = 0; i < n; i++) {
@@ -154,7 +154,7 @@ public void test_insert_times() throws Exception {

@Test
public void test_insert_longs() throws Exception {
BlueCollectionOnDisk<String> stringCollection = db().collectionBuilder(LongKey.class, String.class).withName("test_strings").build();
BlueCollectionOnDisk<String> stringCollection = db().collectionBuilder("test_strings", LongKey.class, String.class).build();
String value = "string";
int n = 100;
for (int i = 0; i < n; i++) {
@@ -168,7 +168,7 @@ public void test_insert_longs() throws Exception {

@Test
public void test_insert_long_strings() throws Exception {
BlueCollectionOnDisk<String> stringCollection = db().collectionBuilder(StringKey.class, String.class).withName("test_strings").build();
BlueCollectionOnDisk<String> stringCollection = db().collectionBuilder("test_strings", StringKey.class, String.class).build();
String value = "string";
int n = 100;
for (int i = 0; i < n; i++) {
@@ -399,8 +399,8 @@ public void test_updateAll_invalid() {

@Test
public void test_ensureCorrectKeyType() throws BlueDbException {
BlueCollectionOnDisk<String> collectionWithTimeKeys = db().collectionBuilder(TimeKey.class, String.class).withName("test_collection_TimeKey").build();
BlueCollectionOnDisk<String> collectionWithLongKeys = db().collectionBuilder(LongKey.class, String.class).withName("test_collection_LongKey").build();
BlueCollectionOnDisk<String> collectionWithTimeKeys = db().collectionBuilder("test_collection_TimeKey", TimeKey.class, String.class).build();
BlueCollectionOnDisk<String> collectionWithLongKeys = db().collectionBuilder("test_collection_LongKey", LongKey.class, String.class).build();

collectionWithTimeKeys.get(new TimeKey(1, 1)); // should not throw an Exception
collectionWithTimeKeys.get(new TimeFrameKey(1, 1, 1)); // should not throw an Exception
@@ -418,17 +418,17 @@ public void test_ensureCorrectKeyType() throws BlueDbException {

@Test
public void test_determineKeyType() throws BlueDbException {
db().collectionBuilder(TimeKey.class, TestValue.class).withName(getTimeCollectionName()).build(); // regular instantiation approach
db().collectionBuilder(getTimeCollectionName(), TimeKey.class, TestValue.class).build(); // regular instantiation approach

BlueDbOnDisk reopenedDatbase = new BlueDbOnDiskBuilder().setPath(db().getPath()).build(); // reopen database without collections instantiated

try {
reopenedDatbase.collectionBuilder(HashGroupedKey.class, TestValue.class).withName(getTimeCollectionName()).build(); // try to open with the wrong key type
reopenedDatbase.collectionBuilder(getTimeCollectionName(), HashGroupedKey.class, TestValue.class).build(); // try to open with the wrong key type
fail();
} catch (BlueDbException e) {
}

BlueCollectionOnDisk<?> collectionWithoutType = (BlueCollectionOnDisk<?>) reopenedDatbase.collectionBuilder(null, TestValue.class).withName(getTimeCollectionName()).build(); // open without specifying key type
BlueCollectionOnDisk<?> collectionWithoutType = (BlueCollectionOnDisk<?>) reopenedDatbase.collectionBuilder(getTimeCollectionName(), null, TestValue.class).build(); // open without specifying key type
assertEquals(TimeKey.class, collectionWithoutType.getKeyType());
}

@@ -262,7 +262,7 @@ public void test_getNext_multiple_time_frames() {

@Test
public void test_insert_longs() throws Exception {
BlueCollectionOnDisk<String> stringCollection = db().collectionBuilder(LongKey.class, String.class).withName("test_strings").build();
BlueCollectionOnDisk<String> stringCollection = db().collectionBuilder("test_strings", LongKey.class, String.class).build();
String value = "longs";
int n = 100;
for (int i = 0; i < n; i++) {
@@ -26,7 +26,7 @@
@Override
protected void setUp() throws Exception {
super.setUp();
longCollection = db().collectionBuilder(LongKey.class, TestValue.class).withName(LONG_COLLECTION_NAME).build();
longCollection = db().collectionBuilder(LONG_COLLECTION_NAME, LongKey.class, TestValue.class).build();
}


@@ -37,9 +37,15 @@ public BlueDbSample() throws BlueDbException {
.setPath(Paths.get("", "sample-db"))
.build();

db.collectionBuilder(UUIDKey.class, NonTimeObject.class).withName(NON_TIME_COLLECTION_NAME).usingClasses(getNonTimeObjectClassesToRegister()).build();
db.collectionBuilder(TimeKey.class, TimeObject.class).withName(TIME_COLLECTION_NAME).usingClasses(getTimeObjectClassesToRegister()).build();
db.collectionBuilder(TimeFrameKey.class, TimeFrameObject.class).withName(TIMEFRAME_COLLECTION_NAME).usingClasses(getTimeframeObjectClassesToRegister()).build();
db.collectionBuilder(NON_TIME_COLLECTION_NAME, UUIDKey.class, NonTimeObject.class)
.withOptimizedClasses(getNonTimeObjectClassesToRegister())
.build();
db.collectionBuilder(TIME_COLLECTION_NAME, TimeKey.class, TimeObject.class)
.withOptimizedClasses(getTimeObjectClassesToRegister())
.build();
db.collectionBuilder(TIMEFRAME_COLLECTION_NAME, TimeFrameKey.class, TimeFrameObject.class)
.withOptimizedClasses(getTimeframeObjectClassesToRegister())
.build();

getTimeCollection().createIndex(TIME_COLLECTION_INDEX_NAME, StringKey.class, new TimeObjectDataIndexExtractor());
}

0 comments on commit fb81db6

Please sign in to comment.
You can’t perform that action at this time.