Skip to content

Commit 36612c5

Browse files
committed
refactoring
1 parent 06c7f3d commit 36612c5

23 files changed

+370
-57
lines changed

pom.xml

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,26 +21,27 @@
2121
<artifactId>jackson-databind</artifactId>
2222
<version>${jackson.version}</version>
2323
</dependency>
24-
<dependency>
25-
<groupId>com.fasterxml.jackson.core</groupId>
26-
<artifactId>jackson-annotations</artifactId>
27-
<version>${jackson.version}</version>
28-
</dependency>
29-
<dependency>
30-
<groupId>com.fasterxml.jackson.core</groupId>
31-
<artifactId>jackson-core</artifactId>
32-
<version>${jackson.version}</version>
33-
</dependency>
3424
<dependency>
3525
<groupId>com.fasterxml.jackson.datatype</groupId>
3626
<artifactId>jackson-datatype-jdk8</artifactId>
3727
<version>${jackson.version}</version>
3828
</dependency>
29+
<dependency>
30+
<groupId>org.lasersonlab</groupId>
31+
<artifactId>jblosc</artifactId>
32+
<version>1.0.1</version>
33+
</dependency>
3934
<dependency>
4035
<groupId>com.squareup.okhttp</groupId>
4136
<artifactId>okhttp</artifactId>
4237
<version>2.7.5</version>
4338
</dependency>
39+
<dependency>
40+
<groupId>junit</groupId>
41+
<artifactId>junit</artifactId>
42+
<version>4.13.1</version>
43+
<scope>test</scope>
44+
</dependency>
4445
</dependencies>
4546

