Correct feature names
[ACE_TAO.git] / ACE / ace / Caching_Utility_T.cpp
blobaa344562d865dea15772078d0d12091bc6aeadc8
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)
7 #pragma once
8 #endif /* ACE_LACKS_PRAGMA_ONCE */
10 #include "ace/ACE.h"
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_,
28 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,
42 double purge_percent)
44 // Check that the purge_percent is non-zero.
45 if (ACE::is_equal (purge_percent, 0.0))
46 return 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)
54 return 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,
68 key_to_remove,
69 value_to_remove);
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
74 // to remove.
75 if (key_to_remove == 0)
76 return 0;
78 if (this->cleanup_strategy_->cleanup (container,
79 key_to_remove,
80 value_to_remove) == -1)
81 return -1;
85 return 0;
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,
90 KEY *&key_to_remove,
91 VALUE *&value_to_remove)
93 // Starting values.
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.
102 for (++iter;
103 iter != end;
104 ++iter)
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_,
127 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))
145 return 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)
154 return 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,
169 key_to_remove,
170 value_to_remove);
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
175 // to remove.
176 if (key_to_remove == 0)
177 return 0;
179 if (this->cleanup_strategy_->cleanup (container,
180 key_to_remove,
181 value_to_remove) == -1)
182 return -1;
185 return 0;
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,
190 KEY *&key_to_remove,
191 VALUE *&value_to_remove)
193 // Starting values.
194 ITERATOR end = container.end ();
195 ITERATOR iter = container.begin ();
196 ATTRIBUTES min = (*iter).int_id_.second;
197 key_to_remove = 0;
198 value_to_remove = 0;
199 // Found the minimum entry to be purged?
200 int found = 0;
202 // The iterator moves thru the container searching for the entry
203 // with the lowest ATTRIBUTES.
204 for (;
205 iter != end;
206 ++iter)
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)
214 if (found == 0)
216 min = (*iter).int_id_.second;
217 key_to_remove = &(*iter).ext_id_;
218 value_to_remove = &(*iter).int_id_;
219 found = 1;
221 else
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_,
247 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))
265 return 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)
274 return 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,
292 key_to_remove,
293 value_to_remove);
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
298 // to remove.
299 if (key_to_remove == 0)
300 return 0;
302 if (this->cleanup_strategy_->cleanup (container,
303 key_to_remove,
304 value_to_remove) == -1)
305 return -1;
307 ++this->marked_as_closed_entries_;
310 return 0;
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,
315 KEY *&key_to_remove,
316 VALUE *&value_to_remove)
318 // Starting values.
319 ITERATOR end = container.end ();
320 ITERATOR iter = container.begin ();
321 ATTRIBUTES min = (*iter).int_id_.second ();
322 key_to_remove = 0;
323 value_to_remove = 0;
324 // Found the minimum entry to be purged?
325 int found = 0;
327 // The iterator moves thru the container searching for the entry
328 // with the lowest ATTRIBUTES.
329 for (;
330 iter != end;
331 ++iter)
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)
339 if (found == 0)
341 min = (*iter).int_id_.second ();
342 key_to_remove = &(*iter).ext_id_;
343 value_to_remove = &(*iter).int_id_;
344 found = 1;
346 else
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_,
371 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))
389 return 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)
397 return 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,
412 key_to_remove,
413 value_to_remove);
415 if (this->cleanup_strategy_->cleanup (container,
416 key_to_remove,
417 value_to_remove) == -1)
418 return -1;
421 return 0;
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,
426 KEY *&key_to_remove,
427 VALUE *&value_to_remove)
429 // Starting values.
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.
438 for (++iter;
439 iter != end;
440 ++iter)
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_,
464 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);
483 return 0;
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,
488 KEY *&key_to_remove,
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 */