Skip to content

Commit

Permalink
Merge pull request #392 from OpenElements/jpametamodel
Browse files Browse the repository at this point in the history
Provide JPA metamodel for better queries
  • Loading branch information
Ndacyayisenga-droid authored Jun 4, 2024
2 parents 63a1eca + c2e017f commit 1e5d1ff
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 56 deletions.
7 changes: 7 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,13 @@
<compilerArgs>
<arg>-parameters</arg>
</compilerArgs>
<annotationProcessorPaths>
<path>
<groupId>org.hibernate.orm</groupId>
<artifactId>hibernate-jpamodelgen</artifactId>
<version>6.5.2.Final</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
<plugin>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package com.openelements.benchscape.server.store.entities;

import com.openelements.benchscape.jmh.model.BenchmarkUnit;
import com.openelements.server.base.data.AbstractEntity;
import com.openelements.server.base.tenantdata.AbstractEntityWithTenant;
import jakarta.persistence.CascadeType;
import jakarta.persistence.CollectionTable;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
package com.openelements.benchscape.server.store.repositories;

import com.openelements.benchscape.server.store.data.EnvironmentQuery;
import com.openelements.benchscape.server.store.data.OperationSystem;
import com.openelements.benchscape.server.store.data.SystemMemory;
import com.openelements.benchscape.server.store.entities.EnvironmentEntity;
import com.openelements.benchscape.server.store.entities.EnvironmentEntity_;
import com.openelements.server.base.tenantdata.EntityWithTenantRepository;
import edu.umd.cs.findbugs.annotations.NonNull;
import jakarta.persistence.criteria.Predicate;
import java.util.ArrayList;
import java.util.List;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

