From 011e17a70e74988e4c75a83e208faf912ec147ac Mon Sep 17 00:00:00 2001 From: Mario Fusco Date: Tue, 24 Sep 2024 11:45:14 +0200 Subject: [PATCH] Cache compiled dtables during the building of a KieProject (#6097) * Cache compiled dtables during the building of a KieProject * wip * wip --- .../kie/builder/impl/KieBuilderImpl.java | 6 +++ .../DecisionTableProviderImpl.java | 40 ++++++++++++++----- .../drl/extensions/DecisionTableFactory.java | 6 +++ .../drl/extensions/DecisionTableProvider.java | 8 ++-- 4 files changed, 45 insertions(+), 15 deletions(-) diff --git a/drools-compiler/src/main/java/org/drools/compiler/kie/builder/impl/KieBuilderImpl.java b/drools-compiler/src/main/java/org/drools/compiler/kie/builder/impl/KieBuilderImpl.java index ec1cb65533d..61e1796506f 100644 --- a/drools-compiler/src/main/java/org/drools/compiler/kie/builder/impl/KieBuilderImpl.java +++ b/drools-compiler/src/main/java/org/drools/compiler/kie/builder/impl/KieBuilderImpl.java @@ -298,6 +298,12 @@ private static void buildKieProject( BuildContext buildContext, kieRepository.addKieModule( kDep ); } } + + clearBuilderCache(); + } + + private static void clearBuilderCache() { + DecisionTableFactory.clearCompilerCache(); } private void addKBasesFilesToTrg() { diff --git a/drools-decisiontables/src/main/java/org/drools/decisiontable/DecisionTableProviderImpl.java b/drools-decisiontables/src/main/java/org/drools/decisiontable/DecisionTableProviderImpl.java index a809b8ff8e1..a843964a7fa 100644 --- a/drools-decisiontables/src/main/java/org/drools/decisiontable/DecisionTableProviderImpl.java +++ b/drools-decisiontables/src/main/java/org/drools/decisiontable/DecisionTableProviderImpl.java @@ -27,6 +27,7 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; import org.drools.decisiontable.parser.xls.PropertiesSheetListener; import org.drools.drl.extensions.DecisionTableProvider; @@ -42,7 +43,9 @@ public class DecisionTableProviderImpl implements DecisionTableProvider { - private static final transient Logger logger = LoggerFactory.getLogger( DecisionTableProviderImpl.class ); + private static final Logger logger = LoggerFactory.getLogger( DecisionTableProviderImpl.class ); + + private Map compiledDtablesCache = new ConcurrentHashMap<>(); @Override public String loadFromResource(Resource resource, @@ -50,8 +53,6 @@ public String loadFromResource(Resource resource, try { return compileResource( resource, configuration ); - } catch (IOException e) { - throw new UncheckedIOException( e ); } catch (Exception e) { throw new DecisionTableParseException(resource, e); } @@ -78,29 +79,46 @@ public List loadFromInputStreamWithTemplates(Resource resource, return drls; } - private String compileResource(Resource resource, - DecisionTableConfiguration configuration) throws IOException { + private String compileResource(Resource resource, DecisionTableConfiguration configuration) { + if (resource.getSourcePath() == null) { + return internalCompileResource(resource, configuration); + } + String resourceKey = resource.getSourcePath() + "?trimCell=" + configuration.isTrimCell() + "&worksheetName=" + configuration.getWorksheetName(); + return compiledDtablesCache.computeIfAbsent(resourceKey, path -> internalCompileResource(resource, configuration)); + } + + private String internalCompileResource(Resource resource, DecisionTableConfiguration configuration) throws UncheckedIOException { SpreadsheetCompiler compiler = new SpreadsheetCompiler(configuration.isTrimCell()); switch ( configuration.getInputType() ) { case XLS : case XLSX : if ( StringUtils.isEmpty( configuration.getWorksheetName() ) ) { - return compiler.compile( resource, - InputType.XLS ); + return compiler.compile( resource, InputType.XLS ); } else { - return compiler.compile( resource.getInputStream(), - configuration.getWorksheetName() ); + try { + return compiler.compile( resource.getInputStream(), configuration.getWorksheetName() ); + } catch (IOException e) { + throw new UncheckedIOException(e); + } } case CSV : { - return compiler.compile( resource.getInputStream(), - InputType.CSV ); + try { + return compiler.compile( resource.getInputStream(), InputType.CSV ); + } catch (IOException e) { + throw new UncheckedIOException(e); + } } } return null; } + @Override + public void clearCompilerCache() { + compiledDtablesCache.clear(); + } + @Override public Map> loadPropertiesFromFile(File file, DecisionTableConfiguration configuration) { switch (configuration.getInputType()) { diff --git a/drools-drl/drools-drl-extensions/src/main/java/org/drools/drl/extensions/DecisionTableFactory.java b/drools-drl/drools-drl-extensions/src/main/java/org/drools/drl/extensions/DecisionTableFactory.java index caef7c8ed27..ab46e273557 100644 --- a/drools-drl/drools-drl-extensions/src/main/java/org/drools/drl/extensions/DecisionTableFactory.java +++ b/drools-drl/drools-drl-extensions/src/main/java/org/drools/drl/extensions/DecisionTableFactory.java @@ -42,6 +42,12 @@ public static List loadFromInputStreamWithTemplates(Resource resource, D return getDecisionTableProvider().loadFromInputStreamWithTemplates( resource, configuration ); } + public static void clearCompilerCache() { + if (provider != null) { + provider.clearCompilerCache(); + } + } + public static synchronized void setDecisionTableProvider(DecisionTableProvider provider) { DecisionTableFactory.provider = provider; } diff --git a/drools-drl/drools-drl-extensions/src/main/java/org/drools/drl/extensions/DecisionTableProvider.java b/drools-drl/drools-drl-extensions/src/main/java/org/drools/drl/extensions/DecisionTableProvider.java index 7b2fbdc0551..c11a1f9a78e 100644 --- a/drools-drl/drools-drl-extensions/src/main/java/org/drools/drl/extensions/DecisionTableProvider.java +++ b/drools-drl/drools-drl-extensions/src/main/java/org/drools/drl/extensions/DecisionTableProvider.java @@ -29,13 +29,13 @@ public interface DecisionTableProvider extends KieService { - String loadFromResource(Resource resource, - DecisionTableConfiguration configuration); + String loadFromResource(Resource resource, DecisionTableConfiguration configuration); - List loadFromInputStreamWithTemplates(Resource resource, - DecisionTableConfiguration configuration); + List loadFromInputStreamWithTemplates(Resource resource, DecisionTableConfiguration configuration); Map> loadPropertiesFromFile(File file, DecisionTableConfiguration configuration); Map> loadPropertiesFromInputStream(InputStream inputStream, DecisionTableConfiguration configuration); + + void clearCompilerCache(); }