Skip to content

Commit

Permalink
[WIP] Fix examples
Browse files Browse the repository at this point in the history
  • Loading branch information
clecat committed Mar 6, 2024
1 parent 2045385 commit c2f7e7d
Show file tree
Hide file tree
Showing 17 changed files with 83 additions and 67 deletions.
7 changes: 4 additions & 3 deletions examples/client_batch.ml
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@ module Store = Irmin_mem.KV.Make (Irmin.Contents.String)
module Client = Irmin_client_unix.Make (Store)
module Error = Irmin_client.Error

let main () =
let main env =
Eio.Switch.run @@ fun sw ->
let fs = Eio.Stdenv.fs env in
let info () = Client.Info.empty in
let uri = Uri.of_string Sys.argv.(1) in
let client = Client.connect ~sw uri in
let client = Client.connect ~sw ~fs uri in

let main = Client.main client in
Client.set_exn ~info main [ "testing" ] "testing";
Expand All @@ -45,4 +46,4 @@ let main () =

let () =
Eio_main.run @@ fun env ->
Lwt_eio.with_event_loop ~clock:env#clock @@ fun _ -> main ()
Lwt_eio.with_event_loop ~clock:env#clock @@ fun _ -> main env
7 changes: 4 additions & 3 deletions examples/custom_graphql.ml
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,12 @@ end
module Server =
Irmin_graphql_unix.Server.Make_ext (Store) (Remote) (Custom_types)

let main () =
let main env =
Eio.Switch.run @@ fun sw ->
let fs = Eio.Stdenv.fs env in
Config.init ();
let config = Irmin_git.config Config.root in
let repo = Store.Repo.v ~sw config in
let repo = Store.Repo.v ~sw ~fs config in
let server = Server.v repo in
let src = "localhost" in
let port = 9876 in
Expand All @@ -123,4 +124,4 @@ let main () =

let () =
Eio_main.run @@ fun env ->
Lwt_eio.with_event_loop ~clock:env#clock @@ fun _ -> main ()
Lwt_eio.with_event_loop ~clock:env#clock @@ fun _ -> main env
7 changes: 4 additions & 3 deletions examples/custom_merge.ml
Original file line number Diff line number Diff line change
Expand Up @@ -137,10 +137,11 @@ let print_logs name t =
let logs = all_logs t in
Fmt.pr "-----------\n%s:\n-----------\n%a%!" name (Irmin.Type.pp Log.t) logs

let main () =
let main env =
Eio.Switch.run @@ fun sw ->
let fs = Eio.Stdenv.fs env in
Config.init ();
let repo = Store.Repo.v ~sw config in
let repo = Store.Repo.v ~sw ~fs config in
let t = Store.main repo in

(* populate the log with some random messages *)
Expand All @@ -162,4 +163,4 @@ let main () =

let () =
Eio_main.run @@ fun env ->
Lwt_eio.with_event_loop ~clock:env#clock @@ fun _ -> main ()
Lwt_eio.with_event_loop ~clock:env#clock @@ fun _ -> main env
9 changes: 5 additions & 4 deletions examples/custom_storage.ml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ functor

(** Initialisation / Closing *)

let v ~sw:_ config =
let v ~sw:_ ~fs:_ config =
let init_size = Irmin.Backend.Conf.get config Hashtbl_config.init_size in
{ t = Tbl.create init_size; l = Eio.Mutex.create () }

Expand Down Expand Up @@ -86,9 +86,10 @@ module Store =
let config ?(config = Hashtbl_config.empty) ?(init_size = 42) () =
Irmin.Backend.Conf.add config Hashtbl_config.init_size init_size

let main () =
let main env =
Eio.Switch.run @@ fun sw ->
let repo = Store.Repo.v ~sw (config ()) in
let fs = Eio.Stdenv.fs env in
let repo = Store.Repo.v ~sw ~fs (config ()) in
let main = Store.main repo in
let info () = Store.Info.v 0L in
let key = "Hello" in
Expand All @@ -98,4 +99,4 @@ let main () =

