From c89192c004a718761aae00b7cefdc2b9dae5fcdb Mon Sep 17 00:00:00 2001 From: yahavi Date: Sun, 24 Oct 2021 12:46:42 +0300 Subject: [PATCH] Improve performance and memory consumption --- .../ide/common/ci/BuildDependencyTree.java | 2 +- .../jfrog/ide/common/go/GoTreeBuilder.java | 4 +--- .../ide/common/gradle/GradleTreeBuilder.java | 9 ++++---- .../jfrog/ide/common/npm/NpmTreeBuilder.java | 8 ++----- .../ide/common/persistency/ScanCache.java | 23 ++++++++++--------- .../com/jfrog/ide/common/utils/Utils.java | 15 ++++++++---- .../com/jfrog/ide/common/filter/Utils.java | 2 +- 7 files changed, 32 insertions(+), 31 deletions(-) diff --git a/src/main/java/com/jfrog/ide/common/ci/BuildDependencyTree.java b/src/main/java/com/jfrog/ide/common/ci/BuildDependencyTree.java index c74b4774..b8f69023 100644 --- a/src/main/java/com/jfrog/ide/common/ci/BuildDependencyTree.java +++ b/src/main/java/com/jfrog/ide/common/ci/BuildDependencyTree.java @@ -244,7 +244,7 @@ private void populateTreeWithUnknownIssues() { Enumeration bfs = depthFirstEnumeration(); while (bfs.hasMoreElements()) { DependencyTree node = (DependencyTree) bfs.nextElement(); - node.setIssues(Sets.newHashSet(new org.jfrog.build.extractor.scan.Issue("", "", "", "", Severity.Unknown, "", null))); + node.setIssues(Sets.newHashSet(new org.jfrog.build.extractor.scan.Issue("", Severity.Unknown, "", null))); } } diff --git a/src/main/java/com/jfrog/ide/common/go/GoTreeBuilder.java b/src/main/java/com/jfrog/ide/common/go/GoTreeBuilder.java index 3a436a2a..087d8049 100644 --- a/src/main/java/com/jfrog/ide/common/go/GoTreeBuilder.java +++ b/src/main/java/com/jfrog/ide/common/go/GoTreeBuilder.java @@ -128,9 +128,7 @@ private void setGeneralInfo(DependencyTree rootNode) { rootNode.setGeneralInfo(new GeneralInfo() .componentId(rootNode.getUserObject().toString()) .pkgType("go") - .path(projectDir.toString()) - .artifactId(rootNode.getUserObject().toString()) - .version("")); + .path(projectDir.toString())); } /** diff --git a/src/main/java/com/jfrog/ide/common/gradle/GradleTreeBuilder.java b/src/main/java/com/jfrog/ide/common/gradle/GradleTreeBuilder.java index 1c017277..f52b6518 100644 --- a/src/main/java/com/jfrog/ide/common/gradle/GradleTreeBuilder.java +++ b/src/main/java/com/jfrog/ide/common/gradle/GradleTreeBuilder.java @@ -19,6 +19,8 @@ import java.util.Set; import java.util.stream.Collectors; +import static com.jfrog.ide.common.utils.Utils.createComponentId; + /** * Build Gradle dependency tree before the Xray scan. * @@ -97,11 +99,8 @@ private void populateDependencyTree(DependencyTree node, GradleDependencyNode gr } private GeneralInfo createGeneralInfo(GradleDependencyNode node) { - return new GeneralInfo() - .groupId(node.getGroupId()) - .artifactId(node.getArtifactId()) - .version(node.getVersion()) - .pkgType("gradle"); + return new GeneralInfo().pkgType("gradle") + .componentId(createComponentId(node.getGroupId(), node.getArtifactId(), node.getVersion())); } /** diff --git a/src/main/java/com/jfrog/ide/common/npm/NpmTreeBuilder.java b/src/main/java/com/jfrog/ide/common/npm/NpmTreeBuilder.java index 18c4ebd4..e07efac0 100644 --- a/src/main/java/com/jfrog/ide/common/npm/NpmTreeBuilder.java +++ b/src/main/java/com/jfrog/ide/common/npm/NpmTreeBuilder.java @@ -18,6 +18,7 @@ import java.util.Map; import static com.jfrog.ide.common.log.Utils.logError; +import static com.jfrog.ide.common.utils.Utils.createComponentId; /** * Build npm dependency tree before the Xray scan. @@ -144,11 +145,6 @@ private String getPostfix(Log logger, JsonNode npmLsResults, boolean shouldToast } private GeneralInfo createGeneralInfo(String packageName, String packageVersion) { - return new GeneralInfo() - .componentId(packageName + ":" + packageVersion) - .pkgType("npm") - .path(projectDir.toString()) - .artifactId(packageName) - .version(packageVersion); + return new GeneralInfo().path(projectDir.toString()).componentId(createComponentId(packageName, packageVersion)).pkgType("npm"); } } diff --git a/src/main/java/com/jfrog/ide/common/persistency/ScanCache.java b/src/main/java/com/jfrog/ide/common/persistency/ScanCache.java index 41d056b2..74ffcc67 100644 --- a/src/main/java/com/jfrog/ide/common/persistency/ScanCache.java +++ b/src/main/java/com/jfrog/ide/common/persistency/ScanCache.java @@ -12,7 +12,10 @@ import java.io.File; import java.io.IOException; -import java.util.*; +import java.util.ArrayList; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; /** * Cache for Xray scan. @@ -37,13 +40,11 @@ public void add(Artifact artifact) { } public void add(Violation violation, String packageType) { - addComponents(violation.getComponents(), violation.getViolationType(), Severity.valueOf(violation.getSeverity()), - violation.getSummary(), packageType); + addComponents(violation.getComponents(), Severity.valueOf(violation.getSeverity()), violation.getSummary(), packageType); } public void add(Vulnerability vulnerability, String packageType) { - addComponents(vulnerability.getComponents(), "vulnerability", - Severity.valueOf(vulnerability.getSeverity()), vulnerability.getSummary(), packageType); + addComponents(vulnerability.getComponents(), Severity.valueOf(vulnerability.getSeverity()), vulnerability.getSummary(), packageType); } public void add(License license, String packageType, boolean violation) { @@ -65,8 +66,8 @@ public void add(License license, String packageType, boolean violation) { continue; } // If not exist, creates a new data object. - GeneralInfo info = new GeneralInfo(id, "", component.getImpactPaths().get(0).get(0).getFullPath(), packageType); - Artifact artifact = new Artifact(info, new HashSet<>(), new HashSet() {{ + GeneralInfo info = new GeneralInfo(id, component.getImpactPaths().get(0).get(0).getFullPath(), packageType); + Artifact artifact = new Artifact(info, new HashSet<>(), new HashSet<>() {{ add(issue); }}); this.add(artifact); @@ -90,12 +91,12 @@ void setScanCacheMap(ScanCacheMap scanCacheMap) { this.scanCacheMap = scanCacheMap; } - private void addComponents(Map components, String issueType, Severity severity, String summary, String packageType) { + private void addComponents(Map components, Severity severity, String summary, String packageType) { for (Map.Entry entry : components.entrySet()) { String id = entry.getKey(); id = id.substring(id.indexOf("://") + 3); Component component = entry.getValue(); - Issue issue = new Issue("", "", issueType, "", severity, summary, component.getFixedVersions()); + Issue issue = new Issue("", severity, summary, component.getFixedVersions()); if (this.contains(id)) { Artifact artifact = get(id); @@ -107,8 +108,8 @@ private void addComponents(Map components, String i continue; } // If not exist, creates a new data object. - GeneralInfo info = new GeneralInfo(id, "", component.getImpactPaths().get(0).get(0).getFullPath(), packageType); - Artifact artifact = new Artifact(info, new HashSet() {{ + GeneralInfo info = new GeneralInfo(id, component.getImpactPaths().get(0).get(0).getFullPath(), packageType); + Artifact artifact = new Artifact(info, new HashSet<>() {{ add(issue); }}, new HashSet<>()); this.add(artifact); diff --git a/src/main/java/com/jfrog/ide/common/utils/Utils.java b/src/main/java/com/jfrog/ide/common/utils/Utils.java index ed06e311..62b85892 100644 --- a/src/main/java/com/jfrog/ide/common/utils/Utils.java +++ b/src/main/java/com/jfrog/ide/common/utils/Utils.java @@ -17,7 +17,7 @@ import java.util.Set; import java.util.stream.Collectors; -import static com.fasterxml.jackson.annotation.JsonInclude.Include.NON_NULL; +import static com.fasterxml.jackson.annotation.JsonInclude.Include.NON_EMPTY; import static com.fasterxml.jackson.databind.DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES; /** @@ -29,10 +29,18 @@ public class Utils { public static ObjectMapper createMapper() { return new ObjectMapper() .configure(FAIL_ON_UNKNOWN_PROPERTIES, false) - .setSerializationInclusion(NON_NULL) + .setSerializationInclusion(NON_EMPTY) .disable(SerializationFeature.FAIL_ON_EMPTY_BEANS); } + public static String createComponentId(String artifactId, String version) { + return String.join(":", artifactId, version); + } + + public static String createComponentId(String groupId, String artifactId, String version) { + return String.join(":", groupId, artifactId, version); + } + public static String createLicenseString(License license) { if (license.isFullNameEmpty() || StringUtils.isBlank(license.getName())) { return license.getName(); @@ -42,7 +50,6 @@ public static String createLicenseString(License license) { public static GeneralInfo getGeneralInfo(General other) { return new GeneralInfo().componentId(other.getComponentId()) - .name(other.getName()) .path(other.getPath()) .pkgType(other.getPkgType()); } @@ -60,7 +67,7 @@ public static Issue toIssue(com.jfrog.xray.client.services.summary.Issue other) VulnerableComponents vulnerableComponents = vulnerableComponentsList.get(0); fixedVersions = vulnerableComponents.getFixedVersions(); } - return new Issue(other.getCreated(), other.getDescription(), other.getIssueType(), other.getProvider(), severity, other.getSummary(), fixedVersions); + return new Issue(other.getDescription(), severity, other.getSummary(), fixedVersions); } public static Artifact getArtifact(com.jfrog.xray.client.services.summary.Artifact other) { diff --git a/src/test/java/com/jfrog/ide/common/filter/Utils.java b/src/test/java/com/jfrog/ide/common/filter/Utils.java index b631c74b..be40da91 100644 --- a/src/test/java/com/jfrog/ide/common/filter/Utils.java +++ b/src/test/java/com/jfrog/ide/common/filter/Utils.java @@ -20,7 +20,7 @@ public class Utils { * @return the random issue */ static Issue createIssue(Severity severity) { - return new Issue(generateUID(), generateUID(), generateUID(), generateUID(), severity, generateUID(), Lists.newArrayList()); + return new Issue(generateUID(), severity, generateUID(), Lists.newArrayList()); } /**