From 056fcabaeec238a7aa927df39a90acea028b1aa0 Mon Sep 17 00:00:00 2001 From: Sébastien Dailly Date: Sun, 22 Nov 2026 13:58:16 +0100 Subject: Added an option for loading a value content without the multivaluated fields (allow single query) --- lib/value/property.ml | 36 +++++++++++++++++++ lib/value/property.mli | 33 +++++++++++++++++ lib/value/tests/load.t | 6 +++- lib/value/value.ml | 96 ++++++++++++++++++++++++++++++++------------------ lib/value/value.mli | 58 ++++++++++++++++++------------ 5 files changed, 171 insertions(+), 58 deletions(-) create mode 100644 lib/value/property.ml create mode 100644 lib/value/property.mli (limited to 'lib/value') diff --git a/lib/value/property.ml b/lib/value/property.ml new file mode 100644 index 0000000..e89f05a --- /dev/null +++ b/lib/value/property.ml @@ -0,0 +1,36 @@ +type 'typ t = { + name : Model.Identifier.t; (** The internal name of the field *) + typ : 'typ; (** The type of the field, e.g. a ['target multireference] *) +} +(** A property of a value: a field name tied to its type — a {!Model.Types.t} + together with the field's {!Model.Identifier.t}. + + Properties are built on the fly with {!named} and used locally as typed + handles — e.g. to read the data of a field from a value. A pre-built + instance exists for the reserved field ["id"] (see {[id_ref]} in the main + module of this package). *) + +(** Build a property from a name and a type — no uniqueness involved, so values + built on the fly denote the same field *) +let named : name:Model.Identifier.t -> typ:'typ -> 'typ t = + fun ~name ~typ -> { name; typ } + +let name : 'typ t -> Model.Identifier.t = fun n -> n.name +let typ : 'typ t -> 'typ = fun n -> n.typ + +(** [provably_equal p1 p2] is [Some Equal] iff [p1] and [p2] denote the same + field — same name — and their types match, which also proves that their + ['ocaml] representations are equal *) +let provably_equal (type a b x y c) (p1 : (a, x, c) Model.Types.t t) + (p2 : (b, y, c) Model.Types.t t) : (x, y) Type.eq option = + if Model.Identifier.equal p1.name p2.name then + match Model.Types.eq p1.typ p2.typ with + | Some Type.Equal -> Some Type.Equal + | None -> None + else None + +(** [same p1 p2] is [true] iff [p1] and [p2] denote the same field with matching + types *) +let same : ('a, 'x, 'c) Model.Types.t t -> ('b, 'y, 'c) Model.Types.t t -> bool + = + fun p1 p2 -> provably_equal p1 p2 <> None diff --git a/lib/value/property.mli b/lib/value/property.mli new file mode 100644 index 0000000..af83dc0 --- /dev/null +++ b/lib/value/property.mli @@ -0,0 +1,33 @@ +(** A property of a value: a field name tied to its type. + + A property is the typed handle used to read a field of a value with + {!Value.get}: the {!Model.Types.t} of the field together with its + {!Model.Identifier.t} name. *) + +type 'typ t = { + name : Model.Identifier.t; (** The internal name of the field *) + typ : 'typ; (** The type of the field, e.g. a ['target multireference] *) +} +(** A {!Model.Types.t} together with the field's name *) + +val named : name:Model.Identifier.t -> typ:'typ -> 'typ t +(** Build a property from a name and a type — no uniqueness involved, so values + built on the fly denote the same field *) + +val name : 'typ t -> Model.Identifier.t +(** The name of the field *) + +val typ : 'typ t -> 'typ +(** The type of the field *) + +val provably_equal : + ('a, 'x, 'c) Model.Types.t t -> + ('b, 'y, 'c) Model.Types.t t -> + ('x, 'y) Type.eq option +(** [provably_equal p1 p2] is [Some Equal] iff [p1] and [p2] denote the same + field — same name — and their types match, which also proves that their + ['ocaml] representations are equal *) + +val same : ('a, 'x, 'c) Model.Types.t t -> ('b, 'y, 'c) Model.Types.t t -> bool +(** [same p1 p2] is [true] iff [p1] and [p2] denote the same field with matching + types *) diff --git a/lib/value/tests/load.t b/lib/value/tests/load.t index e20860a..dee6d0f 100644 --- a/lib/value/tests/load.t +++ b/lib/value/tests/load.t @@ -1,7 +1,9 @@ High level test of [Value.load]: two rows are inserted in the table of the content given as argument (the configuration is read on the standard input), loaded as values with [Value.load] and printed with [Value.pp] — the primary -key and every field, as read back as typed data with [Value.get]. +key and every field, as read back as typed data with [Value.get]. The first row +is then loaded again with [~multivalued:false]: only the row itself is read, so +the multivalued field [aliases] is missing from the output. $ tests --load "entity" < { @@ -38,4 +40,6 @@ key and every field, as read back as typed data with [Value.get]. {id = 1; aliases = [1, 2]; country = 1; name = "name-1"; year = 1} row 2: {id = 2; aliases = [2]; country = 1; name = "name-2"; year = 2} + row 1 (without multivalued fields): + {id = 1; country = 1; name = "name-1"; year = 1} diff --git a/lib/value/value.ml b/lib/value/value.ml index 7cd0e52..1b1112d 100644 --- a/lib/value/value.ml +++ b/lib/value/value.ml @@ -7,24 +7,25 @@ let none : value = Model.Data.any Model.Data.none let single : ('db, 'ocaml, Model.target) Model.Types.t * 'ocaml -> value = fun data -> Model.Data.any (Model.Data.v data) -(** The key of the fields dictionary: an existential pack over the named type of - a field, hiding its ['db] and ['ocaml] parameters *) -type key = - | Named : ('db, 'ocaml, Model.target) Model.Types.t Model.Types.named -> key +module Property = Property + +(** The key of the fields dictionary: an existential pack over the property of a + field, hiding its ['db] and ['ocaml] parameters *) +type key = Named : ('db, 'ocaml, Model.target) Model.Types.t Property.t -> key module Key = struct type t = key - let equal : t -> t -> bool = fun (Named a) (Named b) -> Model.Types.same a b - let hash : t -> int = fun (Named n) -> Hashtbl.hash (Model.Types.name n) + let equal : t -> t -> bool = fun (Named a) (Named b) -> Property.same a b + let hash : t -> int = fun (Named n) -> Hashtbl.hash (Property.name n) end -module Fields = Hashtbl.Make (Key) +module Properties = Hashtbl.Make (Key) type t = { target : Model.target; (** The content or list whose table holds the row *) id : int64; (** The primary key of the row *) - fields : value Fields.t; + fields : value Properties.t; (** Cache of the other fields and their values, keyed by their named type — empty by default, populating it requires reading the database *) } @@ -32,58 +33,83 @@ type t = { its {!Model.target} and its [id] *) let v : Model.target -> int64 -> t = - fun target id -> { target; id; fields = Fields.create 0 } + fun target id -> { target; id; fields = Properties.create 0 } let target : t -> Model.target = fun t -> t.target let id : t -> int64 = fun t -> t.id -let add_field : type ocaml. - t -> - ('db, ocaml, Model.target) Model.Types.t Model.Types.named -> - ocaml -> - unit = +let add_property : type ocaml. + t -> ('db, ocaml, Model.target) Model.Types.t Property.t -> ocaml -> unit = fun t ref data -> - let typ = Model.Types.typ ref in - Fields.replace t.fields (Named ref) + let typ = Property.typ ref in + Properties.replace t.fields (Named ref) (Model.Data.any (Model.Data.v (typ, data))) -let id_ref : Model.target Model.Types.monoreference Model.Types.named = - Model.Types.named ~name:Model.Identifier.id ~typ:Model.Types.Integer +let id_ref : Model.target Model.Types.monoreference Property.t = + Property.named ~name:Model.Identifier.id ~typ:Model.Types.Integer + +(** Cast a stored field data to the ['ocaml] representation of [ref_typ], when + the stored type matches the requested one. The existential parameters of the + stored data are packed behind the locally abstract types of this function, + so that the pair equality of {!Model.Types.eq} can be decomposed inside *) +let cast : type db ocaml db' ocaml'. + (db, ocaml, Model.target) Model.Types.t -> + (db', ocaml', Model.target) Model.Types.t -> + ocaml' -> + ocaml option = + fun ref_typ typ payload -> + match Model.Types.eq ref_typ typ with + | Some Type.Equal -> Some payload + | None -> None let get : type ocaml. - t -> ('db, ocaml, Model.target) Model.Types.t Model.Types.named -> ocaml = + t -> ('db, ocaml, Model.target) Model.Types.t Property.t -> ocaml = fun t ref -> - match Model.Types.provably_equal id_ref ref with + match Property.provably_equal id_ref ref with | Some Type.Equal -> Some t.id | None -> ( - match Fields.find_opt t.fields (Named ref) with + match Properties.find_opt t.fields (Named ref) with | Some (Model.Data.Any (Model.Data.V (typ, payload))) -> ( - match Model.Types.eq_ocaml (Model.Types.typ ref) typ with - | Some Type.Equal -> payload + match cast (Property.typ ref) typ payload with + | Some data -> data | None -> raise Not_found) | _ -> raise Not_found) (** Read the row of [t] from the database and store each field in [t]'s - dictionary — the value is modified in place *) -let load' : Caqti_lwt.connection -> t -> unit Lwt.t = - fun db t -> + dictionary — the value is modified in place. With [~multivalued:false], the + multivalued reference fields are not read *) +let load' : Caqti_lwt.connection -> ?multivalued:bool -> t -> unit Lwt.t = + fun db ?(multivalued = true) t -> let ( let* ) = Lwt.bind in let* () = match t.target with | Model.Reference.Content ref -> - Query.select db (id t) (Model.Reference.get ref) - { f = (fun (named, data) -> add_field t named data) } + Query.select db ~multivalued (id t) (Model.Reference.get ref) + { + f = + (fun ((name, typ), data) -> + add_property t (Property.named ~name ~typ) data); + } | Model.Reference.List list -> - Query.select_list db (id t) list - { f = (fun (named, data) -> add_field t named data) } + Query.select_list db ~multivalued (id t) list + { + f = + (fun ((name, typ), data) -> + add_property t (Property.named ~name ~typ) data); + } in Lwt.return_unit -let load : Caqti_lwt.connection -> Model.target -> Int64.t -> t Lwt.t = - fun db target id -> +let load : + Caqti_lwt.connection -> + ?multivalued:bool -> + Model.target -> + Int64.t -> + t Lwt.t = + fun db ?(multivalued = true) target id -> let ( let* ) = Lwt.bind in let v = v target id in - let* () = load' db v in + let* () = load' db ~multivalued v in Lwt.return v (** Pretty-print the data of a field, whatever its type *) @@ -113,8 +139,8 @@ let pp_data : type db ocaml. let pp : Format.formatter -> t -> unit = fun fmt t -> let fields = - Fields.fold - (fun (Named named) data acc -> (Model.Types.name named, data) :: acc) + Properties.fold + (fun (Named named) data acc -> (Property.name named, data) :: acc) t.fields [] |> List.sort (fun (n1, _) (n2, _) -> Model.Identifier.compare n1 n2) in diff --git a/lib/value/value.mli b/lib/value/value.mli index eb03965..0d00bf7 100644 --- a/lib/value/value.mli +++ b/lib/value/value.mli @@ -14,6 +14,10 @@ val single : ('db, 'ocaml, Model.target) Model.Types.t * 'ocaml -> value ['ocaml] — a single value, or an [int64 Seq.t] for a multivalued reference — together with the type [typ] of the field *) +module Property = Property +(** A property of a value: a field name tied to its {!Model.Types.t} — the typed + handle used to read a field of a value with {!get}. *) + type t (** A value: a row of a content or list table in the database, identified by its {!Model.target} and its [id]. @@ -35,40 +39,50 @@ val id : t -> int64 than [get v id_ref] when the raw [int64] is what is needed (e.g. to run a query on the row) *) -val id_ref : Model.target Model.Types.monoreference Model.Types.named -(** The pre-built {!Model.Types.named} handle of the reserved field ["id"] +val id_ref : Model.target Model.Types.monoreference Property.t +(** The pre-built {!Property.t} handle of the reserved field ["id"] ({!Model.Identifier.id}) — an integer field present in every row, content or list. This is the key to read the primary key of any value. *) -val get : - t -> ('db, 'ocaml, Model.target) Model.Types.t Model.Types.named -> 'ocaml +val get : t -> ('db, 'ocaml, Model.target) Model.Types.t Property.t -> 'ocaml (** Read a field of the value and return its data directly in its code representation ['ocaml] — e.g. [get v id_ref : int64 option] is the primary key of [v], and a multivalued reference field would yield an [int64 Seq.t]. - The match between the requested named type and the readable field is proved - by {!Model.Types.provably_equal} on name and type. The reserved field - [id_ref] is always readable; the other fields are read from the value's - dictionary (populated by {!load} or {!load'}) and raise [Not_found] when - absent. *) - -val load : Caqti_lwt.connection -> Model.target -> Int64.t -> t Lwt.t -(** [load db target id] builds the value for the row [id] of [target] — a - content (wrap it with {!Model.Reference.of_content}) or a static list — and - reads the row from the database (one query for the table, plus one per - multivalued reference field), storing each field in the value's dictionary. - The returned value is complete: {!get} reads every field as typed data. + The match between the requested property and the readable field is proved by + {!Property.provably_equal} on name and type. The reserved field [id_ref] is + always readable; the other fields are read from the value's dictionary + (populated by {!load} or {!load'}) and raise [Not_found] when absent. *) + +val load : + Caqti_lwt.connection -> + ?multivalued:bool -> + Model.target -> + Int64.t -> + t Lwt.t +(** [load ?multivalued db target id] builds the value for the row [id] of + [target] — a content (wrap it with {!Model.Reference.of_content}) or a + static list — and reads the row from the database (one query for the table, + plus one per multivalued reference field), storing each field in the value's + dictionary. The returned value is complete: {!get} reads every field as + typed data. + + [~multivalued] (default [true]) tells whether the multivalued reference + fields are also loaded: with [~multivalued:false], only the row of the table + is read, and the multivalued fields are left out of the dictionary — {!get} + raises [Not_found] on them. The primary key needs no loading — {!get} with {!id_ref} reads it directly from the value. Use {!v} instead when the fields are already known and no database read is wanted. *) -val load' : Caqti_lwt.connection -> t -> unit Lwt.t -(** [load' db v] reads the row of [v] from the database (one query for the - table, plus one per multivalued reference field) and stores each field in - [v]'s dictionary. The value is modified in place — the dictionary is part of - its state — so the function returns [unit]. Use it to populate a value built - with {!v}; {!load} is the shortcut that builds and loads in one call. *) +val load' : Caqti_lwt.connection -> ?multivalued:bool -> t -> unit Lwt.t +(** [load' ?multivalued db v] reads the row of [v] from the database (one query + for the table, plus one per multivalued reference field, unless + [~multivalued:false]) and stores each field in [v]'s dictionary. The value + is modified in place — the dictionary is part of its state — so the function + returns [unit]. Use it to populate a value built with {!v}; {!load} is the + shortcut that builds and loads in one call. *) val pp : Format.formatter -> t -> unit (** Pretty-print a value as [{ id = …; field = …; … }]: the primary key and the -- cgit v1.2.3