]> matita.cs.unibo.it Git - helm.git/blobdiff - helm/software/components/ng_kernel/nCicLibrary.ml
Persistent db (to lookup names in the library) inefficiently implemented as
[helm.git] / helm / software / components / ng_kernel / nCicLibrary.ml
index 1cb3a118b40c44eae03ac7bd6b445c6f9ea02162..984b7ef6840a68552e952c58b882d10addbb2bb7 100644 (file)
@@ -20,7 +20,8 @@ type timestamp =
 
 let time0 = [],[],NUri.UriMap.empty;;
 let storage = ref [];;
-let aliases = ref [];;
+let local_aliases = ref [];;
+let global_aliases = ref [];;
 let cache = ref NUri.UriMap.empty;;
 
 class status =
@@ -28,26 +29,32 @@ class status =
   val timestamp = (time0 : timestamp)
   method timestamp = timestamp
   method set_timestamp v = {< timestamp = v >}
+  method set_library_status
+   : 'status. < timestamp : timestamp; .. > as 'status -> 'self
+   = fun o -> {< timestamp = o#timestamp >}
  end
 
 let time_travel status =
  let sto,ali,cac = status#timestamp in
-  storage := sto; aliases := ali; cache := cac
+  storage := sto; local_aliases := ali; cache := cac
 ;;
 
-let path_of_baseuri baseuri =
+let path_of_baseuri ?(no_suffix=false) baseuri =
  let uri = NUri.string_of_uri baseuri in
  let path = String.sub uri 4 (String.length uri - 4) in
  let path = Helm_registry.get "matita.basedir" ^ path in
  let dirname = Filename.dirname path in
   HExtlib.mkdir dirname;
-  path ^ ".ng"
+  if no_suffix then
+   path
+  else
+   path ^ ".ng"
 ;;
 
 let magic = 0;;
 
-let require0 ~baseuri =
- let ch = open_in (path_of_baseuri baseuri) in
+let require_path path =
+ let ch = open_in path in
  let mmagic,dump = Marshal.from_channel ch in
   close_in ch;
   if mmagic <> magic then
@@ -56,9 +63,20 @@ let require0 ~baseuri =
    dump
 ;;
 
+let require0 ~baseuri = require_path (path_of_baseuri baseuri);;
+
+let db_path () = Helm_registry.get "matita.basedir" ^ "/ng_db.ng";;
+
+let init () =
+ try
+  global_aliases := require_path (db_path ())
+ with
+  Sys_error _ -> ()
+;;
+
 let serialize ~baseuri dump =
  let ch = open_out (path_of_baseuri baseuri) in
- Marshal.to_channel ch (magic,dump) [Marshal.Closures];
+ Marshal.to_channel ch (magic,dump) [];
  close_out ch;
  List.iter
   (fun (uri,obj) ->
@@ -66,6 +84,10 @@ let serialize ~baseuri dump =
     Marshal.to_channel ch (magic,obj) [];
     close_out ch
   ) !storage;
+ global_aliases := !local_aliases @ !global_aliases;
+ let ch = open_out (db_path ()) in
+ Marshal.to_channel ch (magic,!global_aliases) [];
+ close_out ch;
  time_travel (new status)
 ;;
 
@@ -124,10 +146,27 @@ module Serializer(S: sig type status end) =
 end
 
 let decompile ~baseuri =
- Unix.unlink (path_of_baseuri baseuri)
- (* WE ARE NOT REMOVING ALL THE OBJECTS YET! *)
+ HExtlib.safe_remove (path_of_baseuri baseuri);
+ let basepath = path_of_baseuri ~no_suffix:true baseuri in
+ try
+  let od = Unix.opendir basepath in
+  let rec aux names =
+   try
+    let name = Unix.readdir od in
+     if name <> "." && name <> ".." then aux (name::names) else aux names
+   with
+    End_of_file -> names in
+  let names = List.map (fun name -> basepath ^ "/" ^ name) (aux []) in
+   Unix.closedir od;
+   List.iter Unix.unlink names;
+   HExtlib.rmdir_descend basepath
+ with
+  Unix.Unix_error _ -> () (* raised by Unix.opendir, we hope :-) *)
 ;;
 
+LibraryClean.set_decompile_cb
+ (fun ~baseuri -> decompile ~baseuri:(NUri.uri_of_string baseuri));;
+
 let fetch_obj uri =
  let obj = require0 ~baseuri:uri in
   refresh_uri_in_obj obj
@@ -136,7 +175,8 @@ let fetch_obj uri =
 let resolve name =
  try
   HExtlib.filter_map
-   (fun (_,name',nref) -> if name'=name then Some nref else None) !aliases
+   (fun (_,name',nref) -> if name'=name then Some nref else None)
+   (!local_aliases @ !global_aliases)
  with
   Not_found -> raise (NCicEnvironment.ObjectNotFound (lazy name))
 ;;
@@ -144,7 +184,8 @@ let resolve name =
 let aliases_of uri =
  try
   HExtlib.filter_map
-   (fun (uri',_,nref) -> if NUri.eq uri' uri then Some nref else None) !aliases
+   (fun (uri',_,nref) ->
+     if NUri.eq uri' uri then Some nref else None) !local_aliases
  with
   Not_found -> raise (NCicEnvironment.ObjectNotFound (lazy (NUri.string_of_uri uri)))
 ;;
@@ -179,8 +220,8 @@ let add_obj status u obj =
               (NReference.Ind (inductive,i,leftno))]
          ) il)
   in
-  aliases := references @ !aliases;
-  status#set_timestamp (!storage,!aliases,!cache)
+  local_aliases := references @ !local_aliases;
+  status#set_timestamp (!storage,!local_aliases,!cache)
 ;;
 
 let get_obj u =