Skip to content

Commit

Permalink
irmin-(client|server): add small example
Browse files Browse the repository at this point in the history
  • Loading branch information
metanivek committed Aug 24, 2023
1 parent 07d5228 commit 94731f3
Show file tree
Hide file tree
Showing 4 changed files with 138 additions and 0 deletions.
72 changes: 72 additions & 0 deletions examples/server/client.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
(*
* Copyright (c) 2023 Tarides <[email protected]>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*)

open Lwt.Syntax

module Client =
Irmin_client_unix.Make_codec (Irmin_server.Conn.Codec.Bin) (Config.Store)

module Info = Irmin_client_unix.Info (Client.Info)

let info msg = Info.v ~author:"tester" msg

let main () =
let* client = Client.connect Config.uri in
let* main = Client.main client in
let* () =
Client.set_exn ~info:(info "set testing") main [ "testing" ] "testing"
in
let* () =
Client.set_exn ~info:(info "set remove") main [ "remove" ] "remove"
in
let* () = Client.remove_exn ~info:(info "remove remove") main [ "remove" ] in
let batch =
Client.Batch.(
v ()
|> add_value [ "a"; "b"; "c" ] "123"
|> add_value [ "foo" ] "bar"
|> remove [ "testing" ])
in
let* c = Client.Batch.apply ~info:(info "apply batch") ~path:[] main batch in
Logs.info (fun l ->
l "Applied batch -> commit %a" Irmin.Type.(pp Client.commit_key_t) c);

let* abc = Client.get main [ "a"; "b"; "c" ] in
assert (String.equal abc "123");

let* foo = Client.get main [ "foo" ] in
assert (foo = "bar");

let* testing = Client.find main [ "testing" ] in
assert (Option.is_none testing);

let* remove = Client.mem main [ "remove" ] in
assert (remove = false);

let* commit = Client.Commit.of_key client c in
let tree = Client.Commit.tree (Option.get commit) in
let* concrete = Client.Tree.to_concrete tree in

Logs.info (fun l -> l "%a" Irmin.Type.(pp Client.Tree.concrete_t) concrete);

Lwt.return_unit

let () =
Fmt_tty.setup_std_outputs ();
Logs.(set_level @@ Some Debug);
Irmin.Export_for_backends.Logging.reporter (module Mtime_clock)
|> Logs.set_reporter;
Lwt_main.run @@ main ()
19 changes: 19 additions & 0 deletions examples/server/config.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
(*
* Copyright (c) 2023 Tarides <[email protected]>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*)

module Store = Irmin_mem.KV.Make (Irmin.Contents.String)

let uri = Uri.of_string "tcp://localhost:4242"
12 changes: 12 additions & 0 deletions examples/server/dune
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
(executables
(names client server)
(libraries
irmin
irmin-client.unix
irmin-server.unix
irmin-git.unix
irmin-pack.unix
logs
unix
fmt.tty
hex))
35 changes: 35 additions & 0 deletions examples/server/server.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
(*
* Copyright (c) 2023 Tarides <[email protected]>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*)

open Lwt.Syntax

module Server =
Irmin_server_unix.Make_ext (Irmin_server.Conn.Codec.Bin) (Config.Store)

let main () =
let config = Irmin_mem.config () in
let dashboard = `TCP (`Port 1234) in
let uri = Config.uri in
let* server = Server.v ~uri ~dashboard config in
Logs.debug (fun l -> l "Listening on %a@." Uri.pp uri);
Server.serve server

let () =
Fmt_tty.setup_std_outputs ();
Logs.(set_level @@ Some Debug);
Irmin.Export_for_backends.Logging.reporter (module Mtime_clock)
|> Logs.set_reporter;
Lwt_main.run @@ main ()

0 comments on commit 94731f3

Please sign in to comment.