]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/getter/http_getter_map.ml
bumped changelog line to match upload date
[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 open Http_getter_types
30
31 class map dbname =
32   let perm = 420 in (* permission 644 in decimal notation *)
33   let open_dbm () = Dbm.opendbm dbname [ Dbm.Dbm_rdwr; Dbm.Dbm_create ] perm in
34   
35   object (self)
36
37     inherit ThreadSafe.threadSafe
38
39     val mutable db = open_dbm ()
40     val index_RE = Pcre.regexp "^theory:/.*/index.theory$"
41
42     (*initializer Gc.finalise Dbm.close db  (* close db on GC *)*)
43
44     (* Backward compatibility code to map
45        theory:/path/t.theory into theory:/path/t/index.theory
46        when cic:/path/t/ exists *)
47     method private normalize_key key =
48      if Pcre.pmatch ~rex:index_RE key &&
49         (try ignore (Dbm.find db key); false with Not_found -> true)
50      then
51       (* we substitute /index.theory with .theory *)
52       String.sub key 0 (String.length key - 13) ^ ".theory"
53      else key
54
55     method add key value =
56       self#doWriter (lazy (
57         try
58           Dbm.add db key value
59         with Dbm.Dbm_error "Entry already exists" -> raise (Key_already_in key)
60       ))
61
62     method replace key value =
63       self#doWriter (lazy (
64         Dbm.replace db key value
65       ))
66
67     method remove key =
68       self#doWriter (lazy (
69         try
70           Dbm.remove db key
71         with Dbm.Dbm_error "dbm_delete" -> raise (Key_not_found key)
72       ))
73
74     method resolve key =
75       self#doReader (lazy (
76         try
77          Dbm.find db (self#normalize_key key)
78         with Not_found -> raise (Key_not_found key)
79       ))
80
81     method iter (f: string -> string -> unit) =
82       self#doReader (lazy (
83         Dbm.iter f db
84       ))
85
86     method sync =
87       self#doWriter (lazy (
88         Dbm.close db;
89         db <- open_dbm ()
90       ))
91
92     method clear =
93       self#doWriter (lazy (
94         Dbm.close db;
95         List.iter
96           (fun ext ->
97             let file = dbname ^ ext in
98             if Sys.file_exists file then Sys.remove file)
99           [".dir"; ".pag"; ".db"];
100         db <- open_dbm ()
101       ))
102
103     method close =
104       self#doWriter (lazy (
105         Dbm.close db
106       ))
107
108   end
109