Skip to content

Commit

Permalink
feat: add methods needed for the cache functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
loicmathieu committed Jul 31, 2023
1 parent 04a5a6e commit 0fd7a9d
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 5 deletions.
4 changes: 2 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version=0.9.1-SNAPSHOT
kestraVersion=0.9.+
version=0.11.0-SNAPSHOT
kestraVersion=0.11.+
micronautVersion=3.9.3
lombokVersion=1.18.28
27 changes: 27 additions & 0 deletions src/main/java/io/kestra/storage/gcs/GcsStorage.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
import java.nio.ByteBuffer;
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;
import java.nio.file.Files;
import java.nio.file.attribute.FileTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -55,6 +57,16 @@ public InputStream get(URI uri) throws IOException {
}
}

@Override
public boolean exists(URI uri) {
try {
Blob blob = this.client().get(this.blob(URI.create(uri.getPath())));
return blob != null && blob.exists();
} catch (StorageException e) {
return false;
}
}

@Override
public Long size(URI uri) throws IOException {
try {
Expand All @@ -70,6 +82,21 @@ public Long size(URI uri) throws IOException {
}
}

@Override
public Long lastModifiedTime(URI uri) throws IOException {
try {
Blob blob = this.client().get(this.blob(URI.create(uri.getPath())));

if (blob == null || !blob.exists()) {
throw new FileNotFoundException(uri + " (File not found)");
}

return blob.getUpdateTimeOffsetDateTime().toInstant().toEpochMilli();
} catch (StorageException e) {
throw new IOException(e);
}
}

@Override
public URI put(URI uri, InputStream data) throws IOException {
try {
Expand Down
10 changes: 7 additions & 3 deletions src/test/java/io/kestra/storage/gcs/GcsStorageTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@

import static io.kestra.core.utils.Rethrow.throwConsumer;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.*;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

@MicronautTest
class GcsStorageTest {
Expand All @@ -44,8 +44,12 @@ void get() throws Exception {

this.putFile(resource, "/" + prefix + "/storage/get.yml");

InputStream get = storageInterface.get(new URI("/" + prefix + "/storage/get.yml"));
URI item = new URI("/" + prefix + "/storage/get.yml");
InputStream get = storageInterface.get(item);
assertThat(CharStreams.toString(new InputStreamReader(get)), is(content));
assertTrue(storageInterface.exists(item));
assertThat(storageInterface.size(item), is((long) content.length()));
assertThat(storageInterface.lastModifiedTime(item), notNullValue());

InputStream getScheme = storageInterface.get(new URI("kestra:///" + prefix + "/storage/get.yml"));
assertThat(CharStreams.toString(new InputStreamReader(getScheme)), is(content));
Expand Down

0 comments on commit 0fd7a9d

Please sign in to comment.