Skip to content

Commit 178cd61

Browse files
committed
Initial pass at KeySpacePath.importData
1 parent 58205df commit 178cd61

File tree

5 files changed

+889
-0
lines changed

5 files changed

+889
-0
lines changed

fdb-record-layer-core/src/main/java/com/apple/foundationdb/record/provider/foundationdb/keyspace/KeySpacePath.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,4 +410,21 @@ default RecordCursor<DataInKeySpacePath> exportAllData(@Nonnull FDBRecordContext
410410
@Nonnull ScanProperties scanProperties) {
411411
throw new UnsupportedOperationException("exportAllData is not supported");
412412
}
413+
414+
/**
415+
* Imports the provided data exported via {@link #exportAllData} into this {@code KeySpacePath}.
416+
* This will validate that any data provided in {@code dataToImport} has a path that should be in this path,
417+
* or one of the sub-directories, if not the future will complete exceptionally with
418+
* {@link RecordCoreIllegalImportDataException}.
419+
* If there is any data already existing under this path, the new data will overwrite if the keys are the same.
420+
* This will use the logical value in the {@link DataInKeySpacePath#getResolvedPath()} to determine the key, rather
421+
* than the raw key, meaning that this will work even if the data was exported from a different cluster.
422+
* @param context the transaction context in which to save the data
423+
* @param dataToImport the data to be saved to the database
424+
* @return a future to be completed once all data has been important.
425+
*/
426+
@API(API.Status.EXPERIMENTAL)
427+
@Nonnull
428+
CompletableFuture<Void> importData(@Nonnull FDBRecordContext context,
429+
@Nonnull Iterable<DataInKeySpacePath> dataToImport);
413430
}

fdb-record-layer-core/src/main/java/com/apple/foundationdb/record/provider/foundationdb/keyspace/KeySpacePathImpl.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,43 @@ public RecordCursor<DataInKeySpacePath> exportAllData(@Nonnull FDBRecordContext
316316
1);
317317
}
318318

319+
@Nonnull
320+
@Override
321+
public CompletableFuture<Void> importData(@Nonnull FDBRecordContext context,
322+
@Nonnull Iterable<DataInKeySpacePath> dataToImport) {
323+
return toTupleAsync(context).thenCompose(targetTuple -> {
324+
List<CompletableFuture<Void>> importFutures = new ArrayList<>();
325+
326+
for (DataInKeySpacePath dataItem : dataToImport) {
327+
CompletableFuture<Void> importFuture = dataItem.getResolvedPath().thenCompose(resolvedPath -> {
328+
// Validate that this data belongs under this path
329+
Tuple itemTuple = resolvedPath.toTuple();
330+
if (!TupleHelpers.isPrefix(targetTuple, itemTuple)) {
331+
throw new RecordCoreIllegalImportDataException(
332+
"Data item path does not belong under target path",
333+
"target", targetTuple, "item", itemTuple);
334+
}
335+
336+
// Reconstruct the key using logical values from the resolved path
337+
Tuple keyTuple = itemTuple;
338+
if (resolvedPath.getRemainder() != null) {
339+
keyTuple = keyTuple.addAll(resolvedPath.getRemainder());
340+
}
341+
342+
// Store the data
343+
byte[] keyBytes = keyTuple.pack();
344+
byte[] valueBytes = dataItem.getRawKeyValue().getValue();
345+
context.ensureActive().set(keyBytes, valueBytes);
346+
347+
return AsyncUtil.DONE;
348+
});
349+
importFutures.add(importFuture);
350+
}
351+
352+
return AsyncUtil.whenAll(importFutures);
353+
});
354+
}
355+
319356
/**
320357
* Returns this path properly wrapped in whatever implementation the directory the path is contained in dictates.
321358
*/

fdb-record-layer-core/src/main/java/com/apple/foundationdb/record/provider/foundationdb/keyspace/KeySpacePathWrapper.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,4 +202,11 @@ public RecordCursor<DataInKeySpacePath> exportAllData(@Nonnull FDBRecordContext
202202
@Nonnull ScanProperties scanProperties) {
203203
return inner.exportAllData(context, continuation, scanProperties);
204204
}
205+
206+
@Nonnull
207+
@Override
208+
public CompletableFuture<Void> importData(@Nonnull FDBRecordContext context,
209+
@Nonnull Iterable<DataInKeySpacePath> dataToImport) {
210+
return inner.importData(context, dataToImport);
211+
}
205212
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* RecordCoreIllegalImportDataException.java
3+
*
4+
* This source file is part of the FoundationDB open source project
5+
*
6+
* Copyright 2015-2025 Apple Inc. and the FoundationDB project authors
7+
*
8+
* Licensed under the Apache License, Version 2.0 (the "License");
9+
* you may not use this file except in compliance with the License.
10+
* You may obtain a copy of the License at
11+
*
12+
* http://www.apache.org/licenses/LICENSE-2.0
13+
*
14+
* Unless required by applicable law or agreed to in writing, software
15+
* distributed under the License is distributed on an "AS IS" BASIS,
16+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17+
* See the License for the specific language governing permissions and
18+
* limitations under the License.
19+
*/
20+
21+
package com.apple.foundationdb.record.provider.foundationdb.keyspace;
22+
23+
import com.apple.foundationdb.record.RecordCoreArgumentException;
24+
25+
import javax.annotation.Nonnull;
26+
27+
public class RecordCoreIllegalImportDataException extends RecordCoreArgumentException {
28+
private static final long serialVersionUID = 1L;
29+
30+
public RecordCoreIllegalImportDataException(@Nonnull final String msg, @Nonnull final Object... keyValue) {
31+
super(msg, keyValue);
32+
}
33+
}

0 commit comments

Comments
 (0)