From cf3116abd091afd474332ac8ba8730949cff116a Mon Sep 17 00:00:00 2001 From: mariofusco Date: Mon, 23 Sep 2024 16:33:34 +0200 Subject: [PATCH 1/3] Cache compiled dtables during the building of a KieProject --- .../kie/builder/impl/KieBuilderImpl.java | 6 +++ .../DecisionTableProviderImpl.java | 38 +++++++++++++------ .../drl/extensions/DecisionTableFactory.java | 6 +++ .../drl/extensions/DecisionTableProvider.java | 8 ++-- 4 files changed, 43 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..d4f42ae5a50 100644 --- a/drools-decisiontables/src/main/java/org/drools/decisiontable/DecisionTableProviderImpl.java +++ b/drools-decisiontables/src/main/java/org/drools/decisiontable/DecisionTableProviderImpl.java @@ -42,7 +42,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 HashMap<>(); @Override public String loadFromResource(Resource resource, @@ -50,8 +52,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 +78,45 @@ public List loadFromInputStreamWithTemplates(Resource resource, return drls; } - private String compileResource(Resource resource, - DecisionTableConfiguration configuration) throws IOException { + private String compileResource(Resource resource, DecisionTableConfiguration configuration) { + String resourcePath = resource.getSourcePath(); + return resourcePath == null ? + internalCompileResource(resource, configuration) : + compiledDtablesCache.computeIfAbsent(resourcePath, 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(); } From 931395d078f695924474dcbd7ee532a38fffabdc Mon Sep 17 00:00:00 2001 From: mariofusco Date: Mon, 23 Sep 2024 17:19:16 +0200 Subject: [PATCH 2/3] wip --- .../drools/decisiontable/DecisionTableProviderImpl.java | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) 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 d4f42ae5a50..0cc404e43cd 100644 --- a/drools-decisiontables/src/main/java/org/drools/decisiontable/DecisionTableProviderImpl.java +++ b/drools-decisiontables/src/main/java/org/drools/decisiontable/DecisionTableProviderImpl.java @@ -79,10 +79,11 @@ public List loadFromInputStreamWithTemplates(Resource resource, } private String compileResource(Resource resource, DecisionTableConfiguration configuration) { - String resourcePath = resource.getSourcePath(); - return resourcePath == null ? - internalCompileResource(resource, configuration) : - compiledDtablesCache.computeIfAbsent(resourcePath, path -> internalCompileResource(resource, 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 { From 8bb9b23b7987a5775e236b6e5ead81e1c962d2b5 Mon Sep 17 00:00:00 2001 From: mariofusco Date: Tue, 24 Sep 2024 09:35:14 +0200 Subject: [PATCH 3/3] wip --- .../org/drools/decisiontable/DecisionTableProviderImpl.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) 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 0cc404e43cd..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; @@ -44,7 +45,7 @@ public class DecisionTableProviderImpl private static final Logger logger = LoggerFactory.getLogger( DecisionTableProviderImpl.class ); - private Map compiledDtablesCache = new HashMap<>(); + private Map compiledDtablesCache = new ConcurrentHashMap<>(); @Override public String loadFromResource(Resource resource,