Skip to content

Commit e8fb440

Browse files
committed
reduce getArchiveComment store requests
1 parent 72ef229 commit e8fb440

7 files changed

Lines changed: 17 additions & 6 deletions

File tree

src/main/java/dev/zarr/zarrjava/store/FilesystemStore.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,8 @@ public InputStream getInputStream(String[] keys, long start, long end) {
177177
public long getSize(String[] keys) {
178178
try {
179179
return Files.size(resolveKeys(keys));
180+
} catch (NoSuchFileException e) {
181+
return -1;
180182
} catch (IOException e) {
181183
throw new RuntimeException(e);
182184
}

src/main/java/dev/zarr/zarrjava/store/HttpStore.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ public long getSize(String[] keys) {
148148
try {
149149
Response response = call.execute();
150150
if (!response.isSuccessful()) {
151-
throw new IOException("Failed to get size: " + response.code());
151+
return -1;
152152
}
153153

154154
String contentLength = response.header("Content-Length");

src/main/java/dev/zarr/zarrjava/store/MemoryStore.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ public InputStream getInputStream(String[] keys, long start, long end) {
9898
public long getSize(String[] keys) {
9999
byte[] bytes = map.get(resolveKeys(keys));
100100
if (bytes == null) {
101-
throw new RuntimeException(new java.io.FileNotFoundException("Key not found: " + String.join("/", keys)));
101+
return -1;
102102
}
103103
return bytes.length;
104104
}

src/main/java/dev/zarr/zarrjava/store/ReadOnlyZipStore.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ public long getSize(String[] keys) {
213213
}
214214
return size;
215215
}
216-
throw new RuntimeException(new java.io.FileNotFoundException("Key not found: " + resolveKeys(keys)));
216+
return -1; // file not found
217217
} catch (IOException e) {
218218
throw new RuntimeException(e);
219219
}

src/main/java/dev/zarr/zarrjava/store/S3Store.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ public long getSize(String[] keys) {
141141
try {
142142
return s3client.headObject(req).contentLength();
143143
} catch (NoSuchKeyException e) {
144-
throw new RuntimeException(e);
144+
return -1;
145145
}
146146
}
147147

src/main/java/dev/zarr/zarrjava/store/Store.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,5 +50,11 @@ default InputStream getInputStream(String[] keys) {
5050
return getInputStream(keys, 0, -1);
5151
}
5252

53+
/**
54+
* Gets the size in bytes of the data stored at the given keys.
55+
*
56+
* @param keys The keys identifying the data.
57+
* @return The size in bytes of the data stored at the given keys. -1 if the keys do not exist.
58+
*/
5359
long getSize(String[] keys);
5460
}

src/main/java/dev/zarr/zarrjava/store/ZipStore.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@ public String getArchiveComment() throws IOException {
1616
// Attempt to read from the end of the file to find the EOCD record.
1717
// We try a small chunk first (1KB) which covers most short comments (or no comment),
1818
// then the maximum possible EOCD size (approx 65KB).
19-
if (!underlyingStore.exists()) {
19+
long fileSize = underlyingStore.getSize();
20+
if (fileSize < 22) {
2021
return null;
2122
}
2223
int[] readSizes = {1024, 65535 + 22};
2324

2425
for (int size : readSizes) {
2526
ByteBuffer buffer;
26-
long fileSize = underlyingStore.getSize();
2727

2828
if (fileSize < size){
2929
buffer = underlyingStore.read();
@@ -48,6 +48,9 @@ public String getArchiveComment() throws IOException {
4848
if (comment != null) {
4949
return comment;
5050
}
51+
if (fileSize < size){
52+
break;
53+
}
5154
}
5255
return null;
5356
}

0 commit comments

Comments
 (0)