]> matita.cs.unibo.it Git - helm.git/blobdiff - components/library/libraryClean.ml
more fixes to the cleanu phase
[helm.git] / components / library / libraryClean.ml
index 6998c1cbf1ceee98a6d84e4e43c9307b5a8f98df..9a3b172454c00268bd0a4320030bee9711b76a6a 100644 (file)
@@ -34,14 +34,17 @@ module HGT = Http_getter_types;;
 module HG = Http_getter;;
 module UM = UriManager;;
 
-let cache_of_processed_baseuri = Hashtbl.create 1024
 
-let one_step_depend suri dbtype dbd =
-  let buri =
-    try
-      UM.buri_of_uri (UM.uri_of_string suri)
-    with UM.IllFormedUri _ -> suri
-  in
+let strip_xpointer s = Pcre.replace ~pat:"#.*$" s ;;
+
+let safe_buri_of_suri suri =
+  try
+    UM.buri_of_uri (UM.uri_of_string suri)
+  with
+    UM.IllFormedUri _ -> suri
+
+let one_step_depend cache_of_processed_baseuri suri dbtype dbd =
+  let buri = safe_buri_of_suri suri in
   if Hashtbl.mem cache_of_processed_baseuri buri then 
     []
   else
@@ -53,18 +56,22 @@ let one_step_depend suri dbtype dbd =
         let obj_tbl = MetadataTypes.obj_tbl () in
         if HSql.isMysql dbtype dbd then        
           sprintf ("SELECT source, h_occurrence FROM %s WHERE " 
-            ^^ "h_occurrence REGEXP '^%s[^/]*$'") obj_tbl buri
+            ^^ "h_occurrence REGEXP '"^^
+            "^%s\\([^/]+\\|[^/]+#xpointer.*\\)$"^^"'")
+          obj_tbl buri
        else
-               begin
+         begin
             sprintf ("SELECT source, h_occurrence FROM %s WHERE " 
-            ^^ "REGEXP(h_occurrence, '^%s[^/]*$')") obj_tbl buri
-          (* implementation with vanilla ocaml-sqlite3
-          HLog.debug "Warning SELECT without REGEXP";
-         sprintf
-            ("SELECT source, h_occurrence FROM %s WHERE " ^^ 
-             "h_occurrence LIKE '%s%%' " ^^ HSql.escape_string_for_like)
-                 obj_tbl buri*)
-               end
+            ^^ "REGEXP(h_occurrence, '"^^
+            "^%s\\([^/]+\\|[^/]+#xpointer.*\\)$"^^"')")
+            obj_tbl buri
+            (* implementation with vanilla ocaml-sqlite3
+            HLog.debug "Warning SELECT without REGEXP";
+           sprintf
+              ("SELECT source, h_occurrence FROM %s WHERE " ^^ 
+               "h_occurrence LIKE '%s%%' " ^^ HSql.escape_string_for_like)
+                 obj_tbl buri*)
+         end
       in
       try 
         let rc = HSql.exec dbtype dbd query in
@@ -72,8 +79,9 @@ let one_step_depend suri dbtype dbd =
         HSql.iter rc (
           fun row -> 
             match row.(0), row.(1) with 
-            | Some uri, Some occ when Filename.dirname occ = buri -> 
-                l := uri :: !l
+            | Some uri, Some occ when 
+                Filename.dirname (strip_xpointer occ) = buri -> 
+                  l := uri :: !l
             | _ -> ());
         let l = List.sort Pervasives.compare !l in
         HExtlib.list_uniq l
@@ -81,12 +89,6 @@ let one_step_depend suri dbtype dbd =
         exn -> raise exn (* no errors should be accepted *)
     end
     
-let safe_buri_of_suri suri =
-  try
-    UM.buri_of_uri (UM.uri_of_string suri)
-  with
-    UM.IllFormedUri _ -> suri
-
 let db_uris_of_baseuri buri =
  let dbd = LibraryDb.instance () in
  let dbtype = 
@@ -107,22 +109,20 @@ let db_uris_of_baseuri buri =
  in
  try 
   let rc = HSql.exec dbtype dbd query in
-  let strip_xpointer s = Pcre.replace ~pat:"#.*$" s in
   let l = ref [] in
   HSql.iter rc (
     fun row -> 
       match row.(0) with 
       | Some uri when Filename.dirname (strip_xpointer uri) = buri -> 
           l := uri :: !l
-      | _ ->
-          ());
+      | _ -> ());
   let l = List.sort Pervasives.compare !l in
   HExtlib.list_uniq l
  with
   exn -> raise exn (* no errors should be accepted *)
 ;;
 
-let close_uri_list uri_to_remove =
+let close_uri_list cache_of_processed_baseuri uri_to_remove =
   let dbd = LibraryDb.instance () in
   let dbtype = 
     if Helm_registry.get_bool "matita.system" then HSql.Library else HSql.User
@@ -176,17 +176,22 @@ let close_uri_list uri_to_remove =
   (* now we want the list of all uri that depend on them *) 
   let depend = 
     List.fold_left
-    (fun acc u -> one_step_depend u dbtype dbd @ acc) [] uri_to_remove
+    (fun acc u -> one_step_depend cache_of_processed_baseuri u dbtype dbd @ acc)
+    [] uri_to_remove
   in
   let depend = 
     HExtlib.list_uniq (List.fast_sort Pervasives.compare depend) 
   in
   uri_to_remove, depend
+;;
 
-let rec close_db uris next =
+let rec close_db cache_of_processed_baseuri uris next =
   match next with
   | [] -> uris
-  | l -> let uris, next = close_uri_list l in close_db uris next @ uris
+  | l -> 
+     let uris, next = close_uri_list cache_of_processed_baseuri l in
+     close_db cache_of_processed_baseuri uris next @ uris
+;;
   
 let cleaned_no = ref 0;;
 
@@ -203,10 +208,11 @@ let moo_root_dir = lazy (
           | _ -> assert false)
         (Helm_registry.get_list Helm_registry.string "getter.prefix"))
   in
-  String.sub url 7 (String.length url - 7)  (* remove heading "file:///" *)
-)
+  String.sub url 7 (String.length url - 7))
+;;
 
 let clean_baseuris ?(verbose=true) buris =
+  let cache_of_processed_baseuri = Hashtbl.create 1024 in
   let dbd = LibraryDb.instance () in
   let dbtype = 
     if Helm_registry.get_bool "matita.system" then HSql.Library else HSql.User
@@ -216,7 +222,7 @@ let clean_baseuris ?(verbose=true) buris =
   debug_prerr "clean_baseuris called on:";
   if debug then
     List.iter debug_prerr buris; 
-  let l = close_db [] buris in
+  let l = close_db cache_of_processed_baseuri [] buris in
   let l = HExtlib.list_uniq (List.fast_sort Pervasives.compare l) in
   let l = List.map UriManager.uri_of_string l in
   debug_prerr "clean_baseuri will remove:";