Skip to content

Commit

Permalink
Optimize Schema.find
Browse files Browse the repository at this point in the history
Signed-off-by: Edwin Török <[email protected]>
  • Loading branch information
edwintorok committed May 1, 2024
1 parent bbd54ee commit f1835bf
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 9 deletions.
2 changes: 1 addition & 1 deletion ocaml/database/db_cache_types.ml
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ module Row = struct
else
t
)
t schema.Schema.Table.columns
t (Schema.ColumnMap.to_list schema.Schema.Table.columns)
end

module Table = struct
Expand Down
31 changes: 27 additions & 4 deletions ocaml/database/schema.ml
Original file line number Diff line number Diff line change
Expand Up @@ -98,14 +98,37 @@ module Column = struct
[@@deriving sexp]
end

module ColumnMap = struct
module M = Map.Make (String)

type t = Column.t M.t * Column.t list

type compat = Column.t list [@@deriving sexp]

let of_list columns =
( columns |> List.fold_left (fun acc c -> M.add c.Column.name c acc) M.empty
, columns
)

let to_list (_, lst) = lst

let t_of_sexp sexp = sexp |> compat_of_sexp |> of_list

let sexp_of_t (_, compat) = sexp_of_compat compat

let find_opt name (t, _) = M.find_opt name t
end

module Table = struct
type t = {name: string; columns: Column.t list; persistent: bool}
type t = {name: string; columns: ColumnMap.t; persistent: bool}
[@@deriving sexp]

let find name t =
try List.find (fun col -> col.Column.name = name) t.columns
with Not_found ->
raise (Db_exn.DBCache_NotFound ("missing column", t.name, name))
match ColumnMap.find_opt name t.columns with
| Some result ->
result
| None ->
raise (Db_exn.DBCache_NotFound ("missing column", t.name, name))
end

type relationship = OneToMany of string * string * string * string
Expand Down
26 changes: 22 additions & 4 deletions ocaml/database/test_schemas.ml
Original file line number Diff line number Diff line change
Expand Up @@ -103,14 +103,24 @@ let schema =
{
Schema.Table.name= "VM"
; columns=
[_ref; uuid; name_label; vbds; pp; name_description; tags; other_config]
Schema.ColumnMap.of_list
[
_ref
; uuid
; name_label
; vbds
; pp
; name_description
; tags
; other_config
]
; persistent= true
}
in
let vbd_table =
{
Schema.Table.name= "VBD"
; columns= [_ref; uuid; vm; type']
; columns= Schema.ColumnMap.of_list [_ref; uuid; vm; type']
; persistent= true
}
in
Expand Down Expand Up @@ -140,10 +150,18 @@ let many_to_many =
in
let foo_column = {bar_column with Schema.Column.name= "foos"} in
let foo_table =
{Schema.Table.name= "foo"; columns= [bar_column]; persistent= true}
{
Schema.Table.name= "foo"
; columns= Schema.ColumnMap.of_list [bar_column]
; persistent= true
}
in
let bar_table =
{Schema.Table.name= "bar"; columns= [foo_column]; persistent= true}
{
Schema.Table.name= "bar"
; columns= Schema.ColumnMap.of_list [foo_column]
; persistent= true
}
in
let database = {Schema.Database.tables= [foo_table; bar_table]} in
let many_to_many =
Expand Down
1 change: 1 addition & 0 deletions ocaml/idl/datamodel_schema.ml
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ let of_datamodel () =
; columns=
_ref
:: List.map (column obj) (flatten_fields obj.Datamodel_types.contents [])
|> Schema.ColumnMap.of_list
; persistent=
obj.Datamodel_types.persist = Datamodel_types.PersistEverything
}
Expand Down

0 comments on commit f1835bf

Please sign in to comment.