X-Git-Url: http://matita.cs.unibo.it/gitweb/?a=blobdiff_plain;f=components%2Fhmysql%2FhSqlite3.ml;h=b8687c5e9e20e85ce89a3f8a96c06e0508007519;hb=7059335177e9351146e870452e7e6e8ebca8c8c3;hp=3693e445b84642cba44e78a8c3550639946ddd53;hpb=bb9aa02b52977c05fe678a4e15bfc64e27c2c5f5;p=helm.git diff --git a/components/hmysql/hSqlite3.ml b/components/hmysql/hSqlite3.ml index 3693e445b..b8687c5e9 100644 --- a/components/hmysql/hSqlite3.ml +++ b/components/hmysql/hSqlite3.ml @@ -29,6 +29,12 @@ type result = Mysql.result option *) + +let debug = false +let debug_print = + if debug then prerr_endline else (fun _ ->()) +;; + type result = Sqlite3.row list type dbd = Sqlite3.db option @@ -43,8 +49,6 @@ type error_code = exception Error of string -let prerr_endline s = ()(*HLog.debug s;;*) - let profiler = HExtlib.profile "Sqlite3" let quick_connect @@ -54,19 +58,45 @@ let quick_connect = let username = Helm_registry.get "user.name" in let host_mangled = Pcre.replace ~pat:"[/ .]" ~templ:"_" host in - let tmp_db_name = - "/dev/shm/matita.db." ^ username ^ "." ^ host_mangled ^ "." ^ - string_of_int (Unix.getpid ()) + let find_db _ = + let base = "matita.db." ^ username ^ "." ^ host_mangled ^ "." in + let root = "/dev/shm/" in + let files = HExtlib.find ~test:(Pcre.pmatch ~pat:(base^"[0-9]+")) root in + let rec aux = function + | [] -> + debug_print ("HSqlite3: no valid db files found in memory"); + let name = root ^ base ^ string_of_int (Unix.getpid ()) in + debug_print ("HSqlite3: memory db file name: "^name); + name, true + | x::tl -> + debug_print ("HSqlite3: found a .db in memory: " ^ x); + match Array.to_list (Pcre.extract ~pat:"\\.([0-9]+)$" x) with + | [] | _::_::_::_ -> assert false + | [_;p] when HExtlib.is_dir ("/proc/" ^ p) -> + debug_print ("HSqlite3: found valid db file: " ^ x); + x, false + | _ -> + HLog.warn ("HSqlite3: dead process db file found: " ^ x); + HLog.warn ("HSqlite3: removing: " ^ x); + ignore (Sys.command ("rm " ^ x)); + aux tl + in + aux files in let db_name = host ^ "/" ^ database in let db_to_open = if HExtlib.is_dir "/dev/shm/" && HExtlib.writable_dir "/dev/shm/" && (not is_library) - then - (let cp_to_ram_cmd = "cp " ^ db_name ^ " " ^ tmp_db_name in - ignore (Sys.command cp_to_ram_cmd); + then ( + let tmp_db_name, first_time = find_db () in + let cp_to_ram_cmd = "cp " ^ db_name ^ " " ^ tmp_db_name in + if first_time then + if HExtlib.is_regular db_name then ignore (Sys.command cp_to_ram_cmd) + else debug_print ("HSqlite3: no initial db: " ^ db_name) + else debug_print "HSqlite3: not copying the db, already in memory"; let mv_to_disk_cmd _ = - ignore (Sys.command ("mv " ^ tmp_db_name ^ " " ^ db_name)) + if first_time then ignore (Sys.command ("mv "^tmp_db_name^" "^db_name)) + else debug_print "HSqlite3: not copying back the db" in at_exit mv_to_disk_cmd; tmp_db_name) @@ -77,6 +107,7 @@ let quick_connect (* attach the REGEX function *) Sqlite3.create_fun2 db "REGEXP" (fun s rex -> + try match rex, s with | Sqlite3.Data.TEXT rex, Sqlite3.Data.BLOB s | Sqlite3.Data.TEXT rex, Sqlite3.Data.TEXT s -> @@ -85,7 +116,8 @@ let quick_connect Sqlite3.Data.INT 1L else Sqlite3.Data.INT 0L - | _ -> raise (Error "wrong types to 'REGEXP'")); + | _ -> raise (Error "wrong types to 'REGEXP'") + with exn -> HLog.error (Printexc.to_string exn); raise exn); Some db ;; @@ -94,11 +126,17 @@ let disconnect db = | None -> () | Some db -> let b = Sqlite3.db_close db in - if b=false then prerr_endline "No Closed DataBase" + if b=false then debug_print "No Closed DataBase" ;; (* XXX hack, sqlite has a print "%q" that should be used, but is not bound *) -let escape s = Pcre.replace ~pat:"([^'])'([^'])" ~templ:"$1''$2" s +let escape s = + let s_escaped = Pcre.replace ~pat:"'" ~templ:"''" s in + (*let s_escaped = Pcre.replace ~pat:"([^'])'([^'])" ~templ:"$1''$2" s in*) + debug_print s; + debug_print s_escaped; + s_escaped +;; let string_of_rc = function |Sqlite3.Rc.OK -> "Sqlite3.Rc.OK" @@ -134,10 +172,10 @@ let string_of_rc = function "Sqlite3.Rc.UNKNOWN " ^ string_of_int (Sqlite3.Rc.int_of_unknown n) ;; -let pp_rc rc = prerr_endline (string_of_rc rc);; +let pp_rc rc = debug_print (string_of_rc rc);; let exec s db = - prerr_endline s; + debug_print s; let stored_result = ref [] in let store row = stored_result := row :: !stored_result @@ -150,7 +188,7 @@ let exec s db = in match rc with | Sqlite3.Rc.OK -> !stored_result - | _ -> raise (Error (string_of_rc rc)) + | _ -> raise (Error (string_of_rc rc ^ ": " ^ Sqlite3.errmsg db )) ;; let rec map res ~f =