Skip to content

Commit

Permalink
Localize XStoreAssociations based on Runtime Locality
Browse files Browse the repository at this point in the history
  • Loading branch information
haroonsherjan committed Nov 21, 2023
1 parent b3837ac commit be5304d
Show file tree
Hide file tree
Showing 10 changed files with 478 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,10 @@ function meta::pure::mapping::xStore::performXStoreQuery(o:Any[1], p:PropertyMap
multiplicity = PureOne,
values = $returnType.rawType
),
routingStrategy = meta::pure::router::store::routing::getRoutingStrategyFromMappingAndRuntime($objectInfo.static.mapping, $objectInfo.static.runtime)
routingStrategy = meta::pure::router::store::routing::getRoutingStrategyFromMappingAndRuntime($objectInfo.static.mapping, $objectInfo.static.runtime, $extensions)
)
),
routingStrategy = meta::pure::router::store::routing::getRoutingStrategyFromMappingAndRuntime($objectInfo.static.mapping, $objectInfo.static.runtime)
routingStrategy = meta::pure::router::store::routing::getRoutingStrategyFromMappingAndRuntime($objectInfo.static.mapping, $objectInfo.static.runtime, $extensions)
),
^FunctionRoutedValueSpecification
(
Expand All @@ -143,7 +143,7 @@ function meta::pure::mapping::xStore::performXStoreQuery(o:Any[1], p:PropertyMap
)
]
),
routingStrategy = meta::pure::router::store::routing::getRoutingStrategyFromMappingAndRuntime($objectInfo.static.mapping, $objectInfo.static.runtime)
routingStrategy = meta::pure::router::store::routing::getRoutingStrategyFromMappingAndRuntime($objectInfo.static.mapping, $objectInfo.static.runtime, $extensions)
)->evaluateAndDeactivate();

