@@ -15,41 +15,86 @@ type t = {
1515}
1616[@@ deriving show ]
1717
18+ (* hard coding rows for serialization *)
19+ let stop_name_size = 64
20+ let rail_line_size = 64
21+ let id_offset = 0
22+ let stop_offset = id_offset + 4
23+ let rail_offset = stop_name_size + stop_offset
24+ let row_size = 4 + stop_name_size + rail_line_size
1825let make_row ~id ~stop_name ~rail_line : row_t = { id; stop_name; rail_line }
1926
2027let create_table ~(id : int ) ~(table_name : string ) : t =
21- { table_id = id; table_name; table_fields = [] ; rows = [] }
28+ let rows = [] in
29+ { table_id = id; table_name; table_fields = [] ; rows }
2230
2331(* let open_table ~(table_name : string) : t = *)
2432(* match table_name with *)
2533(* | *)
2634
27- let already_in_table ~(table : t ) ~(row : row_t ) : bool =
28- (* List.mem row table.rows *)
29- let rec search (table_rows : row_t list ) (id : int ) : bool =
30- match table_rows with
31- | [] -> false
32- | h :: tail ->
33- if h.id = id then
34- true
35- else
36- search tail id
37- in
38- search table.rows row.id
35+ (* let already_in_table ~(table : t) ~(row : row_t) : bool = *)
36+ (* (* List.mem row table.rows *) *)
37+ (* let rec search (table_rows : row_t list) (id : int) : bool = *)
38+ (* match table_rows with *)
39+ (* | [] -> false *)
40+ (* | h :: tail -> *)
41+ (* if h.id = id then *)
42+ (* true *)
43+ (* else *)
44+ (* search tail id *)
45+ (* in *)
46+ (* search table.rows row.id *)
3947
4048(* currently this is a bit functional in it's nature
4149 always returning a new table rather than adjusting the existing one.
4250 May want to consider changing rows to be mutable, or need to consider disk
4351 writes here - TBD *)
44- let add_row ~(table : t ) ~(row : row_t ) : t =
45- if already_in_table ~table ~row then
46- table
52+ (* let add_row ~(table : t) ~(row : row_t) : t = *)
53+ (* if already_in_table ~table ~row then *)
54+ (* table *)
55+ (* else *)
56+ (* (* let new_row_list = List.fold_left (fun acc l -> acc :: l) table.rows [row] in *) *)
57+ (* let new_row_list = table.rows @ [ row ] in *)
58+ (* { *)
59+ (* table_id = table.table_id; *)
60+ (* table_name = table.table_name; *)
61+ (* table_fields = table.table_fields; *)
62+ (* rows = new_row_list; *)
63+ (* num_rows = List.length new_row_list; *)
64+ (* } *)
65+
66+ let pad_fixed_size (size : int ) (s : string ) : string =
67+ if String. length s > size then
68+ failwith
69+ (Printf. sprintf " String too long.\n Greater than input size of %d" size)
4770 else
48- (* let new_row_list = List.fold_left (fun acc l -> acc :: l) table.rows [row] in *)
49- let new_row_list = table.rows @ [ row ] in
50- {
51- table_id = table.table_id;
52- table_name = table.table_name;
53- table_fields = table.table_fields;
54- rows = new_row_list;
55- }
71+ s ^ String. make (size - String. length s) '\000'
72+
73+ let trim_null (s : string ) : string =
74+ match String. index_opt s '\000' with
75+ | None -> s
76+ | Some thing -> String. sub s 0 thing
77+
78+ let serialize_to_page (row : row_t ) ~(block_size : int ) : Page.Page.t =
79+ if row_size > block_size then
80+ failwith " Row doesn't fit into page" ;
81+
82+ let page = Page.Page. make ~block_size in
83+ (* serialize id, stop name, then rail name *)
84+ Page.Page. set_int32 page id_offset (Int32. of_int row.id);
85+ Page.Page. set_string_raw page stop_offset
86+ (pad_fixed_size stop_name_size row.stop_name);
87+ Page.Page. set_string_raw page rail_offset
88+ (pad_fixed_size rail_line_size row.rail_line);
89+ page
90+
91+ let deserialize_from_page (page : Page.Page.t ) : row_t =
92+ (* get the id, stop name, and rail name from the Int32 offsets*)
93+ let id = Int32. to_int (Page.Page. get_int32 page id_offset) in
94+ let stop_name =
95+ trim_null (Page.Page. get_string_raw page stop_offset stop_name_size)
96+ in
97+ let rail_line =
98+ trim_null (Page.Page. get_string_raw page rail_offset rail_line_size)
99+ in
100+ { id; stop_name; rail_line }
0 commit comments