aboutsummaryrefslogtreecommitdiffstats
path: root/lib/session.ml
blob: ca129d1f31143c468998cb32d5d509f23f90b66d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
let path_of identifier = "\\session\\" ^ identifier

class session ~kernel ~identifier =
  object
    method identifier = identifier
    method path = path_of identifier
    method state = RNT.session kernel (path_of identifier)

    (** Switch to a different branch within the same session. *)
    method set_branch_to branch = RNT.pin kernel (path_of identifier) branch

    method close = RNT.remove kernel (path_of identifier)
  end

(* Open a session on a live (mutable) branch.
   Creates the branch if it does not exist yet. *)
let open_session kernel ~branch_name =
  let open Utilities.Result in
  let* branches = RNT.list kernel "\\branch" in
  let* () =
    if List.mem branch_name branches then Ok ()
    else RNT.create kernel ("\\branch\\" ^ branch_name) RNT.Branch
  in
  let identifier = Printf.sprintf "%016Lx" (Random.State.bits64 (Random.State.make_self_init ())) in
  let* () = RNT.create kernel (path_of identifier) (RNT.Session branch_name) in
  Ok (new session ~kernel ~identifier)

(* Open a session pinned to a specific snapshot hash (detached / read-only).
   Mutations through the returned branch are refused. *)
(* let open_snapshot_session claims ~branch_name ~snapshot_hash = *)
(* Error  *)