Skip to content

Commit 78545f3

Browse files
committed
Initial pass at KeySpacePath.importData
1 parent 1a58550 commit 78545f3

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
@@ -603,4 +603,21 @@ default RecordCursor<DataInKeySpacePath> exportAllData(@Nonnull FDBRecordContext
603603
@Nonnull ScanProperties scanProperties) {
604604
throw new UnsupportedOperationException("exportAllData is not supported");
605605
}
606+
607+
/**
608+
* Imports the provided data exported via {@link #exportAllData} into this {@code KeySpacePath}.
609+
* This will validate that any data provided in {@code dataToImport} has a path that should be in this path,
610+
* or one of the sub-directories, if not the future will complete exceptionally with
611+
* {@link RecordCoreIllegalImportDataException}.
612+
* If there is any data already existing under this path, the new data will overwrite if the keys are the same.
613+
* This will use the logical value in the {@link DataInKeySpacePath#getResolvedPath()} to determine the key, rather
614+
* than the raw key, meaning that this will work even if the data was exported from a different cluster.
615+
* @param context the transaction context in which to save the data
616+
* @param dataToImport the data to be saved to the database
617+
* @return a future to be completed once all data has been important.
618+
*/
619+
@API(API.Status.EXPERIMENTAL)
620+
@Nonnull
621+
CompletableFuture<Void> importData(@Nonnull FDBRecordContext context,
622+
@Nonnull Iterable<DataInKeySpacePath> dataToImport);
606623
}

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
@@ -386,6 +386,43 @@ public RecordCursor<DataInKeySpacePath> exportAllData(@Nonnull FDBRecordContext
386386
.map(keyValue -> new DataInKeySpacePath(this, keyValue, context));
387387
}
388388

389+
@Nonnull
390+
@Override
391+
public CompletableFuture<Void> importData(@Nonnull FDBRecordContext context,
392+
@Nonnull Iterable<DataInKeySpacePath> dataToImport) {
393+
return toTupleAsync(context).thenCompose(targetTuple -> {
394+
List<CompletableFuture<Void>> importFutures = new ArrayList<>();
395+
396+
for (DataInKeySpacePath dataItem : dataToImport) {
397+
CompletableFuture<Void> importFuture = dataItem.getResolvedPath().thenCompose(resolvedPath -> {
398+
// Validate that this data belongs under this path
399+
Tuple itemTuple = resolvedPath.toTuple();
400+
if (!TupleHelpers.isPrefix(targetTuple, itemTuple)) {
401+
throw new RecordCoreIllegalImportDataException(
402+
"Data item path does not belong under target path",
403+
"target", targetTuple, "item", itemTuple);
404+
}
405+
406+
// Reconstruct the key using logical values from the resolved path
407+
Tuple keyTuple = itemTuple;
408+
if (resolvedPath.getRemainder() != null) {
409+
keyTuple = keyTuple.addAll(resolvedPath.getRemainder());
410+
}
411+
412+
// Store the data
413+
byte[] keyBytes = keyTuple.pack();
414+
byte[] valueBytes = dataItem.getRawKeyValue().getValue();
415+
context.ensureActive().set(keyBytes, valueBytes);
416+
417+
return AsyncUtil.DONE;
418+
});
419+
importFutures.add(importFuture);
420+
}
421+
422+
return AsyncUtil.whenAll(importFutures);
423+
});
424+
}
425+
389426
/**
390427
* Returns this path properly wrapped in whatever implementation the directory the path is contained in dictates.
391428
*/

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
@@ -239,4 +239,11 @@ public RecordCursor<DataInKeySpacePath> exportAllData(@Nonnull FDBRecordContext
239239
@Nonnull ScanProperties scanProperties) {
240240
return inner.exportAllData(context, continuation, scanProperties);
241241
}
242+
243+
@Nonnull
244+
@Override
245+
public CompletableFuture<Void> importData(@Nonnull FDBRecordContext context,
246+
@Nonnull Iterable<DataInKeySpacePath> dataToImport) {
247+
return inner.importData(context, dataToImport);
248+
}
242249
}
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)