(* version_check manifest; *)
update_index unmarshalled_db;
iter_over_tables (fun name table -> set_table_in_cache cache name table) unmarshalled_db;
+ Db_cache_types.set_schema_vsn cache (manifest.Db_cache_types.schema_major_vsn, manifest.Db_cache_types.schema_minor_vsn);
+
manifest
let populate dbconn =
(* Atomically read the generation count and take a deep copy of the cache *)
let (gen_count, cache_copy) = Db_lock.with_lock (fun () -> Generation.read_generation(), Db_cache_types.snapshot cache_to_flush) in
debug "Flushing cache to redo-log";
- let db_cache_manifest = Db_cache_types.gen_manifest gen_count in
+ let db_cache_manifest = Db_cache_types.manifest_of_cache cache_to_flush gen_count in
let write_db_to_fd = (fun out_fd -> Db_xml.To.fd out_fd (db_cache_manifest, cache_copy)) in
Redo_log.write_db gen_count write_db_to_fd
end
let do_flush cache_to_flush filename =
flush_db_to_redo_log cache_to_flush;
- let db_cache_manifest = Db_cache_types.gen_manifest (Generation.read_generation()) in
+ let db_cache_manifest = Db_cache_types.manifest_of_cache cache (Generation.read_generation()) in
if not dbconn.Parse_db_conf.compress
then Db_xml.To.file filename (db_cache_manifest, cache_to_flush)
else
let manifest, unmarshalled_db = unmarshall dbconn in
manifest.Db_cache_types.schema_major_vsn, manifest.Db_cache_types.schema_minor_vsn
-let create_empty_db dbconn =
+let create_empty_db (major, minor) dbconn =
let empty_cache = create_empty_cache () in
+ Db_cache_types.set_schema_vsn empty_cache (major, minor);
+
List.iter (fun tbl -> set_table_in_cache empty_cache tbl (create_empty_table ())) db_table_names;
- let manifest = Db_cache_types.gen_manifest 0L (* new gen count *) in
+ let manifest = Db_cache_types.make_manifest major minor 0L (* new gen count *) in
let marshall filename = Db_lock.with_lock (fun () -> Db_xml.To.file filename (manifest, empty_cache)) in
atomically_write_to_db_file dbconn.Parse_db_conf.path marshall;
val flush_dirty: Parse_db_conf.db_connection -> bool
val force_flush_all: Parse_db_conf.db_connection -> Db_cache_types.cache option -> unit
val read_schema_vsn: Parse_db_conf.db_connection -> int*int
-val create_empty_db: Parse_db_conf.db_connection -> unit
+val create_empty_db: int*int -> Parse_db_conf.db_connection -> unit
val populate_and_read_manifest: Parse_db_conf.db_connection -> Db_cache_types.db_dump_manifest
val notify_delete: Parse_db_conf.db_connection -> string -> string -> unit
val flush_db_to_redo_log: Db_cache_types.cache -> unit
module type DB_CACHE =
sig
- val dump_db_cache : Db_cache_types.db_dump_manifest -> Unix.file_descr -> unit
+ val dump_db_cache : Int64.t -> Unix.file_descr -> unit
val stats : unit -> (string * int) list
let processed_str = SExpr.string_of (SExpr.Node processed) in
write_field context tbl objref fld processed_str)
- let dump_db_cache db_cache_manifest fd =
+ let dump_db_cache generation fd =
+ let db_cache_manifest = Db_cache_types.manifest_of_cache Db_backend.cache generation in
let time = Unix.gettimeofday() in
(* Snapshot the cache (uses the lock) and then slowly serialise the copy *)
Db_xml.To.fd fd (db_cache_manifest, snapshot cache);
type row = (string, string) Hashtbl.t
type table = (string, row) Hashtbl.t
-type cache = (string, table) Hashtbl.t
+type cache = {
+ cache: (string, table) Hashtbl.t;
+ schema: (int * int) option ref;
+}
type where_record = {table:string; return:string; where_field:string; where_value:string}
type structured_op_t = AddSet | RemoveSet | AddMap | RemoveMap
generation_count : Generation.t
}
-let gen_manifest gen_count =
+let make_manifest schema_major_vsn schema_minor_vsn gen_count =
{
- schema_major_vsn = Datamodel.schema_major_vsn;
- schema_minor_vsn = Datamodel.schema_minor_vsn;
+ schema_major_vsn = schema_major_vsn;
+ schema_minor_vsn = schema_minor_vsn;
generation_count = gen_count
}
+let schema_of_cache cache = match !(cache.schema) with
+| None -> (0, 0)
+| Some (major, minor) -> major, minor
+
+let manifest_of_cache cache gen_count =
+ let major, minor = schema_of_cache cache in
+ make_manifest major minor gen_count
+
+let set_schema_vsn cache (major, minor) = cache.schema := Some (major, minor)
+
(* Our versions of hashtbl.find *)
let lookup_field_in_row row fld =
try
let lookup_table_in_cache cache tbl =
try
- Hashtbl.find cache tbl
+ Hashtbl.find cache.cache tbl
with Not_found -> raise (DBCache_NotFound ("missing table",tbl,""))
let lookup_row_in_table tbl tblname objref =
Hashtbl.iter func table
let iter_over_tables func cache =
- Hashtbl.iter func cache
+ Hashtbl.iter func cache.cache
let iter_over_fields func row =
Hashtbl.iter func row
Hashtbl.replace table objref newval
let set_table_in_cache cache tblname newtbl =
- Hashtbl.replace cache tblname newtbl
+ Hashtbl.replace cache.cache tblname newtbl
let create_empty_row () = Hashtbl.create 20
let create_empty_table () = Hashtbl.create 20
-let create_empty_cache () = Hashtbl.create 20
+let create_empty_cache () = { cache = Hashtbl.create 20; schema = ref None }
let fold_over_fields func row acc = Hashtbl.fold func row acc
let fold_over_rows func table acc = Hashtbl.fold func table acc
-let fold_over_tables func cache acc = Hashtbl.fold func cache acc
+let fold_over_tables func cache acc = Hashtbl.fold func cache.cache acc
let remove_row_from_table tbl objref = Hashtbl.remove tbl objref
schema_minor_vsn : int;
generation_count : Int64.t;
}
-val gen_manifest : Int64.t -> db_dump_manifest
+
+val make_manifest : int -> int -> Int64.t -> db_dump_manifest
+val manifest_of_cache: cache -> Int64.t -> db_dump_manifest
+
+val schema_of_cache: cache -> int * int
+val set_schema_vsn: cache -> int * int -> unit
val lookup_field_in_row : row -> string -> string
val lookup_table_in_cache : cache -> string -> table
end;
was_anything_flushed
)
-
-let create_empty_db dbconn =
+(*
+let create_empty_db (major, minor) dbconn =
Generation.create_fresh dbconn;
- Backend_xml.create_empty_db dbconn
-
-let maybe_create_new_db dbconn =
+ Backend_xml.create_empty_db (major, minor) dbconn
+ *)
+let maybe_create_new_db (major,minor) dbconn =
if not (Sys.file_exists dbconn.Parse_db_conf.path) then
begin
Generation.create_fresh dbconn;
- Backend_xml.create_empty_db dbconn
+ Backend_xml.create_empty_db (major,minor) dbconn
end
let force_flush_all dbconn =
begin
read_in_database();
if !xmltostdout then
- Db_cache.DBCache.dump_db_cache (Db_cache_types.gen_manifest (Generation.read_generation())) (Unix.descr_of_out_channel stdout)
+ Db_cache.DBCache.dump_db_cache (Generation.read_generation()) (Unix.descr_of_out_channel stdout)
else
write_out_database !filename
end
let len = String.length minimally_compliant_miami_database in
ignore (Unix.write s minimally_compliant_miami_database 0 len)
else
- Db_cache.DBCache.dump_db_cache (Db_cache_types.gen_manifest (Generation.read_generation())) s
+ Db_cache.DBCache.dump_db_cache (Generation.read_generation()) s
(** Make sure the backup database version is compatible *)
let version_check manifest =
raise NoGeneration
| Some generation ->
(* Write the in-memory cache to the file *)
- let manifest = Db_cache_types.gen_manifest generation in
+ let manifest = Db_cache_types.manifest_of_cache Db_backend.cache generation in
Db_xml.To.file staging_path (manifest, Db_backend.cache);
Unixext.write_string_to_file (staging_path ^ ".generation") (Generation.to_string generation)
end
Db_dirty.make_blank_dirty_records();
(* Check if db files exist, if not make them *)
- List.iter Db_connections.maybe_create_new_db (Db_conn_store.read_db_connections());
+ let major, minor = Datamodel.schema_major_vsn, Datamodel.schema_minor_vsn in
+ List.iter (Db_connections.maybe_create_new_db (major,minor)) (Db_conn_store.read_db_connections());
(* Initialise in-memory database cache *)
debug "Populating db cache";