Skip to content

Commit 8b390bc

Browse files
committed
format
1 parent 2927ca7 commit 8b390bc

11 files changed

Lines changed: 325 additions & 346 deletions

File tree

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

Lines changed: 89 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
package dev.zarr.zarrjava.store;
22

3+
import org.apache.commons.compress.archivers.zip.Zip64Mode;
4+
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
5+
import org.apache.commons.compress.archivers.zip.ZipArchiveInputStream;
6+
import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream;
7+
38
import javax.annotation.Nonnull;
49
import javax.annotation.Nullable;
510
import java.io.ByteArrayOutputStream;
@@ -10,21 +15,17 @@
1015
import java.nio.file.Paths;
1116
import java.util.Comparator;
1217
import java.util.stream.Stream;
13-
14-
import org.apache.commons.compress.archivers.zip.*;
15-
1618
import java.util.zip.CRC32;
17-
import java.util.zip.ZipEntry; // for STORED constant
19+
import java.util.zip.ZipEntry;
1820

1921

20-
/** A Store implementation that buffers reads and writes and flushes them to an underlying Store as a zip file.
22+
/**
23+
* A Store implementation that buffers reads and writes and flushes them to an underlying Store as a zip file.
2124
*/
2225
public class BufferedZipStore extends ZipStore {
2326

2427
private final Store.ListableStore bufferStore;
25-
private String archiveComment;
2628
private final boolean flushOnWrite;
27-
2829
private final Comparator<String[]> zipEntryComparator = (a, b) -> {
2930
boolean aIsZarr = a.length > 0 && a[a.length - 1].equals("zarr.json");
3031
boolean bIsZarr = b.length > 0 && b[b.length - 1].equals("zarr.json");
@@ -45,6 +46,79 @@ public class BufferedZipStore extends ZipStore {
4546
return String.join("/", a).compareTo(String.join("/", b));
4647
}
4748
};
49+
private String archiveComment;
50+
51+
public BufferedZipStore(@Nonnull StoreHandle underlyingStore, @Nonnull Store.ListableStore bufferStore, @Nullable String archiveComment, boolean flushOnWrite) {
52+
super(underlyingStore);
53+
this.bufferStore = bufferStore;
54+
this.archiveComment = archiveComment;
55+
this.flushOnWrite = flushOnWrite;
56+
try {
57+
loadBuffer();
58+
} catch (IOException e) {
59+
throw new RuntimeException("Failed to load buffer from underlying store", e);
60+
}
61+
}
62+
63+
public BufferedZipStore(@Nonnull StoreHandle underlyingStore, @Nonnull Store.ListableStore bufferStore, @Nullable String archiveComment) {
64+
this(underlyingStore, bufferStore, archiveComment, false);
65+
}
66+
67+
public BufferedZipStore(@Nonnull StoreHandle underlyingStore, @Nonnull Store.ListableStore bufferStore) {
68+
this(underlyingStore, bufferStore, null);
69+
}
70+
71+
public BufferedZipStore(@Nonnull StoreHandle underlyingStore, String archiveComment) {
72+
this(underlyingStore, new MemoryStore(), archiveComment);
73+
}
74+
75+
public BufferedZipStore(@Nonnull StoreHandle underlyingStore) {
76+
this(underlyingStore, (String) null);
77+
}
78+
79+
public BufferedZipStore(@Nonnull Path underlyingStore, String archiveComment) {
80+
this(new FilesystemStore(underlyingStore.getParent()).resolve(underlyingStore.getFileName().toString()), archiveComment);
81+
}
82+
83+
public BufferedZipStore(@Nonnull Path underlyingStore) {
84+
this(underlyingStore, null);
85+
}
86+
87+
public BufferedZipStore(@Nonnull String underlyingStorePath, String archiveComment) {
88+
this(Paths.get(underlyingStorePath), archiveComment);
89+
}
90+
91+
public BufferedZipStore(@Nonnull String underlyingStorePath) {
92+
this(underlyingStorePath, null);
93+
}
94+
95+
public BufferedZipStore(@Nonnull StoreHandle underlyingStore, @Nonnull Store.ListableStore bufferStore, boolean flushOnWrite) {
96+
this(underlyingStore, bufferStore, null, flushOnWrite);
97+
}
98+
99+
public BufferedZipStore(@Nonnull StoreHandle underlyingStore, String archiveComment, boolean flushOnWrite) {
100+
this(underlyingStore, new MemoryStore(), archiveComment, flushOnWrite);
101+
}
102+
103+
public BufferedZipStore(@Nonnull StoreHandle underlyingStore, boolean flushOnWrite) {
104+
this(underlyingStore, (String) null, flushOnWrite);
105+
}
106+
107+
public BufferedZipStore(@Nonnull Path underlyingStore, String archiveComment, boolean flushOnWrite) {
108+
this(new FilesystemStore(underlyingStore.getParent()).resolve(underlyingStore.getFileName().toString()), archiveComment, flushOnWrite);
109+
}
110+
111+
public BufferedZipStore(@Nonnull Path underlyingStore, boolean flushOnWrite) {
112+
this(underlyingStore, null, flushOnWrite);
113+
}
114+
115+
public BufferedZipStore(@Nonnull String underlyingStorePath, String archiveComment, boolean flushOnWrite) {
116+
this(Paths.get(underlyingStorePath), archiveComment, flushOnWrite);
117+
}
118+
119+
public BufferedZipStore(@Nonnull String underlyingStorePath, boolean flushOnWrite) {
120+
this(underlyingStorePath, null, flushOnWrite);
121+
}
48122

49123
private void writeBuffer() throws IOException {
50124
// create zip file bytes from buffer store and write to underlying store
@@ -111,13 +185,6 @@ private void writeBuffer() throws IOException {
111185
underlyingStore.set(ByteBuffer.wrap(zipBytes));
112186
}
113187

114-
public void setArchiveComment(@Nullable String archiveComment) throws IOException {
115-
this.archiveComment = archiveComment;
116-
if (flushOnWrite) {
117-
writeBuffer();
118-
}
119-
}
120-
121188
public void deleteArchiveComment() throws IOException {
122189
this.setArchiveComment(null);
123190
}
@@ -154,81 +221,8 @@ private void loadBuffer() throws IOException {
154221
}
155222
}
156223

157-
public BufferedZipStore(@Nonnull StoreHandle underlyingStore, @Nonnull Store.ListableStore bufferStore, @Nullable String archiveComment, boolean flushOnWrite) {
158-
super(underlyingStore);
159-
this.bufferStore = bufferStore;
160-
this.archiveComment = archiveComment;
161-
this.flushOnWrite = flushOnWrite;
162-
try {
163-
loadBuffer();
164-
} catch (IOException e) {
165-
throw new RuntimeException("Failed to load buffer from underlying store", e);
166-
}
167-
}
168-
169-
public BufferedZipStore(@Nonnull StoreHandle underlyingStore, @Nonnull Store.ListableStore bufferStore, @Nullable String archiveComment) {
170-
this(underlyingStore, bufferStore, archiveComment, false);
171-
}
172-
173-
public BufferedZipStore(@Nonnull StoreHandle underlyingStore, @Nonnull Store.ListableStore bufferStore) {
174-
this(underlyingStore, bufferStore, null);
175-
}
176-
177-
public BufferedZipStore(@Nonnull StoreHandle underlyingStore, String archiveComment) {
178-
this(underlyingStore, new MemoryStore(), archiveComment);
179-
}
180-
181-
public BufferedZipStore(@Nonnull StoreHandle underlyingStore) {
182-
this(underlyingStore, (String) null);
183-
}
184-
185-
public BufferedZipStore(@Nonnull Path underlyingStore, String archiveComment) {
186-
this(new FilesystemStore(underlyingStore.getParent()).resolve(underlyingStore.getFileName().toString()), archiveComment);
187-
}
188-
189-
public BufferedZipStore(@Nonnull Path underlyingStore) {
190-
this(underlyingStore, null);
191-
}
192-
193-
public BufferedZipStore(@Nonnull String underlyingStorePath, String archiveComment) {
194-
this(Paths.get(underlyingStorePath), archiveComment);
195-
}
196-
197-
public BufferedZipStore(@Nonnull String underlyingStorePath) {
198-
this(underlyingStorePath, null);
199-
}
200-
201-
public BufferedZipStore(@Nonnull StoreHandle underlyingStore, @Nonnull Store.ListableStore bufferStore, boolean flushOnWrite) {
202-
this(underlyingStore, bufferStore, null, flushOnWrite);
203-
}
204-
205-
public BufferedZipStore(@Nonnull StoreHandle underlyingStore, String archiveComment, boolean flushOnWrite) {
206-
this(underlyingStore, new MemoryStore(), archiveComment, flushOnWrite);
207-
}
208-
209-
public BufferedZipStore(@Nonnull StoreHandle underlyingStore, boolean flushOnWrite) {
210-
this(underlyingStore, (String) null, flushOnWrite);
211-
}
212-
213-
public BufferedZipStore(@Nonnull Path underlyingStore, String archiveComment, boolean flushOnWrite) {
214-
this(new FilesystemStore(underlyingStore.getParent()).resolve(underlyingStore.getFileName().toString()), archiveComment, flushOnWrite);
215-
}
216-
217-
public BufferedZipStore(@Nonnull Path underlyingStore, boolean flushOnWrite) {
218-
this(underlyingStore, null, flushOnWrite);
219-
}
220-
221-
public BufferedZipStore(@Nonnull String underlyingStorePath, String archiveComment, boolean flushOnWrite) {
222-
this(Paths.get(underlyingStorePath), archiveComment, flushOnWrite);
223-
}
224-
225-
public BufferedZipStore(@Nonnull String underlyingStorePath, boolean flushOnWrite) {
226-
this(underlyingStorePath, null, flushOnWrite);
227-
}
228-
229-
230224
/**
231-
* Flushes the buffer and archiveComment to the underlying store as a zip file.
225+
* Flushes the buffer and archiveComment to the underlying store as a zip file.
232226
*/
233227
public void flush() throws IOException {
234228
writeBuffer();
@@ -239,6 +233,13 @@ public String getArchiveComment() {
239233
return archiveComment;
240234
}
241235

236+
public void setArchiveComment(@Nullable String archiveComment) throws IOException {
237+
this.archiveComment = archiveComment;
238+
if (flushOnWrite) {
239+
writeBuffer();
240+
}
241+
}
242+
242243
@Override
243244
public Stream<String[]> list(String[] keys) {
244245
return bufferStore.list(keys);

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

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
package dev.zarr.zarrjava.store;
22

33
import dev.zarr.zarrjava.utils.Utils;
4+
import org.apache.commons.io.input.BoundedInputStream;
45

56
import javax.annotation.Nonnull;
67
import javax.annotation.Nullable;
7-
import org.apache.commons.io.input.BoundedInputStream;
8-
98
import java.io.IOException;
109
import java.io.InputStream;
1110
import java.nio.ByteBuffer;
@@ -110,26 +109,27 @@ public void set(String[] keys, ByteBuffer bytes) {
110109
}
111110
}
112111

113-
@Override
114-
public void delete(String[] keys) {
115-
try {
116-
Files.delete(resolveKeys(keys));
117-
} catch (NoSuchFileException e) {
118-
// ignore
119-
} catch (IOException e) {
120-
throw new RuntimeException(e);
112+
@Override
113+
public void delete(String[] keys) {
114+
try {
115+
Files.delete(resolveKeys(keys));
116+
} catch (NoSuchFileException e) {
117+
// ignore
118+
} catch (IOException e) {
119+
throw new RuntimeException(e);
120+
}
121121
}
122-
}
123-
public Stream<String[]> list(String[] keys) {
122+
123+
public Stream<String[]> list(String[] keys) {
124124
try {
125125
return Files.list(resolveKeys(keys)).map(path -> {
126-
Path relativePath = resolveKeys(keys).relativize(path);
127-
String[] parts = new String[relativePath.getNameCount()];
128-
for (int i = 0; i < relativePath.getNameCount(); i++) {
129-
parts[i] = relativePath.getName(i).toString();
130-
}
131-
return parts;
132-
});
126+
Path relativePath = resolveKeys(keys).relativize(path);
127+
String[] parts = new String[relativePath.getNameCount()];
128+
for (int i = 0; i < relativePath.getNameCount(); i++) {
129+
parts[i] = relativePath.getName(i).toString();
130+
}
131+
return parts;
132+
});
133133
} catch (IOException e) {
134134
throw new RuntimeException(e);
135135
}

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

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,12 @@
11
package dev.zarr.zarrjava.store;
22

3-
import com.squareup.okhttp.Call;
4-
import com.squareup.okhttp.OkHttpClient;
5-
import com.squareup.okhttp.Request;
6-
import com.squareup.okhttp.Response;
7-
import com.squareup.okhttp.ResponseBody;
8-
9-
import java.io.FilterInputStream;
10-
import java.io.IOException;
11-
import java.io.InputStream;
12-
import java.nio.ByteBuffer;
133
import com.squareup.okhttp.*;
144

155
import javax.annotation.Nonnull;
166
import javax.annotation.Nullable;
7+
import java.io.FilterInputStream;
178
import java.io.IOException;
9+
import java.io.InputStream;
1810
import java.nio.ByteBuffer;
1911

2012
public class HttpStore implements Store {
@@ -106,10 +98,10 @@ public StoreHandle resolve(String... keys) {
10698
return new StoreHandle(this, keys);
10799
}
108100

109-
@Override
110-
public String toString() {
111-
return uri;
112-
}
101+
@Override
102+
public String toString() {
103+
return uri;
104+
}
113105

114106
@Override
115107
@Nullable
@@ -138,6 +130,7 @@ public void close() throws IOException {
138130
return null;
139131
}
140132
}
133+
141134
@Override
142135
public long getSize(String[] keys) {
143136
// Explicitly request "identity" encoding to prevent OkHttp from adding "gzip"

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

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -61,17 +61,17 @@ public void delete(String[] keys) {
6161
}
6262

6363
public Stream<String[]> list(String[] keys) {
64-
List<String> prefix = resolveKeys(keys);
65-
Set<List<String>> allKeys = new HashSet<>();
66-
67-
for (List<String> k : map.keySet()) {
68-
if (k.size() <= prefix.size() || ! k.subList(0, prefix.size()).equals(prefix))
69-
continue;
70-
for (int i = prefix.size(); i < k.size(); i++) {
71-
allKeys.add(k.subList(0, i+1));
72-
}
73-
}
74-
return allKeys.stream().map(k -> k.toArray(new String[0]));
64+
List<String> prefix = resolveKeys(keys);
65+
Set<List<String>> allKeys = new HashSet<>();
66+
67+
for (List<String> k : map.keySet()) {
68+
if (k.size() <= prefix.size() || !k.subList(0, prefix.size()).equals(prefix))
69+
continue;
70+
for (int i = prefix.size(); i < k.size(); i++) {
71+
allKeys.add(k.subList(0, i + 1));
72+
}
73+
}
74+
return allKeys.stream().map(k -> k.toArray(new String[0]));
7575
}
7676

7777
@Nonnull
@@ -91,7 +91,7 @@ public InputStream getInputStream(String[] keys, long start, long end) {
9191
if (bytes == null) return null;
9292
if (end < 0) end = bytes.length;
9393
if (end > Integer.MAX_VALUE) throw new IllegalArgumentException("End index too large");
94-
return new java.io.ByteArrayInputStream(bytes, (int) start, (int)(end - start));
94+
return new java.io.ByteArrayInputStream(bytes, (int) start, (int) (end - start));
9595
}
9696

9797
@Override

0 commit comments

Comments
 (0)