Skip to content

Commit

Permalink
1. Removed the inverse relations in the job entities - JobEntity, Job…
Browse files Browse the repository at this point in the history
…RequestEntity, JobExecutionEntity and JobMetadataEntity,. This was done to reduce the SQL calls to get the associated entities. (#408)

2. Modified certain queries to use named queries.
  • Loading branch information
ajoymajumdar authored and tgianos committed Oct 11, 2016
1 parent efe6ec3 commit 580fb9b
Show file tree
Hide file tree
Showing 17 changed files with 144 additions and 132 deletions.
4 changes: 4 additions & 0 deletions genie-client/src/test/resources/application-integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ spring:
hibernate:
ddl-auto: update
naming-strategy: org.hibernate.cfg.ImprovedNamingStrategy
properties:
hibernate:
show_sql: true
type: trace
datasource:
url: jdbc:hsqldb:mem:genie-int-db;shutdown=true
username: SA
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@

import javax.annotation.Nullable;
import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
Expand All @@ -36,6 +35,8 @@
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.MapsId;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToOne;
import javax.persistence.OrderColumn;
import javax.persistence.Table;
Expand All @@ -58,7 +59,17 @@
@Setter
@Entity
@Table(name = "jobs")
@NamedQueries({
@NamedQuery(
name = JobEntity.QUERY_GET_STATUS_BY_ID,
query = "select j.status from JobEntity j where j.id = :id"
)
})
public class JobEntity extends CommonFieldsEntity {
/**
* Query name to get job status.
*/
public static final String QUERY_GET_STATUS_BY_ID = "getStatusById";
/**
* Used as default version when one not entered.
*/
Expand Down Expand Up @@ -111,14 +122,6 @@ public class JobEntity extends CommonFieldsEntity {
@MapsId
private JobRequestEntity request;

@OneToOne(
mappedBy = "job",
fetch = FetchType.LAZY,
cascade = CascadeType.ALL,
orphanRemoval = true
)
private JobExecutionEntity execution;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "cluster_id")
private ClusterEntity cluster;
Expand Down Expand Up @@ -278,20 +281,10 @@ protected JobRequestEntity getRequest() {
*
* @param request The job request. Not null.
*/
protected void setRequest(final JobRequestEntity request) {
public void setRequest(final JobRequestEntity request) {
this.request = request;
}

/**
* Set the job execution for this job.
*
* @param execution The execution. Not null.
*/
public void setExecution(@NotNull(message = "Execution can't be null") final JobExecutionEntity execution) {
this.execution = execution;
execution.setJob(this);
}

/**
* Set the cluster this job ran on.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.MapsId;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
Expand All @@ -49,8 +51,25 @@
@Setter
@Entity
@Table(name = "job_executions")
@NamedQueries({
@NamedQuery(
name = JobExecutionEntity.QUERY_FIND_BY_STATUS_HOST,
query = "select e.job from JobExecutionEntity e where e.job.status in :statuses and e.hostName = :hostName"
),
@NamedQuery(
name = JobExecutionEntity.QUERY_FIND_HOSTS_BY_STATUS,
query = "select distinct e.hostName from JobExecutionEntity e where e.job.status in :statuses"
)
})
public class JobExecutionEntity extends BaseEntity {

/**
* Query name to find jobs by statuses and host.
*/
public static final String QUERY_FIND_BY_STATUS_HOST = "findByStatusHost";
/**
* Query name to find hosts by statuses.
*/
public static final String QUERY_FIND_HOSTS_BY_STATUS = "findHostsByStatus";
private static final long serialVersionUID = -5073493356472801960L;
private static final TimeZone UTC = TimeZone.getTimeZone("UTC");

Expand Down Expand Up @@ -154,7 +173,7 @@ public void setTimeout(@NotNull final Date timeout) {
*
* @param job The job
*/
protected void setJob(final JobEntity job) {
public void setJob(final JobEntity job) {
this.job = job;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,10 @@
import org.hibernate.validator.constraints.NotEmpty;

import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.util.List;
import java.util.Optional;
Expand Down Expand Up @@ -117,22 +113,6 @@ public class JobRequestEntity extends SetupFileEntity {
@Min(value = 1)
private Integer timeout;

@OneToOne(
mappedBy = "request",
fetch = FetchType.LAZY,
cascade = CascadeType.ALL,
orphanRemoval = true
)
private JobEntity job;

@OneToOne(
mappedBy = "request",
fetch = FetchType.LAZY,
cascade = CascadeType.ALL,
orphanRemoval = true
)
private JobMetadataEntity jobMetadata;

/**
* Gets the group name of the user who submitted the job.
*
Expand Down Expand Up @@ -334,35 +314,6 @@ protected void setApplications(final String applications) {
this.applications = applications;
}

/**
* Get the job associated with this job request.
*
* @return The job
*/
public JobEntity getJob() {
return this.job;
}

/**
* Set the job for this request.
*
* @param job The job
*/
public void setJob(@NotNull final JobEntity job) {
this.job = job;
job.setRequest(this);
}

/**
* Set the additional metadata for this request.
*
* @param jobMetadata The metadata
*/
public void setJobMetadata(@NotNull final JobMetadataEntity jobMetadata) {
this.jobMetadata = jobMetadata;
this.jobMetadata.setRequest(this);
}

/**
* Get a DTO representing this job request.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.stereotype.Repository;

import javax.validation.constraints.NotNull;
import java.util.List;

/**
* Job repository.
*
Expand All @@ -28,4 +31,10 @@
*/
@Repository
public interface JpaJobExecutionRepository extends JpaRepository<JobExecutionEntity, String>, JpaSpecificationExecutor {
/**
* Deletes all job executions for the given ids.
* @param ids list of ids for which the job requests should be deleted
* @return no. of executions deleted
*/
Long deleteByIdIn(@NotNull final List<String> ids);
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.stereotype.Repository;

import javax.validation.constraints.NotNull;
import java.util.List;

/**
* Job Metadata repository.
*
Expand All @@ -28,4 +31,10 @@
*/
@Repository
public interface JpaJobMetadataRepository extends JpaRepository<JobMetadataEntity, String>, JpaSpecificationExecutor {
/**
* Deletes all job metadatas for the given ids.
* @param ids list of ids for which the job requests should be deleted
* @return no. of metadatas deleted
*/
Long deleteByIdIn(@NotNull final List<String> ids);
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,20 @@
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.stereotype.Repository;

import javax.validation.constraints.NotNull;
import java.util.List;

/**
* Job repository.
*
* @author tgianos
*/
@Repository
public interface JpaJobRepository extends JpaRepository<JobEntity, String>, JpaSpecificationExecutor {
/**
* Deletes all jobs for the given ids.
* @param ids list of ids for which the jobs should be deleted
* @return no. of jobs deleted
*/
Long deleteByIdIn(@NotNull final List<String> ids);
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

import javax.validation.constraints.NotNull;
import java.util.Date;
import java.util.List;

/**
* Job repository.
Expand All @@ -33,10 +34,17 @@
public interface JpaJobRequestRepository extends JpaRepository<JobRequestEntity, String>, JpaSpecificationExecutor {

/**
* Delete all job requests created before the given date.
* Returns all job requests created before the given date.
*
* @param date The date before which all job requests should be deleted.
* @return The number of deleted records
* @param date The date before which all job requests were created.
* @return List of job requests
*/
Long deleteByCreatedBefore(@NotNull final Date date);
List<JobRequestEntity> findByCreatedBefore(@NotNull final Date date);

/**
* Deletes all job requests for the given ids.
* @param ids list of ids for which the job requests should be deleted
* @return no. of requests deleted
*/
Long deleteByIdIn(@NotNull final List<String> ids);
}

0 comments on commit 580fb9b

Please sign in to comment.