-
Notifications
You must be signed in to change notification settings - Fork 481
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* stricter idempotent import executor api * size calculation mode * fixed unused import * Update VideoModel.java Fix JSON override Co-authored-by: William Morland <[email protected]>
- Loading branch information
Showing
11 changed files
with
366 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
...-cloud/src/main/java/org/datatransferproject/spi/cloud/connection/ConnectionProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package org.datatransferproject.spi.cloud.connection; | ||
|
||
import java.io.IOException; | ||
import java.net.HttpURLConnection; | ||
import java.net.URL; | ||
import java.util.UUID; | ||
import org.datatransferproject.spi.cloud.storage.TemporaryPerJobDataStore; | ||
import org.datatransferproject.spi.cloud.storage.TemporaryPerJobDataStore.InputStreamWrapper; | ||
import org.datatransferproject.types.common.DownloadableItem; | ||
|
||
public class ConnectionProvider { | ||
|
||
private final TemporaryPerJobDataStore jobStore; | ||
|
||
public ConnectionProvider(TemporaryPerJobDataStore jobStore) { | ||
this.jobStore = jobStore; | ||
} | ||
|
||
public InputStreamWrapper getInputStreamForItem(UUID jobId, DownloadableItem item) | ||
throws IOException { | ||
|
||
String fetchableUrl = item.getFetchableUrl(); | ||
if (item.isInTempStore()) { | ||
return jobStore.getStream(jobId, fetchableUrl); | ||
} | ||
|
||
HttpURLConnection conn = getConnection(fetchableUrl); | ||
return new InputStreamWrapper( | ||
conn.getInputStream(), Math.max(conn.getContentLengthLong(), 0)); | ||
} | ||
|
||
public static HttpURLConnection getConnection(String urlStr) throws IOException { | ||
URL url = new URL(urlStr); | ||
HttpURLConnection conn = (HttpURLConnection) url.openConnection(); | ||
conn.connect(); | ||
return conn; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
...ud/src/test/java/org/datatransferproject/spi/cloud/connection/ConnectionProviderTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package org.datatransferproject.spi.cloud.connection; | ||
|
||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.ArgumentMatchers.anyString; | ||
import static org.mockito.ArgumentMatchers.eq; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.when; | ||
|
||
import com.google.common.truth.Truth; | ||
import java.util.UUID; | ||
import org.datatransferproject.spi.cloud.storage.TemporaryPerJobDataStore; | ||
import org.datatransferproject.spi.cloud.storage.TemporaryPerJobDataStore.InputStreamWrapper; | ||
import org.datatransferproject.types.common.DownloadableItem; | ||
import org.datatransferproject.types.common.models.photos.PhotoModel; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
public class ConnectionProviderTest { | ||
|
||
private TemporaryPerJobDataStore jobStore; | ||
private ConnectionProvider connectionProvider; | ||
|
||
@Before | ||
public void setUp() throws Exception { | ||
jobStore = mock(TemporaryPerJobDataStore.class); | ||
connectionProvider = new ConnectionProvider(jobStore); | ||
} | ||
|
||
@Test | ||
public void getInputStreamFromTempStore() throws Exception { | ||
long expectedBytes = 323; | ||
when(jobStore.getStream(any(), anyString())).thenReturn( | ||
new InputStreamWrapper(null, expectedBytes)); | ||
boolean inTempStore = true; | ||
String fetchableUrl = "https://example.com"; | ||
DownloadableItem item = new PhotoModel("title", fetchableUrl, "description", "jpeg", | ||
"123", "album", inTempStore); | ||
UUID jobId = UUID.randomUUID(); | ||
InputStreamWrapper streamWrapper = connectionProvider.getInputStreamForItem( | ||
jobId, item); | ||
|
||
Truth.assertThat(streamWrapper.getBytes()).isEqualTo(expectedBytes); | ||
verify(jobStore).getStream(eq(jobId), eq(fetchableUrl)); | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
...ility-transfer/src/main/java/org/datatransferproject/transfer/CallableSizeCalculator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package org.datatransferproject.transfer; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.util.Collection; | ||
import java.util.LinkedHashMap; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
import java.util.UUID; | ||
import java.util.concurrent.Callable; | ||
import org.datatransferproject.spi.cloud.connection.ConnectionProvider; | ||
import org.datatransferproject.spi.cloud.storage.TemporaryPerJobDataStore.InputStreamWrapper; | ||
import org.datatransferproject.types.common.DownloadableItem; | ||
|
||
public class CallableSizeCalculator implements Callable<Map<String, Long>> { | ||
|
||
private final UUID jobId; | ||
private final ConnectionProvider connectionProvider; | ||
private final Collection<? extends DownloadableItem> items; | ||
|
||
public CallableSizeCalculator( | ||
UUID jobId, ConnectionProvider connectionProvider, Collection<? extends DownloadableItem> items) { | ||
this.jobId = Objects.requireNonNull(jobId); | ||
this.connectionProvider = Objects.requireNonNull(connectionProvider); | ||
this.items = Objects.requireNonNull(items); | ||
} | ||
|
||
@Override | ||
public Map<String, Long> call() throws Exception { | ||
Map<String, Long> result = new LinkedHashMap<>(); | ||
for (DownloadableItem item : items) { | ||
InputStreamWrapper stream = connectionProvider.getInputStreamForItem(jobId, item); | ||
long size = stream.getBytes(); | ||
if (size <= 0) { | ||
size = computeSize(stream); | ||
} | ||
|
||
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.