Skip to content

Commit 6094489

Browse files
committed
Array.create and Group.create with no store/path arguments should default to a new MemoryStore
1 parent dbf7541 commit 6094489

File tree

5 files changed

+163
-20
lines changed

5 files changed

+163
-20
lines changed

src/main/java/dev/zarr/zarrjava/v2/Array.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import dev.zarr.zarrjava.ZarrException;
66
import dev.zarr.zarrjava.core.Attributes;
77
import dev.zarr.zarrjava.store.FilesystemStore;
8+
import dev.zarr.zarrjava.store.MemoryStore;
89
import dev.zarr.zarrjava.store.StoreHandle;
910
import dev.zarr.zarrjava.utils.Utils;
1011
import dev.zarr.zarrjava.core.codec.CodecPipeline;
@@ -87,6 +88,16 @@ public static Array open(String path) throws IOException, ZarrException {
8788
return open(Paths.get(path));
8889
}
8990

91+
/**
92+
* Creates a new Zarr array with the provided metadata in an in-memory store
93+
*
94+
* @param arrayMetadata the metadata of the Zarr array
95+
* @throws IOException if the metadata cannot be serialized
96+
* @throws ZarrException if the Zarr array cannot be created
97+
*/
98+
public static Array create(ArrayMetadata arrayMetadata) throws ZarrException, IOException {
99+
return create(new StoreHandle(new MemoryStore()), arrayMetadata);
100+
}
90101
/**
91102
* Creates a new Zarr array with the provided metadata at a specified storage location. This
92103
* method will raise an exception if a Zarr array already exists at the specified storage

src/main/java/dev/zarr/zarrjava/v2/Group.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import dev.zarr.zarrjava.ZarrException;
66
import dev.zarr.zarrjava.core.Attributes;
77
import dev.zarr.zarrjava.store.FilesystemStore;
8+
import dev.zarr.zarrjava.store.MemoryStore;
89
import dev.zarr.zarrjava.store.StoreHandle;
910
import dev.zarr.zarrjava.utils.Utils;
1011

@@ -49,6 +50,10 @@ public static Group open(String path) throws IOException {
4950
return open(Paths.get(path));
5051
}
5152

53+
public static Group create(@Nonnull GroupMetadata groupMetadata) throws IOException {
54+
return new Group(new MemoryStore().resolve(), groupMetadata).writeMetadata();
55+
}
56+
5257
public static Group create(
5358
@Nonnull StoreHandle storeHandle, @Nonnull GroupMetadata groupMetadata
5459
) throws IOException {

src/main/java/dev/zarr/zarrjava/v3/Array.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import dev.zarr.zarrjava.ZarrException;
55
import dev.zarr.zarrjava.core.Attributes;
66
import dev.zarr.zarrjava.store.FilesystemStore;
7+
import dev.zarr.zarrjava.store.MemoryStore;
78
import dev.zarr.zarrjava.store.StoreHandle;
89
import dev.zarr.zarrjava.utils.Utils;
910
import dev.zarr.zarrjava.core.codec.CodecPipeline;
@@ -72,6 +73,18 @@ public static Array open(String path) throws IOException, ZarrException {
7273
return open(Paths.get(path));
7374
}
7475

76+
/**
77+
* Creates a new Zarr array with the provided metadata in an in-memory store
78+
*
79+
* @param arrayMetadata the metadata of the Zarr array
80+
* @throws IOException if the metadata cannot be serialized
81+
* @throws ZarrException if the Zarr array cannot be created
82+
*/
83+
public static Array create(ArrayMetadata arrayMetadata)
84+
throws IOException, ZarrException {
85+
return Array.create(new MemoryStore().resolve(), arrayMetadata);
86+
}
87+
7588
/**
7689
* Creates a new Zarr array with the provided metadata at a specified storage location. This
7790
* method will raise an exception if a Zarr array already exists at the specified storage

src/main/java/dev/zarr/zarrjava/v3/Group.java

Lines changed: 127 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import dev.zarr.zarrjava.ZarrException;
55
import dev.zarr.zarrjava.core.Attributes;
66
import dev.zarr.zarrjava.store.FilesystemStore;
7+
import dev.zarr.zarrjava.store.MemoryStore;
78
import dev.zarr.zarrjava.store.StoreHandle;
89
import dev.zarr.zarrjava.utils.Utils;
910
import java.io.IOException;
@@ -30,48 +31,122 @@ protected Group(@Nonnull StoreHandle storeHandle, @Nonnull GroupMetadata groupMe
3031
public static Group open(@Nonnull StoreHandle storeHandle) throws IOException {
3132
StoreHandle metadataHandle = storeHandle.resolve(ZARR_JSON);
3233
ByteBuffer metadataBytes = metadataHandle.readNonNull();
33-
return new Group(storeHandle, makeObjectMapper()
34-
.readValue(Utils.toArray(metadataBytes), GroupMetadata.class));
34+
return new Group(storeHandle, makeObjectMapper().readValue(Utils.toArray(metadataBytes), GroupMetadata.class));
3535
}
3636

37-
38-
public static Group open(Path path) throws IOException {
39-
return open(new StoreHandle(new FilesystemStore(path)));
40-
}
4137

42-
public static Group open(String path) throws IOException {
43-
return open(Paths.get(path));
44-
}
45-
46-
public static Group create(
47-
@Nonnull StoreHandle storeHandle, @Nonnull GroupMetadata groupMetadata
48-
) throws IOException {
38+
public static Group open(Path path) throws IOException {
39+
return open(new StoreHandle(new FilesystemStore(path)));
40+
}
41+
42+
public static Group open(String path) throws IOException {
43+
return open(Paths.get(path));
44+
}
45+
46+
/**
47+
* Creates a new Zarr group with default metadata in an in-memory store.
48+
*
49+
* @throws IOException if the metadata cannot be serialized
50+
*/
51+
public static Group create() throws IOException {
52+
return new Group(new MemoryStore().resolve(), GroupMetadata.defaultValue()).writeMetadata();
53+
}
54+
55+
/**
56+
* Creates a new Zarr group with the provided metadata in an in-memory store.
57+
*
58+
* @param attributes the attributes of the Zarr group
59+
* @throws IOException if the metadata cannot be serialized
60+
* @throws ZarrException if the attributes are invalid
61+
*/
62+
public static Group create(@Nonnull Attributes attributes) throws IOException, ZarrException {
63+
return new Group(new MemoryStore().resolve(), new GroupMetadata(attributes)).writeMetadata();
64+
}
65+
66+
/**
67+
* Creates a new Zarr group with the provided metadata in an in-memory store.
68+
*
69+
* @param groupMetadata the metadata of the Zarr group
70+
* @throws IOException if the metadata cannot be serialized
71+
*/
72+
public static Group create(@Nonnull GroupMetadata groupMetadata) throws IOException {
73+
return new Group(new MemoryStore().resolve(), groupMetadata).writeMetadata();
74+
}
75+
76+
/**
77+
* Creates a new Zarr group with the provided metadata at a specified storage location.
78+
*
79+
* @param storeHandle the storage location of the Zarr group
80+
* @param groupMetadata the metadata of the Zarr group
81+
* @throws IOException if the metadata cannot be serialized
82+
*/
83+
public static Group create(@Nonnull StoreHandle storeHandle, @Nonnull GroupMetadata groupMetadata) throws IOException {
4984
return new Group(storeHandle, groupMetadata).writeMetadata();
5085
}
5186

