Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cache compiled dtables during the building of a KieProject #6097

Merged
merged 3 commits into from
Sep 24, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In general I believe that there could be other things that we may want to cache when building a KieProject, so we could eventually perform the clean up of all those caches here.

}

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<>();
Copy link
Contributor

@tkobayas tkobayas Sep 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you, @mariofusco . Shouldn't this be ConcurrentHashMap because DecisionTableProviderImpl is a singleton in DecisionTableFactory?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess you're right. In reality I was thinking to it yesterday when I wrote that code, and I decided that we don't have any situation when we do 2 dtable -> drl conversions in parallel, but I was not properly considering that this is a singleton indeed. Better to play on the safe side on this. I will turn it into a ConcurrentHashMap, thanks.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tkobayas done.


@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();
}
Loading