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

[incubator-kie-drools-6088] Performance issue with many kbase models … #6089

Closed
Show file tree
Hide file tree
Changes from all commits
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 @@ -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
Loading