8 (* todo: limit number of entries, and erase all (then better do a ltu)
9 * todo: another cache that behave as in lfs1,
10 * every 100 operation do a flush
12 * todo: choose between oassocb and oassoch ?
15 (* !!take care!!: this class has side effect, not a pure oassoc *)
16 (* can not make it pure, cos the assoc have side effect on the cache *)
17 class ['a
,'b
] oassoc_buffer max cached
=
19 inherit ['a
,'b
] oassoc
22 val cache
= ref (new oassocb
[])
23 val dirty
= ref (new osetb
Setb.empty
)
24 val wrapped
= ref cached
26 method private myflush
=
28 wrapped
:= !wrapped#add
(k
, !cache#assoc k
)
30 dirty
:= (new osetb
Setb.empty
);
31 cache
:= (new oassocb
[]);
34 method misc_op_hook2
= o#myflush
39 cache
:= !cache#add
(k
,v
);
40 dirty
:= !dirty#add k
;
42 if !counter
> max
then o#myflush
;
46 o#myflush
; (* bugfix: have to flush !!! *)
58 cache
:= !cache#del
(k
,v
);
59 (* TODO as for delkey, do a try over wrapped *)
60 wrapped
:= !wrapped#del
(k
,v
);
61 dirty
:= !dirty#del k
;
63 method mem e
= raise Todo
64 method null
= raise Todo
69 (* may launch Not_found, but this time, dont catch it *)
70 let v = !wrapped#assoc k
in
72 cache
:= !cache#add
(k
,v);
73 (* otherwise can use too much mem *)
75 if !counter
> max
then o#myflush
;
80 cache
:= !cache#delkey k
;
81 (* sometimes have not yet flushed, so may not be yet in, (could
82 * also flush in place of doing try).
84 * TODO would be better to see if was in cache (in case mean that
85 * perhaps not flushed and do try and in other case just cos del
86 * (without try) cos forcement flushed ou was an error *)
88 try wrapped
:= !wrapped#delkey k
91 dirty
:= !dirty#del k
;
98 class ['a,'b] oassoc_cache cache cached max =
100 inherit ['a,'b] oassoc
108 val data = Hashtbl.create 100
110 method empty = raise Todo
111 method add (k,v) = (Hashtbl.add data k v; o)
112 method iter f = cached#iter f
113 method view = raise Todo
115 method del (k,v) = (cache#del (k,v); cached#del (k,v); o)
116 method mem e = raise Todo
117 method null = raise Todo
119 method assoc k = Hashtbl.find data k
120 method delkey k = (cache#delkey (k,v); cached#del (k,v); o)