Skip to content

Commit

Permalink
fix bug: change sql-query in localArtifactRepository (#820)
Browse files Browse the repository at this point in the history
Also-by: Nazife Basbaz <[email protected]>
Signed-off-by: Ahmed Sayed <[email protected]>
  • Loading branch information
nazifebasbaz authored and Jeroen Laverman committed Jun 5, 2019
1 parent b8ca7d2 commit fde0cbd
Show file tree
Hide file tree
Showing 31 changed files with 237 additions and 136 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -106,4 +106,8 @@ public void deleteByTenant(final String tenant) {
FileUtils.deleteQuietly(Paths.get(artifactResourceProperties.getPath(), sanitizeTenant(tenant)).toFile());
}

@Override
public boolean existsByTenantAndSha1(final String tenant, final String sha1) {
return getFile(tenant, sha1).exists();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,5 @@ public enum ApiType {
/**
* Support for Direct Device Integration API.
*/
DDI;
DDI
}
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,16 @@ AbstractDbArtifact store(@NotEmpty String tenant, @NotNull InputStream content,
* to erase
*/
void deleteByTenant(@NotEmpty String tenant);

/**
* Checks if an artifact exists for a given tenant by its sha1 hash
*
* @param tenant
* the tenant
* @param sha1Hash
* the sha1-hash of the file to lookup.
*
* @return the boolean whether the atrifact exists or not
*/
boolean existsByTenantAndSha1(@NotEmpty String tenant, @NotEmpty String sha1Hash);
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,6 @@ public enum EventTopic {
/**
* Topic to send multiple actions to the device.
*/
MULTI_ACTION;
MULTI_ACTION

}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,6 @@ public enum MessageType {
/**
* DMF receiver health check reponse type.
*/
PING_RESPONSE;
PING_RESPONSE

}
Original file line number Diff line number Diff line change
Expand Up @@ -64,5 +64,5 @@ public enum DmfActionStatus {
/**
* Action has been downloaded for this target.
*/
DOWNLOADED;
DOWNLOADED
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,6 @@ public enum DmfUpdateMode {
/**
* Removal update strategy
*/
REMOVE;
REMOVE

}
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public interface ArtifactManagement {
* @param artifactSha1Hash
* no longer needed
* @param moduleId
* the garbage colelction call is made for
* the garbage collection call is made for
*
* @return <code>true</code> if an binary was actually garbage collected
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ default boolean isForced() {
* Status enum declaration!
*
*/
public enum Status {
enum Status {
/**
* Action is finished successfully for this target.
*/
Expand Down Expand Up @@ -206,14 +206,14 @@ public enum Status {
* Action has been downloaded by the target and waiting for update to
* start.
*/
DOWNLOADED;
DOWNLOADED
}

/**
* The action type for this action relation.
*
*/
public enum ActionType {
enum ActionType {
/**
* Forced action execution. Target is advised to executed immediately.
*/
Expand All @@ -234,7 +234,7 @@ public enum ActionType {
/**
* Target is only advised to download, but not install
*/
DOWNLOAD_ONLY;
DOWNLOAD_ONLY
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public interface Rollout extends NamedEntity {
* State machine for rollout.
*
*/
public enum RolloutStatus {
enum RolloutStatus {

/**
* Rollouts is being created.
Expand Down Expand Up @@ -181,7 +181,7 @@ public enum RolloutStatus {
* @deprecated legacy status is not used anymore
*/
@Deprecated
ERROR_STARTING;
ERROR_STARTING
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,5 @@ public enum TargetUpdateStatus {
/**
* Controller registered at SP but no {@link DistributionSet} assigned.
*/
REGISTERED;
REGISTERED
}
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ private static EventType readClassHeader(final byte[] typeInformation) {
}

private static byte[] writeContent(final Object payload) {
final Class<? extends Object> serializeClass = payload.getClass();
final Class<?> serializeClass = payload.getClass();
@SuppressWarnings("unchecked")
final Schema<Object> schema = (Schema<Object>) RuntimeSchema.getSchema(serializeClass);
final LinkedBuffer buffer = LinkedBuffer.allocate();
Expand All @@ -137,7 +137,7 @@ private static byte[] writeClassHeader(final Class<?> clazz) {
}
@SuppressWarnings("unchecked")
final Schema<Object> schema = (Schema<Object>) RuntimeSchema
.getSchema((Class<? extends Object>) EventType.class);
.getSchema((Class<?>) EventType.class);
final LinkedBuffer buffer = LinkedBuffer.allocate();
return ProtobufIOUtil.toByteArray(clazzEventType, schema, buffer);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import org.springframework.retry.annotation.Backoff;
import org.springframework.retry.annotation.Retryable;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;

/**
Expand Down Expand Up @@ -98,20 +99,35 @@ private static Artifact checkForExistingArtifact(final String filename, final bo
public Artifact create(final ArtifactUpload artifactUpload) {
final String filename = artifactUpload.getFilename();
final long moduleId = artifactUpload.getModuleId();
AbstractDbArtifact result = null;

final SoftwareModule softwareModule = getModuleAndThrowExceptionIfThatFails(moduleId);

final Artifact existing = checkForExistingArtifact(filename, artifactUpload.overrideExisting(),
softwareModule);
final Artifact existing = checkForExistingArtifact(filename, artifactUpload.overrideExisting(), softwareModule);

assertArtifactQuota(moduleId, 1);
assertMaxArtifactSizeQuota(filename, moduleId, artifactUpload.getFilesize());
assertMaxArtifactStorageQuota(filename, artifactUpload.getFilesize());

return getOrCreateArtifact(artifactUpload)
.map(artifact -> storeArtifactMetadata(softwareModule, filename, artifact, existing))
.orElse(null);
}

private Optional<AbstractDbArtifact> getOrCreateArtifact(final ArtifactUpload artifactUpload) {
final String providedSha1Sum = artifactUpload.getProvidedSha1Sum();
AbstractDbArtifact artifact = null;

if (!StringUtils.isEmpty(providedSha1Sum)) {
artifact = artifactRepository.getArtifactBySha1(tenantAware.getCurrentTenant(), providedSha1Sum);
}
artifact = (artifact == null) ? storeArtifact(artifactUpload) : artifact;
return Optional.ofNullable(artifact);
}

private AbstractDbArtifact storeArtifact(final ArtifactUpload artifactUpload) {
try {
result = artifactRepository.store(tenantAware.getCurrentTenant(), artifactUpload.getInputStream(), filename,
artifactUpload.getContentType(),
return artifactRepository.store(tenantAware.getCurrentTenant(), artifactUpload.getInputStream(),
artifactUpload.getFilename(), artifactUpload.getContentType(),
new DbArtifactHash(artifactUpload.getProvidedSha1Sum(), artifactUpload.getProvidedMd5Sum()));
} catch (final ArtifactStoreException e) {
throw new ArtifactUploadFailedException(e);
Expand All @@ -122,11 +138,6 @@ public Artifact create(final ArtifactUpload artifactUpload) {
throw new InvalidMD5HashException(e.getMessage(), e);
}
}
if (result == null) {
return null;
}

return storeArtifactMetadata(softwareModule, filename, result, existing);
}

private void assertArtifactQuota(final long id, final int requested) {
Expand Down Expand Up @@ -166,12 +177,12 @@ private void assertMaxArtifactStorageQuota(final String filename, final long art
@Retryable(include = {
ConcurrencyFailureException.class }, maxAttempts = Constants.TX_RT_MAX, backoff = @Backoff(delay = Constants.TX_RT_DELAY))
public boolean clearArtifactBinary(final String sha1Hash, final long moduleId) {

if (localArtifactRepository.existsWithSha1HashAndSoftwareModuleIdIsNot(sha1Hash, moduleId)) {
final long count = localArtifactRepository.countBySha1HashAndTenantAndSoftwareModuleDeletedIsFalse(sha1Hash,
tenantAware.getCurrentTenant());
if (count > 1) {
// there are still other artifacts that need the binary
return false;
}

try {
LOG.debug("deleting artifact from repository {}", sha1Hash);
artifactRepository.deleteBySha1(tenantAware.getCurrentTenant(), sha1Hash);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,19 +56,18 @@ public interface LocalArtifactRepository extends BaseEntityRepository<JpaArtifac
List<Artifact> findBySha1Hash(String sha1Hash);

/**
* Verifies if an artifact exists that has given hash and is still related
* to a {@link SoftwareModule} other than a given one and not
* {@link SoftwareModule#isDeleted()}.
*
* Counts current elements based on the sha1 and tenant, as well as having
* the {@link SoftwareModule} property 'deleted' with value 'false
*
* @param sha1
* to search for
* @param moduleId
* to ignore in relationship check
* the sha1 of the {@link Artifact}
* @param tenant
* the current tenant
*
* @return <code>true</code> if such an artifact exists
* @return the count of the elements
*/
@Query("SELECT CASE WHEN COUNT(a)>0 THEN 'true' ELSE 'false' END FROM JpaArtifact a WHERE a.sha1Hash = :sha1 AND a.softwareModule.id != :moduleId AND a.softwareModule.deleted = 0")
boolean existsWithSha1HashAndSoftwareModuleIdIsNot(@Param("sha1") String sha1, @Param("moduleId") Long moduleId);
long countBySha1HashAndTenantAndSoftwareModuleDeletedIsFalse(@Param("sha1") String sha1,
@Param("tenant") String tenant);

/**
* Searches for a {@link Artifact} based on given gridFsFileName.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,7 @@ private Object convertValueIfNecessary(final ComparisonNode node, final A fieldN
// values JPA can do it by it's own but not for classes like enums.
// So we need to transform the given value string into the enum
// class.
final Class<? extends Object> javaType = fieldPath.getJavaType();
final Class<?> javaType = fieldPath.getJavaType();
if (javaType != null && javaType.isEnum()) {
return transformEnumValue(node, value, javaType);
}
Expand All @@ -485,7 +485,7 @@ private Object convertValueIfNecessary(final ComparisonNode node, final A fieldN
}

private Object convertBooleanValue(final ComparisonNode node, final String value,
final Class<? extends Object> javaType) {
final Class<?> javaType) {
try {
return simpleTypeConverter.convertIfNecessary(value, javaType);
} catch (final TypeMismatchException e) {
Expand Down Expand Up @@ -513,7 +513,7 @@ private Object convertFieldConverterValue(final ComparisonNode node, final A fie
// https://jira.sonarsource.com/browse/SONARJAVA-1478
@SuppressWarnings({ "rawtypes", "unchecked", "squid:S2095" })
private static Object transformEnumValue(final ComparisonNode node, final String value,
final Class<? extends Object> javaType) {
final Class<?> javaType) {
final Class<? extends Enum> tmpEnumType = (Class<? extends Enum>) javaType;
try {
return Enum.valueOf(tmpEnumType, value.toUpperCase());
Expand Down Expand Up @@ -680,13 +680,10 @@ private static String escapeValueToSQL(final String transformedValue, final Data
final char escapeChar) {
final String escaped;

switch (database) {
case SQL_SERVER:
if (database == Database.SQL_SERVER) {
escaped = transformedValue.replace("%", "[%]").replace("_", "[_]");
break;
default:
} else {
escaped = transformedValue.replace("%", escapeChar + "%").replace("_", escapeChar + "_");
break;
}

return escaped.replace(LIKE_WILDCARD, '%');
Expand Down
Loading

0 comments on commit fde0cbd

Please sign in to comment.