]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/getter/http_getter_map.ml
Back compatibility code introduced:
[helm.git] / helm / ocaml / getter / http_getter_map.ml
1 (*
2  * Copyright (C) 2003-2004:
3  *    Stefano Zacchiroli <zack@cs.unibo.it>
4  *    for the HELM Team http://helm.cs.unibo.it/
5  *
6  *  This file is part of HELM, an Hypertextual, Electronic
7  *  Library of Mathematics, developed at the Computer Science
8  *  Department, University of Bologna, Italy.
9  *
10  *  HELM is free software; you can redistribute it and/or
11  *  modify it under the terms of the GNU General Public License
12  *  as published by the Free Software Foundation; either version 2
13  *  of the License, or (at your option) any later version.
14  *
15  *  HELM is distributed in the hope that it will be useful,
16  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  *  GNU General Public License for more details.
19  *
20  *  You should have received a copy of the GNU General Public License
21  *  along with HELM; if not, write to the Free Software
22  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston,
23  *  MA  02111-1307, USA.
24  *
25  *  For details, see the HELM World-Wide-Web page,
26  *  http://helm.cs.unibo.it/
27  *)
28
29 exception Key_already_in of string;;
30 exception Key_not_found of string;;
31
32 class map dbname =
33   let perm = 420 in (* permission 644 in decimal notation *)
34   let open_dbm () = Dbm.opendbm dbname [ Dbm.Dbm_rdwr; Dbm.Dbm_create ] perm in
35   
36   object (self)
37
38     inherit ThreadSafe.threadSafe
39
40     val mutable db = open_dbm ()
41     val index_RE = Pcre.regexp "^theory:/.*/index.theory$"
42
43     (*initializer Gc.finalise Dbm.close db  (* close db on GC *)*)
44
45     (* Backward compatibility code to map
46        theory:/path/t.theory into theory:/path/t/index.theory
47        when cic:/path/t/ exists *)
48     method private normalize_key key =
49      if Pcre.pmatch ~rex:index_RE key then
50       (* we substitute /index.theory with .theory *)
51       String.sub key 0 (String.length key - 13) ^ ".theory"
52      else key
53
54     method add key value =
55       self#doWriter (lazy (
56         try
57           Dbm.add db (self#normalize_key key) value
58         with Dbm.Dbm_error "Entry already exists" -> raise (Key_already_in key)
59       ))
60
61     method replace key value =
62       self#doWriter (lazy (
63         Dbm.replace db (self#normalize_key key) value
64       ))
65
66     method remove key =
67       self#doWriter (lazy (
68         try
69           Dbm.remove db (self#normalize_key key)
70         with Dbm.Dbm_error "dbm_delete" -> raise (Key_not_found key)
71       ))
72
73     method resolve key =
74       self#doReader (lazy (
75         try
76          Dbm.find db (self#normalize_key key)
77         with Not_found -> raise (Key_not_found key)
78       ))
79
80     method iter (f: string -> string -> unit) =
81       self#doReader (lazy (
82         Dbm.iter f db
83       ))
84
85     method sync =
86       self#doWriter (lazy (
87         Dbm.close db;
88         db <- open_dbm ()
89       ))
90
91     method clear =
92       self#doWriter (lazy (
93         Dbm.close db;
94         List.iter
95           (fun ext ->
96             let file = dbname ^ ext in
97             if Sys.file_exists file then Sys.remove file)
98           [".dir"; ".pag"; ".db"];
99         db <- open_dbm ()
100       ))
101
102     method close =
103       self#doWriter (lazy (
104         Dbm.close db
105       ))
106
107   end
108