4647
</project>
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package dev.zarr.zarrjava.indexing;
2+
3+
import java.util.Arrays;
4+
5+
public class Indexer {
6+
public static long[][] computeChunkCoords(long[] shape, int[] chunkShape, int[] bufferShape, long[] offset) {
7+
final int depth = shape.length;
8+
long[] start = new long[depth];
9+
long[] end = new long[depth];
10+
int numChunks = 1;
11+
for (int i = 0; i < depth; i++) {
12+
final int staIdx = (int)(offset[i] / chunkShape[i]);
13+
final int endIdx = (int)((offset[i] + bufferShape[i] - 1) / chunkShape[i]);
14+
numChunks *= (endIdx - staIdx + 1);
15+
start[i] = staIdx;
16+
end[i] = endIdx;
17+
}
18+
19+
final long[][] chunkIndices = new long[numChunks][];
20+
21+
final long[] currentIdx = Arrays.copyOf(start, depth);
22+
for (int i = 0; i < chunkIndices.length; i++) {
23+
chunkIndices[i] = Arrays.copyOf(currentIdx, depth);
24+
int depthIdx = depth - 1;
25+
while (depthIdx >= 0) {
26+
if (currentIdx[depthIdx] >= end[depthIdx]) {
27+
currentIdx[depthIdx] = start[depthIdx];
28+
depthIdx--;
29+
} else {
30+
currentIdx[depthIdx]++;
31+
depthIdx = -1;
32+
}
33+
}
34+
}
35+
return chunkIndices;
36+
}
37+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package dev.zarr.zarrjava.indexing;
2+
3+
public class OpenSlice {
4+
public Long start;
5+
public Long end;
6+
7+
public OpenSlice(long start, long end) {
8+
this.start = start;
9+
this.end = end;
10+
}
11+
12+
public StrictSlice normalize(long length) {
13+
long _start = 0;
14+
long _end = 0;
15+
if (start != null) {
16+
_start = start;
17+
if (_start < 0) {
18+
_start += length;
19+
if (_start < 0) {
20+
_start = 0;
21+
}
22+
}
23+
}
24+
if (end != null) {
25+
_end = end;
26+
if (_end < 0) {
27+
_end += length;
28+
if (_end < 0) {
29+
_end = 0;
30+
}
31+
}
32+
}
33+
return new StrictSlice(_start, _end);
34+
}
35+
36+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package dev.zarr.zarrjava.indexing;
2+
3+
public class Selector {
4+
public OpenSlice[] value;
5+
public Selector(int ndim) {
6+
value = new OpenSlice[ndim];
7+
}
8+
9+
public void assertDimensions(int ndim) {
10+
assert ndim == value.length;
11+
}
12+
13+
public StrictSlice[] normalize(long[] shape) {
14+
assert shape.length == value.length;
15+
StrictSlice[] output = new StrictSlice[shape.length];
16+
for (int i = 0; i < shape.length; i++) {
17+
output[i] = value[i].normalize(shape[i]);
18+
}
19+
return output;
20+
}
21+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package dev.zarr.zarrjava.indexing;
2+
3+
public class StrictSlice {
4+
public long start;
5+
public long end;
6+
7+
public StrictSlice(long start, long end) {
8+
this.start = start;
9+
this.end = end;
10+
}
11+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package dev.zarr.zarrjava.store;
2+
3+
import dev.zarr.zarrjava.indexing.Selector;
4+
5+
public class BufferValueHandle extends ValueHandle {
6+
7+
byte[] bytes;
8+
public BufferValueHandle(byte[] bytes) {
9+
this.bytes=bytes;
10+
}
11+
12+
@Override
13+
public ValueHandle get(Selector selector) {
14+
return null;
15+
}
16+
17+
@Override
18+
public void set(Selector selector, ValueHandle value) {
19+
20+
}
21+
22+
@Override
23+
public byte[] toBytes() {
24+
return bytes;
25+
}
26+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package dev.zarr.zarrjava.store;
2+
3+
import dev.zarr.zarrjava.indexing.Selector;
4+
5+
import java.util.Optional;
6+
7+
public class FileValueHandle extends ValueHandle {
8+
Store store;
9+
String key;
10+
11+
public FileValueHandle(Store store, String key) {
12+
this.store = store;
13+
this.key = key;
14+
}
15+
16+
@Override
17+
public ValueHandle get(Selector selector) {
18+
if (selector.value.length != 1) throw new AssertionError();
19+
Optional<BufferValueHandle> valueHandle =
20+
store.get(key, selector.value[0]).map(BufferValueHandle::new);
21+
if (valueHandle.isPresent()) {
22+
return valueHandle.get();
23+
}
24+
return new NoneHandle();
25+
}
26+
27+
@Override
28+
public void set(Selector selector, ValueHandle value) {
29+
30+
}
31+
32+
@Override
33+
public byte[] toBytes() {
34+
return store.get(key, null).orElse(null);
35+
}
36+
}

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

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

3+
import dev.zarr.zarrjava.indexing.OpenSlice;
4+
35
import java.io.IOException;
46
import java.nio.ByteBuffer;
57
import java.nio.channels.SeekableByteChannel;
@@ -22,8 +24,10 @@ public FilesystemStore(String path) {
2224
}
2325

2426
@Override
25-
public Optional<byte[]> get(String key, ByteRange byteRange) {
27+
public Optional<byte[]> get(String key, OpenSlice byteRange) {
2628
Path keyPath = fileSystem.getPath(this.path, key);
29+
30+
System.out.println(keyPath);
2731
if (byteRange == null) {
2832
try {
2933
byte[] bytes = Files.readAllBytes(keyPath);
@@ -60,7 +64,7 @@ public Optional<byte[]> get(String key, ByteRange byteRange) {
6064
}
6165

6266
@Override
63-
public void set(String key, byte[] bytes, ByteRange byteRange) {
67+
public void set(String key, byte[] bytes, OpenSlice byteRange) {
6468
Path keyPath = fileSystem.getPath(this.path, key);
6569
try {
6670
Files.createDirectories(keyPath.getParent());
@@ -102,4 +106,5 @@ public List<String> list(String key) {
102106
public String toString() {
103107
return fileSystem.getPath(this.path).toUri().toString().replaceAll("\\/$", "");
104108
}
109+
105110
}

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import com.squareup.okhttp.OkHttpClient;
55
import com.squareup.okhttp.Request;
66
import com.squareup.okhttp.Response;
7+
import dev.zarr.zarrjava.indexing.OpenSlice;
78

89
import java.io.IOException;
910
import java.util.List;
@@ -19,12 +20,12 @@ public HttpStore(String path) {
1920
this.path = path;
2021
}
2122

22-
private String getRangeHeader(ByteRange byteRange) {
23+
private String getRangeHeader(OpenSlice byteRange) {
2324
if (byteRange.start != null) {
2425
if (byteRange.start < 0) {
2526
return String.format("bytes=-%d", -byteRange.start);
2627
} else if (byteRange.end != null && byteRange.end > 0) {
27-
return String.format("bytes=%d-%d", byteRange.start, byteRange.end);
28+
return String.format("bytes=%d-%d", byteRange.start, byteRange.end + 1);
2829
} else {
2930
return String.format("bytes=%d", byteRange.start);
3031
}
@@ -33,7 +34,7 @@ private String getRangeHeader(ByteRange byteRange) {
3334
}
3435

3536
@Override
36-
public Optional<byte[]> get(String key, ByteRange byteRange) {
37+
public Optional<byte[]> get(String key, OpenSlice byteRange) {
3738
Request.Builder builder = new Request.Builder()
3839
.url(path + "/" + key);
3940

@@ -53,7 +54,7 @@ public Optional<byte[]> get(String key, ByteRange byteRange) {
5354
}
5455

5556
@Override
56-
public void set(String key, byte[] bytes, ByteRange byteRange) {
57+
public void set(String key, byte[] bytes, OpenSlice byteRange) {
5758
throw new UnsupportedOperationException("Not implemented");
5859
}
5960

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package dev.zarr.zarrjava.store;
2+
3+
import dev.zarr.zarrjava.indexing.Selector;
4+
5+
public class NoneHandle extends ValueHandle{
6+
@Override
7+
public ValueHandle get(Selector selector) {
8+
return this;
9+
}
10+
11+
@Override
12+
public void set(Selector selector, ValueHandle value) {
13+
}
14+
15+
@Override
16+
public byte[] toBytes() {
17+
return null;
18+
}
19+
}

0 commit comments

Comments
 (0)