87+
/**
88+
* Creates a new Zarr group with the provided metadata at a specified storage location.
89+
*
90+
* @param storeHandle the storage location of the Zarr group
91+
* @param attributes attributes of the Zarr group
92+
* @throws IOException if the metadata cannot be serialized
93+
* @throws ZarrException if the attributes are invalid
94+
*/
5295
public static Group create(
5396
@Nonnull StoreHandle storeHandle,
5497
@Nonnull Attributes attributes
5598
) throws IOException, ZarrException {
5699
return create(storeHandle, new GroupMetadata(attributes));
57100
}
58101

59-
public static Group create(@Nonnull StoreHandle storeHandle) throws IOException, ZarrException {
102+
/**
103+
* Creates a new Zarr group with the provided metadata at a specified storage location.
104+
*
105+
* @param storeHandle the storage location of the Zarr group
106+
* @throws IOException if the metadata cannot be serialized
107+
*/
108+
public static Group create(@Nonnull StoreHandle storeHandle) throws IOException {
60109
return create(storeHandle, GroupMetadata.defaultValue());
61110
}
62111

112+
/**
113+
* Creates a new Zarr group with the provided metadata at a specified storage location.
114+
*
115+
* @param path the storage location of the Zarr group
116+
* @param groupMetadata the metadata of the Zarr group
117+
* @throws IOException if the metadata cannot be serialized
118+
*/
63119
public static Group create(Path path, GroupMetadata groupMetadata) throws IOException, ZarrException {
64120
return create(new FilesystemStore(path).resolve(), groupMetadata);
65121
}
66122

123+
/**
124+
* Creates a new Zarr group with the provided metadata at a specified storage location.
125+
*
126+
* @param path the storage location of the Zarr group
127+
* @param groupMetadata the metadata of the Zarr group
128+
* @throws IOException if the metadata cannot be serialized
129+
*/
67130
public static Group create(String path, GroupMetadata groupMetadata) throws IOException, ZarrException {
68131
return create(Paths.get(path), groupMetadata);
69132
}
70133

134+
/**
135+
* Creates a new Zarr group with the provided metadata at a specified storage location.
136+
*
137+
* @param path the storage location of the Zarr group
138+
* @throws IOException if the metadata cannot be serialized
139+
*/
71140
public static Group create(Path path) throws IOException, ZarrException {
72141
return create(new FilesystemStore(path).resolve());
73142
}
74143

144+
/**
145+
* Creates a new Zarr group with the provided metadata at a specified storage location.
146+
*
147+
* @param path the storage location of the Zarr group
148+
* @throws IOException if the metadata cannot be serialized
149+
*/
75150
public static Group create(String path) throws IOException, ZarrException {
76151
return create(Paths.get(path));
77152
}
@@ -86,25 +161,59 @@ public Node get(String key) throws ZarrException, IOException{
86161
}
87162
}
88163

