1 #ifndef ACE_CACHING_UTILITY_T_CPP
2 #define ACE_CACHING_UTILITY_T_CPP
4 #include "ace/Caching_Utility_T.h"
6 #if !defined (ACE_LACKS_PRAGMA_ONCE)
8 #endif /* ACE_LACKS_PRAGMA_ONCE */
11 #include "ace/Min_Max.h"
12 #include "ace/OS_Memory.h"
13 #include "ace/Recyclable.h"
15 //////////////////////////////////////////////////////////////////////////////
17 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
19 template <class KEY
, class VALUE
, class CONTAINER
, class ITERATOR
, class ATTRIBUTES
>
20 ACE_Pair_Caching_Utility
<KEY
, VALUE
, CONTAINER
, ITERATOR
, ATTRIBUTES
>::ACE_Pair_Caching_Utility (ACE_Cleanup_Strategy
<KEY
, VALUE
, CONTAINER
> *cleanup_strategy
,
21 bool delete_cleanup_strategy
)
22 : cleanup_strategy_ (cleanup_strategy
),
23 delete_cleanup_strategy_ (delete_cleanup_strategy
)
25 if (cleanup_strategy
== 0)
27 ACE_NEW (this->cleanup_strategy_
,
29 this->delete_cleanup_strategy_
= true;
33 template <class KEY
, class VALUE
, class CONTAINER
, class ITERATOR
, class ATTRIBUTES
>
34 ACE_Pair_Caching_Utility
<KEY
, VALUE
, CONTAINER
, ITERATOR
, ATTRIBUTES
>::~ACE_Pair_Caching_Utility ()
36 if (this->delete_cleanup_strategy_
)
37 delete this->cleanup_strategy_
;
40 template <class KEY
, class VALUE
, class CONTAINER
, class ITERATOR
, class ATTRIBUTES
> int
41 ACE_Pair_Caching_Utility
<KEY
, VALUE
, CONTAINER
, ITERATOR
, ATTRIBUTES
>::clear_cache (CONTAINER
&container
,
44 // Check that the purge_percent is non-zero.
45 if (ACE::is_equal (purge_percent
, 0.0))
48 // Get the number of entries in the container.
49 size_t current_map_size
= container
.current_size ();
51 // Also whether the number of entries in the cache!
52 // Oops! then there is no way out but exiting. So return an error.
53 if (current_map_size
== 0)
56 // Calculate the no of entries to remove from the cache depending
57 // upon the <purge_percent>.
58 size_t const entries_to_remove
59 = ACE_MAX (static_cast<size_t> (1),
60 static_cast<size_t> (static_cast<double> (purge_percent
)
61 / 100 * current_map_size
));
62 KEY
*key_to_remove
= 0;
63 VALUE
*value_to_remove
= 0;
65 for (size_t i
= 0; i
< entries_to_remove
; ++i
)
67 this->minimum (container
,
71 // Simply verifying that the key is non-zero.
72 // This is important for strategies where the minimum
73 // entry cant be found due to constraints on the type of entry
75 if (key_to_remove
== 0)
78 if (this->cleanup_strategy_
->cleanup (container
,
80 value_to_remove
) == -1)
87 template <class KEY
, class VALUE
, class CONTAINER
, class ITERATOR
, class ATTRIBUTES
> void
88 ACE_Pair_Caching_Utility
<KEY
, VALUE
, CONTAINER
, ITERATOR
, ATTRIBUTES
>::minimum (CONTAINER
&container
,
90 VALUE
*&value_to_remove
)
93 ITERATOR iter
= container
.begin ();
94 ITERATOR end
= container
.end ();
95 ATTRIBUTES min
= (*iter
).int_id_
.second
;
96 key_to_remove
= &(*iter
).ext_id_
;
97 value_to_remove
= &(*iter
).int_id_
;
99 // The iterator moves thru the container searching for the entry
100 // with the lowest ATTRIBUTES.
105 if (min
> (*iter
).int_id_
.second
)
107 // Ah! an item with lower ATTTRIBUTES...
108 min
= (*iter
).int_id_
.second
;
109 key_to_remove
= &(*iter
).ext_id_
;
110 value_to_remove
= &(*iter
).int_id_
;
115 ////////////////////////////////////////////////////////////////////////////////////////////////////////
117 template <class KEY
, class VALUE
, class CONTAINER
, class ITERATOR
, class ATTRIBUTES
>
118 ACE_Recyclable_Handler_Caching_Utility
<KEY
, VALUE
, CONTAINER
, ITERATOR
, ATTRIBUTES
>::ACE_Recyclable_Handler_Caching_Utility (ACE_Cleanup_Strategy
<KEY
, VALUE
, CONTAINER
> *cleanup_strategy
,
119 bool delete_cleanup_strategy
)
120 : cleanup_strategy_ (cleanup_strategy
),
121 delete_cleanup_strategy_ (delete_cleanup_strategy
)
123 if (cleanup_strategy
== 0)
125 ACE_NEW (this->cleanup_strategy_
,
127 this->delete_cleanup_strategy_
= true;
131 template <class KEY
, class VALUE
, class CONTAINER
, class ITERATOR
, class ATTRIBUTES
>
132 ACE_Recyclable_Handler_Caching_Utility
<KEY
, VALUE
, CONTAINER
, ITERATOR
, ATTRIBUTES
>::~ACE_Recyclable_Handler_Caching_Utility ()
134 if (this->delete_cleanup_strategy_
)
135 delete this->cleanup_strategy_
;
138 template <class KEY
, class VALUE
, class CONTAINER
, class ITERATOR
, class ATTRIBUTES
> int
139 ACE_Recyclable_Handler_Caching_Utility
<KEY
, VALUE
, CONTAINER
, ITERATOR
, ATTRIBUTES
>::clear_cache (CONTAINER
&container
,
140 double purge_percent
)
142 // Check that the purge_percent is non-zero.
143 if (ACE::is_equal (purge_percent
, 0.0))
146 // Get the number of entries in the container.
147 size_t current_map_size
= container
.current_size ();
149 // Also whether the number of entries in the cache is just one!
150 // Oops! then there is no way out but exiting. So return an error.
151 // if (current_map_size <= 1)
152 if (current_map_size
== 0)
155 // Calculate the no of entries to remove from the cache depending
156 // upon the <purge_percent>.
157 size_t const entries_to_remove
158 = ACE_MAX (static_cast<size_t> (1),
159 static_cast<size_t> (static_cast<double> (purge_percent
)
160 / 100 * current_map_size
));
162 KEY
*key_to_remove
= 0;
163 VALUE
*value_to_remove
= 0;
165 for (size_t i
= 0; i
< entries_to_remove
; ++i
)
167 this->minimum (container
,
171 // Simply verifying that the key is non-zero.
172 // This is important for strategies where the minimum
173 // entry cant be found due to constraints on the type of entry
175 if (key_to_remove
== 0)
178 if (this->cleanup_strategy_
->cleanup (container
,
180 value_to_remove
) == -1)
187 template <class KEY
, class VALUE
, class CONTAINER
, class ITERATOR
, class ATTRIBUTES
> void
188 ACE_Recyclable_Handler_Caching_Utility
<KEY
, VALUE
, CONTAINER
, ITERATOR
, ATTRIBUTES
>::minimum (CONTAINER
&container
,
190 VALUE
*&value_to_remove
)
193 ITERATOR end
= container
.end ();
194 ITERATOR iter
= container
.begin ();
195 ATTRIBUTES min
= (*iter
).int_id_
.second
;
198 // Found the minimum entry to be purged?
201 // The iterator moves thru the container searching for the entry
202 // with the lowest ATTRIBUTES.
207 // If the <min> entry isnt IDLE_AND_PURGABLE continue until you reach
208 // the first entry which can be purged. This is the minimum with
209 // which you will compare the rest of the purgable entries.
210 if ((*iter
).ext_id_
.recycle_state () == ACE_RECYCLABLE_IDLE_AND_PURGABLE
||
211 (*iter
).ext_id_
.recycle_state () == ACE_RECYCLABLE_PURGABLE_BUT_NOT_IDLE
)
215 min
= (*iter
).int_id_
.second
;
216 key_to_remove
= &(*iter
).ext_id_
;
217 value_to_remove
= &(*iter
).int_id_
;
222 // Ah! an entry with lower ATTTRIBUTES...
223 if (min
> (*iter
).int_id_
.second
)
225 min
= (*iter
).int_id_
.second
;
226 key_to_remove
= &(*iter
).ext_id_
;
227 value_to_remove
= &(*iter
).int_id_
;
234 ////////////////////////////////////////////////////////////////////////////////
236 template <class KEY
, class VALUE
, class CONTAINER
, class ITERATOR
, class ATTRIBUTES
>
237 ACE_Refcounted_Recyclable_Handler_Caching_Utility
<KEY
, VALUE
, CONTAINER
, ITERATOR
, ATTRIBUTES
>::ACE_Refcounted_Recyclable_Handler_Caching_Utility (ACE_Cleanup_Strategy
<KEY
, VALUE
, CONTAINER
> *cleanup_strategy
,
238 bool delete_cleanup_strategy
)
239 : cleanup_strategy_ (cleanup_strategy
),
240 delete_cleanup_strategy_ (delete_cleanup_strategy
),
241 marked_as_closed_entries_ (0)
243 if (cleanup_strategy
== 0)
245 ACE_NEW (this->cleanup_strategy_
,
247 this->delete_cleanup_strategy_
= true;
251 template <class KEY
, class VALUE
, class CONTAINER
, class ITERATOR
, class ATTRIBUTES
>
252 ACE_Refcounted_Recyclable_Handler_Caching_Utility
<KEY
, VALUE
, CONTAINER
, ITERATOR
, ATTRIBUTES
>::~ACE_Refcounted_Recyclable_Handler_Caching_Utility ()
254 if (this->delete_cleanup_strategy_
)
255 delete this->cleanup_strategy_
;
258 template <class KEY
, class VALUE
, class CONTAINER
, class ITERATOR
, class ATTRIBUTES
> int
259 ACE_Refcounted_Recyclable_Handler_Caching_Utility
<KEY
, VALUE
, CONTAINER
, ITERATOR
, ATTRIBUTES
>::clear_cache (CONTAINER
&container
,
260 double purge_percent
)
262 // Check that the purge_percent is non-zero.
263 if (ACE::is_equal (purge_percent
, 0.0))
266 // Get the number of entries in the container which can be considered for purging.
267 size_t const available_entries
=
268 container
.current_size () - this->marked_as_closed_entries_
;
270 // Also whether the number of entries in the cache zero.
271 // Oops! then there is no way out but exiting.
272 if (available_entries
<= 0)
275 // Calculate the no of entries to remove from the cache depending
276 // upon the <purge_percent>.
277 size_t entries_to_remove
278 = ACE_MAX (static_cast<size_t> (1),
279 static_cast<size_t> (static_cast<double> (purge_percent
)
280 / 100 * available_entries
));
282 if (entries_to_remove
>= available_entries
|| entries_to_remove
== 0)
283 entries_to_remove
= available_entries
- 1;
285 KEY
*key_to_remove
= 0;
286 VALUE
*value_to_remove
= 0;
288 for (size_t i
= 0; i
< entries_to_remove
; ++i
)
290 this->minimum (container
,
294 // Simply verifying that the key is non-zero.
295 // This is important for strategies where the minimum
296 // entry cant be found due to constraints on the type of entry
298 if (key_to_remove
== 0)
301 if (this->cleanup_strategy_
->cleanup (container
,
303 value_to_remove
) == -1)
306 ++this->marked_as_closed_entries_
;
312 template <class KEY
, class VALUE
, class CONTAINER
, class ITERATOR
, class ATTRIBUTES
> void
313 ACE_Refcounted_Recyclable_Handler_Caching_Utility
<KEY
, VALUE
, CONTAINER
, ITERATOR
, ATTRIBUTES
>::minimum (CONTAINER
&container
,
315 VALUE
*&value_to_remove
)
318 ITERATOR end
= container
.end ();
319 ITERATOR iter
= container
.begin ();
320 ATTRIBUTES min
= (*iter
).int_id_
.second ();
323 // Found the minimum entry to be purged?
326 // The iterator moves thru the container searching for the entry
327 // with the lowest ATTRIBUTES.
332 // If the <min> entry isnt IDLE_AND_PURGABLE continue until you reach
333 // the first entry which can be purged. This is the minimum with
334 // which you will compare the rest of the purgable entries.
335 if ((*iter
).ext_id_
.recycle_state () == ACE_RECYCLABLE_IDLE_AND_PURGABLE
||
336 (*iter
).ext_id_
.recycle_state () == ACE_RECYCLABLE_PURGABLE_BUT_NOT_IDLE
)
340 min
= (*iter
).int_id_
.second ();
341 key_to_remove
= &(*iter
).ext_id_
;
342 value_to_remove
= &(*iter
).int_id_
;
347 // Ah! an entry with lower ATTTRIBUTES...
348 if (min
> (*iter
).int_id_
.second ())
350 min
= (*iter
).int_id_
.second ();
351 key_to_remove
= &(*iter
).ext_id_
;
352 value_to_remove
= &(*iter
).int_id_
;
359 ////////////////////////////////////////////////////////////////////////////////
361 template <class KEY
, class VALUE
, class CONTAINER
, class ITERATOR
, class ATTRIBUTES
>
362 ACE_Handler_Caching_Utility
<KEY
, VALUE
, CONTAINER
, ITERATOR
, ATTRIBUTES
>::ACE_Handler_Caching_Utility (ACE_Cleanup_Strategy
<KEY
, VALUE
, CONTAINER
> *cleanup_strategy
,
363 bool delete_cleanup_strategy
)
364 : cleanup_strategy_ (cleanup_strategy
),
365 delete_cleanup_strategy_ (delete_cleanup_strategy
)
367 if (cleanup_strategy
== 0)
369 ACE_NEW (this->cleanup_strategy_
,
371 this->delete_cleanup_strategy_
= true;
375 template <class KEY
, class VALUE
, class CONTAINER
, class ITERATOR
, class ATTRIBUTES
>
376 ACE_Handler_Caching_Utility
<KEY
, VALUE
, CONTAINER
, ITERATOR
, ATTRIBUTES
>::~ACE_Handler_Caching_Utility ()
378 if (this->delete_cleanup_strategy_
)
379 delete this->cleanup_strategy_
;
382 template <class KEY
, class VALUE
, class CONTAINER
, class ITERATOR
, class ATTRIBUTES
> int
383 ACE_Handler_Caching_Utility
<KEY
, VALUE
, CONTAINER
, ITERATOR
, ATTRIBUTES
>::clear_cache (CONTAINER
&container
,
384 double purge_percent
)
386 // Check that the purge_percent is non-zero.
387 if (ACE::is_equal (purge_percent
, 0.0))
390 // Get the number of entries in the container.
391 size_t current_map_size
= container
.current_size ();
393 // Also whether the number of entries in the cache is just one!
394 // Oops! then there is no way out but exiting. So return an error.
395 if (current_map_size
== 0)
398 // Calculate the no of entries to remove from the cache depending
399 // upon the <purge_percent>.
400 size_t entries_to_remove
401 = ACE_MAX (static_cast<size_t> (1),
402 static_cast<size_t> (static_cast<double> (purge_percent
)
403 / 100 * current_map_size
));
405 KEY
*key_to_remove
= 0;
406 VALUE
*value_to_remove
= 0;
408 for (size_t i
= 0; i
< entries_to_remove
; ++i
)
410 this->minimum (container
,
414 if (this->cleanup_strategy_
->cleanup (container
,
416 value_to_remove
) == -1)
423 template <class KEY
, class VALUE
, class CONTAINER
, class ITERATOR
, class ATTRIBUTES
> void
424 ACE_Handler_Caching_Utility
<KEY
, VALUE
, CONTAINER
, ITERATOR
, ATTRIBUTES
>::minimum (CONTAINER
&container
,
426 VALUE
*&value_to_remove
)
429 ITERATOR iter
= container
.begin ();
430 ITERATOR end
= container
.end ();
431 ATTRIBUTES min
= (*iter
).int_id_
->caching_attributes ();
432 key_to_remove
= &(*iter
).ext_id_
;
433 value_to_remove
= &(*iter
).int_id_
;
435 // The iterator moves thru the container searching for the entry
436 // with the lowest ATTRIBUTES.
441 if (min
> (*iter
).int_id_
->caching_attributes () &&
442 (*iter
).int_id_
->active () != 1)
444 // Ah! an item with lower ATTTRIBUTES...
445 min
= (*iter
).int_id_
->caching_attributes ();
446 key_to_remove
= &(*iter
).ext_id_
;
447 value_to_remove
= &(*iter
).int_id_
;
452 ////////////////////////////////////////////////////////////////////////////////////////////////////////
454 template <class KEY
, class VALUE
, class CONTAINER
, class ITERATOR
, class ATTRIBUTES
>
455 ACE_Null_Caching_Utility
<KEY
, VALUE
, CONTAINER
, ITERATOR
, ATTRIBUTES
>::ACE_Null_Caching_Utility (ACE_Cleanup_Strategy
<KEY
, VALUE
, CONTAINER
> *cleanup_strategy
,
456 bool delete_cleanup_strategy
)
457 : cleanup_strategy_ (cleanup_strategy
),
458 delete_cleanup_strategy_ (delete_cleanup_strategy
)
460 if (cleanup_strategy
== 0)
462 ACE_NEW (this->cleanup_strategy_
,
464 this->delete_cleanup_strategy_
= true;
468 template <class KEY
, class VALUE
, class CONTAINER
, class ITERATOR
, class ATTRIBUTES
>
469 ACE_Null_Caching_Utility
<KEY
, VALUE
, CONTAINER
, ITERATOR
, ATTRIBUTES
>::~ACE_Null_Caching_Utility ()
471 if (this->delete_cleanup_strategy_
)
472 delete this->cleanup_strategy_
;
475 template <class KEY
, class VALUE
, class CONTAINER
, class ITERATOR
, class ATTRIBUTES
> int
476 ACE_Null_Caching_Utility
<KEY
, VALUE
, CONTAINER
, ITERATOR
, ATTRIBUTES
>::clear_cache (CONTAINER
&container
,
477 double purge_percent
)
479 ACE_UNUSED_ARG (container
);
480 ACE_UNUSED_ARG (purge_percent
);
485 template <class KEY
, class VALUE
, class CONTAINER
, class ITERATOR
, class ATTRIBUTES
> void
486 ACE_Null_Caching_Utility
<KEY
, VALUE
, CONTAINER
, ITERATOR
, ATTRIBUTES
>::minimum (CONTAINER
&container
,
488 VALUE
*&value_to_remove
)
490 ACE_UNUSED_ARG (container
);
491 ACE_UNUSED_ARG (key_to_remove
);
492 ACE_UNUSED_ARG (value_to_remove
);
495 ACE_END_VERSIONED_NAMESPACE_DECL
497 #endif /* ACE_CACHING_UTILITY_T_CPP */