Release coccinelle-0.1.7
[bpt/coccinelle.git] / commons / ocollection / oassocbdb.ml
CommitLineData
34e49164
C
1open Common
2
3open Bdb
4
5open Oassoc
6
7(* !!take care!!: this class does side effect, not a pure oassoc
8 *
9 * The fv/unv are to give the opportunity to translate the value from
10 * the dbm, before marshalling. Cf oassocdbm.mli for more about this.
11 *
12 * Quite similar to oassocdbm.ml. New: Take transact argument.
0708f913
C
13 *
14 * How to optimize when using this oassoc is slow ?
15 * - use oassoc_buffer as a front-end of this oassoc
16 * - reduce the size of the key or value
34e49164
C
17 *)
18class ['a,'b] oassoc_btree db namedb transact (*fkey unkey*) fv unv =
19let namedb = if namedb = "" then "" else "(" ^ namedb ^ ")" in
20object(o)
21 inherit ['a,'b] oassoc
22
23 val data = db
24
25 method empty =
26 raise Todo
27
91eba41f 28 method private addbis (k,v) =
34e49164
C
29 (* pr2 (fkey k); *)
30 (* pr2 (debugv v); *)
31
32 (* try Db.del data None
33 (Marshal.to_string k []) []
34 with Not_found -> ());
35 *)
0708f913 36 let k' = Common.marshal__to_string k [] in
91eba41f
C
37 let v' =
38 try
0708f913 39 Common.marshal__to_string (fv v) [(*Marshal.Closures*)]
91eba41f
C
40 with Out_of_memory ->
41 pr2 ("PBBBBBBB Out_of_memory in: " ^ namedb);
42 raise Out_of_memory
43
44 in (* still clos? *)
34e49164
C
45 Db.put data (transact()) k' v' [];
46 (* minsky wrapper ? Db.put data ~txn:(transact()) ~key:k' ~data:v' *)
47 o
48 method add x =
91eba41f 49 Common.profile_code ("Btree.add" ^ namedb) (fun () -> o#addbis x)
34e49164
C
50
51 (* bugfix: if not tail call (because of a try for instance),
52 * then strange behaviour in native mode
53 *)
54 method private iter2 f =
55 let dbc = Cursor.db_cursor db (transact()) [] in
56 (* minsky wrapper? Cursor.create ~writecursor:false ~txn:(transact()) db *)
57 let rec aux dbc =
58 if
59 (try
60 let a = Cursor.dbc_get dbc [Cursor.DB_NEXT] in
61 (* minsky ? Cursor.get dbc Cursor.NEXT [] *)
0708f913
C
62 let key = (* unkey *) Common.marshal__from_string (fst a) 0 in
63 let valu = unv (Common.marshal__from_string (snd a) 0) in
34e49164
C
64 f (key, valu);
65 true
66 with Failure "ending" -> false
67 )
68 then aux dbc
69 else ()
70
71 in
72 aux dbc;
73 Cursor.dbc_close dbc (* minsky Cursor.close dbc *)
74
75 method iter x =
76 Common.profile_code ("Btree.iter" ^ namedb) (fun () -> o#iter2 x)
77
78 method view =
79 raise Todo
80
81
82
83 method private length2 =
84 let dbc = Cursor.db_cursor db (transact()) [] in
85
86 let count = ref 0 in
87 let rec aux dbc =
88 if (
89 try
90 let _a = Cursor.dbc_get dbc [Cursor.DB_NEXT] in
91 incr count;
92 true
93 with Failure "ending" -> false
94 )
95 then aux dbc
96 else ()
97
98 in
99 aux dbc;
100 Cursor.dbc_close dbc;
101 !count
102
103 method length =
104 Common.profile_code ("Btree.length" ^ namedb) (fun () -> o#length2)
105
106
107 method del (k,v) = raise Todo
108 method mem e = raise Todo
109 method null = raise Todo
110
111 method private assoc2 k =
112 try
0708f913 113 let k' = Common.marshal__to_string k [] in
34e49164
C
114 let vget = Db.get data (transact()) k' [] in
115 (* minsky ? Db.get data ~txn:(transact() *)
0708f913 116 unv (Common.marshal__from_string vget 0)
34e49164
C
117 with Not_found ->
118 log3 ("pb assoc with k = " ^ (Dumper.dump k));
119 raise Not_found
120 method assoc x =
121 Common.profile_code ("Btree.assoc" ^ namedb) (fun () -> o#assoc2 x)
122
123 method private delkey2 k =
0708f913 124 let k' = Common.marshal__to_string k [] in
34e49164
C
125 Db.del data (transact()) k' [];
126 o
127 method delkey x =
128 Common.profile_code ("Btree.delkey" ^ namedb) (fun () -> o#delkey2 x)
129
91eba41f
C
130
131 method keys =
132 let res = ref [] in
133 let dbc = Cursor.db_cursor db (transact()) [] in
134 let rec aux dbc =
135 if
136 (try
137 let a = Cursor.dbc_get dbc [Cursor.DB_NEXT] in
138 (* minsky ? Cursor.get dbc Cursor.NEXT [] *)
0708f913 139 let key = (* unkey *) Common.marshal__from_string (fst a) 0 in
91eba41f 140 (*
0708f913 141 let valu = unv (Common.marshal__from_string (snd a) 0) in
91eba41f
C
142 f (key, valu);
143 *)
144 Common.push2 key res;
145 true
146 with Failure "ending" -> false
147 )
148 then aux dbc
149 else ()
150
151 in
152 aux dbc;
153 Cursor.dbc_close dbc (* minsky Cursor.close dbc *);
154 !res
155
156
157 method clear =
158 let dbc = Cursor.db_cursor db (transact()) [] in
159 let rec aux dbc =
160 if
161 (try
162 let a = Cursor.dbc_get dbc [Cursor.DB_NEXT] in
163 Db.del data (transact()) (fst a) [];
164 true
165 with Failure "ending" -> false
166 )
167 then aux dbc
168 else ()
169
170 in
171 aux dbc;
172 Cursor.dbc_close dbc (* minsky Cursor.close dbc *);
173 ()
174
34e49164 175end
91eba41f
C
176
177
178let create_bdb metapath dbname env transact (fv, unv) size_buffer_oassoc_buffer =
179 let db = Bdb.Db.create env [] in
180 Bdb.Db.db_open db (transact())
181 (spf "%s/%s.db4" metapath dbname)
182 (spf "/%s.db4" dbname)
183 Bdb.Db.DB_BTREE [Bdb.Db.DB_CREATE] 0;
184 db,
185 new Oassoc_buffer.oassoc_buffer size_buffer_oassoc_buffer
186 (new oassoc_btree db dbname transact fv unv)
187