Skip to content

Commit d7bc5c5

Browse files
author
ylexus
committed
#106 Make paths in progress panels show actual file being worked on regardless of batching
1 parent bb18c02 commit d7bc5c5

File tree

6 files changed

+53
-24
lines changed

6 files changed

+53
-24
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ dependencies {
5151
ext.orgJunitJupiterVersion = '5.6.0'
5252
ext.orgMockitoVersion = '3.2.4'
5353
ext.orgImmutablesVersion = '2.8.3'
54-
ext.netYudichevJiottyVersion = '1.7'
54+
ext.netYudichevJiottyVersion = '1.7.1'
5555

5656
annotationProcessor "org.immutables:value:$orgImmutablesVersion"
5757
testAnnotationProcessor "org.immutables:value:$orgImmutablesVersion"

src/main/java/net/yudichev/googlephotosupload/core/AddToAlbumAfterCreatingStrategy.java

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import java.util.Optional;
1313
import java.util.concurrent.CompletableFuture;
1414
import java.util.concurrent.CompletionStage;
15+
import java.util.concurrent.Executor;
1516
import java.util.concurrent.ExecutorService;
1617
import java.util.function.BiFunction;
1718
import java.util.function.Function;
@@ -43,19 +44,23 @@ final class AddToAlbumAfterCreatingStrategy implements AddToAlbumStrategy {
4344
public CompletableFuture<Void> addToAlbum(CompletableFuture<List<PathState>> createMediaDataResultsFuture,
4445
Optional<GooglePhotosAlbum> googlePhotosAlbum,
4546
ProgressStatus fileProgressStatus,
47+
ProgressStatus directoryProgressStatus,
4648
BiFunction<Optional<String>, List<PathState>, CompletableFuture<List<PathMediaItemOrError>>> createMediaItems,
4749
Function<Path, ItemState> itemStateRetriever) {
4850
return createMediaDataResultsFuture
4951
.thenCompose(createMediaDataResults -> partition(createMediaDataResults, GOOGLE_PHOTOS_API_BATCH_SIZE).stream()
5052
.collect(toFutureOfListChaining(pathStates -> createMediaItems.apply(Optional.empty(), pathStates)))
5153
.thenApply(lists -> lists.stream().flatMap(Collection::stream)))
52-
.thenCompose(pathMediaItemOrErrorStream -> addToAlbum(googlePhotosAlbum, pathMediaItemOrErrorStream, fileProgressStatus, itemStateRetriever));
54+
.thenCompose(pathMediaItemOrErrorStream -> addToAlbum(googlePhotosAlbum,
55+
pathMediaItemOrErrorStream,
56+
fileProgressStatus,
57+
directoryProgressStatus));
5358
}
5459

5560
private CompletionStage<Void> addToAlbum(Optional<GooglePhotosAlbum> googlePhotosAlbum,
5661
Stream<PathMediaItemOrError> pathMediaItemOrErrorStream,
5762
ProgressStatus fileProgressStatus,
58-
Function<Path, ItemState> itemStateRetriever) {
63+
ProgressStatus directoryProgressStatus) {
5964
return googlePhotosAlbum
6065
.map(album -> {
6166
var pathMediaItemOrErrors = pathMediaItemOrErrorStream
@@ -69,7 +74,8 @@ private CompletionStage<Void> addToAlbum(Optional<GooglePhotosAlbum> googlePhoto
6974
.collect(toImmutableList());
7075
return cloudOperationHelper.withBackOffAndRetry("add items to album",
7176
() -> partition(mediaItemsToAddToAlbum, GOOGLE_PHOTOS_API_BATCH_SIZE).stream()
72-
.collect(toFutureOfListChaining(mediaItems -> album.addMediaItems(mediaItems, executorServiceProvider.get())))
77+
.collect(toFutureOfListChaining(mediaItems -> album
78+
.addMediaItems(mediaItems, statusUpdatingExecutor(album, directoryProgressStatus))))
7379
.<Void>thenApply(ignored -> null),
7480
fileProgressStatus::onBackoffDelay)
7581
.exceptionallyCompose(exception -> fatalUserCorrectableHandler.handle(
@@ -84,4 +90,11 @@ private CompletionStage<Void> addToAlbum(Optional<GooglePhotosAlbum> googlePhoto
8490
})
8591
.orElseGet(CompletableFutures::completedFuture);
8692
}
93+
94+
private Executor statusUpdatingExecutor(GooglePhotosAlbum album, ProgressStatus directoryProgressStatus) {
95+
return command -> executorServiceProvider.get().execute(() -> {
96+
directoryProgressStatus.updateDescription(album.getTitle());
97+
command.run();
98+
});
99+
}
87100
}

src/main/java/net/yudichev/googlephotosupload/core/AddToAlbumStrategy.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ interface AddToAlbumStrategy {
1313
CompletableFuture<Void> addToAlbum(CompletableFuture<List<PathState>> createMediaDataResultsFuture,
1414
Optional<GooglePhotosAlbum> googlePhotosAlbum,
1515
ProgressStatus fileProgressStatus,
16+
ProgressStatus directoryProgressStatus,
1617
BiFunction<Optional<String>, List<PathState>, CompletableFuture<List<PathMediaItemOrError>>> createMediaItems,
1718
Function<Path, ItemState> itemStateRetriever);
1819
}

src/main/java/net/yudichev/googlephotosupload/core/AddToAlbumWhileCreatingStrategy.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,15 @@ final class AddToAlbumWhileCreatingStrategy implements AddToAlbumStrategy {
1818
public CompletableFuture<Void> addToAlbum(CompletableFuture<List<PathState>> createMediaDataResultsFuture,
1919
Optional<GooglePhotosAlbum> googlePhotosAlbum,
2020
ProgressStatus fileProgressStatus,
21+
ProgressStatus directoryProgressStatus,
2122
BiFunction<Optional<String>, List<PathState>, CompletableFuture<List<PathMediaItemOrError>>> createMediaItems,
2223
Function<Path, ItemState> itemStateRetriever) {
2324
return createMediaDataResultsFuture
2425
.thenCompose(createMediaDataResults -> partition(createMediaDataResults, GOOGLE_PHOTOS_API_BATCH_SIZE).stream()
25-
.map(pathStates -> createMediaItems.apply(googlePhotosAlbum.map(GooglePhotosAlbum::getId), pathStates))
26+
.map(pathStates -> {
27+
directoryProgressStatus.updateDescription(googlePhotosAlbum.map(GooglePhotosAlbum::getTitle).orElse(""));
28+
return createMediaItems.apply(googlePhotosAlbum.map(GooglePhotosAlbum::getId), pathStates);
29+
})
2630
.collect(toFutureOfList())
2731
.thenApply(voids -> null));
2832
}

src/main/java/net/yudichev/googlephotosupload/core/GooglePhotosUploaderImpl.java

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.util.Map;
2020
import java.util.Optional;
2121
import java.util.concurrent.CompletableFuture;
22+
import java.util.concurrent.Executor;
2223
import java.util.concurrent.ExecutorService;
2324

2425
import static com.google.common.base.Preconditions.checkNotNull;
@@ -86,23 +87,20 @@ public CompletableFuture<Void> uploadDirectory(Optional<GooglePhotosAlbum> googl
8687
directoryProgressStatus.updateDescription(googlePhotosAlbum.map(GooglePhotosAlbum::getTitle).orElse(""));
8788
var createMediaDataResultsFuture = paths.stream()
8889
.sorted(comparing(path -> path.getFileName().toString()))
89-
.map(path -> {
90-
fileProgressStatus.updateDescription(path.toAbsolutePath().toString());
91-
return createMediaData(path)
92-
.thenApply(itemState -> {
93-
itemState.toFailure().ifPresentOrElse(
94-
error -> fileProgressStatus.addFailure(KeyedError.of(path, error)),
95-
fileProgressStatus::incrementSuccess);
96-
return PathState.of(path, itemState);
97-
});
98-
})
90+
.map(path -> createMediaData(path, fileProgressStatus)
91+
.thenApply(itemState -> {
92+
itemState.toFailure().ifPresentOrElse(
93+
error -> fileProgressStatus.addFailure(KeyedError.of(path, error)),
94+
fileProgressStatus::incrementSuccess);
95+
return PathState.of(path, itemState);
96+
}))
9997
.collect(toFutureOfList());
10098
return addToAlbumStrategy.addToAlbum(
10199
createMediaDataResultsFuture,
102100
googlePhotosAlbum,
103101
fileProgressStatus,
104-
(albumId, pathStates) -> createMediaItems(albumId, fileProgressStatus, pathStates),
105-
this::getItemState);
102+
directoryProgressStatus,
103+
(albumId, pathStates) -> createMediaItems(albumId, fileProgressStatus, pathStates), this::getItemState);
106104
});
107105
}
108106

@@ -212,14 +210,14 @@ private void forgetUploadState() {
212210
memoryBarrier = true;
213211
}
214212

215-
private CompletableFuture<ResultOrFailure<ItemState>> createMediaData(Path file) {
213+
private CompletableFuture<ResultOrFailure<ItemState>> createMediaData(Path file, ProgressStatus fileProgressStatus) {
216214
checkStarted();
217215
checkState(memoryBarrier);
218216
return uploadedItemStateByPath.compute(file,
219217
(theFile, currentFuture) -> {
220218
if (currentFuture == null || currentFuture.isCompletedExceptionally()) {
221219
logger.info("Scheduling upload of {}", file);
222-
currentFuture = doCreateMediaData(theFile);
220+
currentFuture = doCreateMediaData(theFile, fileProgressStatus);
223221
} else {
224222
var itemState = currentFuture.getNow(null);
225223
if (itemState != null) {
@@ -236,7 +234,7 @@ private CompletableFuture<ResultOrFailure<ItemState>> createMediaData(Path file)
236234
})
237235
.orElseGet(() -> {
238236
logger.info("Uploaded, but upload token expired, re-uploading: {}", file);
239-
return doCreateMediaData(theFile);
237+
return doCreateMediaData(theFile, fileProgressStatus);
240238
});
241239
} else {
242240
logger.error("Unexpected future state for {}: {}", file, currentFuture);
@@ -257,7 +255,7 @@ private CompletableFuture<ResultOrFailure<ItemState>> createMediaData(Path file)
257255
.orElseGet(() -> {
258256
if (backOffHandler.handle(operationName, exception).isPresent()) {
259257
logger.debug("Retrying upload of {}", file);
260-
return createMediaData(file);
258+
return createMediaData(file, fileProgressStatus);
261259
} else {
262260
throw new RuntimeException(exception);
263261
}
@@ -274,8 +272,8 @@ private boolean uploadTokenNotExpired(Path file, UploadMediaItemState uploadMedi
274272
return notExpired;
275273
}
276274

277-
private CompletableFuture<ItemState> doCreateMediaData(Path file) {
278-
return googlePhotosClient.uploadMediaData(file, executorService)
275+
private CompletableFuture<ItemState> doCreateMediaData(Path file, ProgressStatus fileProgressStatus) {
276+
return googlePhotosClient.uploadMediaData(file, statusUpdatingExecutor(file, fileProgressStatus))
279277
.thenApply(uploadToken -> {
280278
logger.info("Uploaded file {}", file);
281279
logger.debug("Upload token {}", uploadToken);
@@ -284,4 +282,11 @@ private CompletableFuture<ItemState> doCreateMediaData(Path file) {
284282
.build();
285283
});
286284
}
285+
286+
private Executor statusUpdatingExecutor(Path file, ProgressStatus fileProgressStatus) {
287+
return command -> executorService.execute(() -> {
288+
fileProgressStatus.updateDescription(file.toAbsolutePath().toString());
289+
command.run();
290+
});
291+
}
287292
}

src/main/java/net/yudichev/googlephotosupload/core/SelectingAddToAlbumStrategy.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,15 @@ final class SelectingAddToAlbumStrategy implements AddToAlbumStrategy {
3737
public CompletableFuture<Void> addToAlbum(CompletableFuture<List<PathState>> createMediaDataResultsFuture,
3838
Optional<GooglePhotosAlbum> googlePhotosAlbum,
3939
ProgressStatus fileProgressStatus,
40+
ProgressStatus directoryProgressStatus,
4041
BiFunction<Optional<String>, List<PathState>, CompletableFuture<List<PathMediaItemOrError>>> createMediaItems,
4142
Function<Path, ItemState> itemStateRetriever) {
42-
return selectDelegate().addToAlbum(createMediaDataResultsFuture, googlePhotosAlbum, fileProgressStatus, createMediaItems, itemStateRetriever);
43+
return selectDelegate().addToAlbum(createMediaDataResultsFuture,
44+
googlePhotosAlbum,
45+
fileProgressStatus,
46+
directoryProgressStatus,
47+
createMediaItems,
48+
itemStateRetriever);
4349
}
4450

4551
private AddToAlbumStrategy selectDelegate() {

0 commit comments

Comments
 (0)