Skip to content

Commit f32e4bc

Browse files
Wire secondary-schema stores into the execution context at connection time
TransactionBoundDatabase now populates storesBySchemaName from the additional stores map on RecordStoreAndRecordContextTransaction, so that loadRecordStore(schemaId) can return the secondary FDBRecordStoreBase that RecordQueryStoreBindingPlan looks up at execution time.
1 parent 6793445 commit f32e4bc

3 files changed

Lines changed: 26 additions & 1 deletion

File tree

fdb-relational-core/src/main/java/com/apple/foundationdb/relational/recordlayer/RecordStoreAndRecordContextTransaction.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
import com.google.protobuf.Message;
3434

3535
import javax.annotation.Nonnull;
36+
import java.util.HashMap;
37+
import java.util.Map;
3638
import java.util.Optional;
3739

3840
/**
@@ -45,6 +47,7 @@
4547
public class RecordStoreAndRecordContextTransaction implements Transaction {
4648
FDBRecordStoreBase<Message> store;
4749
RecordContextTransaction transaction;
50+
Map<String, FDBRecordStoreBase<Message>> additionalStores = new HashMap<>();
4851

4952
/**
5053
* the schema template this transaction is bound to. This is mainly needed when accessing the plan cache
@@ -105,6 +108,18 @@ public FDBRecordStoreBase<Message> getRecordStore() {
105108
return store;
106109
}
107110

111+
@Nonnull
112+
public Map<String, FDBRecordStoreBase<Message>> getAdditionalStores() {
113+
return additionalStores;
114+
}
115+
116+
@Nonnull
117+
public RecordStoreAndRecordContextTransaction withAdditionalStore(@Nonnull String schemaName,
118+
@Nonnull FDBRecordStoreBase<Message> additionalStore) {
119+
this.additionalStores.put(schemaName, additionalStore);
120+
return this;
121+
}
122+
108123
@Nonnull
109124
public SchemaTemplate getBoundSchemaTemplate() {
110125
return boundSchemaTemplate;

fdb-relational-core/src/main/java/com/apple/foundationdb/relational/recordlayer/catalog/TransactionBoundDatabase.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@
4949
import javax.annotation.Nonnull;
5050
import javax.annotation.Nullable;
5151
import java.net.URI;
52+
import java.util.HashMap;
53+
import java.util.Map;
5254

5355
/**
5456
* There can only be 1 Database object per Connection instance, and its lifecycle is managed by the connection
@@ -66,6 +68,7 @@ public class TransactionBoundDatabase extends AbstractDatabase {
6668
@Nullable
6769
private final KeySpace keySpace;
6870
BackingStore store;
71+
Map<String, BackingStore> storesBySchemaName = new HashMap<>();
6972
URI uri;
7073

7174
private static final MetadataOperationsFactory onlyTemporaryFunctionOperationsFactory = new AbstractMetadataOperationsFactory() {
@@ -97,6 +100,9 @@ public RelationalConnection connect(@Nullable Transaction transaction) throws Re
97100
}
98101
final var recordStoreAndRecordContextTx = transaction.unwrap(RecordStoreAndRecordContextTransaction.class);
99102
store = BackingRecordStore.fromTransactionWithStore(recordStoreAndRecordContextTx);
103+
storesBySchemaName.clear();
104+
recordStoreAndRecordContextTx.getAdditionalStores().forEach((schemaName, additionalStore) ->
105+
storesBySchemaName.put(schemaName, BackingRecordStore.fromTransactionAndStore(recordStoreAndRecordContextTx, additionalStore)));
100106
final var boundSchemaTemplate = recordStoreAndRecordContextTx.getBoundSchemaTemplate();
101107
EmbeddedRelationalConnection connection = new EmbeddedRelationalConnection(this, new HollowStoreCatalog(boundSchemaTemplate, keySpace),
102108
((RecordStoreAndRecordContextTransaction) transaction).getRecordContextTransaction(), options);
@@ -106,7 +112,7 @@ public RelationalConnection connect(@Nullable Transaction transaction) throws Re
106112

107113
@Override
108114
public BackingStore loadRecordStore(@Nonnull String schemaId, @Nonnull FDBRecordStoreBase.StoreExistenceCheck existenceCheck) {
109-
return store;
115+
return storesBySchemaName.getOrDefault(schemaId, store);
110116
}
111117

112118
@Override

fdb-relational-core/src/main/java/com/apple/foundationdb/relational/recordlayer/storage/BackingRecordStore.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,10 @@ public static BackingRecordStore fromTransactionWithStore(@Nonnull RecordStoreAn
221221
return new BackingRecordStore(txn, txn.getRecordStore());
222222
}
223223

224+
public static BackingRecordStore fromTransactionAndStore(@Nonnull Transaction txn, @Nonnull FDBRecordStoreBase<Message> store) {
225+
return new BackingRecordStore(txn, store);
226+
}
227+
224228
@SuppressWarnings("PMD.PreserveStackTrace")
225229
public static BackingRecordStore load(@Nonnull Transaction txn, @Nonnull StoreConfig config, @Nonnull FDBRecordStoreBase.StoreExistenceCheck existenceCheck) throws RelationalException {
226230
//TODO(bfines) error handling if this store doesn't exist

0 commit comments

Comments
 (0)