@@ -152,31 +152,92 @@ let _ =
152152 test_storage (module FilesystemStore ) s;
153153 HttpStore. with_open ~net: env#net (Uri. of_string " http://127.0.0.1:8080" ) (fun store ->
154154 let module S = Tiny_httpd in
155- let dir_behavior = S.Dir. Forbidden and download = true and delete = true and upload = true in
156- let config = S.Dir. config ~dir_behavior ~delete ~upload ~download ()
157- and addr = " 127.0.0.1" and port = 8080 in
158- let server = S. create ~max_connections: 4 ~addr ~port () in
159- (* let dir = "/home/zoj/dev/zarr-ml/testdata.zarr" in *)
160- let dir = Sys. getenv " HTTPSTORE_DIR" in
161- S.Dir. add_dir_path ~config ~dir ~prefix: " " server;
155+ let server = S. create ~max_connections: 1000 ~addr: " 127.0.0.1" ~port: 8080 () in
156+ let dir = tmp_dir in
157+ S. add_route_handler server ~meth: `HEAD S.Route. rest_of_path_urlencoded (fun path _ ->
158+ let fspath = Filename. concat dir path in
159+ match In_channel. (with_open_gen [Open_rdonly ] 0o700 fspath length) with
160+ | exception Sys_error e -> S.Response. make_raw ~code: 404 e
161+ | s ->
162+ let headers =
163+ [(" Content-Length" , Int64. to_string s)
164+ ;(" Content-Type" , " application/octet-stream" )]
165+ in
166+ let r = S.Response. make_raw ~code: 200 " " in
167+ S.Response. update_headers (List. append headers) r
168+ );
169+ S. add_route_handler server ~meth: `GET S.Route. rest_of_path_urlencoded (fun path _ ->
170+ let fspath = Filename. concat dir path in
171+ match In_channel. (with_open_gen [Open_rdonly ] 0o700 fspath input_all) with
172+ | exception Sys_error _ -> S.Response. make_raw ~code: 404 (Printf. sprintf " %s not found" path)
173+ | s -> S.Response. make_raw ~code: 200 s
174+ );
175+ S. add_route_handler server ~meth: `POST S.Route. rest_of_path_urlencoded (fun path req ->
176+ let write oc = Out_channel. (output_string oc req.body; flush oc) in
177+ let fspath = Filename. concat dir path in
178+ Zarr.Util. create_parent_dir fspath 0o700 ;
179+ let f = [Open_wronly ; Open_trunc ; Open_creat ] in
180+ match Out_channel. (with_open_gen f 0o700 fspath write) with
181+ | exception Sys_error e -> S.Response. make_raw ~code: 500 e
182+ | () -> S.Response. make_raw ~code: 201 req.body
183+ );
184+ S. add_route_handler server ~meth: `DELETE S.Route. rest_of_path_urlencoded (fun path _ ->
185+ let fspath = Filename. concat dir path in
186+ match Sys. remove fspath with
187+ | exception Sys_error e -> S.Response. make_raw ~code: 404 e
188+ | () -> S.Response. make_raw ~code: 200 (Printf. sprintf " %s deleted successfully" path)
189+ );
162190 let _ = Thread. create S. run server in
163- let gnode = Node.Group. of_path " /some/group" in
191+ let gnode = Node.Group. root in
192+ let attrs = `Assoc [(" questions" , `String " answer" )] in
193+ HttpStore.Group. create ~attrs store gnode;
194+ let exists = HttpStore.Group. exists store gnode in
195+ assert_equal ~printer: string_of_bool true exists;
164196 let meta = HttpStore.Group. metadata store gnode in
165- assert_equal ~printer: Metadata.Group. show Metadata.Group. default meta;
166- let anode = Node.Array. of_path " /some/group/another" in
197+ assert_equal ~printer: Yojson.Safe. show attrs (Metadata.Group. attributes meta);
198+ let exists = HttpStore.Array. exists store Node.Array. (gnode / " non-member" ) in
199+ assert_equal ~printer: string_of_bool false exists;
200+
201+ let cfg =
202+ {chunk_shape = [|2 ; 5 ; 5 |]
203+ ;index_location = End
204+ ;index_codecs = [`Bytes LE ]
205+ ;codecs = [`Transpose [|2 ; 0 ; 1 |]; `Bytes BE ]} in
206+ let anode = Node.Array. (gnode / " arrnode" ) in
167207 let slice = [|R [|0 ; 5 |]; I 10 ; R [|0 ; 10 |]|] in
168- let _ = HttpStore.Array. read store anode slice Complex32 in
169- assert_raises (HttpStore. Not_implemented ) (fun () -> HttpStore. hierarchy store);
170- assert_raises (HttpStore. Not_implemented ) (fun () -> HttpStore.Group. create store (Node.Group. of_path " /blah" ));
171- (* assert_raises (HttpStore.Not_implemented) (fun () -> HttpStore.Group.children store gnode);
172- assert_raises (HttpStore.Not_implemented) (fun () -> HttpStore.Array.rename store anode "newname"); *)
173- (* assert_raises (HttpStore.Not_implemented) (fun () -> HttpStore.Array.reshape store anode [|1;1;1|]); *)
174- assert_raises (HttpStore. Not_implemented ) (fun () -> HttpStore. clear store);
208+ let bigger_slice = [|R [|0 ; 6 |]; L [|9 ; 10 |] ; R [|0 ; 11 |]|] in
209+ HttpStore.Array. create
210+ ~codecs: [`ShardingIndexed cfg] ~shape: [|100 ; 100 ; 50 |] ~chunks: [|10 ; 15 ; 20 |]
211+ Complex32 Complex. one anode store;
212+ let exp = Ndarray. init Complex32 [|6 ; 1 ; 11 |] (Fun. const Complex. one) in
213+ let got = HttpStore.Array. read store anode slice Complex32 in
214+ assert_equal exp got;
215+ Ndarray. fill exp Complex. {re= 2.0 ; im= 0. };
216+ HttpStore.Array. write store anode slice exp;
217+ let got = HttpStore.Array. read store anode slice Complex32 in
218+ (* test if a bigger slice containing new elements can be read from store *)
219+ let _ = HttpStore.Array. read store anode bigger_slice Complex32 in
220+ assert_equal exp got;
221+ (* test writing a bigger slice to store *)
222+ HttpStore.Array. write store anode bigger_slice @@ Ndarray. init Complex32 [|7 ; 2 ; 12 |] (Fun. const Complex. {re= 0. ; im= 3.0 });
223+ let got = HttpStore.Array. read store anode slice Complex32 in
224+ Ndarray. fill exp Complex. {re= 0. ; im= 3.0 };
225+ assert_equal exp got;
226+ let nshape = [|25 ; 28 ; 10 |] in
227+ HttpStore.Array. reshape store anode nshape;
228+ let meta = HttpStore.Array. metadata store anode in
229+ assert_equal ~printer: print_int_array nshape (Metadata.Array. shape meta);
175230 assert_raises
176- (HttpStore. Not_implemented )
177- (fun () ->
178- let exp = Ndarray. init Complex32 [|6 ; 1 ; 11 |] (Fun. const Complex. one) in
179- HttpStore.Array. write store anode slice exp);
231+ (Zarr.Storage. Invalid_resize_shape )
232+ (fun () -> HttpStore.Array. reshape store anode [|25 ; 10 |]);
233+ assert_raises
234+ (Zarr.Storage. Key_not_found " fakegroup/zarr.json" )
235+ (fun () -> HttpStore.Array. metadata store Node.Array. (gnode / " fakegroup" ));
236+ assert_raises HttpStore. Not_implemented (fun () -> HttpStore.Array. rename store anode " newname" );
237+ assert_raises HttpStore. Not_implemented (fun () -> HttpStore.Group. children store gnode);
238+ assert_raises HttpStore. Not_implemented (fun () -> HttpStore. hierarchy store);
239+ assert_raises HttpStore. Not_implemented (fun () -> HttpStore.Group. delete store gnode);
240+ assert_raises HttpStore. Not_implemented (fun () -> HttpStore. clear store);
180241 Tiny_httpd. stop server)
181242 )
182243 ])
0 commit comments