Removed deprecated GridFS column from repository (#419)

* Merged artifact sha1 hash and gridfs column.

Signed-off-by: kaizimmerm <kai.zimmermann@bosch-si.com>

* Renamed exception to get rid of old GridFS name.

Signed-off-by: kaizimmerm <kai.zimmermann@bosch-si.com>

* Added test description.

Signed-off-by: kaizimmerm <kai.zimmermann@bosch-si.com>

* Fix typo.

Signed-off-by: kaizimmerm <kai.zimmermann@bosch-si.com>
This commit is contained in:
Kai Zimmermann
2017-01-19 08:43:04 +01:00
committed by GitHub
parent c091342012
commit 9d0a064912
21 changed files with 156 additions and 125 deletions

View File

@@ -18,11 +18,11 @@ import org.eclipse.hawkbit.artifact.repository.HashNotMatchException;
import org.eclipse.hawkbit.artifact.repository.model.DbArtifact;
import org.eclipse.hawkbit.artifact.repository.model.DbArtifactHash;
import org.eclipse.hawkbit.repository.ArtifactManagement;
import org.eclipse.hawkbit.repository.exception.ArtifactBinaryNotFoundException;
import org.eclipse.hawkbit.repository.exception.ArtifactDeleteFailedException;
import org.eclipse.hawkbit.repository.exception.ArtifactUploadFailedException;
import org.eclipse.hawkbit.repository.exception.EntityAlreadyExistsException;
import org.eclipse.hawkbit.repository.exception.EntityNotFoundException;
import org.eclipse.hawkbit.repository.exception.GridFSDBFileNotFoundException;
import org.eclipse.hawkbit.repository.exception.InvalidMD5HashException;
import org.eclipse.hawkbit.repository.exception.InvalidSHA1HashException;
import org.eclipse.hawkbit.repository.jpa.model.JpaArtifact;
@@ -112,7 +112,7 @@ public class JpaArtifactManagement implements ArtifactManagement {
private boolean clearArtifactBinary(final JpaArtifact existing) {
for (final Artifact lArtifact : localArtifactRepository.findByGridFsFileName(existing.getGridFsFileName())) {
for (final Artifact lArtifact : localArtifactRepository.findBySha1Hash(existing.getSha1Hash())) {
if (!lArtifact.getSoftwareModule().isDeleted()
&& Long.compare(lArtifact.getSoftwareModule().getId(), existing.getSoftwareModule().getId()) != 0) {
return false;
@@ -120,8 +120,8 @@ public class JpaArtifactManagement implements ArtifactManagement {
}
try {
LOG.debug("deleting artifact from repository {}", existing.getGridFsFileName());
artifactRepository.deleteBySha1(existing.getGridFsFileName());
LOG.debug("deleting artifact from repository {}", existing.getSha1Hash());
artifactRepository.deleteBySha1(existing.getSha1Hash());
return true;
} catch (final ArtifactStoreException e) {
throw new ArtifactDeleteFailedException(e);
@@ -156,8 +156,8 @@ public class JpaArtifactManagement implements ArtifactManagement {
}
@Override
public Artifact findFirstArtifactBySHA1(final String sha1) {
return localArtifactRepository.findFirstByGridFsFileName(sha1);
public Artifact findFirstArtifactBySHA1(final String sha1Hash) {
return localArtifactRepository.findFirstBySha1Hash(sha1Hash);
}
@Override
@@ -171,16 +171,9 @@ public class JpaArtifactManagement implements ArtifactManagement {
}
@Override
public DbArtifact loadArtifactBinary(final Long artifactId) {
final JpaArtifact artifact = Optional.ofNullable(localArtifactRepository.findOne(artifactId))
.orElseThrow(() -> new EntityNotFoundException("Artifact with given id " + artifactId + " not found."));
final DbArtifact result = artifactRepository.getArtifactBySha1(artifact.getGridFsFileName());
if (result == null) {
throw new GridFSDBFileNotFoundException(artifact.getGridFsFileName());
}
return result;
public DbArtifact loadArtifactBinary(final String sha1Hash) {
return Optional.ofNullable(artifactRepository.getArtifactBySha1(sha1Hash))
.orElseThrow(() -> new ArtifactBinaryNotFoundException(sha1Hash));
}
private Artifact storeArtifactMetadata(final SoftwareModule softwareModule, final String providedFilename,

View File

@@ -156,21 +156,21 @@ public class JpaControllerManagement implements ControllerManagement {
}
@Override
public boolean hasTargetArtifactAssigned(final String controllerId, final Long localArtifact) {
public boolean hasTargetArtifactAssigned(final String controllerId, final String sha1Hash) {
final Target target = targetRepository.findByControllerId(controllerId);
if (target == null) {
return false;
}
return actionRepository.count(ActionSpecifications.hasTargetAssignedArtifact(target, localArtifact)) > 0;
return actionRepository.count(ActionSpecifications.hasTargetAssignedArtifact(target, sha1Hash)) > 0;
}
@Override
public boolean hasTargetArtifactAssigned(final Long targetId, final Long localArtifact) {
public boolean hasTargetArtifactAssigned(final Long targetId, final String sha1Hash) {
final Target target = targetRepository.findOne(targetId);
if (target == null) {
return false;
}
return actionRepository.count(ActionSpecifications.hasTargetAssignedArtifact(target, localArtifact)) > 0;
return actionRepository.count(ActionSpecifications.hasTargetAssignedArtifact(target, sha1Hash)) > 0;
}
@Override

View File

@@ -48,20 +48,20 @@ public interface LocalArtifactRepository extends BaseEntityRepository<JpaArtifac
/**
* Searches for a {@link Artifact} based on given gridFsFileName.
*
* @param gridFsFileName
* @param sha1Hash
* to search
* @return list of {@link Artifact}s.
*/
List<Artifact> findByGridFsFileName(String gridFsFileName);
List<Artifact> findBySha1Hash(String sha1Hash);
/**
* Searches for a {@link Artifact} based on given gridFsFileName.
*
* @param gridFsFileName
* @param sha1Hash
* to search
* @return {@link Artifact} the first in the result list
*/
JpaArtifact findFirstByGridFsFileName(String gridFsFileName);
JpaArtifact findFirstBySha1Hash(String sha1Hash);
/**
* Searches for a {@link Artifact} based user provided filename at upload.

View File

@@ -30,6 +30,7 @@ import org.hibernate.validator.constraints.NotEmpty;
*
*/
@Table(name = "sp_artifact", indexes = { @Index(name = "sp_idx_artifact_01", columnList = "tenant,software_module"),
@Index(name = "sp_idx_artifact_02", columnList = "tenant,sha1_hash"),
@Index(name = "sp_idx_artifact_prim", columnList = "tenant,id") })
@Entity
// exception squid:S2160 - BaseEntity equals/hashcode is handling correctly for
@@ -38,10 +39,10 @@ import org.hibernate.validator.constraints.NotEmpty;
public class JpaArtifact extends AbstractJpaTenantAwareBaseEntity implements Artifact {
private static final long serialVersionUID = 1L;
@Column(name = "gridfs_file_name", length = 40)
@Column(name = "sha1_hash", length = 40, nullable = false, updatable = false)
@Size(max = 40)
@NotEmpty
private String gridFsFileName;
private String sha1Hash;
@Column(name = "provided_file_name", length = 256)
@Size(max = 256)
@@ -52,9 +53,6 @@ public class JpaArtifact extends AbstractJpaTenantAwareBaseEntity implements Art
@JoinColumn(name = "software_module", nullable = false, updatable = false, foreignKey = @ForeignKey(value = ConstraintMode.CONSTRAINT, name = "fk_assigned_sm"))
private JpaSoftwareModule softwareModule;
@Column(name = "sha1_hash", length = 40, nullable = true)
private String sha1Hash;
@Column(name = "md5_hash", length = 32, nullable = true)
private String md5Hash;
@@ -71,17 +69,17 @@ public class JpaArtifact extends AbstractJpaTenantAwareBaseEntity implements Art
/**
* Constructs artifact.
*
* @param gridFsFileName
* @param sha1Hash
* that is the link to the {@link DbArtifact} entity.
* @param filename
* that is used by {@link DbArtifact} store.
* @param softwareModule
* of this artifact
*/
public JpaArtifact(@NotNull final String gridFsFileName, @NotNull final String filename,
public JpaArtifact(@NotEmpty final String sha1Hash, @NotNull final String filename,
final SoftwareModule softwareModule) {
setSoftwareModule(softwareModule);
this.gridFsFileName = gridFsFileName;
this.sha1Hash = sha1Hash;
this.filename = filename;
}
@@ -122,10 +120,6 @@ public class JpaArtifact extends AbstractJpaTenantAwareBaseEntity implements Art
this.softwareModule.addArtifact(this);
}
public String getGridFsFileName() {
return gridFsFileName;
}
@Override
public String getFilename() {
return filename;

View File

@@ -43,18 +43,18 @@ public final class ActionSpecifications {
* @param target
* the target to verfiy if the given artifact is currently
* assigned or had been assigned
* @param localArtifact
* the local artifact to check wherever the target had ever been
* assigned
* @param sha1Hash
* of the local artifact to check wherever the target had ever
* been assigned
* @return a specification to use with spring JPA
*/
public static Specification<JpaAction> hasTargetAssignedArtifact(final Target target, final Long localArtifact) {
public static Specification<JpaAction> hasTargetAssignedArtifact(final Target target, final String sha1Hash) {
return (actionRoot, query, criteriaBuilder) -> {
final Join<JpaAction, JpaDistributionSet> dsJoin = actionRoot.join(JpaAction_.distributionSet);
final SetJoin<JpaDistributionSet, JpaSoftwareModule> modulesJoin = dsJoin.join(JpaDistributionSet_.modules);
final ListJoin<JpaSoftwareModule, JpaArtifact> artifactsJoin = modulesJoin
.join(JpaSoftwareModule_.artifacts);
return criteriaBuilder.and(criteriaBuilder.equal(artifactsJoin.get(JpaArtifact_.id), localArtifact),
return criteriaBuilder.and(criteriaBuilder.equal(artifactsJoin.get(JpaArtifact_.sha1Hash), sha1Hash),
criteriaBuilder.equal(actionRoot.get(JpaAction_.target), target));
};
}

View File

@@ -0,0 +1,3 @@
ALTER TABLE sp_artifact DROP COLUMN sha1_hash;
ALTER TABLE sp_artifact CHANGE gridfs_file_name sha1_hash varchar(40) not null;
CREATE INDEX sp_idx_artifact_02 ON sp_artifact (tenant, sha1_hash);

View File

@@ -0,0 +1,3 @@
ALTER TABLE sp_artifact DROP COLUMN sha1_hash;
ALTER TABLE sp_artifact CHANGE gridfs_file_name sha1_hash varchar(40) not null;
CREATE INDEX sp_idx_artifact_02 ON sp_artifact (tenant, sha1_hash);