Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

noop(cleanup) dedup w/factored-out (dtinit#1392) counter #1393

Merged
merged 3 commits into from
Nov 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions portability-transfer/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ configurations {

dependencies {
compile project(':portability-api-launcher')
compile project(':portability-spi-api')
compile project(':portability-spi-service')
compile project(':portability-spi-cloud')
compile project(':portability-spi-transfer')
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package org.datatransferproject.transfer;

import java.io.IOException;
import java.io.InputStream;
import static org.datatransferproject.spi.api.transport.DiscardingStreamCounter.discardForLength;

import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.Map;
Expand All @@ -19,7 +19,9 @@ public class CallableSizeCalculator implements Callable<Map<String, Long>> {
private final Collection<? extends DownloadableItem> items;

public CallableSizeCalculator(
UUID jobId, ConnectionProvider connectionProvider, Collection<? extends DownloadableItem> items) {
UUID jobId,
ConnectionProvider connectionProvider,
Collection<? extends DownloadableItem> items) {
this.jobId = Objects.requireNonNull(jobId);
this.connectionProvider = Objects.requireNonNull(connectionProvider);
this.items = Objects.requireNonNull(items);
Expand All @@ -32,26 +34,12 @@ public Map<String, Long> call() throws Exception {
InputStreamWrapper stream = connectionProvider.getInputStreamForItem(jobId, item);
long size = stream.getBytes();
if (size <= 0) {
size = computeSize(stream);
size = discardForLength(stream.getStream());
}

result.put(item.getIdempotentId(), size);
}

return result;
}

// Reads the input stream in full
private Long computeSize(InputStreamWrapper stream) throws IOException {
long size = 0;
try (InputStream inStream = stream.getStream()) {
byte[] buffer = new byte[1024 * 1024]; // 1MB
int chunkBytesRead;
while ((chunkBytesRead = inStream.read(buffer)) != -1) {
size += chunkBytesRead;
}
}

return size;
}
}
Loading