Skip to content

Commit 86b7606

Browse files
committed
Cleanup some of the tests for importing data
1 parent e7d33c2 commit 86b7606

File tree

1 file changed

+44
-61
lines changed

1 file changed

+44
-61
lines changed

fdb-record-layer-core/src/test/java/com/apple/foundationdb/record/provider/foundationdb/keyspace/KeySpacePathImportDataTest.java

Lines changed: 44 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@
4242
import java.util.Collections;
4343
import java.util.List;
4444
import java.util.UUID;
45-
import java.util.concurrent.CompletableFuture;
4645
import java.util.concurrent.CompletionException;
4746

4847
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
@@ -111,11 +110,11 @@ void importBasicData() {
111110
byte[] key1 = emp1Path.toSubspace(context).pack(Tuple.from("profile", "name"));
112111
byte[] key2 = emp2Path.toSubspace(context).pack(Tuple.from("profile", "name"));
113112

114-
byte[] value1 = getJoin(context, key1);
115-
byte[] value2 = getJoin(context, key2);
113+
Tuple value1 = getTuple(context, key1);
114+
Tuple value2 = getTuple(context, key2);
116115

117-
assertEquals("John Doe", Tuple.fromBytes(value1).getString(0));
118-
assertEquals("Jane Smith", Tuple.fromBytes(value2).getString(0));
116+
assertEquals("John Doe", value1.getString(0));
117+
assertEquals("Jane Smith", value2.getString(0));
119118
}
120119
}
121120

