Skip to content

Commit 0471acd

Browse files
committed
Merge pull request #2018 from djs55/forupstream/database-schema
Database: document, re-add a test and use a value type rather than strings
2 parents bd3f2ac + 54a3698 commit 0471acd

24 files changed

+685
-421
lines changed

Makefile

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,7 @@ test:
6060
@echo
6161
@echo @ HA binpack test
6262
@./ocaml/xapi/binpack
63-
# The following test no longer runs:
64-
# ./ocaml/database/database_test
63+
./ocaml/database/database_test
6564
# The following test no longer compiles:
6665
# ./ocaml/xenops/device_number_test
6766
# The following test must be run in dom0:

ocaml/database/OMakefile

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11

22
OCAMLINCLUDES = ../idl/ocaml_backend ../xapi ../idl ../util ../autogen ..
3-
OCAMLPACKS = xml-light2 stdext stunnel http-svr xcp sexpr rpclib uuid gzip xcp-inventory
3+
OCAMLPACKS = xml-light2 stdext stunnel http-svr xcp sexpr rpclib uuid gzip xcp-inventory oUnit
44
#OCAMLPPFLAGS = -pp "camlp4o"
55
#OCAMLDEPFLAGS = -pp "camlp4o"
66

77
UseCamlp4(rpclib.syntax, db_rpc_common_v2 db_cache_types db_filter_types)
8+
UseCamlp4(sexplib.syntax, schema)
89

910
OCamlGeneratedFiles(db_filter_parse.ml db_filter_parse.mli db_filter_lex.ml)
1011

@@ -19,8 +20,8 @@ BLOCK_DEVICE_IO_FILES = \
1920
OCamlProgram(block_device_io, $(BLOCK_DEVICE_IO_FILES))
2021
OCamlDocProgram(block_device_io, $(BLOCK_DEVICE_IO_FILES))
2122

22-
DATABASE_SERVER_FILES = database_server_main test_schemas database_test ../autogen/db_actions
23-
DATABASE_TEST_FILES = database_test database_test_main test_schemas
23+
DATABASE_SERVER_FILES = database_server_main test_schemas ../autogen/db_actions
24+
DATABASE_TEST_FILES = db_cache_test test_schemas
2425
section:
2526
#XXX there are lots of interdependencies which we should be aim to remove
2627
OCAML_LIBS += ../util/version ../idl/ocaml_backend/common ../idl/ocaml_backend/client ../util/stats ../idl/ocaml_backend/server