print(if(!$debug.debug, |'',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,5 @@ function <<doc.deprecated>> meta::pure::router::routeFunction(f:FunctionDefiniti

function <<doc.deprecated>> meta::pure::router::routeFunction(f:FunctionDefinition<Any>[1], mapping:Mapping[1], runtime:Runtime[1], exeCtx: ExecutionContext[1], extensions:meta::pure::extension::Extension[*], debug:DebugContext[1]):FunctionDefinition<Any>[1]
{
routeFunction($f, getRoutingStrategyFromMappingAndRuntime($mapping, $runtime), $exeCtx, [], $extensions, $debug)
routeFunction($f, getRoutingStrategyFromMappingAndRuntime($mapping, $runtime, $extensions), $exeCtx, [], $extensions, $debug)
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ Class meta::pure::router::extension::RouterExtension
shouldStopPreeval : Function<{Any[*] -> Boolean[1]}>[0..1];
routeFunctionExpressions : Pair<Function<{FunctionExpression[1]->Boolean[1]}>, Function<{Function<Any>[1], FunctionExpression[1], RoutingState[1], ExecutionContext[1], Map<VariableExpression, ValueSpecification>[1], Map<String, List<Any>>[1], meta::pure::extension::Extension[*], DebugContext[1]->RoutingState[1]}>>[*];
connectionEquality : Function<{Connection[1] -> Function<{Nil[1]->Boolean[1]}>[*]}>[0..1];
processModelJoinsInExecution : Function<{Mapping[1], Runtime[1] -> Function<{Nil[1]->Mapping[1]}>[*]}>[0..1];
splitGraphFetchTreeForPropertyUnion : Boolean[1] = false;
// Execution
traceQueryInfoBuilder : Function<{FunctionDefinition<Any>[1], Mapping[1], meta::core::runtime::Runtime[1] -> String[1]}>[0..1];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -704,10 +704,10 @@ function meta::pure::router::clustering::clusteredGetAll(set: SetImplementation[
(
genericType = ^GenericType(rawType=Class, typeArguments=$sourceType), multiplicity = PureOne, values = $sourceType.rawType
),
routingStrategy = meta::pure::router::store::routing::getRoutingStrategyFromMappingAndRuntime($mapping, $runtime)
routingStrategy = meta::pure::router::store::routing::getRoutingStrategyFromMappingAndRuntime($mapping, $runtime, $extensions)
)
),
routingStrategy = meta::pure::router::store::routing::getRoutingStrategyFromMappingAndRuntime($mapping, $runtime)
routingStrategy = meta::pure::router::store::routing::getRoutingStrategyFromMappingAndRuntime($mapping, $runtime, $extensions)
)->evaluateAndDeactivate();

let clustered = list($newExpr)->doCluster(^LambdaFunction<{->Any[1]}>(expressionSequence=^InstanceValue(values='', genericType=^GenericType(rawType=String), multiplicity=PureOne)),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.

import meta::relational::metamodel::*;
import meta::pure::extension::*;
import meta::pure::graphFetch::*;
import meta::pure::mapping::*;
import meta::pure::router::builder::*;
Expand All @@ -29,16 +31,67 @@ import meta::pure::runtime::*;
// Store Mapping Routing Strategy Builder Helper
// =========================================================================================

function meta::pure::router::store::routing::getRoutingStrategyFromMappingAndRuntime(mapping:Mapping[1], runtime:Runtime[1]):StoreMappingRoutingStrategy[1]
function meta::pure::router::store::routing::getRoutingStrategyFromMappingAndRuntime(mapping:Mapping[1], runtime:Runtime[1], extensions: Extension[*]):StoreMappingRoutingStrategy[1]
{
^StoreMappingRoutingStrategy(mapping = $mapping,
runtime = $runtime,
setsByDepth = ^Map<String, PermutationSet>(),
classMappingsByClass = $mapping->buildClassMappingsByClassMap(),
processClass = processClass_Class_1__InstanceValue_1__RoutingState_1__ExecutionContext_1__DebugContext_1__RoutingState_1_,
processProperty = routeFunctionExpressionProperty_Property_1__FunctionExpression_1__RoutingState_1__ExecutionContext_1__Map_1__Map_1__Extension_MANY__DebugContext_1__RoutingState_1_,
wrapValueSpec = wrapValueSpecification_ValueSpecification_1__RoutingStrategy_1__String_1__ExecutionContext_1__Extension_MANY__DebugContext_1__ExtendedRoutedValueSpecification_1_,
toString = {strategy:RoutingStrategy[1] | $strategy->cast(@StoreMappingRoutingStrategy).sets->size()->toString()});
getRoutingStrategyFromMappingAndRuntime([],$mapping, $runtime, $extensions);
}

function meta::pure::router::store::routing::getRoutingStrategyFromMappingAndRuntime(routingStrategy: RoutingStrategy[0..1], mapping:Mapping[1], runtime:Runtime[1], extensions: Extension[*]):StoreMappingRoutingStrategy[1]
{
let modifiedRuntime = $runtime;
let modifiedMapping = modifyMappingBasedOnRuntimeLocality($mapping, $modifiedRuntime, $extensions);
let currentStoreMappingRoutingStrategy = $routingStrategy->filter(x | $x->instanceOf(StoreMappingRoutingStrategy))->cast(@StoreMappingRoutingStrategy);
if($currentStoreMappingRoutingStrategy->isEmpty(),
| ^StoreMappingRoutingStrategy(mapping = $modifiedMapping,
runtime = $modifiedRuntime,
setsByDepth = ^Map<String, PermutationSet>(),
classMappingsByClass = $modifiedMapping->buildClassMappingsByClassMap(),
processClass = processClass_Class_1__InstanceValue_1__RoutingState_1__ExecutionContext_1__DebugContext_1__RoutingState_1_,
processProperty = routeFunctionExpressionProperty_Property_1__FunctionExpression_1__RoutingState_1__ExecutionContext_1__Map_1__Map_1__Extension_MANY__DebugContext_1__RoutingState_1_,
wrapValueSpec = wrapValueSpecification_ValueSpecification_1__RoutingStrategy_1__String_1__ExecutionContext_1__Extension_MANY__DebugContext_1__ExtendedRoutedValueSpecification_1_,
toString = {strategy:RoutingStrategy[1] | $strategy->cast(@StoreMappingRoutingStrategy).sets->size()->toString()}
);,
| let newStoreMappingRoutingStrategy = $currentStoreMappingRoutingStrategy->toOne();
^$newStoreMappingRoutingStrategy(mapping = $modifiedMapping, runtime = $modifiedRuntime, classMappingsByClass = $modifiedMapping->buildClassMappingsByClassMap(), setsByDepth = ^Map<String, PermutationSet>());
);
}

function <<access.private>> meta::pure::router::store::routing::modifyMappingBasedOnRuntimeLocality(mapping:Mapping[1], runtime:meta::core::runtime::Runtime[1], extensions: meta::pure::extension::Extension[*]):Mapping[1]
{
let allMappings = $mapping->getIncludedMappingsRecursively();
let classMappings = $allMappings.classMappings;
let associationMappings = $allMappings.associationMappings->map(associationMap|$associationMap->match(
[
x:meta::pure::mapping::xStore::XStoreAssociationImplementation[1]|
let mappingIds = $x.propertyMappings.sourceSetImplementationId->concatenate($x.propertyMappings.targetSetImplementationId);
let inScopeClassMappings = $classMappings->filter(x|$x.id->in($mappingIds))->distinct();
let inScopeStores = $inScopeClassMappings->map(setImpl | meta::pure::router::clustering::storeContractForSetImplementation($setImpl, $mapping, $extensions).second);
if(
$runtime.connectionStores->size() >= 1,
| let connection = $inScopeStores->map(s|$runtime->connectionByElement($s))->distinct();
if(
$connection->size() == 1,
| $connection->toOne()->match(
$extensions.availableStores.localizeXStoreAssociation
->map(e|$e->eval($x, $inScopeClassMappings, $inScopeStores))
->concatenate([a:Connection[1] | $x;])->toOneMany()
);,
| $x
);,
| $x
);,

a:AssociationImplementation[1]|$a
]
));
let newMapping = ^Mapping(name=$mapping.name, includes=[], associationMappings=$associationMappings);
let includedMappings = $allMappings->map(map|^$map(associationMappings=[]))->map(map|^MappingInclude(owner=$newMapping,included=$map));
$newMapping->mutateAdd('includes', $includedMappings);
}

function <<access.private>> meta::pure::router::store::routing::getIncludedMappingsRecursively(m : Mapping[1]): Mapping[*]
{
$m->concatenate(if($m.includes->isEmpty(), |[], |$m.includes.included->map(i|$i->getIncludedMappingsRecursively())));
}

function <<access.private>> meta::pure::router::store::routing::processClass(c:Class<Any>[1], i:InstanceValue[1], s:RoutingState[1], executionContext:ExecutionContext[1], debug:DebugContext[1]):RoutingState[1]
Expand Down Expand Up @@ -230,10 +283,7 @@ function meta::pure::router::store::routing::specializedFunctionExpressionRoute
let fromExecutionContext = if($resolvedParameters->size() == 3, | $resolvedParameters->at(2)->cast(@ExecutionContext), | $executionContext);

let currentRoutingStrategy = $state.routingStrategy;
let newRoutingStrategy = if($currentRoutingStrategy->instanceOf(StoreMappingRoutingStrategy),
| let storeMapStrategy = $currentRoutingStrategy->cast(@StoreMappingRoutingStrategy);
^$storeMapStrategy(mapping = $fromMapping, runtime = $fromRuntime, classMappingsByClass = $fromMapping->buildClassMappingsByClassMap(), setsByDepth = ^Map<String, PermutationSet>());,
| getRoutingStrategyFromMappingAndRuntime($fromMapping, $fromRuntime));
let newRoutingStrategy = getRoutingStrategyFromMappingAndRuntime($currentRoutingStrategy, $fromMapping, $fromRuntime, $extensions);
let newState = ^$state(routingStrategy = $newRoutingStrategy);

let processedFunction = routeFunctionExpression($fe.parametersValues->at(0)->cast(@FunctionExpression), $newState, $fromExecutionContext, $vars, $inScopeVars, $extensions, $debug);
Expand Down Expand Up @@ -283,10 +333,7 @@ function meta::pure::router::store::routing::specializedFunctionExpressionRoute
let runtime = $prevalFunc.parametersValues->at(3)->cast(@InstanceValue).values->toOne()->cast(@Runtime);

let currentRoutingStrategy = $state.routingStrategy;
let newRoutingStrategy = if($currentRoutingStrategy->instanceOf(StoreMappingRoutingStrategy),
| let storeMapStrategy = $currentRoutingStrategy->cast(@StoreMappingRoutingStrategy);
^$storeMapStrategy(mapping = $mapping, runtime = $runtime, classMappingsByClass = $mapping->buildClassMappingsByClassMap(), setsByDepth = ^Map<String, PermutationSet>());,
| getRoutingStrategyFromMappingAndRuntime($mapping, $runtime));
let newRoutingStrategy = getRoutingStrategyFromMappingAndRuntime($currentRoutingStrategy, $mapping, $runtime, $extensions);

let processedFirstParam = processCollection($state, $fe.parametersValues->at(0), $executionContext, $vars, $inScopeVars, {x:Any[1] | true}, $extensions, $debug)->toOne();
let processedSave = ^$fe(parametersValues = $processedFirstParam.value->cast(@ValueSpecification)->concatenate($fe.parametersValues->tail()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import meta::pure::store::*;
import meta::core::runtime::*;
import meta::pure::runtime::*;
import meta::pure::executionPlan::*;
import meta::pure::mapping::xStore::*;

Class meta::pure::store::StoreContract extends meta::pure::router::extension::RouterExtension, meta::pure::executionPlan::extension::ExecutionPlanExtension
{
Expand All @@ -16,6 +17,7 @@ Class meta::pure::store::StoreContract extends meta::pure::router::extension::Ro
supports : Function<{FunctionExpression[1]->Boolean[1]}>[0..1];
supportsNativeJoinAmongElements : Boolean[1];
isPropertyAutoMapped : Function<{AbstractProperty<Any>[1], InstanceSetImplementation[1]->Boolean[1]}>[0..1];
localizeXStoreAssociation : Function<{XStoreAssociationImplementation[1], SetImplementation[*], Store[*] -> Function<{Nil[1]->AssociationImplementation[1]}>[*]}>[0..1];
// ------------------------------------------------------------------------------------------ Routing

// ExecutionPlan Generation -------------------------------------------------------------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

import meta::pure::mapping::xStore::*;
import meta::relational::contract::*;
import meta::pure::router::metamodel::*;
import meta::pure::router::systemMapping::tests::*;
Expand Down Expand Up @@ -43,7 +44,12 @@ function meta::relational::contract::relationalStoreContract():StoreContract[1]
planExecution = meta::relational::contract::planExecution_StoreQuery_1__RoutedValueSpecification_$0_1$__Mapping_$0_1$__Runtime_$0_1$__ExecutionContext_1__Extension_MANY__DebugContext_1__ExecutionNode_1_,
planGraphFetchExecution = meta::relational::contract::planGraphFetchExecution_StoreMappingLocalGraphFetchExecutionNodeGenerationInput_1__LocalGraphFetchExecutionNode_1_,
planCrossGraphFetchExecution = meta::relational::contract::planCrossGraphFetchExecution_StoreMappingCrossLocalGraphFetchExecutionNodeGenerationInput_1__LocalGraphFetchExecutionNode_1_,

localizeXStoreAssociation = {
x : XStoreAssociationImplementation[1], setImpl : SetImplementation[*], stores : Store[*] |
[
d: DatabaseConnection[1]| meta::external::store::relational::modelJoins::localizeXStoreAssociation($x, $setImpl, $stores);
]
},
connectionEquality = { b : Connection [1] |
[
d: RelationalDatabaseConnection[1]|
Expand Down
Loading

0 comments on commit be5304d

Please sign in to comment.