Skip to content

Commit fd68743

Browse files
committed
Implement {to/of}_yojson functions for Metadata types.
Implementing these functions manually allows us to have more flexibility regarding how much varification is done during parsing of the JSON metadata documents and very specific error reporting.
1 parent 4cb6563 commit fd68743

File tree

14 files changed

+607
-116
lines changed

14 files changed

+607
-116
lines changed

lib/codecs/array_to_bytes.ml

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ type error =
3535
[ `Bytes_encode_error of string
3636
| `Bytes_decode_error of string
3737
| `Sharding_shape_mismatch of int array * int array * string
38+
| Extensions.error
3839
| Array_to_array.error
3940
| Bytes_to_bytes.error ]
4041

@@ -259,21 +260,21 @@ end = struct
259260
(string, [> error]) result
260261
= fun x t ->
261262
let open Util in
263+
let open Extensions in
262264
let open Util.Result_syntax in
263265
let shard_shape = Ndarray.shape x in
264266
let cps = Array.map2 (/) shard_shape t.chunk_shape in
265267
let idx_shp = Array.append cps [|2|] in
266-
let shard_idx =
267-
Ndarray.create Bigarray.Int64 idx_shp Int64.max_int in
268-
let sg =
269-
Extensions.RegularGrid.create t.chunk_shape in
268+
let shard_idx = Ndarray.create Bigarray.Int64 idx_shp Int64.max_int in
269+
RegularGrid.create ~array_shape:shard_shape t.chunk_shape
270+
>>= fun grid ->
270271
let slice =
271272
Array.make
272273
(Ndarray.num_dims x) (Owl_types.R []) in
273274
let coords = Indexing.coords_of_slice slice shard_shape in
274275
let tbl = Arraytbl.create @@ Array.length coords in
275276
Ndarray.iteri (fun i y ->
276-
let k, c = Extensions.RegularGrid.index_coord_pair sg coords.(i) in
277+
let k, c = RegularGrid.index_coord_pair grid coords.(i) in
277278
Arraytbl.add tbl k (c, y)) x;
278279
let fill_value =
279280
Arraytbl.to_seq_values tbl
@@ -378,7 +379,8 @@ end = struct
378379
if Ndarray.for_all (Int64.equal Int64.max_int) shard_idx then
379380
Ok (Ndarray.create repr.kind repr.shape repr.fill_value)
380381
else
381-
let sg = RegularGrid.create t.chunk_shape in
382+
RegularGrid.create ~array_shape:repr.shape t.chunk_shape
383+
>>= fun sg ->
382384
let slice =
383385
Array.make
384386
(Array.length repr.shape)
@@ -416,10 +418,10 @@ end = struct
416418
inner.kind (Array.of_list res) repr.shape
417419

418420
let rec chain_to_yojson chain =
419-
[%to_yojson: Yojson.Safe.t list] @@
420-
List.map ArrayToArray.to_yojson chain.a2a @
421-
(ArrayToBytes.to_yojson chain.a2b) ::
422-
List.map BytesToBytes.to_yojson chain.b2b
421+
`List
422+
(List.map ArrayToArray.to_yojson chain.a2a @
423+
(ArrayToBytes.to_yojson chain.a2b) ::
424+
List.map BytesToBytes.to_yojson chain.b2b)
423425

424426
and to_yojson t =
425427
let codecs = chain_to_yojson t.codecs

lib/codecs/array_to_bytes.mli

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,12 @@ val pp_chain : Format.formatter -> chain -> unit
2424
val show_chain : chain -> string
2525

2626
type error =
27-
[ `Bytes_encode_error of string
28-
| `Bytes_decode_error of string
29-
| `Sharding_shape_mismatch of int array * int array * string
27+
[ Extensions.error
3028
| Array_to_array.error
31-
| Bytes_to_bytes.error ]
29+
| Bytes_to_bytes.error
30+
| `Bytes_encode_error of string
31+
| `Bytes_decode_error of string
32+
| `Sharding_shape_mismatch of int array * int array * string ]
3233

3334
module ArrayToBytes : sig
3435
val parse

lib/codecs/codecs.ml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,14 @@ module Chain = struct
5555
(fun c acc -> acc >>= ArrayToArray.decode c)
5656
t.a2a (ArrayToBytes.decode y repr' t.a2b)
5757

58-
let equal x y =
58+
let ( = ) x y =
5959
x.a2a = y.a2a && x.a2b = y.a2b && x.b2b = y.b2b
6060

6161
let to_yojson t =
62-
[%to_yojson: Yojson.Safe.t list] @@
63-
List.map ArrayToArray.to_yojson t.a2a @
64-
(ArrayToBytes.to_yojson t.a2b) ::
65-
List.map BytesToBytes.to_yojson t.b2b
62+
`List
63+
(List.map ArrayToArray.to_yojson t.a2a @
64+
(ArrayToBytes.to_yojson t.a2b) ::
65+
List.map BytesToBytes.to_yojson t.b2b)
6666

6767
let of_yojson x =
6868
let filter_partition f encoded =

lib/codecs/codecs.mli

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ module Chain : sig
5353
string ->
5454
(('a, 'b) Ndarray.t, [> error]) result
5555

56-
val equal : t -> t -> bool
56+
val ( = ) : t -> t -> bool
5757

5858
val of_yojson : Yojson.Safe.t -> (t, string) result
5959

lib/dune

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
(public_name zarr)
44
(libraries
55
yojson
6-
ppx_deriving_yojson.runtime
76
ezgzip
87
owl
98
stdint
@@ -12,9 +11,7 @@
1211
(:standard -O3))
1312
(preprocess
1413
(pps
15-
ppx_deriving.eq
16-
ppx_deriving.show
17-
ppx_deriving_yojson))
14+
ppx_deriving.show))
1815
(instrumentation
1916
(backend bisect_ppx)))
2017

lib/extensions.ml

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,25 @@
1+
type grid_info =
2+
{msg : string
3+
;chunk_shape : int array
4+
;array_shape : int array}
5+
6+
type error =
7+
[ `Grid of grid_info ]
8+
19
module RegularGrid = struct
210
type t = int array
311

412
let chunk_shape t = t
513

6-
let create chunk_shape = chunk_shape
14+
let create ~array_shape chunk_shape =
15+
match chunk_shape, array_shape with
16+
| c, a when Array.(length c <> length a) ->
17+
let msg = "grid chunk and array shape must have the same the length." in
18+
Result.error @@ `Grid {msg; array_shape; chunk_shape}
19+
| c, a when Util.(max c > max a) ->
20+
let msg = "grid chunk dimension size must not be larger than array's." in
21+
Result.error @@ `Grid {msg; array_shape; chunk_shape}
22+
| c, _ -> Ok c
723

824
let ceildiv x y =
925
Float.(to_int @@ ceil (of_int x /. of_int y))
@@ -27,7 +43,7 @@ module RegularGrid = struct
2743
|> Util.Indexing.cartesian_prod
2844
|> List.map Array.of_list
2945

30-
let equal x y = x = y
46+
let ( = ) x y = x = y
3147

3248
let to_yojson t =
3349
let chunk_shape =
@@ -54,7 +70,7 @@ module RegularGrid = struct
5470
"Regular grid chunk_shape must only contain positive integers."
5571
in
5672
Error msg) xs (Ok [])
57-
>>| fun l' -> Array.of_list l'
73+
>>| Array.of_list
5874
| _ -> Error "Invalid Chunk grid name or configuration."
5975
end
6076

