use of com.apple.foundationdb.record.provider.foundationdb.FDBDatabase in project fdb-record-layer by FoundationDB.
the class KeySpaceDirectoryTest method testPathToAndFromTuple.
@Test
public void testPathToAndFromTuple() throws Exception {
KeySpace root = new KeySpace(new DirectoryLayerDirectory("production", "production").addSubdirectory(new KeySpaceDirectory("userid", KeyType.LONG).addSubdirectory(new DirectoryLayerDirectory("application").addSubdirectory(new KeySpaceDirectory("dataStore", KeyType.NULL)).addSubdirectory(new DirectoryLayerDirectory("metadataStore", "S")))), new DirectoryLayerDirectory("test", "test").addSubdirectory(new KeySpaceDirectory("userid", KeyType.LONG).addSubdirectory(new DirectoryLayerDirectory("application").addSubdirectory(new KeySpaceDirectory("dataStore", KeyType.NULL)).addSubdirectory(new DirectoryLayerDirectory("metadataStore", "S")))));
final KeySpacePath path1 = root.path("production").add("userid", 123456789L).add("application", "com.mybiz.application1").add("dataStore");
final KeySpacePath path2 = root.path("test").add("userid", 987654321L).add("application", "com.mybiz.application2").add("metadataStore");
final FDBDatabase database = FDBDatabaseFactory.instance().getDatabase();
final Tuple path1Tuple;
final Tuple path2Tuple;
try (FDBRecordContext context = database.openContext()) {
path1Tuple = path1.toTuple(context);
path2Tuple = path2.toTuple(context);
context.commit();
}
final Tuple path1ExpectedTuple;
final Tuple path2ExpectedTuple;
try (FDBRecordContext context = database.openContext()) {
List<Long> entries = resolveBatch(context, "production", "test", "com.mybiz.application1", "com.mybiz.application2", "S");
path1ExpectedTuple = Tuple.from(entries.get(0), 123456789L, entries.get(2), null);
path2ExpectedTuple = Tuple.from(entries.get(1), 987654321L, entries.get(3), entries.get(4));
assertEquals(path1ExpectedTuple, path1Tuple);
assertEquals(path2ExpectedTuple, path2Tuple);
// Now, make sure that we can take a tuple and turn it back into a keyspace path.
List<ResolvedKeySpacePath> revPath1 = root.resolveFromKey(context, path1ExpectedTuple).flatten();
assertEquals("production", revPath1.get(0).getDirectoryName());
assertEquals(entries.get(0), revPath1.get(0).getResolvedValue());
assertEquals("userid", revPath1.get(1).getDirectoryName());
assertEquals(123456789L, revPath1.get(1).getResolvedValue());
assertEquals("application", revPath1.get(2).getDirectoryName());
assertEquals(entries.get(2), revPath1.get(2).getResolvedValue());
assertEquals("dataStore", revPath1.get(3).getDirectoryName());
assertEquals(null, revPath1.get(3).getResolvedValue());
// Tack on extra value to make sure it is in the remainder.
Tuple extendedPath2 = path2ExpectedTuple.add(10L);
List<ResolvedKeySpacePath> revPath2 = root.resolveFromKey(context, extendedPath2).flatten();
assertEquals("test", revPath2.get(0).getDirectoryName());
assertEquals("userid", revPath2.get(1).getDirectoryName());
assertEquals("application", revPath2.get(2).getDirectoryName());
assertEquals("metadataStore", revPath2.get(3).getDirectoryName());
assertEquals(Tuple.from(10L), revPath2.get(3).getRemainder());
}
}
use of com.apple.foundationdb.record.provider.foundationdb.FDBDatabase in project fdb-record-layer by FoundationDB.
the class KeySpaceDirectoryTest method flattenPreservesWrapper.
@Test
public void flattenPreservesWrapper() {
KeySpace keySpace = new KeySpace(new KeySpaceDirectory("a", KeyType.STRING, PathA::new).addSubdirectory(new KeySpaceDirectory("b", KeyType.STRING, PathB::new)));
final FDBDatabase database = FDBDatabaseFactory.instance().getDatabase();
List<KeySpacePath> path = keySpace.path("a", "foo").add("b", "bar").flatten();
assertThat("a should be pathA", path.get(0), instanceOf(PathA.class));
assertThat("b should be pathB", path.get(1), instanceOf(PathB.class));
}
use of com.apple.foundationdb.record.provider.foundationdb.FDBDatabase in project fdb-record-layer by FoundationDB.
the class KeySpaceDirectoryTest method testCustomDirectoryResolver.
@Test
public void testCustomDirectoryResolver() throws Exception {
KeySpace root = new KeySpace(new KeySpaceDirectory("root", KeyType.LONG, 1L).addSubdirectory(new ConstantResolvingKeySpaceDirectory("toString", KeyType.STRING, KeySpaceDirectory.ANY_VALUE, v -> "val" + v.toString())).addSubdirectory(new ConstantResolvingKeySpaceDirectory("toWrongType", KeyType.LONG, KeySpaceDirectory.ANY_VALUE, v -> "val" + v.toString())));
final FDBDatabase database = FDBDatabaseFactory.instance().getDatabase();
try (FDBRecordContext context = database.openContext()) {
assertEquals(Tuple.from(1L, "val15"), root.path("root").add("toString", 15).toTuple(context));
assertThrows(RecordCoreArgumentException.class, () -> root.path("root").add("toWrongType", 21L).toTuple(context));
}
}
use of com.apple.foundationdb.record.provider.foundationdb.FDBDatabase in project fdb-record-layer by FoundationDB.
the class KeySpaceDirectoryTest method testListAnyValue.
@Test
public void testListAnyValue() throws Exception {
// Create a root directory called "a" with subdirs of every type (no constants for now)
Long rootValue = random.nextLong();
KeySpaceDirectory dirA = new KeySpaceDirectory("a", KeyType.LONG, rootValue);
for (KeyTypeValue kv : valueOfEveryType) {
dirA.addSubdirectory(new KeySpaceDirectory(kv.keyType.toString(), kv.keyType));
}
KeySpace root = new KeySpace(dirA);
final FDBDatabase database = FDBDatabaseFactory.instance().getDatabase();
final Map<KeyType, List<Tuple>> valuesForType = new HashMap<>();
// Create an entry in the keyspace with a row for every type that we support
try (FDBRecordContext context = database.openContext()) {
Transaction tr = context.ensureActive();
for (KeyTypeValue kv : valueOfEveryType) {
List<Tuple> values = new ArrayList<>();
for (int i = 0; i < 5; i++) {
Object value = kv.generator.get();
Tuple tupleValue = Tuple.from(value);
if (!values.contains(tupleValue)) {
values.add(tupleValue);
// final results.
for (int j = 0; j < 5; j++) {
tr.set(Tuple.from(rootValue, value, j).pack(), Tuple.from(i).pack());
}
}
}
valuesForType.put(kv.keyType, values);
}
context.commit();
}
try (FDBRecordContext context = database.openContext()) {
for (KeyTypeValue kv : valueOfEveryType) {
if (kv.keyType != KeyType.NULL) {
List<Tuple> values = valuesForType.get(kv.keyType);
for (Pair<ValueRange<Object>, List<Tuple>> testCase : listRangeTestCases(values)) {
testListRange(testCase.getLeft(), testCase.getRight(), context, root, kv.keyType);
}
}
}
}
}
use of com.apple.foundationdb.record.provider.foundationdb.FDBDatabase in project fdb-record-layer by FoundationDB.
the class KeySpaceDirectoryTest method testListObeysReturnedRowAndScanLimits.
@Test
public void testListObeysReturnedRowAndScanLimits() {
KeySpace root = new KeySpace(new KeySpaceDirectory("root", KeyType.STRING, "root-" + random.nextInt(Integer.MAX_VALUE)).addSubdirectory(new KeySpaceDirectory("a", KeyType.LONG).addSubdirectory(new KeySpaceDirectory("b", KeyType.LONG))));
final FDBDatabase database = FDBDatabaseFactory.instance().getDatabase();
try (FDBRecordContext context = database.openContext()) {
Transaction tr = context.ensureActive();
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 2; j++) {
tr.set(root.path("root").add("a", i).add("b", j).toTuple(context).pack(), Tuple.from(i + j).pack());
}
}
tr.commit().join();
}
doLimitedScan(database, root, 5, Integer.MAX_VALUE, RecordCursor.NoNextReason.RETURN_LIMIT_REACHED);
doLimitedScan(database, root, Integer.MAX_VALUE, 5, RecordCursor.NoNextReason.SCAN_LIMIT_REACHED);
}
Aggregations