Skip to content

Commit 05e9154

Browse files
committed
Implement Rpc.Dict <-> Hashtbl serialization funs
This fixes the "missing Hashtbl.rpc_of_t" and related compilation errors. This is cherry-picked from 59544ba. Signed-off-by: Gabor Igloi <[email protected]>
1 parent 7953204 commit 05e9154

File tree

4 files changed

+38
-13
lines changed

4 files changed

+38
-13
lines changed

ocaml/xapi/OMakefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ OCAMLPACKS = $(OCAMLPACKS) $(XEN_OCAMLPACKS)
3939
OCamlProgram(http_test, http_test)
4040
OCamlProgram(show_bat, show_bat)
4141

42-
OCamlProgram(storage_impl_test, sparse_encoding sparse_dd_wrapper storage_migrate storage_impl task_server updates storage_task storage_locks storage_impl_test)
42+
OCamlProgram(storage_impl_test, sparse_encoding sparse_dd_wrapper storage_migrate storage_impl task_server updates storage_task storage_locks storage_impl_test rpc_std_helpers)
4343

4444
COMMON = \
4545
xapi_templates \
@@ -49,6 +49,7 @@ COMMON = \
4949
xapi_mgmt_iface \
5050
smint \
5151
../gpg/gpg \
52+
rpc_std_helpers \
5253
helpers \
5354
at_least_once_more \
5455
fileserver

ocaml/xapi/rpc_std_helpers.ml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
(** Helpers to marshal and unmarshal Rpc.Dict into Hashtbl *)
2+
3+
let rpc_of_hashtbl ~rpc_of t =
4+
let dict = Hashtbl.fold (fun k v acc -> (k, rpc_of v) :: acc) t [] in
5+
Rpc.Dict dict
6+
7+
let hashtbl_of_rpc ~of_rpc = function
8+
| Rpc.Dict d ->
9+
let h = Hashtbl.create (List.length d) in
10+
List.iter (function (k, r) -> Hashtbl.add h k (of_rpc r)) d;
11+
h
12+
| r -> failwith (Printf.sprintf "Expected Rpc.Dict, but got %s" (Xmlrpc.to_string r))
13+

ocaml/xapi/storage_impl.ml

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,10 @@ end
159159

160160
module Sr = struct
161161
(** Represents the state of an SR *)
162-
type vdis = (string, Vdi.t) Hashtbl.t [@@deriving rpc]
162+
type vdis = (string, Vdi.t) Hashtbl.t
163+
164+
let vdis_of_rpc = Rpc_std_helpers.hashtbl_of_rpc ~of_rpc:Vdi.t_of_rpc
165+
let rpc_of_vdis = Rpc_std_helpers.rpc_of_hashtbl ~rpc_of:Vdi.rpc_of_t
163166

164167
type t = {
165168
vdis: vdis; (** All tracked VDIs *)
@@ -181,9 +184,14 @@ module Sr = struct
181184
end
182185

183186
module Host = struct
187+
type srs = (string, Sr.t) Hashtbl.t
188+
189+
let srs_of_rpc = Rpc_std_helpers.hashtbl_of_rpc ~of_rpc:Sr.t_of_rpc
190+
let rpc_of_srs = Rpc_std_helpers.rpc_of_hashtbl ~rpc_of:Sr.rpc_of_t
191+
184192
(** Represents the state of a host *)
185193
type t = {
186-
srs: (string, Sr.t) Hashtbl.t;
194+
srs: srs;
187195
} [@@deriving rpc]
188196

189197
let empty () = {

ocaml/xapi/storage_migrate.ml

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,9 @@ module State = struct
7272
let loaded = ref false
7373
let mutex = Mutex.create ()
7474

75-
type send_table = (string, Send_state.t) Hashtbl.t [@@deriving rpc]
76-
type recv_table = (string, Receive_state.t) Hashtbl.t [@@deriving rpc]
77-
type copy_table = (string, Copy_state.t) Hashtbl.t [@@deriving rpc]
75+
type send_table = (string, Send_state.t) Hashtbl.t
76+
type recv_table = (string, Receive_state.t) Hashtbl.t
77+
type copy_table = (string, Copy_state.t) Hashtbl.t
7878

7979
type osend
8080
type orecv
@@ -105,10 +105,12 @@ module State = struct
105105
| Recv_table _ -> Filename.concat !persist_root "storage_mirrors_recv.json"
106106
| Copy_table _ -> Filename.concat !persist_root "storage_mirrors_copy.json"
107107

108-
let rpc_of_table : type a. a table -> Rpc.t = function
109-
| Send_table send_table -> rpc_of_send_table send_table
110-
| Recv_table recv_table -> rpc_of_recv_table recv_table
111-
| Copy_table copy_table -> rpc_of_copy_table copy_table
108+
let rpc_of_table : type a. a table -> Rpc.t =
109+
let open Rpc_std_helpers in
110+
function
111+
| Send_table send_table -> rpc_of_hashtbl ~rpc_of:Send_state.rpc_of_t send_table
112+
| Recv_table recv_table -> rpc_of_hashtbl ~rpc_of:Receive_state.rpc_of_t recv_table
113+
| Copy_table copy_table -> rpc_of_hashtbl ~rpc_of:Copy_state.rpc_of_t copy_table
112114

113115
let to_string : type a. a table -> string =
114116
(fun table -> rpc_of_table table |> Jsonrpc.to_string)
@@ -118,13 +120,14 @@ module State = struct
118120

119121
let load_one : type a. a table -> unit = (fun table ->
120122
let rpc = path_of_table table |> rpc_of_path in
123+
let open Rpc_std_helpers in
121124
match table with
122125
| Send_table table ->
123-
Hashtbl.iter (Hashtbl.replace table) (send_table_of_rpc rpc)
126+
Hashtbl.iter (Hashtbl.replace table) (hashtbl_of_rpc ~of_rpc:Send_state.t_of_rpc rpc)
124127
| Recv_table table ->
125-
Hashtbl.iter (Hashtbl.replace table) (recv_table_of_rpc rpc)
128+
Hashtbl.iter (Hashtbl.replace table) (hashtbl_of_rpc ~of_rpc:Receive_state.t_of_rpc rpc)
126129
| Copy_table table ->
127-
Hashtbl.iter (Hashtbl.replace table) (copy_table_of_rpc rpc))
130+
Hashtbl.iter (Hashtbl.replace table) (hashtbl_of_rpc ~of_rpc:Copy_state.t_of_rpc rpc))
128131

129132
let load () =
130133
try load_one (Send_table active_send) with _ -> ();

0 commit comments

Comments
 (0)