Skip to content

Commit

Permalink
CP-49140: [prep]: database: drop unused of_sexp conversions
Browse files Browse the repository at this point in the history
sexp_of is used when saving the schema, but of_sexp is unused.

This enables a future commit to store the deserialized value as a pair value,
and deserialization (unmarshaling) requires knowing the schema,
whereas serialization (marshaling) does not.
Thus we couldn't implement a generic `t_of_sexp` function, but with this change
we won't have to.

No functional change.
  • Loading branch information
edwintorok committed Dec 11, 2024
1 parent 98e7414 commit e7d933d
Showing 1 changed file with 9 additions and 22 deletions.
31 changes: 9 additions & 22 deletions ocaml/database/schema.ml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ open Sexplib0.Sexp_conv

module Type = struct
type t = String | Set (** of strings *) | Pairs (** of strings *)
[@@deriving sexp]
[@@deriving sexp_of]

exception Error of t * t

Expand All @@ -38,7 +38,7 @@ module Value = struct
| String of string
| Set of string list
| Pairs of (string * string) list
[@@deriving sexp]
[@@deriving sexp_of]

let marshal = function
| String x ->
Expand Down Expand Up @@ -95,7 +95,7 @@ module Column = struct
; issetref: bool
(** only so we can special case set refs in the interface *)
}
[@@deriving sexp]
[@@deriving sexp_of]

let name_of t = t.name
end
Expand All @@ -109,7 +109,7 @@ let values_of_table tbl = Hashtbl.fold (fun _ v vs -> v :: vs) tbl []

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

type t = {
name: string
Expand All @@ -133,11 +133,6 @@ module Table = struct
let t' = t'_of_t t in
sexp_of_t' t'

let t_of_sexp s =
let ({name; columns; persistent} : t') = t'_of_sexp s in
let columns = tabulate columns ~key_fn:Column.name_of in
({name; columns; persistent} : t)

let find name (t : t) =
match Hashtbl.find_opt t.columns name with
| Some c ->
Expand All @@ -157,10 +152,10 @@ module Table = struct
end

type relationship = OneToMany of string * string * string * string
[@@deriving sexp]
[@@deriving sexp_of]

module Database = struct
type t' = {tables: Table.t list} [@@deriving sexp]
type t' = {tables: Table.t list} [@@deriving sexp_of]

type t = {tables: (string, Table.t) Hashtbl.t}

Expand All @@ -180,10 +175,6 @@ module Database = struct
let t' = t'_of_t t in
sexp_of_t' t'

let t_of_sexp s =
let t' = t'_of_sexp s in
t_of_t' t'

let find name t =
match Hashtbl.find_opt t.tables name with
| Some tbl ->
Expand All @@ -197,7 +188,7 @@ module Database = struct
end

(** indexed by table name, a list of (this field, foreign table, foreign field) *)
type foreign = (string * string * string) list [@@deriving sexp]
type foreign = (string * string * string) list [@@deriving sexp_of]

module ForeignMap = struct
include Map.Make (struct
Expand All @@ -206,17 +197,13 @@ module ForeignMap = struct
let compare = Stdlib.compare
end)

type t' = (string * foreign) list [@@deriving sexp]
type t' = (string * foreign) list [@@deriving sexp_of]

type m = foreign t

let sexp_of_m t : Sexp.t =
let t' = fold (fun key foreign acc -> (key, foreign) :: acc) t [] in
sexp_of_t' t'

let m_of_sexp sexp : m =
let t' = t'_of_sexp sexp in
List.fold_left (fun acc (key, foreign) -> add key foreign acc) empty t'
end

type t = {
Expand All @@ -227,7 +214,7 @@ type t = {
; one_to_many: ForeignMap.m
; many_to_many: ForeignMap.m
}
[@@deriving sexp]
[@@deriving sexp_of]

let database x = x.database

Expand Down

0 comments on commit e7d933d

Please sign in to comment.