Skip to content

Commit

Permalink
CP-49140: [prep] database: use separate types, not string everywhere
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
edwintorok committed Dec 12, 2024
1 parent 83f4517 commit db7bd49
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 22 deletions.
2 changes: 1 addition & 1 deletion ocaml/database/db_cache_impl.ml
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ let read_refs t tblname =
Table.fold (fun r _ _ acc -> r :: acc) tbl []

(* Return a list of all the refs for which the expression returns true. *)
let find_refs_with_filter_internal db (tblname : string)
let find_refs_with_filter_internal db (tblname : Db_interface.table)
(expr : Db_filter_types.expr) =
let tbl = TableSet.find tblname (Database.tableset db) in
let eval_val row = function
Expand Down
53 changes: 33 additions & 20 deletions ocaml/database/db_interface.ml
Original file line number Diff line number Diff line change
Expand Up @@ -23,74 +23,87 @@ module type RPC = sig
(** [rpc request] transmits [request] and receives a response *)
end

type table = string

type field_name = string

type field = string

type db_ref = string

type uuid = string

type regular_fields = (field_name * field) list

type associated_fields = (field_name * db_ref list) list

(** dictionary of regular fields x dictionary of associated set_ref values *)
type db_record = (string * string) list * (string * string list) list
type db_record = regular_fields * associated_fields

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

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

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

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

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

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

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

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

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

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

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

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

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

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

val process_structured_field :
Db_ref.t
-> string * string
-> string
-> string
-> string
-> field_name * field
-> table
-> field_name
-> db_ref
-> Db_cache_types.structured_op_t
-> unit
(** [process_structured_field context kv tbl fld ref op] modifies the
Expand Down
1 change: 1 addition & 0 deletions ocaml/tests/dune
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
(modules suite_alcotest_server test_client test_valid_ref_list test_vm_group)
(libraries
alcotest
xapi_database
httpsvr
tests_common
xapi-client
Expand Down
2 changes: 1 addition & 1 deletion ocaml/xapi/dune
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@
(name xapi_internal_server_only)
(modes best)
(modules server)
(libraries xapi_internal_minimal http_lib rpclib.core xapi-types xapi-log xapi-stdext-encodings xapi-consts xapi-backtrace xapi-stdext-date rpclib.json)
(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)
(wrapped false)
)

Expand Down

0 comments on commit db7bd49

Please sign in to comment.