diff --git a/legend-engine-core/legend-engine-core-base/legend-engine-core-language-pure/legend-engine-language-pure-modelManager-sdlc/src/main/java/org/finos/legend/engine/language/pure/modelManager/sdlc/SDLCLoader.java b/legend-engine-core/legend-engine-core-base/legend-engine-core-language-pure/legend-engine-language-pure-modelManager-sdlc/src/main/java/org/finos/legend/engine/language/pure/modelManager/sdlc/SDLCLoader.java index b8beaf94748..aca85e2af35 100644 --- a/legend-engine-core/legend-engine-core-base/legend-engine-core-language-pure/legend-engine-language-pure-modelManager-sdlc/src/main/java/org/finos/legend/engine/language/pure/modelManager/sdlc/SDLCLoader.java +++ b/legend-engine-core/legend-engine-core-base/legend-engine-core-language-pure/legend-engine-language-pure-modelManager-sdlc/src/main/java/org/finos/legend/engine/language/pure/modelManager/sdlc/SDLCLoader.java @@ -22,6 +22,7 @@ import java.util.Collection; import java.util.HashMap; +import java.util.List; import java.util.Map; import java.util.function.Function; import java.util.function.Supplier; @@ -132,17 +133,25 @@ public void setModelManager(ModelManager modelManager) @Override public boolean shouldCache(PureModelContext context) { - return this.supports(context) && ( - (context instanceof PureModelContextCollection && isCacheableCollection((PureModelContextCollection) context)) || - (isCacheablePureSDLC(((PureModelContextPointer) context).sdlcInfo) || isCacheableAlloySDLC(((PureModelContextPointer) context).sdlcInfo)) + return this.supports(context) && + ( + ( + context instanceof PureModelContextCollection && + isCacheableCollection((PureModelContextCollection) context) + ) || + ( + context instanceof PureModelContextPointer && + ( + isCacheablePureSDLC(((PureModelContextPointer) context).sdlcInfo) || + isCacheableAlloySDLC(((PureModelContextPointer) context).sdlcInfo) + ) + ) ); } - private boolean isCacheableCollection(PureModelContextCollection contextCollection) + private boolean isCacheableCollection(PureModelContextCollection context) { - return contextCollection.getContexts().stream().allMatch(pureModelContext -> pureModelContext instanceof PureModelContextPointer) && - contextCollection.getContexts().stream().map(pureModelContext -> (PureModelContextPointer) pureModelContext).allMatch(pureModelContextPointer -> pureModelContextPointer.sdlcInfo instanceof AlloySDLC) && - contextCollection.getContexts().stream().map(pureModelContext -> (PureModelContextPointer) pureModelContext).map(pureModelContextPointer -> (AlloySDLC)pureModelContextPointer.sdlcInfo).allMatch(this::isCacheableAlloySDLC); + return !context.getContexts().isEmpty() && context.getContexts().stream().allMatch(this::shouldCache); } private boolean isCacheablePureSDLC(SDLC sdlc) @@ -177,10 +186,8 @@ public PureModelContext cacheKey(PureModelContext context, Identity identity) } else if (context instanceof PureModelContextCollection) { - PureModelContextCollection pureModelContextCollection = (PureModelContextCollection) context; - Assert.assertTrue(pureModelContextCollection.getContexts().stream().allMatch(pureModelContext -> pureModelContext instanceof PureModelContextPointer), () -> "Invalid type of PureModelContext in PureModelContextCollection for cacheKey"); - Assert.assertTrue(pureModelContextCollection.getContexts().stream().map(pureModelContext -> (PureModelContextPointer)pureModelContext).allMatch(pureModelContextPointer -> pureModelContextPointer.sdlcInfo instanceof AlloySDLC), () -> "Invalid type of SDLC in PureModelContextPointer (in PureModelContextCollection) for cacheKey"); - return pureModelContextCollection; + List contextsCacheKeys = ((PureModelContextCollection) context).getContexts().stream().map(pureModelContext -> this.cacheKey(pureModelContext, identity)).collect(Collectors.toList()); + return new PureModelContextCollection(contextsCacheKeys); } throw new RuntimeException("Invalid PureModelContext type for generating cache key"); } @@ -214,11 +221,7 @@ public PureModelContextData load(Identity identity, PureModelContext ctx, String } else if (ctx instanceof PureModelContextCollection) { - PureModelContextCollection pureModelContextCollection = (PureModelContextCollection) ctx; - Assert.assertTrue(pureModelContextCollection.getContexts().stream().allMatch(pureModelContext -> pureModelContext instanceof PureModelContextPointer), () -> "All elements in PureModelContextCollection should be of type PureModelContextPointer"); - Collection sdlcList = pureModelContextCollection.getContexts().stream().map(context1 -> ((PureModelContextPointer) context1).sdlcInfo).collect(Collectors.toList()); - Assert.assertTrue(sdlcList.stream().allMatch(sdlc -> sdlc instanceof AlloySDLC), () -> "All elements in PureModelContextCollection should have AlloySDLC info"); - + Collection sdlcList = ((PureModelContextCollection) ctx).getContexts().stream().map(context1 -> ((PureModelContextPointer) context1).sdlcInfo).collect(Collectors.toList()); metaData = subject == null ? fetcher.visit(sdlcList) : exec(subject, () -> fetcher.visit(sdlcList)); } else