Skip to content

Commit

Permalink
[incubator-kie-drools-6088] Performance issue with many kbase models …
Browse files Browse the repository at this point in the history
…and many large spreadsheets
  • Loading branch information
tkobayas committed Sep 18, 2024
1 parent d7ca8b3 commit ff8e8cf
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.drools.compiler.kproject.models.KieBaseModelImpl;
import org.drools.compiler.kproject.models.KieModuleModelImpl;
import org.drools.compiler.kproject.models.KieSessionModelImpl;
import org.drools.io.InternalResource;
import org.drools.util.StringUtils;
import org.kie.api.builder.Message;
import org.kie.api.builder.model.KieBaseModel;
Expand Down Expand Up @@ -65,6 +66,8 @@ public abstract class AbstractKieProject implements KieProject {

private final Map<String, KieSessionModel> kSessionModels = new HashMap<>();

private Map<InternalResource, String> packageNameCache = new HashMap<>();

private static final Predicate<String> BUILD_ALL = s -> true;

public ResultsImpl verify() {
Expand All @@ -83,6 +86,7 @@ public void verify(BuildContext buildContext) {
for ( KieBaseModel model : kBaseModels.values() ) {
buildKnowledgePackages((KieBaseModelImpl) model, buildContext);
}
packageNameCache.clear();
}

private void verify(String[] kBaseNames, BuildContext buildContext) {
Expand All @@ -93,6 +97,7 @@ private void verify(String[] kBaseNames, BuildContext buildContext) {
}
buildKnowledgePackages( kieBaseModel, buildContext );
}
packageNameCache.clear();
}

public KieBaseModel getDefaultKieBaseModel() {
Expand Down Expand Up @@ -331,7 +336,7 @@ private void addFiles( Predicate<String> buildFilter, Set<Asset> assets, KieBase
InternalKieModule kieModule, boolean useFolders) {
for (String fileName : kieModule.getFileNames()) {
if (buildFilter.test( fileName ) && !fileName.startsWith(".") && !fileName.endsWith(".properties") &&
filterFileInKBase(kieModule, kieBaseModel, fileName, () -> kieModule.getResource( fileName ), useFolders)) {
filterFileInKBase(kieModule, kieBaseModel, fileName, () -> kieModule.getResource( fileName ), useFolders, packageNameCache)) {
assets.add(new Asset( kieModule, fileName ));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Enumeration;
import java.util.List;
import java.util.Map;
import java.util.function.BiFunction;
import java.util.function.Predicate;
import java.util.function.Supplier;
Expand Down Expand Up @@ -363,15 +365,19 @@ private static ResourceType getResourceType( ResourceConfiguration conf ) {
}

public static boolean filterFileInKBase( InternalKieModule kieModule, KieBaseModel kieBase, String fileName, Supplier<InternalResource> file, boolean useFolders ) {
return isFileInKieBase( kieBase, fileName, file, useFolders ) &&
return filterFileInKBase(kieModule, kieBase, fileName, file, useFolders, null);
}

public static boolean filterFileInKBase( InternalKieModule kieModule, KieBaseModel kieBase, String fileName, Supplier<InternalResource> file, boolean useFolders, Map<InternalResource, String> packageNameCache) {
return isFileInKieBase( kieBase, fileName, file, useFolders, packageNameCache ) &&
( isKieExtension( fileName ) || getResourceType( kieModule, fileName ) != null );
}

private static boolean isKieExtension(String fileName) {
return !isJavaSourceFile( fileName ) && ResourceType.determineResourceType(fileName) != null;
}

private static boolean isFileInKieBase( KieBaseModel kieBase, String fileName, Supplier<InternalResource> file, boolean useFolders ) {
private static boolean isFileInKieBase( KieBaseModel kieBase, String fileName, Supplier<InternalResource> file, boolean useFolders, Map<InternalResource, String> packageNameCache ) {
int lastSep = fileName.lastIndexOf( "/" );
if ( lastSep + 1 < fileName.length() && fileName.charAt( lastSep + 1 ) == '.' ) {
// skip dot files
Expand All @@ -381,15 +387,15 @@ private static boolean isFileInKieBase( KieBaseModel kieBase, String fileName, S
return true;
} else {
String folderNameForFile = lastSep > 0 ? fileName.substring( 0, lastSep ) : "";
String pkgNameForFile = packageNameForFile( fileName, folderNameForFile, !useFolders, file );
String pkgNameForFile = packageNameForFile( fileName, folderNameForFile, !useFolders, file, packageNameCache );
return isPackageInKieBase( kieBase, pkgNameForFile );
}
}

private static String packageNameForFile( String fileName, String folderNameForFile, boolean discoverPackage, Supplier<InternalResource> file ) {
private static String packageNameForFile( String fileName, String folderNameForFile, boolean discoverPackage, Supplier<InternalResource> file, Map<InternalResource, String> packageNameCache ) {
String packageNameFromFolder = getRelativePackageName(folderNameForFile.replace( '/', '.' ));
if (discoverPackage) {
String packageNameForFile = packageNameFromAsset(fileName, file.get());
String packageNameForFile = packageNameFromAsset(fileName, file.get(), packageNameCache);
if (packageNameForFile != null) {
packageNameForFile = getRelativePackageName( packageNameForFile );
if ( !packageNameForFile.equals( packageNameFromFolder ) ) {
Expand All @@ -403,10 +409,18 @@ private static String packageNameForFile( String fileName, String folderNameForF
return packageNameFromFolder;
}

private static String packageNameFromAsset(String fileName, InternalResource file) {
private static String packageNameFromAsset(String fileName, InternalResource file, Map<InternalResource, String> packageNameCache) {
if (file == null) {
return null;
}
if (packageNameCache != null) {
return packageNameCache.computeIfAbsent(file, f -> packageNameFromAsset(fileName, f));
} else {
return packageNameFromAsset(fileName, file);
}
}

private static String packageNameFromAsset(String fileName, InternalResource file) {
if (fileName.endsWith( ".drl" )) {
return packageNameFromDrl( new String(file.getBytes()) );
}
Expand Down

0 comments on commit ff8e8cf

Please sign in to comment.