Skip to content

Commit

Permalink
couple minor changes to improve performance and readability
Browse files Browse the repository at this point in the history
  • Loading branch information
evgeniimv committed Dec 4, 2019
1 parent 8a9dfb5 commit e5b8a8e
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,15 @@

package com.epam.pipeline.manager.datastorage.providers;

import com.epam.pipeline.controller.vo.data.storage.RestoreFolderVO;
import com.epam.pipeline.entity.datastorage.AbstractDataStorageItem;
import com.epam.pipeline.entity.datastorage.DataStorageFile;
import com.epam.pipeline.entity.datastorage.DataStorageItemType;
import com.epam.pipeline.entity.datastorage.PathDescription;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.AntPathMatcher;

import java.util.Optional;
import java.util.function.Function;

public final class ProviderUtils {
Expand Down Expand Up @@ -75,4 +81,16 @@ public static <T> PathDescription getSizeByPath(final Iterable<T> items, final S
public static boolean isRootOrFolder(final String requestPath) {
return StringUtils.isBlank(requestPath) || requestPath.endsWith(DELIMITER);
}

public static boolean isFileWithDeleteMarkerAndShouldBeRestore(final AbstractDataStorageItem item,
final RestoreFolderVO restoreFolderVO) {
final AntPathMatcher matcher = new AntPathMatcher();
return item.getType() == DataStorageItemType.File &&
((DataStorageFile) item).getDeleteMarker() &&
((DataStorageFile) item).getVersion() != null &&
Optional.ofNullable(restoreFolderVO.getIncludeList()).map(includeList -> includeList.stream()
.anyMatch(pattern -> matcher.match(pattern, item.getName()))).orElse(true) &&
Optional.ofNullable(restoreFolderVO.getExcludeList()).map(excludeList -> excludeList.stream()
.noneMatch(pattern -> matcher.match(pattern, item.getName()))).orElse(true);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.AntPathMatcher;
import org.springframework.util.Assert;

import java.io.IOException;
Expand Down Expand Up @@ -122,10 +121,7 @@ public void deleteGoogleStorage(final String bucketName) {

public DataStorageListing listItems(final GSBucketStorage storage, final String path, final Boolean showVersion,
final Integer pageSize, final String marker) {
String requestPath = Optional.ofNullable(path).orElse(EMPTY_PREFIX);
if (StringUtils.isNotBlank(requestPath)) {
requestPath = normalizeFolderPath(requestPath);
}
final String requestPath = prepareRequestPathToViewFolderContent(path);
final Storage client = gcpClient.buildStorageClient(region);
final String bucketName = storage.getPath();

Expand Down Expand Up @@ -370,15 +366,8 @@ public void restoreFileVersion(final GSBucketStorage storage, final String path,
checkVersionHasNotDeletedMarker(version);
final Storage client = gcpClient.buildStorageClient(region);
final String bucketName = storage.getPath();
final Blob blob = checkBlobExistsAndGet(bucketName, path, client, version);

final Storage.CopyRequest request = Storage.CopyRequest.newBuilder()
.setSource(blob.getBlobId())
.setSourceOptions(Storage.BlobSourceOption.generationMatch())
.setTarget(BlobId.of(bucketName, path))
.build();
client.copy(request).getResult();
deleteBlob(blob, client, true);
client.copy(buildCopyRequestForFileRestore(bucketName, path, client, version)).getResult();
deleteBlob(checkBlobExistsAndGet(bucketName, path, client, version), client, true);
}

public void restoreFolder(final GSBucketStorage storage, final String path,
Expand All @@ -391,10 +380,7 @@ public void restoreFolder(final GSBucketStorage storage, final String path,
private void cleanDeleteMarkers(final Storage client,
final String bucketName, final String requestPath,
final RestoreFolderVO restoreFolderVO) {
String folderPath = Optional.ofNullable(requestPath).orElse(EMPTY_PREFIX);
if (StringUtils.isNotBlank(folderPath)) {
folderPath = normalizeFolderPath(requestPath);
}
final String folderPath = prepareRequestPathToViewFolderContent(requestPath);
final Page<Blob> blobs = client.list(bucketName,
Storage.BlobListOption.versions(true),
Storage.BlobListOption.currentDirectory(),
Expand All @@ -406,19 +392,30 @@ private void cleanDeleteMarkers(final Storage client,
listItemsWithVersions(blobs)
.forEach(item -> {
recursiveRestoreFolderCall(item, client, bucketName, restoreFolderVO);
if (isFileWithDeleteMarkerAndShouldBeRestore(item, restoreFolderVO)) {
final Blob blob = checkBlobExistsAndGet(bucketName, item.getPath(), client,
removeDeletedMarkerFromVersion(((DataStorageFile) item).getVersion()));
final Storage.CopyRequest request = Storage.CopyRequest.newBuilder()
.setSource(blob.getBlobId())
.setSourceOptions(Storage.BlobSourceOption.generationMatch())
.setTarget(BlobId.of(bucketName, item.getPath()))
.build();
client.copy(request).getResult();
if (ProviderUtils.isFileWithDeleteMarkerAndShouldBeRestore(item, restoreFolderVO)) {
client.copy(buildCopyRequestForFileRestore(bucketName, item.getPath(), client,
removeDeletedMarkerFromVersion(((DataStorageFile) item).getVersion())))
.getResult();
}
});
}

private String prepareRequestPathToViewFolderContent(final String requestPath) {
return Optional.ofNullable(requestPath)
.filter(StringUtils::isNotBlank)
.map(this::normalizeFolderPath)
.orElse(EMPTY_PREFIX);
}

private Storage.CopyRequest buildCopyRequestForFileRestore(final String bucketName, final String destinationPath,
final Storage client, final String version) {
return Storage.CopyRequest.newBuilder()
.setSource(checkBlobExistsAndGet(bucketName, destinationPath, client, version).getBlobId())
.setSourceOptions(Storage.BlobSourceOption.generationMatch())
.setTarget(BlobId.of(bucketName, destinationPath))
.build();
}

private String removeDeletedMarkerFromVersion(final String versionWithDeletedMarker) {
if (latestVersionHasDeletedMarker(versionWithDeletedMarker)) {
return versionWithDeletedMarker.substring(0, versionWithDeletedMarker.length() - 2);
Expand All @@ -435,18 +432,6 @@ private void recursiveRestoreFolderCall(final AbstractDataStorageItem item, fina
}
}

private boolean isFileWithDeleteMarkerAndShouldBeRestore(final AbstractDataStorageItem item,
final RestoreFolderVO restoreFolderVO) {
final AntPathMatcher matcher = new AntPathMatcher();
return item.getType() == DataStorageItemType.File &&
((DataStorageFile) item).getDeleteMarker() &&
((DataStorageFile) item).getVersion() != null &&
Optional.ofNullable(restoreFolderVO.getIncludeList()).map(includeList -> includeList.stream()
.anyMatch(pattern -> matcher.match(pattern, item.getName()))).orElse(true) &&
Optional.ofNullable(restoreFolderVO.getExcludeList()).map(excludeList -> excludeList.stream()
.noneMatch(pattern -> matcher.match(pattern, item.getName()))).orElse(true);
}

public void applyStoragePolicy(final GSBucketStorage storage, final StoragePolicy policy) {
final Storage client = gcpClient.buildStorageClient(region);
final String bucketName = storage.getPath();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -224,4 +224,4 @@ public void describeTo(Description description) {
}
};
}
}
}

0 comments on commit e5b8a8e

Please sign in to comment.