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