Skip to content

Commit

Permalink
Cache compiled dtables during the building of a KieProject (apache#6097)
Browse files Browse the repository at this point in the history
* Cache compiled dtables during the building of a KieProject

* wip

* wip
  • Loading branch information
mariofusco committed Sep 24, 2024
1 parent 4675dab commit 011e17a
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,12 @@ private static void buildKieProject( BuildContext buildContext,
kieRepository.addKieModule( kDep );
}
}

clearBuilderCache();
}

private static void clearBuilderCache() {
DecisionTableFactory.clearCompilerCache();
}

private void addKBasesFilesToTrg() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -42,16 +43,16 @@ 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<String, String> compiledDtablesCache = new ConcurrentHashMap<>();

@Override
public String loadFromResource(Resource resource,
DecisionTableConfiguration configuration) {

try {
return compileResource( resource, configuration );
} catch (IOException e) {
throw new UncheckedIOException( e );
} catch (Exception e) {
throw new DecisionTableParseException(resource, e);
}
Expand All @@ -78,29 +79,46 @@ public List<String> 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<String, List<String[]>> loadPropertiesFromFile(File file, DecisionTableConfiguration configuration) {
switch (configuration.getInputType()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ public static List<String> 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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@

public interface DecisionTableProvider extends KieService {

String loadFromResource(Resource resource,
DecisionTableConfiguration configuration);
String loadFromResource(Resource resource, DecisionTableConfiguration configuration);

List<String> loadFromInputStreamWithTemplates(Resource resource,
DecisionTableConfiguration configuration);
List<String> loadFromInputStreamWithTemplates(Resource resource, DecisionTableConfiguration configuration);

Map<String,List<String[]>> loadPropertiesFromFile(File file, DecisionTableConfiguration configuration);

Map<String,List<String[]>> loadPropertiesFromInputStream(InputStream inputStream, DecisionTableConfiguration configuration);

void clearCompilerCache();
}

0 comments on commit 011e17a

Please sign in to comment.