Release coccinelle-0.1.6
[bpt/coccinelle.git] / commons / ocollection / oassocdbm.ml
1 open Common
2
3 open Oassoc
4
5 (* !!take care!!: this class does side effect, not a pure oassoc.
6 *
7 * The fv/unv are here to give the opportunity to translate the value
8 * from the dbm, before marshalling. This is useful for instance if you
9 * want to store objects such as oset. Indeed we cant marshall
10 * conveniently functions/closures, and so objects (you can but you can
11 * load them back only from the same binary, which limits the
12 * practicallibity of the approach). You have to translate them to
13 * traditionnal data structures before marshalling them, and you have
14 * to rebuild the object from the traditionnal data structure when you
15 * get them from the dbm. Hence fv/unv. You can do the same for the key
16 * with fkey/unkey, but as key are usually simple data structures,
17 * there is less need for them, so I have commented them. *)
18 class ['a,'b] oassocdbm xs db (*fkey unkey*) fv unv =
19 object(o)
20 inherit ['a,'b] oassoc
21
22 val db = db
23
24 method empty = raise Todo
25 method add (k,v) =
26 (* pr2 (fkey k); *)
27 (* pr2 (debugv v); *)
28
29 (* try Db.del data None
30 (Marshal.to_string k []) []
31 with Not_found -> ());
32 *)
33 let k' = Common.marshal__to_string k [] in
34 let v' = (Common.marshal__to_string (fv v) [(*Common.marshal__Closures*)]) in
35 (try Dbm.add db k' v'
36 with _ -> Dbm.replace db k' v'
37 );
38 o
39
40 method iter f =
41 db +> Dbm.iter (fun key data ->
42 let k' = (* unkey *) Common.marshal__from_string key 0 in
43 let v' = unv (Common.marshal__from_string data 0) in
44 f (k', v')
45 )
46
47 method view = raise Todo
48
49 method del (k,v) = raise Todo
50 method mem e = raise Todo
51 method null = raise Todo
52
53 method assoc k =
54 let k' = Common.marshal__to_string k [] in
55 unv (Common.marshal__from_string (Dbm.find db k') 0)
56
57 method delkey k =
58 let k' = Common.marshal__to_string k [] in
59 try
60 Dbm.remove db k';
61 o
62 with Dbm.Dbm_error "dbm_delete" ->
63 raise Not_found
64
65 method keys =
66 let res = ref [] in
67 db +> Dbm.iter (fun key data ->
68 let k' = (* unkey *) Common.marshal__from_string key 0 in
69 (*
70 let v' = unv (Common.marshal__from_string data 0) in
71 f (k', v')
72 *)
73 Common.push2 k' res;
74 );
75 !res
76
77 end
78
79
80 let create_dbm metapath dbname =
81 let x_db = Dbm.opendbm (metapath^dbname) [Dbm.Dbm_create;Dbm.Dbm_rdwr] 0o777
82 in
83 let assoc = new oassocdbm [] x_db id id in
84 x_db, assoc
85