164+
/**
165+
* Creates a new subgroup with the provided metadata at the specified key.
166+
*
167+
* @param key the key of the new Zarr group within the current group
168+
* @param groupMetadata the metadata of the Zarr group
169+
* @throws IOException if the metadata cannot be serialized
170+
*/
89171
public Group createGroup(String key, GroupMetadata groupMetadata)
90-
throws IOException, ZarrException {
172+
throws IOException, ZarrException {
91173
return Group.create(storeHandle.resolve(key), groupMetadata);
92174
}
93175

176+
/**
177+
* Creates a new subgroup with the provided attributes at the specified key.
178+
*
179+
* @param key the key of the new Zarr group within the current group
180+
* @param attributes attributes of the Zarr group
181+
* @throws IOException if the metadata cannot be serialized
182+
*/
94183
public Group createGroup(String key, Attributes attributes)
95-
throws IOException, ZarrException {
184+
throws IOException, ZarrException {
96185
return Group.create(storeHandle.resolve(key), new GroupMetadata(attributes));
97186
}
98187

99-
public Group createGroup(String key) throws IOException, ZarrException {
188+
/**
189+
* Creates a new subgroup with default metadata at the specified key.
190+
*
191+
* @param key the key of the new Zarr group within the current group
192+
* @throws IOException if the metadata cannot be serialized
193+
*/
194+
public Group createGroup(String key) throws IOException {
100195
return Group.create(storeHandle.resolve(key), GroupMetadata.defaultValue());
101196
}
102197

198+
/**
199+
* Creates a new array with the provided metadata at the specified key.
200+
*
201+
* @param key the key of the new Zarr array within the current group
202+
* @param arrayMetadata the metadata of the Zarr array
203+
* @throws IOException if the metadata cannot be serialized
204+
*/
103205
public Array createArray(String key, ArrayMetadata arrayMetadata)
104-
throws IOException, ZarrException {
206+
throws IOException, ZarrException {
105207
return Array.create(storeHandle.resolve(key), arrayMetadata);
106208
}
107209

210+
/**
211+
* Creates a new array with the provided metadata builder mapper at the specified key.
212+
*
213+
* @param key the key of the new Zarr array within the current group
214+
* @param arrayMetadataBuilderMapper a function building the metadata of the Zarr array
215+
* @throws IOException if the metadata cannot be serialized
216+
*/
108217
public Array createArray(String key,
109218
Function<ArrayMetadataBuilder, ArrayMetadataBuilder> arrayMetadataBuilderMapper)
110219
throws IOException, ZarrException {

src/main/java/dev/zarr/zarrjava/v3/GroupMetadata.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,13 @@ public GroupMetadata(
4444
this.attributes = attributes;
4545
}
4646

47-
public static GroupMetadata defaultValue() throws ZarrException {
48-
return new GroupMetadata(ZARR_FORMAT, NODE_TYPE, new Attributes());
47+
public static GroupMetadata defaultValue() {
48+
try {
49+
return new GroupMetadata(ZARR_FORMAT, NODE_TYPE, new Attributes());
50+
} catch (ZarrException e) {
51+
// This should never happen
52+
throw new RuntimeException(e);
53+
}
4954
}
5055

5156
@Override

0 commit comments

Comments
 (0)