@Repository
public interface EnvironmentRepository extends EntityWithTenantRepository<EnvironmentEntity>,
JpaSpecificationExecutor<EnvironmentEntity> {

default List<EnvironmentEntity> findFilteredEnvironments(String name, String gitOriginUrl, String gitBranch, String systemArch,
Integer systemProcessors, Integer systemProcessorsMin,
Integer systemProcessorsMax, OperationSystem osFamily, String osName, String osVersion, String jvmVersion,
Integer systemProcessorsMax, OperationSystem osFamily,
String osName, String osVersion, String jvmVersion,
String jvmName, String jmhVersion) {
return findAll(createSpecificationForQuery(name, gitOriginUrl, gitBranch, systemArch,
systemProcessors, systemProcessorsMin, systemProcessorsMax,
Expand All @@ -35,71 +35,53 @@ private static Specification<EnvironmentEntity> createSpecificationForQuery(Stri
return (root, criteriaQuery, criteriaBuilder) -> {
List<Predicate> predicates = new ArrayList<>();

final String SYSTEM_ARCH_QUERY_FIELD = "systemArch";
final String GIT_ORIGIN_URL_QUERY_FIELD = "gitOriginUrl";
final String GIT_BRANCH_QUERY_FIELD = "gitBranch";
final String SYSTEM_PROCESSORS_QUERY_FIELD = "systemProcessors";
final String SYSTEM_PROCESSORS_MIN_QUERY_FIELD = "systemProcessorsMin";
final String SYSTEM_PROCESSORS_MAX_QUERY_FIELD = "systemProcessorsMax";
final String SYSTEM_MEMORY_QUERY_FIELD = "systemMemory";
final String SYSTEM_MEMORY_MIN_QUERY_FIELD = "systemMemoryMin";
final String SYSTEM_MEMORY_MAX_QUERY_FIELD = "systemMemoryMax";
final String OS_NAME_QUERY_FIELD = "osName";
final String OS_VERSION_QUERY_FIELD = "osVersion";
final String JVM_VERSION_QUERY_FIELD = "jvmVersion";
final String JVM_NAME_QUERY_FIELD = "jvmName";
final String OS_FAMILY_QUERY_FIELD = "osFamily";
final String JMH_VERSION_QUERY_FIELD = "jmhVersion";

if (name != null) {
predicates.add(criteriaBuilder.like(criteriaBuilder.lower(root.get("name")), "%" + name.toLowerCase() + "%"));
predicates.add(criteriaBuilder.like(criteriaBuilder.lower(root.get(EnvironmentEntity_.name)), "%" +
name.toLowerCase() + "%"));
}
if (gitOriginUrl != null) {
predicates.add(criteriaBuilder.like(criteriaBuilder.lower(root.get(GIT_ORIGIN_URL_QUERY_FIELD)), "%" + gitOriginUrl.toLowerCase() + "%"));
predicates.add(criteriaBuilder.like(criteriaBuilder.lower(root.get(EnvironmentEntity_.gitOriginUrl)), "%" +
gitOriginUrl.toLowerCase() + "%"));
}
if (gitBranch != null) {
predicates.add(criteriaBuilder.equal(criteriaBuilder.lower(root.get(GIT_BRANCH_QUERY_FIELD)), gitBranch.toLowerCase()));
predicates.add(criteriaBuilder.equal(criteriaBuilder.lower(root.get(EnvironmentEntity_.gitBranch)),
gitBranch.toLowerCase()));
}
if (systemArch != null) {
predicates.add(criteriaBuilder.equal(criteriaBuilder.lower(root.get(SYSTEM_ARCH_QUERY_FIELD)), systemArch.toLowerCase()));
predicates.add(criteriaBuilder.equal(criteriaBuilder.lower(root.get(EnvironmentEntity_.systemArch)),
systemArch.toLowerCase()));
}
if (systemProcessors != null) {
predicates.add(criteriaBuilder.equal(root.get(SYSTEM_PROCESSORS_QUERY_FIELD), systemProcessors));
predicates.add(criteriaBuilder.equal(root.get(EnvironmentEntity_.systemProcessors), systemProcessors));
}
if (systemProcessorsMin != null) {
predicates.add(criteriaBuilder.greaterThanOrEqualTo(root.get(SYSTEM_PROCESSORS_MIN_QUERY_FIELD), systemProcessorsMin));
predicates.add(criteriaBuilder.greaterThanOrEqualTo(root.get(EnvironmentEntity_.systemProcessors), systemProcessorsMin));
}
if (systemProcessorsMax != null) {
predicates.add(criteriaBuilder.lessThanOrEqualTo(root.get(SYSTEM_PROCESSORS_MAX_QUERY_FIELD), systemProcessorsMax));
predicates.add(criteriaBuilder.lessThanOrEqualTo(root.get(EnvironmentEntity_.systemProcessors), systemProcessorsMax));
}
// if (systemMemory != null) {
// predicates.add(criteriaBuilder.equal(root.get(SYSTEM_MEMORY_QUERY_FIELD), SystemMemory.getToByteConverter().apply(systemMemory)));
// }
// if (systemMemoryMin != null) {
// predicates.add(criteriaBuilder.greaterThanOrEqualTo(root.get(SYSTEM_MEMORY_MIN_QUERY_FIELD),
// SystemMemory.getToByteConverter().apply(systemMemoryMin)));
// }
// if (systemMemoryMax != null) {
// predicates.add(criteriaBuilder.lessThanOrEqualTo(root.get(SYSTEM_MEMORY_MAX_QUERY_FIELD),
// SystemMemory.getToByteConverter().apply(systemMemoryMax)));
// }
if (osFamily != null) {
predicates.add(criteriaBuilder.equal(root.get(OS_FAMILY_QUERY_FIELD), osFamily));
predicates.add(criteriaBuilder.equal(root.get(EnvironmentEntity_.osFamily), osFamily));
}
if (osName != null) {
predicates.add(criteriaBuilder.like(criteriaBuilder.lower(root.get(OS_NAME_QUERY_FIELD)), "%" + osName.toLowerCase() + "%"));
predicates.add(criteriaBuilder.like(criteriaBuilder.lower(root.get(EnvironmentEntity_.osName)), "%" +
osName.toLowerCase() + "%"));
}
if (osVersion != null) {
predicates.add(criteriaBuilder.like(criteriaBuilder.lower(root.get(OS_VERSION_QUERY_FIELD)), "%" + osVersion.toLowerCase() + "%"));
predicates.add(criteriaBuilder.like(criteriaBuilder.lower(root.get(EnvironmentEntity_.osVersion)), "%" +
osVersion.toLowerCase() + "%"));
}
if (jvmVersion != null) {
predicates.add(criteriaBuilder.like(criteriaBuilder.lower(root.get(JVM_VERSION_QUERY_FIELD)), "%" + jvmVersion.toLowerCase() + "%"));
predicates.add(criteriaBuilder.like(criteriaBuilder.lower(root.get(EnvironmentEntity_.jvmVersion)), "%" +
jvmVersion.toLowerCase() + "%"));
}
if (jvmName != null) {
predicates.add(criteriaBuilder.like(criteriaBuilder.lower(root.get(JVM_NAME_QUERY_FIELD)), "%" + jvmName.toLowerCase() + "%"));
predicates.add(criteriaBuilder.like(criteriaBuilder.lower(root.get(EnvironmentEntity_.jvmName)), "%" +
jvmName.toLowerCase() + "%"));
}
if (jmhVersion != null) {
predicates.add(criteriaBuilder.like(criteriaBuilder.lower(root.get(JMH_VERSION_QUERY_FIELD)), "%" + jmhVersion.toLowerCase() + "%"));
predicates.add(criteriaBuilder.like(criteriaBuilder.lower(root.get(EnvironmentEntity_.jmhVersion)), "%" +
jmhVersion.toLowerCase() + "%"));
}
return criteriaBuilder.and(predicates.toArray(new Predicate[0]));
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.openelements.benchscape.server.store.data.MeasurementQuery;
import com.openelements.benchscape.server.store.entities.MeasurementEntity;
import com.openelements.benchscape.server.store.entities.MeasurementEntity_;
import org.springframework.data.jpa.domain.Specification;
import jakarta.persistence.criteria.Predicate;
import java.util.ArrayList;
Expand All @@ -14,20 +15,16 @@ public static Specification<MeasurementEntity> createSpecification(MeasurementQu
return (root, query1, cb) -> {
List<Predicate> predicates = new ArrayList<>();

predicates.add(cb.equal(root.get("benchmarkId"), UUID.fromString(query.benchmarkId())));
predicates.add(cb.equal(root.get(MeasurementEntity_.benchmarkId), UUID.fromString(query.benchmarkId())));

predicates.add(cb.between(root.get("timestamp"), query.start(), query.end()));

if (!query.environmentIds().isEmpty()) {
predicates.add(root.get("environmentId").in(query.environmentIds()));
}
predicates.add(cb.between(root.get(MeasurementEntity_.timestamp), query.start(), query.end()));

if (query.gitOriginUrl() != null) {
predicates.add(cb.equal(root.get("metadata").get("gitOriginUrl"), query.gitOriginUrl()));
predicates.add(cb.equal(root.get(MeasurementEntity_.metadata).get("gitOriginUrl"), query.gitOriginUrl()));
}

if (query.gitBranch() != null) {
predicates.add(cb.equal(root.get("metadata").get("gitBranch"), query.gitBranch()));
predicates.add(cb.equal(root.get(MeasurementEntity_.metadata).get("gitBranch"), query.gitBranch()));
}

return cb.and(predicates.toArray(new Predicate[0]));
Expand Down

0 comments on commit 1e5d1ff

Please sign in to comment.