Skip to content

Commit bb17702

Browse files
committed
CP-49140: [prep] database: use separate types, not string everywhere
Currently all of these are strings, but it may change: * table * db_ref * field_name * field * uuid This enables changing the `field` type from `string` and avoiding costly serialization/deserialization when not needed. No functional change. Signed-off-by: Edwin Török <edwin.torok@cloud.com>
1 parent 8862cfc commit bb17702

File tree

4 files changed

+38
-24
lines changed

4 files changed

+38
-24
lines changed

ocaml/database/db_cache_impl.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ let read_refs t tblname =
271271
Table.fold (fun r _ _ acc -> r :: acc) tbl []
272272

273273
(* Return a list of all the refs for which the expression returns true. *)
274-
let find_refs_with_filter_internal db (tblname : string)
274+
let find_refs_with_filter_internal db (tblname : Db_interface.table)
275275
(expr : Db_filter_types.expr) =
276276
let tbl = TableSet.find tblname (Database.tableset db) in
277277
let eval_val row = function

ocaml/database/db_interface.ml

Lines changed: 35 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -23,79 +23,92 @@ module type RPC = sig
2323
(** [rpc request] transmits [request] and receives a response *)
2424
end
2525

26+
type table = string
27+
28+
type field_name = string
29+
30+
type field = string
31+
32+
type db_ref = string
33+
34+
type uuid = string
35+
36+
type regular_fields = (field_name * field) list
37+
38+
type associated_fields = (field_name * db_ref list) list
39+
2640
(** dictionary of regular fields x dictionary of associated set_ref values *)
27-
type db_record = (string * string) list * (string * string list) list
41+
type db_record = regular_fields * associated_fields
2842

2943
(** The client interface to the database *)
3044
module type DB_ACCESS = sig
3145
val initialise : unit -> unit
3246
(** [initialise ()] must be called before any other function in this
3347
interface *)
3448

35-
val get_table_from_ref : Db_ref.t -> string -> string option
36-
(** [get_table_from_ref ref] returns [Some tbl] if [ref] is a
49+
val get_table_from_ref : Db_ref.t -> db_ref -> table option
50+
(** [get_table_from_ref ref tbl] returns [Some tbl] if [ref] is a
3751
valid reference; None otherwise *)
3852

39-
val is_valid_ref : Db_ref.t -> string -> bool
53+
val is_valid_ref : Db_ref.t -> db_ref -> bool
4054
(** [is_valid_ref ref] returns true if [ref] is valid; false otherwise *)
4155

42-
val read_refs : Db_ref.t -> string -> string list
56+
val read_refs : Db_ref.t -> table -> db_ref list
4357
(** [read_refs tbl] returns a list of all references in table [tbl] *)
4458

4559
val find_refs_with_filter :
46-
Db_ref.t -> string -> Db_filter_types.expr -> string list
60+
Db_ref.t -> table -> Db_filter_types.expr -> db_ref list
4761
(** [find_refs_with_filter tbl expr] returns a list of all references
4862
to rows which match [expr] *)
4963

50-
val read_field_where : Db_ref.t -> Db_cache_types.where_record -> string list
64+
val read_field_where : Db_ref.t -> Db_cache_types.where_record -> field list
5165
(** [read_field_where {tbl,return,where_field,where_value}] returns a
5266
list of the [return] fields in table [tbl] where the [where_field]
5367
equals [where_value] *)
5468

55-
val db_get_by_uuid : Db_ref.t -> string -> string -> string
69+
val db_get_by_uuid : Db_ref.t -> table -> uuid -> db_ref
5670
(** [db_get_by_uuid tbl uuid] returns the single object reference
5771
associated with [uuid] *)
5872

59-
val db_get_by_uuid_opt : Db_ref.t -> string -> string -> string option
73+
val db_get_by_uuid_opt : Db_ref.t -> table -> uuid -> db_ref option
6074
(** [db_get_by_uuid_opt tbl uuid] returns [Some obj] with the single object
6175
reference associated with [uuid] if one exists and [None] otherwise,
6276
instead of raising an exception like [get_by_uuid] *)
6377

64-
val db_get_by_name_label : Db_ref.t -> string -> string -> string list
78+
val db_get_by_name_label : Db_ref.t -> table -> string -> db_ref list
6579
(** [db_get_by_name_label tbl label] returns the list of object references
6680
associated with [label] *)
6781

68-
val create_row :
69-
Db_ref.t -> string -> (string * string) list -> string -> unit
82+
val create_row : Db_ref.t -> table -> regular_fields -> db_ref -> unit
7083
(** [create_row tbl kvpairs ref] create a new row in [tbl] with
7184
key [ref] and contents [kvpairs] *)
7285

73-
val delete_row : Db_ref.t -> string -> string -> unit
86+
val delete_row : Db_ref.t -> db_ref -> table -> unit
7487
(** [delete_row context tbl ref] deletes row [ref] from table [tbl] *)
7588

76-
val write_field : Db_ref.t -> string -> string -> string -> string -> unit
89+
val write_field : Db_ref.t -> table -> db_ref -> field_name -> field -> unit
7790
(** [write_field context tbl ref fld val] changes field [fld] to [val] in
7891
row [ref] in table [tbl] *)
7992

80-
val read_field : Db_ref.t -> string -> string -> string -> string
81-
(** [read_field context tbl ref fld] returns the value of field [fld]
93+
val read_field : Db_ref.t -> table -> field_name -> db_ref -> field
94+
(** [read_field context tbl fld ref] returns the value of field [fld]
8295
in row [ref] in table [tbl] *)
8396

84-
val read_record : Db_ref.t -> string -> string -> db_record
97+
val read_record : Db_ref.t -> table -> db_ref -> db_record
8598
(** [read_record tbl ref] returns
8699
[ (field, value) ] * [ (set_ref fieldname * [ ref ]) ] *)
87100

88101
val read_records_where :
89-
Db_ref.t -> string -> Db_filter_types.expr -> (string * db_record) list
102+
Db_ref.t -> table -> Db_filter_types.expr -> (db_ref * db_record) list
90103
(** [read_records_where tbl expr] returns a list of the values returned
91104
by read_record that match the expression *)
92105

93106
val process_structured_field :
94107
Db_ref.t
95-
-> string * string
96-
-> string
97-
-> string
98-
-> string
108+
-> field_name * field
109+
-> table
110+
-> field_name
111+
-> db_ref
99112
-> Db_cache_types.structured_op_t
100113
-> unit
101114
(** [process_structured_field context kv tbl fld ref op] modifies the

ocaml/tests/dune

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
(modules suite_alcotest_server test_client test_valid_ref_list test_vm_group)
6666
(libraries
6767
alcotest
68+
xapi_database
6869
httpsvr
6970
tests_common
7071
xapi-client

ocaml/xapi/dune

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@
227227
(name xapi_internal_server_only)
228228
(modes best)
229229
(modules server)
230-
(libraries xapi_internal_minimal http_lib rpclib.core xapi-types xapi-log xapi-stdext-encodings xapi-consts xapi-backtrace xapi-stdext-date rpclib.json)
230+
(libraries xapi_database xapi_internal_minimal http_lib rpclib.core xapi-types xapi-log xapi-stdext-encodings xapi-consts xapi-backtrace xapi-stdext-date rpclib.json)
231231
(wrapped false)
232232
)
233233

0 commit comments

Comments
 (0)