Skip to content

Commit 6259244

Browse files
committed
refactoring
1 parent a7d090a commit 6259244

28 files changed

+771
-331
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,5 @@ build/
3636

3737

3838
### Custom ###
39-
/l4_sample
39+
/testdata
40+
/testoutput
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.scalableminds.zarrjava;
2+
3+
public class ZarrException extends Exception {
4+
public ZarrException(String message, Throwable cause) {
5+
super(message, cause);
6+
}
7+
8+
public ZarrException(String message) {
9+
super(message);
10+
}
11+
}

src/main/java/com/scalableminds/zarrjava/store/FilesystemStore.java

Lines changed: 42 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.scalableminds.zarrjava.store;
22

3-
import com.scalableminds.zarrjava.v3.Utils;
3+
import com.scalableminds.zarrjava.utils.Utils;
44

55
import javax.annotation.Nonnull;
66
import javax.annotation.Nullable;
@@ -14,33 +14,43 @@
1414
public class FilesystemStore implements Store, Store.ListableStore {
1515

1616
@Nonnull
17-
private final FileSystem fileSystem;
18-
@Nonnull
19-
private final String path;
17+
private final Path path;
2018

21-
public FilesystemStore(@Nonnull String path) {
22-
this.fileSystem = FileSystems.getDefault();
19+
public FilesystemStore(@Nonnull Path path) {
2320
this.path = path;
2421
}
2522

23+
public FilesystemStore(@Nonnull String path) {
24+
this.path = Paths.get(path);
25+
}
26+
27+
Path resolveKeys(String[] keys) {
28+
Path newPath = path;
29+
for (String key : keys) {
30+
newPath = newPath.resolve(key);
31+
}
32+
return newPath;
33+
}
34+
35+
@Override
36+
public boolean exists(String[] keys) {
37+
return Files.exists(resolveKeys(keys));
38+
}
39+
2640
@Nullable
2741
@Override
28-
public ByteBuffer get(String key) {
29-
Path keyPath = fileSystem.getPath(this.path, key);
42+
public ByteBuffer get(String[] keys) {
3043
try {
31-
ByteBuffer bytes = ByteBuffer.wrap(Files.readAllBytes(keyPath));
32-
return bytes;
44+
return ByteBuffer.wrap(Files.readAllBytes(resolveKeys(keys)));
3345
} catch (IOException e) {
3446
return null;
3547
}
3648
}
3749

3850
@Nullable
3951
@Override
40-
public ByteBuffer get(String key, long start) {
41-
Path keyPath = fileSystem.getPath(this.path, key);
42-
43-
try (SeekableByteChannel byteChannel = Files.newByteChannel(keyPath)) {
52+
public ByteBuffer get(String[] keys, long start) {
53+
try (SeekableByteChannel byteChannel = Files.newByteChannel(resolveKeys(keys))) {
4454
long startOffset = 0;
4555
if (start >= 0) {
4656
startOffset = start;
@@ -60,10 +70,8 @@ public ByteBuffer get(String key, long start) {
6070

6171
@Nullable
6272
@Override
63-
public ByteBuffer get(String key, long start, long end) {
64-
Path keyPath = fileSystem.getPath(this.path, key);
65-
66-
try (SeekableByteChannel byteChannel = Files.newByteChannel(keyPath)) {
73+
public ByteBuffer get(String[] keys, long start, long end) {
74+
try (SeekableByteChannel byteChannel = Files.newByteChannel(resolveKeys(keys))) {
6775
long startOffset = 0;
6876
if (start >= 0) {
6977
startOffset = start;
@@ -82,11 +90,10 @@ public ByteBuffer get(String key, long start, long end) {
8290

8391

8492
@Override
85-
public void set(String key, ByteBuffer bytes) {
86-
Path keyPath = fileSystem.getPath(this.path, key);
93+
public void set(String[] keys, ByteBuffer bytes) {
94+
Path keyPath = resolveKeys(keys);
8795
try {
8896
Files.createDirectories(keyPath.getParent());
89-
// Files.createFile(keyPath);
9097
} catch (IOException e) {
9198
throw new RuntimeException(e);
9299
}
@@ -99,27 +106,33 @@ public void set(String key, ByteBuffer bytes) {
99106
}
100107

101108
@Override
102-
public void delete(String key) {
103-
Path keyPath = fileSystem.getPath(this.path, key);
109+
public void delete(String[] keys) {
104110
try {
105-
Files.delete(keyPath);
111+
Files.delete(resolveKeys(keys));
112+
} catch (NoSuchFileException e) {
113+
// ignore
106114
} catch (IOException e) {
107115
throw new RuntimeException(e);
108116
}
109117
}
110118

111-
public Iterator<String> list(String key) {
112-
Path keyPath = fileSystem.getPath(this.path, key);
113-
try (Stream<Path> paths = Files.list(keyPath)) {
114-
return paths.map(p -> p.toFile().getName()).iterator();
119+
public String[] list(String[] keys) {
120+
try (Stream<Path> paths = Files.list(resolveKeys(keys))) {
121+
return paths.map(p -> p.toFile().getName()).toArray(String[]::new);
115122
} catch (IOException e) {
116123
throw new RuntimeException(e);
117124
}
118125
}
119126

127+
@Nonnull
128+
@Override
129+
public StoreHandle resolve(String... keys) {
130+
return new StoreHandle(this, keys);
131+
}
132+
120133
@Override
121134
public String toString() {
122-
return fileSystem.getPath(this.path).toUri().toString().replaceAll("\\/$", "");
135+
return this.path.toUri().toString().replaceAll("\\/$", "");
123136
}
124137

125138
}
Lines changed: 41 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.scalableminds.zarrjava.store;
22

3+
import com.scalableminds.zarrjava.ZarrException;
34
import com.squareup.okhttp.*;
45

56
import javax.annotation.Nonnull;
@@ -12,11 +13,19 @@ public class HttpStore implements Store {
1213
@Nonnull
1314
private final OkHttpClient httpClient;
1415
@Nonnull
15-
private final String path;
16+
private final String uri;
1617

17-
public HttpStore(@Nonnull String path) {
18+
public HttpStore(@Nonnull String uri) {
1819
this.httpClient = new OkHttpClient();
19-
this.path = path;
20+
this.uri = uri;
21+
}
22+
23+
String resolveKeys(String[] keys) {
24+
String newUri = uri.replaceAll("\\/+$", "");
25+
for (String key : keys) {
26+
newUri = newUri + "/" + key;
27+
}
28+
return newUri;
2029
}
2130

2231
@Nullable
@@ -32,44 +41,63 @@ ByteBuffer get(Request request) {
3241
}
3342
}
3443

44+
@Override
45+
public boolean exists(String[] keys) {
46+
Request request = new Request.Builder().head().url(resolveKeys(keys)).build();
47+
Call call = httpClient.newCall(request);
48+
try {
49+
Response response = call.execute();
50+
return response.isSuccessful();
51+
} catch (IOException e) {
52+
return false;
53+
}
54+
}
55+
3556
@Nullable
3657
@Override
37-
public ByteBuffer get(String key) {
38-
Request request = new Request.Builder().url(path + "/" + key).build();
58+
public ByteBuffer get(String[] keys) {
59+
Request request = new Request.Builder().url(resolveKeys(keys)).build();
3960
return get(request);
4061
}
4162

4263
@Nullable
4364
@Override
44-
public ByteBuffer get(String key, long start) {
45-
Request request = new Request.Builder().url(path + "/" + key).header("Range",
65+
public ByteBuffer get(String[] keys, long start) {
66+
Request request = new Request.Builder().url(resolveKeys(keys)).header("Range",
4667
start < 0 ? String.format("Bytes=%d", start) : String.format("Bytes=%d-", start)).build();
4768

4869
return get(request);
4970
}
5071

5172
@Nullable
5273
@Override
53-
public ByteBuffer get(String key, long start, long end) {
54-
assert start >= 0;
55-
Request request = new Request.Builder().url(path + "/" + key).header("Range",
74+
public ByteBuffer get(String[] keys, long start, long end) {
75+
if (start < 0) {
76+
throw new IllegalArgumentException("Argument 'start' needs to be non-negative.");
77+
}
78+
Request request = new Request.Builder().url(resolveKeys(keys)).header("Range",
5679
String.format("Bytes=%d-%d", start, end + 1)).build();
5780
return get(request);
5881
}
5982

6083
@Override
61-
public void set(String key, ByteBuffer bytes) {
84+
public void set(String[] keys, ByteBuffer bytes) {
6285
throw new UnsupportedOperationException("Not implemented");
6386
}
6487

6588
@Override
66-
public void delete(String key) {
89+
public void delete(String[] keys) {
6790
throw new UnsupportedOperationException("Not implemented");
6891
}
6992

93+
@Nonnull
94+
@Override
95+
public StoreHandle resolve(String... keys) {
96+
return new StoreHandle(this, keys);
97+
}
7098

7199
@Override
72100
public String toString() {
73-
return path;
101+
return uri.toString();
74102
}
75103
}

src/main/java/com/scalableminds/zarrjava/store/S3Store.java

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,14 @@
44
import com.amazonaws.services.s3.model.GetObjectRequest;
55
import com.amazonaws.services.s3.model.ObjectMetadata;
66
import com.amazonaws.services.s3.model.S3ObjectInputStream;
7-
import com.scalableminds.zarrjava.v3.Utils;
7+
import com.scalableminds.zarrjava.utils.Utils;
88

99
import javax.annotation.Nonnull;
1010
import javax.annotation.Nullable;
1111
import java.io.ByteArrayInputStream;
1212
import java.io.IOException;
1313
import java.io.InputStream;
1414
import java.nio.ByteBuffer;
15-
import java.util.Iterator;
1615

1716
public class S3Store implements Store, Store.ListableStore {
1817
@Nonnull
@@ -28,14 +27,14 @@ public S3Store(@Nonnull AmazonS3 s3client, @Nonnull String bucketName, @Nullable
2827
this.prefix = prefix;
2928
}
3029

31-
String dereferencePath(String key) {
30+
String resolveKeys(String[] keys) {
3231
if (prefix == null) {
33-
return key;
32+
return String.join("/", keys);
3433
}
35-
if (key == null || key.length() == 0) {
34+
if (keys == null || keys.length == 0) {
3635
return prefix;
3736
}
38-
return prefix + "/" + key;
37+
return prefix + "/" + String.join("/", keys);
3938
}
4039

4140
@Nullable
@@ -47,43 +46,54 @@ ByteBuffer get(GetObjectRequest getObjectRequest) {
4746
}
4847
}
4948

49+
@Override
50+
public boolean exists(String[] keys) {
51+
return s3client.doesObjectExist(bucketName, resolveKeys(keys));
52+
}
53+
5054
@Nullable
5155
@Override
52-
public ByteBuffer get(String key) {
53-
return get(new GetObjectRequest(bucketName, dereferencePath(key)));
56+
public ByteBuffer get(String[] keys) {
57+
return get(new GetObjectRequest(bucketName, resolveKeys(keys)));
5458
}
5559

5660
@Nullable
5761
@Override
58-
public ByteBuffer get(String key, long start) {
59-
return get(new GetObjectRequest(bucketName, dereferencePath(key)).withRange(start));
62+
public ByteBuffer get(String[] keys, long start) {
63+
return get(new GetObjectRequest(bucketName, resolveKeys(keys)).withRange(start));
6064
}
6165

6266
@Nullable
6367
@Override
64-
public ByteBuffer get(String key, long start, long end) {
65-
return get(new GetObjectRequest(bucketName, dereferencePath(key)).withRange(start, end));
68+
public ByteBuffer get(String[] keys, long start, long end) {
69+
return get(new GetObjectRequest(bucketName, resolveKeys(keys)).withRange(start, end));
6670
}
6771

6872
@Override
69-
public void set(String key, ByteBuffer bytes) {
73+
public void set(String[] keys, ByteBuffer bytes) {
7074
try (InputStream byteStream = new ByteArrayInputStream(bytes.array())) {
71-
s3client.putObject(bucketName, dereferencePath(key), byteStream, new ObjectMetadata());
75+
s3client.putObject(bucketName, resolveKeys(keys), byteStream, new ObjectMetadata());
7276
} catch (IOException e) {
7377
throw new RuntimeException(e);
7478
}
7579
}
7680

7781
@Override
78-
public void delete(String key) {
79-
s3client.deleteObject(bucketName, dereferencePath(key));
82+
public void delete(String[] keys) {
83+
s3client.deleteObject(bucketName, resolveKeys(keys));
8084
}
8185

8286
@Override
83-
public Iterator<String> list(String key) {
84-
final String fullKey = dereferencePath(key);
87+
public String[] list(String[] keys) {
88+
final String fullKey = resolveKeys(keys);
8589
return s3client.listObjects(bucketName, fullKey).getObjectSummaries().stream().map(
86-
p -> p.getKey().substring(fullKey.length() + 1)).iterator();
90+
p -> p.getKey().substring(fullKey.length() + 1)).toArray(String[]::new);
91+
}
92+
93+
@Nonnull
94+
@Override
95+
public StoreHandle resolve(String... keys) {
96+
return new StoreHandle(this, keys);
8797
}
8898

8999
@Override
Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,31 @@
11
package com.scalableminds.zarrjava.store;
22

3+
import javax.annotation.Nonnull;
34
import javax.annotation.Nullable;
45
import java.nio.ByteBuffer;
56
import java.util.Iterator;
7+
import java.util.stream.Stream;
68

79
public interface Store {
10+
boolean exists(String[] keys);
11+
812
@Nullable
9-
ByteBuffer get(String key);
13+
ByteBuffer get(String[] keys);
1014

1115
@Nullable
12-
ByteBuffer get(String key, long start);
16+
ByteBuffer get(String[] keys, long start);
1317

1418
@Nullable
15-
ByteBuffer get(String key, long start, long end);
19+
ByteBuffer get(String[] keys, long start, long end);
20+
21+
void set(String[] keys, ByteBuffer bytes);
1622

17-
void set(String key, ByteBuffer bytes);
23+
void delete(String[] keys);
1824

19-
void delete(String key);
25+
@Nonnull
26+
StoreHandle resolve(String ...keys);
2027

2128
interface ListableStore extends Store {
22-
Iterator<String> list(String key);
29+
String[] list(String[] keys);
2330
}
2431
}

0 commit comments

Comments
 (0)