let () =
Eio_main.run @@ fun env ->
Lwt_eio.with_event_loop ~clock:env#clock @@ fun _ -> main ()
Lwt_eio.with_event_loop ~clock:env#clock @@ fun _ -> main env
13 changes: 7 additions & 6 deletions examples/deploy.ml
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ let revert repo =
Eio_unix.sleep 2.;
Store.Head.set prod h2)

let main () =
let main env =
let cmd = Sys.argv.(0) in
let help () =
Printf.eprintf
Expand All @@ -102,36 +102,37 @@ let main () =
cmd cmd cmd cmd Config.root
in
Eio.Switch.run @@ fun sw ->
let fs = Eio.Stdenv.fs env in
if Array.length Sys.argv <> 2 then help ()
else
match Sys.argv.(1) with
| "provision" ->
(let repo = Store.Repo.v ~sw config in
(let repo = Store.Repo.v ~sw ~fs config in
provision repo);
Printf.printf
"The VM is now provisioned. Run `%s configure` to simulate a sysadmin \n\
configuration.\n"
cmd
| "configure" ->
(let repo = Store.Repo.v ~sw config in
(let repo = Store.Repo.v ~sw ~fs config in
configure repo);
Printf.printf
"The VM is now configured. Run `%s attack` to simulate an attack by \
an \n\
intruder.\n"
cmd
| "attack" ->
(let repo = Store.Repo.v ~sw config in
(let repo = Store.Repo.v ~sw ~fs config in
attack repo);
Printf.printf
"The VM has been attacked. Run `%s revert` to revert the VM state to \
a safe one.\n"
cmd
| "revert" ->
let repo = Store.Repo.v ~sw config in
let repo = Store.Repo.v ~sw ~fs config in
revert repo
| _ -> help ()

let () =
Eio_main.run @@ fun env ->
Lwt_eio.with_event_loop ~clock:env#clock @@ fun _ -> main ()
Lwt_eio.with_event_loop ~clock:env#clock @@ fun _ -> main env
7 changes: 4 additions & 3 deletions examples/fold.ml
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,12 @@ end = struct
| None -> failwith "no kind"
end

let main () =
let main env =
let ps name = Fmt.(pf stdout "\n%s\n" name) in
ps "Demo of how tree folders visit nodes.";
Eio.Switch.run @@ fun sw ->
let repo = Store.Repo.v ~sw config in
let fs = Eio.Stdenv.fs env in
let repo = Store.Repo.v ~sw ~fs config in
let main_b = Store.main repo in
Store.set_exn ~info:(info "add c1") main_b [ "c1" ] "c1";
Store.set_exn ~info:(info "add c2") main_b [ "c2" ] "c2";
Expand All @@ -91,4 +92,4 @@ let main () =

let () =
Eio_main.run @@ fun env ->
Lwt_eio.with_event_loop ~clock:env#clock @@ fun _ -> main ()
Lwt_eio.with_event_loop ~clock:env#clock @@ fun _ -> main env
12 changes: 6 additions & 6 deletions examples/irmin-pack/gc.ml
Original file line number Diff line number Diff line change
Expand Up @@ -160,11 +160,13 @@ let run_gc domain_mgr config repo tracker =
let () = Store.split repo in
Tracker.mark_next_gc_commit tracker

let run_experiment domain_mgr config =
let run_experiment env config =
Eio.Switch.run @@ fun sw ->
let fs = Eio.Stdenv.fs env in
let domain_mgr = Eio.Stdenv.domain_mgr env in
let num_of_commits = 200_000 in
let gc_every = 1_000 in
let repo = Store.Repo.v ~sw config in
let repo = Store.Repo.v ~sw ~fs config in
let tracker = Tracker.v () in
(* Create commits *)
let _ =
Expand All @@ -190,9 +192,7 @@ let run_experiment domain_mgr config =

let () =
Eio_main.run @@ fun env ->
let domain_mgr = Eio.Stdenv.domain_mgr env in
Irmin_pack_unix.Io.set_env (Eio.Stdenv.fs env);
Printf.printf "== RUN 1: deleting discarded data ==\n";
run_experiment domain_mgr Repo_config.config;
run_experiment env Repo_config.config;
Printf.printf "== RUN 2: archiving discarded data ==\n";
run_experiment domain_mgr Repo_config.config_with_lower
run_experiment env Repo_config.config_with_lower
8 changes: 4 additions & 4 deletions examples/irmin-pack/kv.ml
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,12 @@ end
module StoreMaker = Irmin_pack_unix.KV (Conf)
module Store = StoreMaker.Make (Irmin.Contents.String)

let main () =
let main env =
(* Create a switch *)
Eio.Switch.run @@ fun sw ->
let fs = Eio.Stdenv.fs env in
(* Instantiate a repository *)
let repo = Store.Repo.v ~sw Repo_config.config in
let repo = Store.Repo.v ~sw ~fs Repo_config.config in

(* Get the store from the main branch. *)
let store = Store.main repo in
Expand All @@ -96,6 +97,5 @@ let setup_logs () =

let () =
Eio_main.run @@ fun env ->
Irmin_pack_unix.Io.set_env (Eio.Stdenv.fs env);
setup_logs ();
main ()
main env
11 changes: 6 additions & 5 deletions examples/irmin_git_store.ml
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,12 @@ let read_exn t k =
print_endline msg;
Store.get t k

let main () =
let main env =
Eio.Switch.run @@ fun sw ->
let fs = Eio.Stdenv.fs env in
Config.init ();
let config = Irmin_git.config ~bare:true Config.root in
let repo = Store.Repo.v ~sw config in
let repo = Store.Repo.v ~sw ~fs config in
let t = Store.main repo in
update t [ "root"; "misc"; "1.txt" ] "Hello world!";
update t [ "root"; "misc"; "2.txt" ] "Hi!";
Expand All @@ -53,16 +54,16 @@ let main () =
let _ = read_exn t [ "root"; "misc"; "3.txt" ] in
()

let main () =
let main env =
Printf.printf
"This example creates a Git repository in %s and use it to read \n\
and write data:\n"
Config.root;
let _ = Sys.command (Printf.sprintf "rm -rf %s" Config.root) in
main ();
main env;
Printf.printf "You can now run `cd %s && tig` to inspect the store.\n"
Config.root

let () =
Eio_main.run @@ fun env ->
Lwt_eio.with_event_loop ~clock:env#clock @@ fun _ -> main ()
Lwt_eio.with_event_loop ~clock:env#clock @@ fun _ -> main env
3 changes: 2 additions & 1 deletion examples/merkle_proofs.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,15 @@ module Store = Irmin_git_unix.FS.KV (Contents)
let eio_run fn =
Eio_main.run @@ fun env ->
Eio.Switch.run @@ fun sw ->
let fs = Eio.Stdenv.fs env in
Lwt_eio.with_event_loop ~clock:env#clock (fn ~sw)
```

Open a repo.

```ocaml
# let config = Irmin_git.config ~bare:true "./tmp-irmin/test"
let repo = eio_run @@ fun ~sw _ -> Store.Repo.v ~sw config;;
let repo = eio_run @@ fun ~sw _ -> Store.Repo.v ~sw ~fs config;;
val config : Irmin.config = <abstr>
val repo : Store.repo = <abstr>
```
Expand Down
3 changes: 1 addition & 2 deletions examples/plugin/plugin.ml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ let () =
@@ Logs.Src.list ()

let () =
Eio_main.run @@ fun env ->
Irmin_pack_unix.Io.set_env (Eio.Stdenv.fs env);
Eio_main.run @@ fun _env ->
Resolver.Contents.add ~default:true "int" (module Int);
Resolver.Store.add ~default:true "mem-int" (Fixed store)
21 changes: 12 additions & 9 deletions examples/process.ml
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,11 @@ let info image message () =

let main = branch images.(0)

let init () =
let init env =
Eio.Switch.run @@ fun sw ->
let fs = Eio.Stdenv.fs env in
Config.init ();
let repo = Store.Repo.v ~sw config in
let repo = Store.Repo.v ~sw ~fs config in
let t = Store.of_branch repo main in
Store.set_exn t ~info:(info images.(0) "init") [ "0" ] "0";
List.iter
Expand All @@ -125,8 +126,9 @@ let init () =
let random_array a = a.(Random.int (Array.length a))
let random_list l = random_array (Array.of_list l)

let rec process image =
let rec process env image =
Eio.Switch.run @@ fun sw ->
let fs = Eio.Stdenv.fs env in
let id = branch image in
Printf.printf "Processing %s\n%!" id;
let actions = random_list image.actions in
Expand All @@ -135,7 +137,7 @@ let rec process image =
with _ ->
([ "log"; id; "0" ], fun () -> id ^ string_of_int (Random.int 10))
in
let repo = Store.Repo.v ~sw config in
let repo = Store.Repo.v ~sw ~fs config in
let t = Store.of_branch repo id in
Store.set_exn t ~info:(info image actions.message) key (value ());
let () =
Expand All @@ -153,7 +155,7 @@ let rec process image =
| Error _ -> failwith "conflict!")
in
Eio_unix.sleep (max 0.1 (Random.float 0.3));
process image
process env image

let rec protect fn x () =
try fn x
Expand All @@ -166,10 +168,11 @@ let rec watchdog () =
Eio_unix.sleep 1.;
watchdog ()

let main () =
init ();
Eio.Fiber.any (watchdog :: List.map (protect process) (Array.to_list images))
let main env =
init env;
Eio.Fiber.any
(watchdog :: List.map (protect (process env)) (Array.to_list images))

let () =
Eio_main.run @@ fun env ->
Lwt_eio.with_event_loop ~clock:env#clock @@ fun _ -> main ()
Lwt_eio.with_event_loop ~clock:env#clock @@ fun _ -> main env
7 changes: 4 additions & 3 deletions examples/push.ml
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,12 @@ let headers =
let e = Cohttp.Header.of_list [] in
Cohttp.Header.add_authorization e (`Basic (user, token))

let test () =
let test env =
Eio.Switch.run @@ fun sw ->
let fs = Eio.Stdenv.fs env in
Config.init ();
let config = Irmin_git.config Config.root in
let repo = Store.Repo.v ~sw config in
let repo = Store.Repo.v ~sw ~fs config in
let t = Store.main repo in
let remote = Store.remote ~headers url () in
let _ = Sync.pull_exn t remote `Set in
Expand All @@ -52,4 +53,4 @@ let test () =

let () =
Eio_main.run @@ fun env ->
Lwt_eio.with_event_loop ~clock:env#clock @@ fun _ -> test ()
Lwt_eio.with_event_loop ~clock:env#clock @@ fun _ -> test env
7 changes: 4 additions & 3 deletions examples/readme.ml
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ let author = "Example <[email protected]>"
(* Commit information *)
let info fmt = Irmin_git_unix.info ~author fmt

let main () =
let main env =
(* Create the switch *)
Eio.Switch.run @@ fun sw ->
let fs = Eio.Stdenv.fs env in
(* Open the repo *)
let repo = Store.Repo.v ~sw config in
let repo = Store.Repo.v ~sw ~fs config in

(* Load the main branch *)
let t = Store.main repo in
Expand All @@ -29,4 +30,4 @@ let main () =
(* Run the program *)
let () =
Eio_main.run @@ fun env ->
Lwt_eio.with_event_loop ~clock:env#clock @@ fun _ -> main ()
Lwt_eio.with_event_loop ~clock:env#clock @@ fun _ -> main env
Loading

0 comments on commit c2f7e7d

Please sign in to comment.