ocaml/database/database_test.ml

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -175,61 +175,61 @@ module Tests = functor(Client: Db_interface.DB_ACCESS) -> struct
175175
let dump db g =
176176
let tables = Db_cache_types.Database.tableset db in
177177
Db_cache_types.TableSet.fold_over_recent g
178-
(fun c u d name table acc ->
178+
(fun name _ table acc ->
179179
Db_cache_types.Table.fold_over_recent g
180-
(fun c u d r acc ->
180+
(fun r { Db_cache_types.Stat.created; modified; deleted } _ acc ->
181181
let s =
182182
try
183183
let row = Db_cache_types.Table.find r table in
184184
let s = Db_cache_types.Row.fold_over_recent g
185-
(fun c u d k v acc ->
185+
(fun k _ v acc ->
186186
Printf.sprintf "%s %s=%s" acc k v) row "" in
187187
s
188188
with _ -> "(deleted)"
189189
in
190-
Printf.printf "%s(%s): (%Ld %Ld %Ld) %s\n" name r c u d s;
191-
())
192-
(fun () -> ()) table ()) tables ()
190+
Printf.printf "%s(%s): (%Ld %Ld %Ld) %s\n" name r created modified deleted s;
191+
()
192+
) table ()) tables ()
193193
in
194194

195195
let get_created db g =
196196
let tables = Db_cache_types.Database.tableset db in
197197
Db_cache_types.TableSet.fold_over_recent g
198-
(fun c u d name table acc ->
198+
(fun name _ table acc ->
199199
Db_cache_types.Table.fold_over_recent g
200-
(fun c u d r acc ->
201-
if c>=g then (name,r)::acc else acc) ignore table acc
200+
(fun r { Db_cache_types.Stat.created } _ acc ->
201+
if created>=g then (name,r)::acc else acc) table acc
202202
) tables []
203203
in
204204

205205
let get_updated db g =
206206
let tables = Db_cache_types.Database.tableset db in
207207
Db_cache_types.TableSet.fold_over_recent g
208-
(fun c u d name table acc ->
208+
(fun name _ table acc ->
209209
Db_cache_types.Table.fold_over_recent g
210-
(fun c u d r acc ->
210+
(fun r _ _ acc ->
211211
let row = Db_cache_types.Table.find r table in
212212
Db_cache_types.Row.fold_over_recent g
213-
(fun c u d k v acc ->
213+
(fun k _ v acc ->
214214
(r,(k,v))::acc) row acc)
215-
ignore table acc) tables []
215+
table acc) tables []
216216
in
217217

218218
let get_deleted db g =
219219
let tables = Db_cache_types.Database.tableset db in
220220
Db_cache_types.TableSet.fold_over_recent g
221-
(fun c u d name table acc ->
222-
Db_cache_types.Table.fold_over_recent g
223-
(fun c u d r acc ->
224-
if d > g then r::acc else acc)
225-
ignore table acc) tables []
221+
(fun name _ table acc ->
222+
Db_cache_types.Table.fold_over_deleted g
223+
(fun r { Db_cache_types.Stat.deleted } acc ->
224+
if deleted > g then r::acc else acc)
225+
table acc) tables []
226226
in
227227

228228
let get_max db =
229229
let tables = Db_cache_types.Database.tableset db in
230230
Db_cache_types.TableSet.fold_over_recent (-1L)
231-
(fun c u d _ _ largest ->
232-
max c (max u (max d largest))) tables (-1L)
231+
(fun _ { Db_cache_types.Stat.created; modified; deleted } _ largest ->
232+
max created (max modified (max deleted largest))) tables (-1L)
233233
in
234234

235235
let db = Db_ref.get_database t in

ocaml/database/db_action_helper.ml

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,11 @@ let events_notify ?(snapshot) ty op ref =
2222
match !__callback with
2323
| None -> ()
2424
| Some f -> f ?snapshot ty op ref
25-
25+
(*
2626
exception Db_set_or_map_parse_fail of string
2727
2828
let parse_sexpr s : SExpr.t list =
2929
match SExpr_TS.of_string s with
3030
| SExpr.Node xs -> xs
3131
| _ -> raise (Db_set_or_map_parse_fail s)
32-
33-
let add_key_to_set key set =
34-
if List.mem (SExpr.String key) set
35-
then set
36-
else SExpr.String key :: set
32+
*)

ocaml/database/db_backend.ml

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,27 +42,28 @@ let blow_away_non_persistent_fields (schema: Schema.t) db =
4242
(* Generate a new row given a table schema *)
4343
let row schema row : Row.t * int64 =
4444
Row.fold
45-
(fun name created updated v (acc,max_upd) ->
45+
(fun name { Stat.created; modified } v (acc,max_upd) ->
4646
try
4747
let col = Schema.Table.find name schema in
48-
let v',updated' = if col.Schema.Column.persistent then v,updated else col.Schema.Column.empty,g in
49-
(Row.update updated' name "" (fun _ -> v') (Row.add created name v' acc),max max_upd updated')
48+
let empty = col.Schema.Column.empty in
49+
let v',modified' = if col.Schema.Column.persistent then v,modified else empty,g in
50+
(Row.update modified' name empty (fun _ -> v') (Row.add created name v' acc),max max_upd modified')
5051
with Not_found ->
5152
Printf.printf "Skipping unknown column: %s\n%!" name;
52-
(acc,max max_upd updated)) row (Row.empty,0L) in
53+
(acc,max max_upd modified)) row (Row.empty,0L) in
5354
(* Generate a new table *)
5455
let table tblname tbl : Table.t =
5556
let schema = Schema.Database.find tblname schema.Schema.database in
5657
Table.fold
57-
(fun objref created updated r acc ->
58+
(fun objref { Stat.created; modified } r acc ->
5859
let (r,updated) = row schema r in
59-
Table.update updated objref Row.empty (fun _ -> r) (Table.add created objref r acc)) tbl Table.empty in
60+
Table.update modified objref Row.empty (fun _ -> r) (Table.add created objref r acc)) tbl Table.empty in
6061
Database.update
6162
(fun ts ->
6263
TableSet.fold
63-
(fun tblname created updated tbl acc ->
64+
(fun tblname { Stat.created; modified } tbl acc ->
6465
let tbl' = table tblname tbl in
65-
TableSet.add updated tblname tbl' acc) ts TableSet.empty)
66+
TableSet.add modified tblname tbl' acc) ts TableSet.empty)
6667
db
6768

6869
let db_registration_mutex = Mutex.create ()

0 commit comments

Comments
 (0)