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 (void)
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)
88 template <class KEY
, class VALUE
, class CONTAINER
, class ITERATOR
, class ATTRIBUTES
> void
89 ACE_Pair_Caching_Utility
<KEY
, VALUE
, CONTAINER
, ITERATOR
, ATTRIBUTES
>::minimum (CONTAINER
&container
,
91 VALUE
*&value_to_remove
)
94 ITERATOR iter
= container
.begin ();
95 ITERATOR end
= container
.end ();
96 ATTRIBUTES min
= (*iter
).int_id_
.second
;
97 key_to_remove
= &(*iter
).ext_id_
;
98 value_to_remove
= &(*iter
).int_id_
;
100 // The iterator moves thru the container searching for the entry
101 // with the lowest ATTRIBUTES.
106 if (min
> (*iter
).int_id_
.second
)
108 // Ah! an item with lower ATTTRIBUTES...
109 min
= (*iter
).int_id_
.second
;
110 key_to_remove
= &(*iter
).ext_id_
;
111 value_to_remove
= &(*iter
).int_id_
;
116 ////////////////////////////////////////////////////////////////////////////////////////////////////////
118 template <class KEY
, class VALUE
, class CONTAINER
, class ITERATOR
, class ATTRIBUTES
>
119 ACE_Recyclable_Handler_Caching_Utility
<KEY
, VALUE
, CONTAINER
, ITERATOR
, ATTRIBUTES
>::ACE_Recyclable_Handler_Caching_Utility (ACE_Cleanup_Strategy
<KEY
, VALUE
, CONTAINER
> *cleanup_strategy
,
120 bool delete_cleanup_strategy
)
121 : cleanup_strategy_ (cleanup_strategy
),
122 delete_cleanup_strategy_ (delete_cleanup_strategy
)
124 if (cleanup_strategy
== 0)
126 ACE_NEW (this->cleanup_strategy_
,
128 this->delete_cleanup_strategy_
= true;
132 template <class KEY
, class VALUE
, class CONTAINER
, class ITERATOR
, class ATTRIBUTES
>
133 ACE_Recyclable_Handler_Caching_Utility
<KEY
, VALUE
, CONTAINER
, ITERATOR
, ATTRIBUTES
>::~ACE_Recyclable_Handler_Caching_Utility (void)
135 if (this->delete_cleanup_strategy_
)
136 delete this->cleanup_strategy_
;
139 template <class KEY
, class VALUE
, class CONTAINER
, class ITERATOR
, class ATTRIBUTES
> int
140 ACE_Recyclable_Handler_Caching_Utility
<KEY
, VALUE
, CONTAINER
, ITERATOR
, ATTRIBUTES
>::clear_cache (CONTAINER
&container
,
141 double purge_percent
)
143 // Check that the purge_percent is non-zero.
144 if (ACE::is_equal (purge_percent
, 0.0))
147 // Get the number of entries in the container.
148 size_t current_map_size
= container
.current_size ();
150 // Also whether the number of entries in the cache is just one!
151 // Oops! then there is no way out but exiting. So return an error.
152 // if (current_map_size <= 1)
153 if (current_map_size
== 0)
156 // Calculate the no of entries to remove from the cache depending
157 // upon the <purge_percent>.
158 size_t const entries_to_remove
159 = ACE_MAX (static_cast<size_t> (1),
160 static_cast<size_t> (static_cast<double> (purge_percent
)
161 / 100 * current_map_size
));
163 KEY
*key_to_remove
= 0;
164 VALUE
*value_to_remove
= 0;
166 for (size_t i
= 0; i
< entries_to_remove
; ++i
)
168 this->minimum (container
,
172 // Simply verifying that the key is non-zero.
173 // This is important for strategies where the minimum
174 // entry cant be found due to constraints on the type of entry
176 if (key_to_remove
== 0)
179 if (this->cleanup_strategy_
->cleanup (container
,
181 value_to_remove
) == -1)
188 template <class KEY
, class VALUE
, class CONTAINER
, class ITERATOR
, class ATTRIBUTES
> void
189 ACE_Recyclable_Handler_Caching_Utility
<KEY
, VALUE
, CONTAINER
, ITERATOR
, ATTRIBUTES
>::minimum (CONTAINER
&container
,
191 VALUE
*&value_to_remove
)
194 ITERATOR end
= container
.end ();
195 ITERATOR iter
= container
.begin ();
196 ATTRIBUTES min
= (*iter
).int_id_
.second
;
199 // Found the minimum entry to be purged?
202 // The iterator moves thru the container searching for the entry
203 // with the lowest ATTRIBUTES.
208 // If the <min> entry isnt IDLE_AND_PURGABLE continue until you reach
209 // the first entry which can be purged. This is the minimum with
210 // which you will compare the rest of the purgable entries.
211 if ((*iter
).ext_id_
.recycle_state () == ACE_RECYCLABLE_IDLE_AND_PURGABLE
||
212 (*iter
).ext_id_
.recycle_state () == ACE_RECYCLABLE_PURGABLE_BUT_NOT_IDLE
)
216 min
= (*iter
).int_id_
.second
;
217 key_to_remove
= &(*iter
).ext_id_
;
218 value_to_remove
= &(*iter
).int_id_
;
223 // Ah! an entry with lower ATTTRIBUTES...
224 if (min
> (*iter
).int_id_
.second
)
226 min
= (*iter
).int_id_
.second
;
227 key_to_remove
= &(*iter
).ext_id_
;
228 value_to_remove
= &(*iter
).int_id_
;
235 ////////////////////////////////////////////////////////////////////////////////
237 template <class KEY
, class VALUE
, class CONTAINER
, class ITERATOR
, class ATTRIBUTES
>
238 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
,
239 bool delete_cleanup_strategy
)
240 : cleanup_strategy_ (cleanup_strategy
),
241 delete_cleanup_strategy_ (delete_cleanup_strategy
),
242 marked_as_closed_entries_ (0)
244 if (cleanup_strategy
== 0)
246 ACE_NEW (this->cleanup_strategy_
,
248 this->delete_cleanup_strategy_
= true;
252 template <class KEY
, class VALUE
, class CONTAINER
, class ITERATOR
, class ATTRIBUTES
>
253 ACE_Refcounted_Recyclable_Handler_Caching_Utility
<KEY
, VALUE
, CONTAINER
, ITERATOR
, ATTRIBUTES
>::~ACE_Refcounted_Recyclable_Handler_Caching_Utility (void)
255 if (this->delete_cleanup_strategy_
)
256 delete this->cleanup_strategy_
;
259 template <class KEY
, class VALUE
, class CONTAINER
, class ITERATOR
, class ATTRIBUTES
> int
260 ACE_Refcounted_Recyclable_Handler_Caching_Utility
<KEY
, VALUE
, CONTAINER
, ITERATOR
, ATTRIBUTES
>::clear_cache (CONTAINER
&container
,
261 double purge_percent
)
263 // Check that the purge_percent is non-zero.
264 if (ACE::is_equal (purge_percent
, 0.0))
267 // Get the number of entries in the container which can be considered for purging.
268 size_t const available_entries
=
269 container
.current_size () - this->marked_as_closed_entries_
;
271 // Also whether the number of entries in the cache zero.
272 // Oops! then there is no way out but exiting.
273 if (available_entries
<= 0)
276 // Calculate the no of entries to remove from the cache depending
277 // upon the <purge_percent>.
278 size_t entries_to_remove
279 = ACE_MAX (static_cast<size_t> (1),
280 static_cast<size_t> (static_cast<double> (purge_percent
)
281 / 100 * available_entries
));
283 if (entries_to_remove
>= available_entries
|| entries_to_remove
== 0)
284 entries_to_remove
= available_entries
- 1;
286 KEY
*key_to_remove
= 0;
287 VALUE
*value_to_remove
= 0;
289 for (size_t i
= 0; i
< entries_to_remove
; ++i
)
291 this->minimum (container
,
295 // Simply verifying that the key is non-zero.
296 // This is important for strategies where the minimum
297 // entry cant be found due to constraints on the type of entry
299 if (key_to_remove
== 0)
302 if (this->cleanup_strategy_
->cleanup (container
,
304 value_to_remove
) == -1)
307 ++this->marked_as_closed_entries_
;
313 template <class KEY
, class VALUE
, class CONTAINER
, class ITERATOR
, class ATTRIBUTES
> void
314 ACE_Refcounted_Recyclable_Handler_Caching_Utility
<KEY
, VALUE
, CONTAINER
, ITERATOR
, ATTRIBUTES
>::minimum (CONTAINER
&container
,
316 VALUE
*&value_to_remove
)
319 ITERATOR end
= container
.end ();
320 ITERATOR iter
= container
.begin ();
321 ATTRIBUTES min
= (*iter
).int_id_
.second ();
324 // Found the minimum entry to be purged?
327 // The iterator moves thru the container searching for the entry
328 // with the lowest ATTRIBUTES.
333 // If the <min> entry isnt IDLE_AND_PURGABLE continue until you reach
334 // the first entry which can be purged. This is the minimum with
335 // which you will compare the rest of the purgable entries.
336 if ((*iter
).ext_id_
.recycle_state () == ACE_RECYCLABLE_IDLE_AND_PURGABLE
||
337 (*iter
).ext_id_
.recycle_state () == ACE_RECYCLABLE_PURGABLE_BUT_NOT_IDLE
)
341 min
= (*iter
).int_id_
.second ();
342 key_to_remove
= &(*iter
).ext_id_
;
343 value_to_remove
= &(*iter
).int_id_
;
348 // Ah! an entry with lower ATTTRIBUTES...
349 if (min
> (*iter
).int_id_
.second ())
351 min
= (*iter
).int_id_
.second ();
352 key_to_remove
= &(*iter
).ext_id_
;
353 value_to_remove
= &(*iter
).int_id_
;
360 ////////////////////////////////////////////////////////////////////////////////
362 template <class KEY
, class VALUE
, class CONTAINER
, class ITERATOR
, class ATTRIBUTES
>
363 ACE_Handler_Caching_Utility
<KEY
, VALUE
, CONTAINER
, ITERATOR
, ATTRIBUTES
>::ACE_Handler_Caching_Utility (ACE_Cleanup_Strategy
<KEY
, VALUE
, CONTAINER
> *cleanup_strategy
,
364 bool delete_cleanup_strategy
)
365 : cleanup_strategy_ (cleanup_strategy
),
366 delete_cleanup_strategy_ (delete_cleanup_strategy
)
368 if (cleanup_strategy
== 0)
370 ACE_NEW (this->cleanup_strategy_
,
372 this->delete_cleanup_strategy_
= true;
376 template <class KEY
, class VALUE
, class CONTAINER
, class ITERATOR
, class ATTRIBUTES
>
377 ACE_Handler_Caching_Utility
<KEY
, VALUE
, CONTAINER
, ITERATOR
, ATTRIBUTES
>::~ACE_Handler_Caching_Utility (void)
379 if (this->delete_cleanup_strategy_
)
380 delete this->cleanup_strategy_
;
383 template <class KEY
, class VALUE
, class CONTAINER
, class ITERATOR
, class ATTRIBUTES
> int
384 ACE_Handler_Caching_Utility
<KEY
, VALUE
, CONTAINER
, ITERATOR
, ATTRIBUTES
>::clear_cache (CONTAINER
&container
,
385 double purge_percent
)
387 // Check that the purge_percent is non-zero.
388 if (ACE::is_equal (purge_percent
, 0.0))
391 // Get the number of entries in the container.
392 size_t current_map_size
= container
.current_size ();
394 // Also whether the number of entries in the cache is just one!
395 // Oops! then there is no way out but exiting. So return an error.
396 if (current_map_size
== 0)
399 // Calculate the no of entries to remove from the cache depending
400 // upon the <purge_percent>.
401 size_t entries_to_remove
402 = ACE_MAX (static_cast<size_t> (1),
403 static_cast<size_t> (static_cast<double> (purge_percent
)
404 / 100 * current_map_size
));
406 KEY
*key_to_remove
= 0;
407 VALUE
*value_to_remove
= 0;
409 for (size_t i
= 0; i
< entries_to_remove
; ++i
)
411 this->minimum (container
,
415 if (this->cleanup_strategy_
->cleanup (container
,
417 value_to_remove
) == -1)
424 template <class KEY
, class VALUE
, class CONTAINER
, class ITERATOR
, class ATTRIBUTES
> void
425 ACE_Handler_Caching_Utility
<KEY
, VALUE
, CONTAINER
, ITERATOR
, ATTRIBUTES
>::minimum (CONTAINER
&container
,
427 VALUE
*&value_to_remove
)
430 ITERATOR iter
= container
.begin ();
431 ITERATOR end
= container
.end ();
432 ATTRIBUTES min
= (*iter
).int_id_
->caching_attributes ();
433 key_to_remove
= &(*iter
).ext_id_
;
434 value_to_remove
= &(*iter
).int_id_
;
436 // The iterator moves thru the container searching for the entry
437 // with the lowest ATTRIBUTES.
442 if (min
> (*iter
).int_id_
->caching_attributes () &&
443 (*iter
).int_id_
->active () != 1)
445 // Ah! an item with lower ATTTRIBUTES...
446 min
= (*iter
).int_id_
->caching_attributes ();
447 key_to_remove
= &(*iter
).ext_id_
;
448 value_to_remove
= &(*iter
).int_id_
;
453 ////////////////////////////////////////////////////////////////////////////////////////////////////////
455 template <class KEY
, class VALUE
, class CONTAINER
, class ITERATOR
, class ATTRIBUTES
>
456 ACE_Null_Caching_Utility
<KEY
, VALUE
, CONTAINER
, ITERATOR
, ATTRIBUTES
>::ACE_Null_Caching_Utility (ACE_Cleanup_Strategy
<KEY
, VALUE
, CONTAINER
> *cleanup_strategy
,
457 bool delete_cleanup_strategy
)
458 : cleanup_strategy_ (cleanup_strategy
),
459 delete_cleanup_strategy_ (delete_cleanup_strategy
)
461 if (cleanup_strategy
== 0)
463 ACE_NEW (this->cleanup_strategy_
,
465 this->delete_cleanup_strategy_
= true;
469 template <class KEY
, class VALUE
, class CONTAINER
, class ITERATOR
, class ATTRIBUTES
>
470 ACE_Null_Caching_Utility
<KEY
, VALUE
, CONTAINER
, ITERATOR
, ATTRIBUTES
>::~ACE_Null_Caching_Utility (void)
472 if (this->delete_cleanup_strategy_
)
473 delete this->cleanup_strategy_
;
476 template <class KEY
, class VALUE
, class CONTAINER
, class ITERATOR
, class ATTRIBUTES
> int
477 ACE_Null_Caching_Utility
<KEY
, VALUE
, CONTAINER
, ITERATOR
, ATTRIBUTES
>::clear_cache (CONTAINER
&container
,
478 double purge_percent
)
480 ACE_UNUSED_ARG (container
);
481 ACE_UNUSED_ARG (purge_percent
);
486 template <class KEY
, class VALUE
, class CONTAINER
, class ITERATOR
, class ATTRIBUTES
> void
487 ACE_Null_Caching_Utility
<KEY
, VALUE
, CONTAINER
, ITERATOR
, ATTRIBUTES
>::minimum (CONTAINER
&container
,
489 VALUE
*&value_to_remove
)
491 ACE_UNUSED_ARG (container
);
492 ACE_UNUSED_ARG (key_to_remove
);
493 ACE_UNUSED_ARG (value_to_remove
);
496 ACE_END_VERSIONED_NAMESPACE_DECL
498 #endif /* ACE_CACHING_UTILITY_T_CPP */