[db] name tables with respect to their keys

Summary: These names will make more sense as we add more columns.

Reviewed By: mbouaziz

Differential Revision: D6711767

fbshipit-source-id: a135a35
master
Jules Villard 7 years ago committed by Facebook Github Bot
parent 3d7878ef25
commit 8fe2d793b9

@ -42,14 +42,14 @@ let replace_statement =
values so the lexicographic ordering for (:akind, :sfile) is done by hand *) values so the lexicographic ordering for (:akind, :sfile) is done by hand *)
ResultsDatabase.register_statement ResultsDatabase.register_statement
{| {|
INSERT OR REPLACE INTO attributes INSERT OR REPLACE INTO procedures
SELECT :pname, :akind, :sfile, :pattr SELECT :pname, :akind, :sfile, :pattr
FROM ( FROM (
SELECT NULL SELECT NULL
FROM ( FROM (
SELECT COALESCE(MAX(attr_kind),-1) AS attr_kind, SELECT COALESCE(MAX(attr_kind),-1) AS attr_kind,
COALESCE(MAX(source_file),"") AS source_file COALESCE(MAX(source_file),"") AS source_file
FROM attributes FROM procedures
WHERE proc_name = :pname ) WHERE proc_name = :pname )
WHERE attr_kind < :akind WHERE attr_kind < :akind
OR (attr_kind = :akind AND source_file < :sfile) )|} OR (attr_kind = :akind AND source_file < :sfile) )|}
@ -72,7 +72,7 @@ let find_more_defined_statement =
ResultsDatabase.register_statement ResultsDatabase.register_statement
{| {|
SELECT attr_kind SELECT attr_kind
FROM attributes FROM procedures
WHERE proc_name = :pname WHERE proc_name = :pname
AND attr_kind > :akind AND attr_kind > :akind
|} |}
@ -90,12 +90,12 @@ let should_try_to_update pname_blob akind =
let select_statement = let select_statement =
ResultsDatabase.register_statement "SELECT proc_attributes FROM attributes WHERE proc_name = :k" ResultsDatabase.register_statement "SELECT proc_attributes FROM procedures WHERE proc_name = :k"
let select_defined_statement = let select_defined_statement =
ResultsDatabase.register_statement ResultsDatabase.register_statement
"SELECT proc_attributes FROM attributes WHERE proc_name = :k AND attr_kind = %Ld" "SELECT proc_attributes FROM procedures WHERE proc_name = :k AND attr_kind = %Ld"
(int64_of_attributes_kind ProcDefined) (int64_of_attributes_kind ProcDefined)

@ -92,7 +92,7 @@ let check_cfg_connectedness cfg =
let load_statement = let load_statement =
ResultsDatabase.register_statement "SELECT cfgs FROM cfg WHERE source_file = :k" ResultsDatabase.register_statement "SELECT cfgs FROM source_files WHERE source_file = :k"
module SQLite = SqliteUtils.MarshalledData (struct module SQLite = SqliteUtils.MarshalledData (struct
@ -277,7 +277,7 @@ let mark_unchanged_pdescs cfg_new cfg_old =
let store_statement = let store_statement =
ResultsDatabase.register_statement "INSERT OR REPLACE INTO cfg VALUES (:source, :cfgs)" ResultsDatabase.register_statement "INSERT OR REPLACE INTO source_files VALUES (:source, :cfgs)"
let store source_file cfg = let store source_file cfg =
@ -632,6 +632,6 @@ let exists_for_source_file source =
let get_captured_source_files () = let get_captured_source_files () =
let db = ResultsDatabase.get_database () in let db = ResultsDatabase.get_database () in
Sqlite3.prepare db "SELECT source_file FROM cfg" Sqlite3.prepare db "SELECT source_file FROM source_files"
|> SqliteUtils.sqlite_result_rev_list_step db ~log:"getting all source files" |> SqliteUtils.sqlite_result_rev_list_step db ~log:"getting all source files"
|> List.filter_map ~f:(Option.map ~f:SourceFile.SQLite.deserialize) |> List.filter_map ~f:(Option.map ~f:SourceFile.SQLite.deserialize)

@ -9,7 +9,7 @@
open! IStd open! IStd
module L = Logging module L = Logging
let merge_attributes_table ~db_file = let merge_procedures_table ~db_file =
let db = ResultsDatabase.get_database () in let db = ResultsDatabase.get_database () in
(* Do the merge purely in SQL for great speed. The query works by doing a left join between the (* Do the merge purely in SQL for great speed. The query works by doing a left join between the
sub-table and the main one, and applying the same "more defined" logic as in Attributes in the sub-table and the main one, and applying the same "more defined" logic as in Attributes in the
@ -17,11 +17,11 @@ let merge_attributes_table ~db_file =
NULL). All the rows that pass this filter are inserted/updated into the main table. *) NULL). All the rows that pass this filter are inserted/updated into the main table. *)
Sqlite3.exec db Sqlite3.exec db
{| {|
INSERT OR REPLACE INTO attributes INSERT OR REPLACE INTO procedures
SELECT sub.proc_name, sub.attr_kind, sub.source_file, sub.proc_attributes SELECT sub.proc_name, sub.attr_kind, sub.source_file, sub.proc_attributes
FROM ( FROM (
attached.attributes AS sub attached.procedures AS sub
LEFT OUTER JOIN attributes AS main LEFT OUTER JOIN procedures AS main
ON sub.proc_name = main.proc_name ) ON sub.proc_name = main.proc_name )
WHERE WHERE
main.attr_kind IS NULL main.attr_kind IS NULL
@ -29,13 +29,14 @@ WHERE
OR (main.attr_kind = sub.attr_kind AND main.source_file < sub.source_file) OR (main.attr_kind = sub.attr_kind AND main.source_file < sub.source_file)
|} |}
|> SqliteUtils.check_sqlite_error db |> SqliteUtils.check_sqlite_error db
~log:(Printf.sprintf "copying attributes of database '%s'" db_file) ~log:(Printf.sprintf "copying procedures of database '%s'" db_file)
let merge_cfg_table ~db_file = let merge_source_files_table ~db_file =
let db = ResultsDatabase.get_database () in let db = ResultsDatabase.get_database () in
Sqlite3.exec db "INSERT OR REPLACE INTO cfg SELECT * FROM attached.cfg" Sqlite3.exec db "INSERT OR REPLACE INTO source_files SELECT * FROM attached.source_files"
|> SqliteUtils.check_sqlite_error db ~log:(Printf.sprintf "copying cfg of database '%s'" db_file) |> SqliteUtils.check_sqlite_error db
~log:(Printf.sprintf "copying source_files of database '%s'" db_file)
let merge ~db_file = let merge ~db_file =
@ -43,8 +44,8 @@ let merge ~db_file =
Sqlite3.exec main_db (Printf.sprintf "ATTACH '%s' AS attached" db_file) Sqlite3.exec main_db (Printf.sprintf "ATTACH '%s' AS attached" db_file)
|> SqliteUtils.check_sqlite_error ~fatal:true main_db |> SqliteUtils.check_sqlite_error ~fatal:true main_db
~log:(Printf.sprintf "attaching database '%s'" db_file) ; ~log:(Printf.sprintf "attaching database '%s'" db_file) ;
merge_attributes_table ~db_file ; merge_procedures_table ~db_file ;
merge_cfg_table ~db_file ; merge_source_files_table ~db_file ;
Sqlite3.exec main_db "DETACH attached" Sqlite3.exec main_db "DETACH attached"
|> SqliteUtils.check_sqlite_error ~fatal:true main_db |> SqliteUtils.check_sqlite_error ~fatal:true main_db
~log:(Printf.sprintf "detaching database '%s'" db_file) ; ~log:(Printf.sprintf "detaching database '%s'" db_file) ;