@@ -126,15 +125,7 @@ void importEmptyData() {
126125
KeySpace root = new KeySpace(
127126
new KeySpaceDirectory("test", KeyType.STRING, UUID.randomUUID().toString()));
128127

129-
try (FDBRecordContext context = database.openContext()) {
130-
KeySpacePath testPath = root.path("test");
131-
132-
// Import empty data - should complete successfully
133-
CompletableFuture<Void> importFuture = testPath.importData(context, Collections.emptyList());
134-
importFuture.join(); // Should not throw any exception
135-
136-
context.commit();
137-
}
128+
importData(database, root.path("test"), Collections.emptyList()); // should not throw any exception
138129

139130
try (FDBRecordContext context = database.openContext()) {
140131
KeySpacePath testPath = root.path("test");
@@ -154,7 +145,6 @@ void importOverwriteExistingData() {
154145
new KeySpaceDirectory("root", KeyType.STRING, rootUuid)
155146
.addSubdirectory(new KeySpaceDirectory("data", KeyType.LONG)));
156147

157-
158148
try (FDBRecordContext context = database.openContext()) {
159149
Transaction tr = context.ensureActive();
160150

@@ -186,16 +176,11 @@ void importOverwriteExistingData() {
186176
KeySpacePath dataPath = root.path("root").add("data", 1L);
187177
final Subspace subspace = dataPath.toSubspace(context);
188178
byte[] key = subspace.pack(Tuple.from("record"));
189-
byte[] value = getJoin(context, key);
190-
assertEquals(Tuple.from("new_value"), Tuple.fromBytes(value));
191-
assertEquals(Tuple.from("other_value"), Tuple.fromBytes(getJoin(context, subspace.pack("other"))));
179+
assertEquals(Tuple.from("new_value"), getTuple(context, key));
180+
assertEquals(Tuple.from("other_value"), getTuple(context, subspace.pack("other")));
192181
}
193182
}
194183

195-
private static byte[] getJoin(final FDBRecordContext context, final byte[] key) {
196-
return context.ensureActive().get(key).join();
197-
}
198-
199184
@Test
200185
void importDataWithConstantValues() {
201186
// Test importing data into a keyspace with constant directory values
@@ -229,16 +214,8 @@ void importDataWithConstantValues() {
229214
try (FDBRecordContext context = database.openContext()) {
230215
KeySpacePath dataPath = root.path("application").add("version").add("data", "config");
231216
byte[] key = dataPath.toSubspace(context).pack(Tuple.from("setting"));
232-
byte[] value = getJoin(context, key);
233-
assertEquals("value1", Tuple.fromBytes(value).getString(0));
234-
}
235-
}
236-
237-
private static void clearPath(final FDBDatabase database, final KeySpacePath root) {
238-
try (FDBRecordContext context = database.openContext()) {
239-
Transaction tr = context.ensureActive();
240-
tr.clear(root.toSubspace(context).range());
241-
context.commit();
217+
Tuple value = getTuple(context, key);
218+
assertEquals("value1", value.getString(0));
242219
}
243220
}
244221

@@ -279,8 +256,8 @@ void importDataWithDirectoryLayer() {
279256
try (FDBRecordContext context = database.openContext()) {
280257
KeySpacePath userPath = root.path("tenant").add("user_id", 999L);
281258
byte[] key = userPath.toSubspace(context).pack(Tuple.from("data"));
282-
byte[] value = getJoin(context, key);
283-
assertEquals("directory_test", Tuple.fromBytes(value).getString(0));
259+
Tuple value = getTuple(context, key);
260+
assertEquals("directory_test", value.getString(0));
284261
}
285262
}
286263

@@ -314,10 +291,7 @@ void importDataWithBinaryValues() {
314291

315292
clearPath(database, root.path("binary_store"));
316293

317-
try (FDBRecordContext context = database.openContext()) {
318-
root.path("binary_store").importData(context, exportedData).join();
319-
context.commit();
320-
}
294+
importData(database, root.path("binary_store"), exportedData);
321295

322296
// Verify
323297
try (FDBRecordContext context = database.openContext()) {
@@ -365,8 +339,8 @@ void importDataWithNullKeyType() {
365339
try (FDBRecordContext context = database.openContext()) {
366340
KeySpacePath nullPath = root.path("base").add("null_dir");
367341
byte[] key = nullPath.toSubspace(context).pack(Tuple.from("item"));
368-
byte[] value = getJoin(context, key);
369-
assertEquals("null_test", Tuple.fromBytes(value).getString(0));
342+
Tuple value = getTuple(context, key);
343+
assertEquals("null_test", value.getString(0));
370344
}
371345
}
372346

@@ -394,26 +368,16 @@ void importDataWithComplexHierarchy() {
394368
}
395369

396370
// Export and import
397-
List<DataInKeySpacePath> exportedData = new ArrayList<>();
398-
try (FDBRecordContext context = database.openContext()) {
399-
root.path("org").exportAllData(context, null, ScanProperties.FORWARD_SCAN)
400-
.forEach(exportedData::add).join();
401-
}
402-
371+
List<DataInKeySpacePath> exportedData = getExportedData(database, root.path("org"));
403372
clearPath(database, root.path("org"));
404-
405-
try (FDBRecordContext context = database.openContext()) {
406-
root.path("org").importData(context, exportedData).join();
407-
context.commit();
408-
}
373+
importData(database, root.path("org"), exportedData);
409374

410375
// Verify
411376
try (FDBRecordContext context = database.openContext()) {
412377
KeySpacePath memberPath = root.path("org").add("dept", "engineering")
413378
.add("team", 42L).add("member", memberId);
414379
byte[] key = memberPath.toSubspace(context).pack(Tuple.from("info", "name"));
415-
byte[] value = getJoin(context, key);
416-
assertEquals("Complex Test", Tuple.fromBytes(value).getString(0));
380+
assertEquals(Tuple.from("Complex Test"), getTuple(context, key));
417381
}
418382
}
419383

@@ -455,8 +419,8 @@ void importDataFromDifferentCluster() {
455419
try (FDBRecordContext context = database.openContext()) {
456420
KeySpacePath userPath = root.path("tenant").add("user_id", 123L);
457421
byte[] key = userPath.toSubspace(context).pack(Tuple.from("profile"));
458-
byte[] value = getJoin(context, key);
459-
assertEquals("cross_cluster_test", Tuple.fromBytes(value).getString(0));
422+
Tuple value = getTuple(context, key);
423+
assertEquals("cross_cluster_test", value.getString(0));
460424
}
461425
}
462426

@@ -569,8 +533,8 @@ void importDataWithSubdirectoryPath() {
569533
try (FDBRecordContext context = database.openContext()) {
570534
KeySpacePath level2Path = root.path("root").add("level1", 1L).add("level2", "data");
571535
byte[] key2 = level2Path.toSubspace(context).pack(Tuple.from("item2"));
572-
byte[] value2 = getJoin(context, key2);
573-
assertEquals("value2", Tuple.fromBytes(value2).getString(0));
536+
Tuple value2 = getTuple(context, key2);
537+
assertEquals("value2", value2.getString(0));
574538
}
575539
}
576540

@@ -685,8 +649,8 @@ void importDataWithDuplicateKeys() {
685649
try (FDBRecordContext context = database.openContext()) {
686650
KeySpacePath dataPath = root.path("root").add("data", 1L);
687651
byte[] key = dataPath.toSubspace(context).pack(Tuple.from("item"));
688-
byte[] value = getJoin(context, key);
689-
assertEquals("final_value", Tuple.fromBytes(value).getString(0));
652+
Tuple value = getTuple(context, key);
653+
assertEquals("final_value", value.getString(0));
690654
}
691655
}
692656

@@ -771,18 +735,37 @@ void importDataValidation() {
771735
try (FDBRecordContext context = database.openContext()) {
772736
KeySpacePath dataPath = root.path("root").add("data", 1L);
773737
byte[] key = dataPath.toSubspace(context).pack();
774-
byte[] value = getJoin(context, key);
775-
assertEquals("test", Tuple.fromBytes(value).getString(0));
738+
Tuple value = getTuple(context, key);
739+
assertEquals("test", value.getString(0));
776740
}
777741
}
778742

743+
private static Tuple getTuple(final FDBRecordContext context, final byte[] key) {
744+
return Tuple.fromBytes(context.ensureActive().get(key).join());
745+
}
746+
747+
private static byte[] getJoin(final FDBRecordContext context, final byte[] key) {
748+
return context.ensureActive().get(key).join();
749+
}
750+
779751
private static void importData(final FDBDatabase database, final KeySpacePath path, final List<DataInKeySpacePath> exportedData) {
780752
try (FDBRecordContext context = database.openContext()) {
781753
path.importData(context, exportedData).join();
782754
context.commit();
783755
}
784756
}
785757

758+
private static void clearPath(final FDBDatabase database, final KeySpacePath path) {
759+
try (FDBRecordContext context = database.openContext()) {
760+
Transaction tr = context.ensureActive();
761+
tr.clear(path.toSubspace(context).range());
762+
context.commit();
763+
}
764+
// just an extra check to make sure the test is working as expected
765+
assertTrue(getExportedData(database, path).isEmpty(),
766+
"Clearing should remove all the data");
767+
}
768+
786769
@Nonnull
787770
private static List<DataInKeySpacePath> getExportedData(final FDBDatabase database, final KeySpacePath path) {
788771
List<DataInKeySpacePath> exportedData = new ArrayList<>();

0 commit comments

Comments
 (0)