@ -59,24 +59,28 @@ let create_serializer (key : key) : 'a serializer =
try
match_data ( Marshal . from_string str 0 ) " string "
with Sys_error _ -> None in
let from_file ( _ fname : DB . filename ) : ' a option =
let read () =
try
let fname = DB . filename_to_string _ fname in
let inc = open_in_bin fname in
let value_option = match_data ( Marshal . from_channel inc ) fname in
close_in inc ;
value_option
with
| Sys_error _ -> None in
let timeout = 1 . 0 in
let catch_exn = function
| End_of_file -> true
| Failure _ -> true (* handle input_value: truncated object *)
| _ -> false in
(* Retry to read for 1 second in case of end of file, *)
(* which indicates that another process is writing the same file. *)
retry_exception timeout catch_exn read () in
let from_file ( fname_ : DB . filename ) : ' a option =
let fname = DB . filename_to_string fname_ in
match open_in_bin fname with
| exception Sys_error _ ->
None
| inc ->
let read () =
try
seek_in inc 0 ;
match_data ( Marshal . from_channel inc ) fname
with
| Sys_error _ -> None in
let timeout = 1 . 0 in
let catch_exn = function
| End_of_file -> true
| Failure _ -> true (* handle input_value: truncated object *)
| _ -> false in
(* Retry to read for 1 second in case of end of file, *)
(* which indicates that another process is writing the same file. *)
SymOp . try_finally
( fun () -> retry_exception timeout catch_exn read () )
( fun () -> close_in inc ) in
let to_file ( fname : DB . filename ) ( value : ' a ) =
let fname_str = DB . filename_to_string fname in
(* support nonblocking reads and writes in parallel: *)