@ -17,24 +17,24 @@ let database_filename = "results.db"
let database_fullpath = Config.results_dir ^/ database_filename let database_fullpath = Config.results_dir ^/ database_filename
let create_attributes_table db = let create_procedures_table db =
(* it would be nice to use "WITHOUT ROWID" here but ancient versions of sqlite do not support (* it would be nice to use "WITHOUT ROWID" here but ancient versions of sqlite do not support
it *) it *)
SqliteUtils.exec db ~log:"creating attributes table" SqliteUtils.exec db ~log:"creating procedures table"
~stmt: ~stmt:
{| {|
CREATE TABLE IF NOT EXISTS attributes CREATE TABLE IF NOT EXISTS procedures
( proc_name TEXT PRIMARY KEY ( proc_name TEXT PRIMARY KEY
, attr_kind INTEGER NOT NULL , attr_kind INTEGER NOT NULL
, source_file TEXT NOT NULL , source_file TEXT NOT NULL
, proc_attributes BLOB NOT NULL )|} , proc_attributes BLOB NOT NULL )|}
let create_cfg_table db = let create_source_files_table db =
SqliteUtils.exec db ~log:"creating cfg table" SqliteUtils.exec db ~log:"creating source_files table"
~stmt: ~stmt:
{| {|
CREATE TABLE IF NOT EXISTS cfg CREATE TABLE IF NOT EXISTS source_files
( source_file TEXT PRIMARY KEY ( source_file TEXT PRIMARY KEY
, cfgs BLOB NOT NULL )|} , cfgs BLOB NOT NULL )|}
@ -42,8 +42,8 @@ CREATE TABLE IF NOT EXISTS cfg
let create_db () = let create_db () =
let temp_db = Filename.temp_file ~in_dir:Config.results_dir database_filename ".tmp" in let temp_db = Filename.temp_file ~in_dir:Config.results_dir database_filename ".tmp" in
let db = Sqlite3.db_open ~mutex:`FULL temp_db in let db = Sqlite3.db_open ~mutex:`FULL temp_db in
create_attributes_table db ; create_procedures_table db ;
create_cfg_table db ; create_source_files_table db ;
(* This should be the default but better be sure, otherwise we cannot access the database concurrently. This has to happen before setting WAL mode. *) (* This should be the default but better be sure, otherwise we cannot access the database concurrently. This has to happen before setting WAL mode. *)
SqliteUtils.exec db ~log:"locking mode=NORMAL" ~stmt:"PRAGMA locking_mode=NORMAL" ; SqliteUtils.exec db ~log:"locking mode=NORMAL" ~stmt:"PRAGMA locking_mode=NORMAL" ;
( match Config.sqlite_vfs with ( match Config.sqlite_vfs with
@ -70,10 +70,10 @@ let get_database () = Option.value_exn !database
let reset_capture_tables () = let reset_capture_tables () =
let db = get_database () in let db = get_database () in
SqliteUtils.exec db ~log:"drop attributes table" ~stmt:"DROP TABLE attributes" ; SqliteUtils.exec db ~log:"drop procedures table" ~stmt:"DROP TABLE procedures" ;
create_attributes_table db ; create_procedures_table db ;
SqliteUtils.exec db ~log:"drop cfg table" ~stmt:"DROP TABLE cfg" ; SqliteUtils.exec db ~log:"drop source_files table" ~stmt:"DROP TABLE source_files" ;
create_cfg_table db create_source_files_table db
let db_canonicalize () = let db_canonicalize () =

Loading…
Cancel
Save