use of com.apple.foundationdb.record.provider.foundationdb.FDBDatabase in project fdb-record-layer by FoundationDB.
the class KeySpaceDirectoryTest method testPathToString.
@Test
public void testPathToString() throws Exception {
KeySpace root = new KeySpace("foo", new KeySpaceDirectory("root", KeyType.LONG).addSubdirectory(new KeySpaceDirectory("dir1", KeyType.STRING).addSubdirectory(new KeySpaceDirectory("dir2", KeyType.BYTES))).addSubdirectory(new KeySpaceDirectory("dir3", KeyType.LONG).addSubdirectory(new DirectoryLayerDirectory("dir4"))));
final FDBDatabase database = FDBDatabaseFactory.instance().getDatabase();
final Long fooValue;
final Long barValue;
try (FDBRecordContext context = database.openContext()) {
List<Long> entries = resolveBatch(context, "_foo", "_bar");
context.commit();
fooValue = entries.get(0);
barValue = entries.get(1);
}
assertEquals("/foo/root/dir3/dir4", root.getDirectory("root").getSubdirectory("dir3").getSubdirectory("dir4").toPathString());
try (FDBRecordContext context = database.openContext()) {
assertEquals("/root:4/dir1:\"hi\"/dir2:0x4142+(\"blah\")", root.resolveFromKey(context, Tuple.from(4L, "hi", new byte[] { 0x41, 0x42 }, "blah")).toString());
assertEquals("/root:11", root.resolveFromKey(context, Tuple.from(11L)).toString());
assertEquals("/root:14/dir3:4/dir4:\"_bar\"[" + barValue + "]", root.resolveFromKey(context, Tuple.from(14L, 4L, barValue)).toString());
assertEquals("/root:11/dir3:17/dir4:" + fooValue, root.path("root", 11L).add("dir3", 17L).add("dir4", fooValue).toString());
}
}
use of com.apple.foundationdb.record.provider.foundationdb.FDBDatabase in project fdb-record-layer by FoundationDB.
the class KeySpaceDirectoryTest method testListReverse.
@Test
public void testListReverse() {
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();
}
final List<Tuple> results;
try (FDBRecordContext context = database.openContext()) {
ScanProperties props = new ScanProperties(ExecuteProperties.newBuilder().build(), true);
results = root.path("root").listSubdirectoryAsync(context, "a", null, props).asList().join().stream().map(path -> path.toTuple()).collect(Collectors.toList());
}
assertEquals(5, results.size());
for (int i = 0; i < 5; i++) {
assertEquals(i, ((Long) results.get(4 - i).getLong(1)).intValue());
}
}
use of com.apple.foundationdb.record.provider.foundationdb.FDBDatabase in project fdb-record-layer by FoundationDB.
the class KeySpaceDirectoryTest method testSeesMetadataUpdates.
@Test
public void testSeesMetadataUpdates() {
String testRoot = "test-root-" + random.nextInt();
Function<FDBRecordContext, CompletableFuture<LocatableResolver>> generator = getGenerator();
KeySpace root = rootForMetadataTests(testRoot, generator);
final FDBDatabase database = FDBDatabaseFactory.instance().getDatabase();
database.setResolverStateRefreshTimeMillis(100);
String dir = "test-string-" + random.nextInt();
try (FDBRecordContext context = database.openContext()) {
KeySpacePath path1 = root.path(testRoot).add("dir_with_metadata_name", dir);
DirWithMetadataWrapper wrapped = (DirWithMetadataWrapper) path1;
assertThat("there's no metadata", wrapped.metadata(context).join(), is(nullValue()));
}
try (FDBRecordContext context = database.openContext()) {
generator.apply(context).thenCompose(scope -> scope.updateMetadataAndVersion(dir, Tuple.from("new-metadata").pack())).join();
}
eventually("we see the new metadata for the path", () -> {
try (FDBRecordContext context = database.openContext()) {
KeySpacePath path1 = root.path(testRoot).add("dir_with_metadata_name", dir);
return ((DirWithMetadataWrapper) path1).metadata(context).join();
}
}, is(Tuple.from("new-metadata").pack()), 120, 10);
}
use of com.apple.foundationdb.record.provider.foundationdb.FDBDatabase in project lionrock by panghy.
the class RemoteFDBDatabaseFactory method getDatabase.
@Override
@Nonnull
public synchronized FDBDatabase getDatabase(@Nullable String clusterFile) {
FDBDatabase database = databases.get(clusterFile);
if (database == null) {
database = new FDBDatabase(this, clusterFile);
database.setDirectoryCacheSize(getDirectoryCacheSize());
database.setTrackLastSeenVersion(getTrackLastSeenVersion());
database.setResolverStateRefreshTimeMillis(getStateRefreshTimeMillis());
database.setDatacenterId(getDatacenterId());
database.setStoreStateCache(storeStateCacheFactory.getCache(database));
databases.put(clusterFile, database);
}
return database;
}
use of com.apple.foundationdb.record.provider.foundationdb.FDBDatabase in project fdb-record-layer by FoundationDB.
the class ChainedCursorTest method testObeysTimeLimit.
@Test
public void testObeysTimeLimit() {
FDBDatabase database = FDBDatabaseFactory.instance().getDatabase();
try (FDBRecordContext context = database.openContext()) {
ScanProperties props = new ScanProperties(ExecuteProperties.newBuilder().setTimeLimit(4L).setFailOnScanLimitReached(false).build());
RecordCursorIterator<Long> cursor = new ChainedCursor<>(context, (lastKey) -> nextKey(lastKey).thenApply(value -> {
sleep(1L);
return value;
}), (key) -> Tuple.from(key).pack(), (prevContinuation) -> Tuple.fromBytes(prevContinuation).getLong(0), null, props).asIterator();
int count = 0;
while (cursor.hasNext()) {
assertEquals(Long.valueOf(count), cursor.next());
++count;
}
assertEquals(cursor.getNoNextReason(), RecordCursor.NoNextReason.TIME_LIMIT_REACHED);
assertTrue(count < 5, "Too many values returned");
}
}
Aggregations