-
Notifications
You must be signed in to change notification settings - Fork 16
Add a basic caching infrastructure in Superbol_platform
#569
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| (**************************************************************************) | ||
| (* *) | ||
| (* SuperBOL OSS Studio *) | ||
| (* *) | ||
| (* Copyright (c) 2022-2026 OCamlPro SAS *) | ||
| (* *) | ||
| (* All rights reserved. *) | ||
| (* This source code is licensed under the GNU Affero General Public *) | ||
| (* License version 3 found in the LICENSE.md file in the root directory *) | ||
| (* of this source tree. *) | ||
| (* *) | ||
| (**************************************************************************) | ||
|
|
||
| open Ez_file.V1 | ||
|
|
||
| let with_in_bin file_name ~f = | ||
| let ic = EzFile.open_in_bin file_name in | ||
| match f ic with | ||
| | v -> close_in ic; v | ||
| | exception exn -> close_in ic; raise exn | ||
|
|
||
| (* --- *) | ||
|
|
||
| let version_tag_length = 40 (* use full commit hash when available *) | ||
|
|
||
| (** Internal version tag *) | ||
| let version_tag = | ||
| let str = Option.value Version.commit_hash ~default:Version.version in | ||
| if String.length str >= version_tag_length | ||
| then String.sub str 0 version_tag_length | ||
| else str ^ String.make (version_tag_length - String.length str) '_' | ||
|
|
||
| let write_cache_item ?(version_tag = version_tag) oc item = | ||
| output_string oc version_tag; | ||
| Marshal.to_channel oc item [] | ||
|
|
||
| let read_cache_item ?(version_tag = version_tag) ic = | ||
| let version_tag' = really_input_string ic version_tag_length in | ||
| if version_tag' <> version_tag | ||
| then Fmt.failwith "Bad version tag: got %s, expected %s\ | ||
| " version_tag' version_tag; | ||
| Marshal.from_channel ic | ||
|
|
||
| let cache_file_for ~item_name ~cache_dir = | ||
| EzFile.concat cache_dir item_name | ||
|
|
||
| let save_named_item_cache ~cache_dir ~item_name ~write_item item = | ||
| let cache_file = cache_file_for ~item_name ~cache_dir in | ||
| EzFile.make_dir ~p:true @@ EzFile.dirname cache_file; | ||
| EzFile.with_out_bin cache_file (write_item item) | ||
|
|
||
| let load_named_item_cache ~cache_dir ~item_name ~read_item = | ||
| with_in_bin (cache_file_for ~item_name ~cache_dir) ~f:read_item |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| (**************************************************************************) | ||
| (* *) | ||
| (* SuperBOL OSS Studio *) | ||
| (* *) | ||
| (* Copyright (c) 2022-2026 OCamlPro SAS *) | ||
| (* *) | ||
| (* All rights reserved. *) | ||
| (* This source code is licensed under the GNU Affero General Public *) | ||
| (* License version 3 found in the LICENSE.md file in the root directory *) | ||
| (* of this source tree. *) | ||
| (* *) | ||
| (**************************************************************************) | ||
|
|
||
| (** Helpers to construct persistent cache storage where each named individual | ||
| item is stored as a single file. | ||
|
|
||
| I/O operations are not performed via a generic platform. Instead, they are | ||
| based on {!Ez_file.V1} (indirectly {!Unix}). *) | ||
|
|
||
| (** [write_cache_item ?version_tag oc item] dumps a version tag, followed by | ||
| [item], on the stream [oc]. Relies on {!Marshal}. *) | ||
| val write_cache_item: ?version_tag:string -> out_channel -> 'a -> unit | ||
|
|
||
| (** [read_cache_item ?version_tag ic] reads and checks a version tag from [oc], | ||
| and then reads an [item] from the same stream. Relies on {!Marshal}. *) | ||
| val read_cache_item: ?version_tag:string -> in_channel -> 'a | ||
|
|
||
| (** [save_named_item_cache ~cache_dir ~item_name ~write_item item] saves an | ||
| [item] with a unique name [item_name] in a cache file within [cache_dir]. | ||
| [write_item] is used to dump the item: it should typically be a call to | ||
| {!write_cache_item}. *) | ||
| val save_named_item_cache | ||
| : cache_dir:string | ||
| -> item_name:string | ||
| -> write_item:('a -> out_channel -> unit) | ||
| -> 'a | ||
| -> unit | ||
|
|
||
| (** [load_named_item_cache ~cache_dir ~item_name ~read_item] loads an item named | ||
| [item_name] from a cache file in [cache_dir]. [read_item] is used to read | ||
| the item: it should typically be a call to {!read_cache_item}. | ||
|
|
||
| Raises {!Sys_error} in case no item with name [item_name] is found in cache | ||
| directory [cache_dir]. *) | ||
| val load_named_item_cache | ||
| : cache_dir:string | ||
| -> item_name:string | ||
| -> read_item:(in_channel -> 'a) -> 'a |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| (**************************************************************************) | ||
| (* *) | ||
| (* SuperBOL OSS Studio *) | ||
| (* *) | ||
| (* Copyright (c) 2022-2026 OCamlPro SAS *) | ||
| (* *) | ||
| (* All rights reserved. *) | ||
| (* This source code is licensed under the GNU Affero General Public *) | ||
| (* License version 3 found in the LICENSE.md file in the root directory *) | ||
| (* of this source tree. *) | ||
| (* *) | ||
| (**************************************************************************) | ||
|
|
||
| (* https://discuss.ocaml.org/t/how-to-create-a-temporary-directory-in-ocaml/1815/4 *) | ||
| let rand_digits () = | ||
| Printf.sprintf "%06x" Random.State.(bits (make_self_init ()) land 0xFFFFFF) | ||
|
|
||
| let create ?(mode=0o700) ?dir pat = | ||
| let dir = match dir with | ||
| | Some d -> d | ||
| | None -> Filename.get_temp_dir_name () | ||
| in | ||
| let raise_err msg = raise (Sys_error msg) in | ||
| let rec loop count = | ||
| if count < 0 | ||
| then | ||
| raise_err "mk_temp_dir: too many failing attemps" | ||
| else | ||
| let dir = Printf.sprintf "%s/%s%s" dir pat (rand_digits ()) in | ||
| try (Unix.mkdir dir mode; dir) with | ||
| | Unix.Unix_error (Unix.EEXIST, _, _) -> loop (count - 1) | ||
| | Unix.Unix_error (Unix.EINTR, _, _) -> loop count | ||
| | Unix.Unix_error (e, _, _) -> | ||
| raise_err ("mk_temp_dir: " ^ (Unix.error_message e)) | ||
| in | ||
| loop 1000 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| (**************************************************************************) | ||
| (* *) | ||
| (* SuperBOL OSS Studio *) | ||
| (* *) | ||
| (* Copyright (c) 2022-2026 OCamlPro SAS *) | ||
| (* *) | ||
| (* All rights reserved. *) | ||
| (* This source code is licensed under the GNU Affero General Public *) | ||
| (* License version 3 found in the LICENSE.md file in the root directory *) | ||
| (* of this source tree. *) | ||
| (* *) | ||
| (**************************************************************************) | ||
|
|
||
| val create: ?mode:int -> ?dir:string -> string -> string |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,6 @@ | ||
| (library | ||
| (name superbol_testutils) | ||
| (wrapped true) | ||
| (libraries autofonce_lib) | ||
| (libraries autofonce_lib superbol_platform) | ||
| (flags (:standard))) | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.