Skip to content

Commit

Permalink
Cache compiled dtables during the building of a KieProject
Browse files Browse the repository at this point in the history
  • Loading branch information
mariofusco committed Sep 23, 2024
1 parent 1a0f74f commit cf3116a
Show file tree
Hide file tree
Showing 4 changed files with 43 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 @@ -42,16 +42,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 HashMap<>();

@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 +78,45 @@ public List<String> 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<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 cf3116a

Please sign in to comment.