@@ -83,7 +99,7 @@ module ChunkKeyEncoding = struct
8399
String.concat sep @@
84100
Array.fold_right f index []
85101

86-
let equal x y =
102+
let ( = ) x y =
87103
x.name = y.name && x.sep = y.sep
88104

89105
let to_yojson {name; sep} =
@@ -128,7 +144,7 @@ module Datatype = struct
128144
| Int
129145
| Nativeint
130146

131-
let equal : t -> t -> bool = fun x y -> x = y
147+
let ( = ) : t -> t -> bool = fun x y -> x = y
132148

133149
let of_kind : type a b. (a, b) Bigarray.kind -> t = function
134150
| Bigarray.Char -> Char

lib/extensions.mli

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
1+
type grid_info =
2+
{msg : string
3+
;chunk_shape : int array
4+
;array_shape : int array}
5+
6+
type error =
7+
[ `Grid of grid_info ]
8+
19
module RegularGrid : sig
210
type t
3-
val create : int array -> t
11+
val create : array_shape:int array -> int array -> (t, [> error]) result
412
val chunk_shape : t -> int array
513
val grid_shape : t -> int array -> int array
614
val indices : t -> int array -> int array list
715
val index_coord_pair : t -> int array -> int array * int array
8-
val equal : t -> t -> bool
16+
val ( = ) : t -> t -> bool
917
val of_yojson : Yojson.Safe.t -> (t, string) result
1018
val to_yojson : t -> Yojson.Safe.t
1119
end
@@ -14,7 +22,7 @@ module ChunkKeyEncoding : sig
1422
type t
1523
val create : [< `Slash | `Dot > `Slash ] -> t
1624
val encode : t -> int array -> string
17-
val equal : t -> t -> bool
25+
val ( = ) : t -> t -> bool
1826
val of_yojson : Yojson.Safe.t -> (t, string) result
1927
val to_yojson : t -> Yojson.Safe.t
2028
end
@@ -38,7 +46,7 @@ module Datatype : sig
3846
| Nativeint
3947
(** A type for the supported data types of a Zarr array. *)
4048

41-
val equal : t -> t -> bool
49+
val ( = ) : t -> t -> bool
4250
val of_kind : ('a, 'b) Bigarray.kind -> t
4351
val of_yojson : Yojson.Safe.t -> (t, string) result
4452
val to_yojson : t -> Yojson.Safe.t

0 commit comments

Comments
 (0)