Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members   Related Pages  

Caching_Utility_T.cpp

Go to the documentation of this file.
00001 // $Id: Caching_Utility_T.cpp,v 1.1.1.4 2003/02/21 18:36:32 chad Exp $
00002 
00003 #ifndef CACHING_UTILITY_T_C
00004 #define CACHING_UTILITY_T_C
00005 
00006 #include "ace/Caching_Utility_T.h"
00007 
00008 #if !defined (ACE_LACKS_PRAGMA_ONCE)
00009 #define ACE_LACKS_PRAGMA_ONCE
00010 #endif /* ACE_LACKS_PRAGMA_ONCE */
00011 
00012 #include "ace/Min_Max.h"
00013 #include "ace/OS_Memory.h"
00014 // #include "ace/Strategies.h"
00015 #include "ace/Recyclable.h"
00016 
00017 ACE_RCSID(ace, Caching_Utility_T, "$Id: Caching_Utility_T.cpp,v 1.1.1.4 2003/02/21 18:36:32 chad Exp $")
00018 
00019 //////////////////////////////////////////////////////////////////////////////
00020 
00021 template <class KEY, class VALUE, class CONTAINER, class ITERATOR, class ATTRIBUTES>
00022 ACE_Pair_Caching_Utility<KEY, VALUE, CONTAINER, ITERATOR, ATTRIBUTES>::ACE_Pair_Caching_Utility (ACE_Cleanup_Strategy<KEY, VALUE, CONTAINER> *cleanup_strategy,
00023                                                                                                  int delete_cleanup_strategy)
00024   : cleanup_strategy_ (cleanup_strategy),
00025     delete_cleanup_strategy_ (delete_cleanup_strategy)
00026 {
00027   if (cleanup_strategy == 0)
00028     {
00029       ACE_NEW (this->cleanup_strategy_,
00030                CLEANUP_STRATEGY);
00031       this->delete_cleanup_strategy_ = 1;
00032     }
00033 }
00034 
00035 template <class KEY, class VALUE, class CONTAINER, class ITERATOR, class ATTRIBUTES>
00036 ACE_Pair_Caching_Utility<KEY, VALUE, CONTAINER, ITERATOR, ATTRIBUTES>::~ACE_Pair_Caching_Utility (void)
00037 {
00038   if (this->delete_cleanup_strategy_)
00039     delete this->cleanup_strategy_;
00040 }
00041 
00042 template <class KEY, class VALUE, class CONTAINER, class ITERATOR, class ATTRIBUTES> int
00043 ACE_Pair_Caching_Utility<KEY, VALUE, CONTAINER, ITERATOR, ATTRIBUTES>::clear_cache (CONTAINER &container,
00044                                                                                     double purge_percent)
00045 {
00046   // Check that the purge_percent is non-zero.
00047   if (purge_percent == 0)
00048     return 0;
00049 
00050   // Get the number of entries in the container.
00051   size_t current_map_size = container.current_size ();
00052 
00053   // Also whether the number of entries in the cache!
00054   // Oops! then there is no way out but exiting. So return an error.
00055   if (current_map_size == 0)
00056     return 0;
00057 
00058   // Calculate the no of entries to remove from the cache depending
00059   // upon the <purge_percent>.
00060   size_t entries_to_remove
00061     = ACE_MAX (ACE_static_cast (size_t, 1),
00062                ACE_static_cast (size_t,
00063                                 ACE_static_cast(double, purge_percent)
00064                                 / 100 * current_map_size));
00065   KEY *key_to_remove = 0;
00066   VALUE *value_to_remove = 0;
00067 
00068   for (size_t i = 0; i < entries_to_remove ; ++i)
00069     {
00070       this->minimum (container,
00071                      key_to_remove,
00072                      value_to_remove);
00073 
00074       // Simply verifying that the key is non-zero.
00075       // This is important for strategies where the minimum
00076       // entry cant be found due to constraints on the type of entry
00077       // to remove.
00078       if (key_to_remove == 0)
00079         return 0;
00080 
00081       if (this->cleanup_strategy_->cleanup (container,
00082                                             key_to_remove,
00083                                             value_to_remove) == -1)
00084         return -1;
00085 
00086     }
00087 
00088   return 0;
00089 }
00090 
00091 template <class KEY, class VALUE, class CONTAINER, class ITERATOR, class ATTRIBUTES> void
00092 ACE_Pair_Caching_Utility<KEY, VALUE, CONTAINER, ITERATOR, ATTRIBUTES>::minimum (CONTAINER &container,
00093                                                                                 KEY *&key_to_remove,
00094                                                                                 VALUE *&value_to_remove)
00095 {
00096   // Starting values.
00097   ITERATOR iter = container.begin ();
00098   ITERATOR end = container.end ();
00099   ATTRIBUTES min = (*iter).int_id_.second ();
00100   key_to_remove = &(*iter).ext_id_;
00101   value_to_remove = &(*iter).int_id_;
00102 
00103   // The iterator moves thru the container searching for the entry
00104   // with the lowest ATTRIBUTES.
00105   for (++iter;
00106        iter != end;
00107        ++iter)
00108     {
00109       if (min > (*iter).int_id_.second ())
00110         {
00111           // Ah! an item with lower ATTTRIBUTES...
00112           min = (*iter).int_id_.second ();
00113           key_to_remove = &(*iter).ext_id_;
00114           value_to_remove = &(*iter).int_id_;
00115         }
00116     }
00117 }
00118 
00119 ////////////////////////////////////////////////////////////////////////////////////////////////////////
00120 
00121 template <class KEY, class VALUE, class CONTAINER, class ITERATOR, class ATTRIBUTES>
00122 ACE_Recyclable_Handler_Caching_Utility<KEY, VALUE, CONTAINER, ITERATOR, ATTRIBUTES>::ACE_Recyclable_Handler_Caching_Utility (ACE_Cleanup_Strategy<KEY, VALUE, CONTAINER> *cleanup_strategy,
00123                                                                                                                              int delete_cleanup_strategy)
00124   : cleanup_strategy_ (cleanup_strategy),
00125     delete_cleanup_strategy_ (delete_cleanup_strategy)
00126 {
00127   if (cleanup_strategy == 0)
00128     {
00129       ACE_NEW (this->cleanup_strategy_,
00130                CLEANUP_STRATEGY);
00131       this->delete_cleanup_strategy_ = 1;
00132     }
00133 }
00134 
00135 template <class KEY, class VALUE, class CONTAINER, class ITERATOR, class ATTRIBUTES>
00136 ACE_Recyclable_Handler_Caching_Utility<KEY, VALUE, CONTAINER, ITERATOR, ATTRIBUTES>::~ACE_Recyclable_Handler_Caching_Utility (void)
00137 {
00138   if (this->delete_cleanup_strategy_)
00139     delete this->cleanup_strategy_;
00140 }
00141 
00142 template <class KEY, class VALUE, class CONTAINER, class ITERATOR, class ATTRIBUTES> int
00143 ACE_Recyclable_Handler_Caching_Utility<KEY, VALUE, CONTAINER, ITERATOR, ATTRIBUTES>::clear_cache (CONTAINER &container,
00144                                                                                                   double purge_percent)
00145 {
00146   // Check that the purge_percent is non-zero.
00147   if (purge_percent == 0)
00148     return 0;
00149 
00150   // Get the number of entries in the container.
00151   size_t current_map_size = container.current_size ();
00152 
00153   // Also whether the number of entries in the cache is just one!
00154   // Oops! then there is no way out but exiting. So return an error.
00155   //  if (current_map_size <= 1)
00156   if (current_map_size == 0)
00157     return 0;
00158 
00159   // Calculate the no of entries to remove from the cache depending
00160   // upon the <purge_percent>.
00161   size_t entries_to_remove
00162     = ACE_MAX (ACE_static_cast (size_t, 1),
00163                ACE_static_cast(size_t,
00164                                ACE_static_cast(double, purge_percent)
00165                                / 100 * current_map_size));
00166 
00167   KEY *key_to_remove = 0;
00168   VALUE *value_to_remove = 0;
00169 
00170   for (size_t i = 0; i < entries_to_remove ; ++i)
00171     {
00172       this->minimum (container,
00173                      key_to_remove,
00174                      value_to_remove);
00175 
00176       // Simply verifying that the key is non-zero.
00177       // This is important for strategies where the minimum
00178       // entry cant be found due to constraints on the type of entry
00179       // to remove.
00180       if (key_to_remove == 0)
00181         return 0;
00182 
00183       if (this->cleanup_strategy_->cleanup (container,
00184                                             key_to_remove,
00185                                             value_to_remove) == -1)
00186         return -1;
00187     }
00188 
00189   return 0;
00190 }
00191 
00192 template <class KEY, class VALUE, class CONTAINER, class ITERATOR, class ATTRIBUTES> void
00193 ACE_Recyclable_Handler_Caching_Utility<KEY, VALUE, CONTAINER, ITERATOR, ATTRIBUTES>::minimum (CONTAINER &container,
00194                                                                                               KEY *&key_to_remove,
00195                                                                                               VALUE *&value_to_remove)
00196 {
00197   // Starting values.
00198   ITERATOR end = container.end ();
00199   ITERATOR iter = container.begin ();
00200   ATTRIBUTES min = (*iter).int_id_.second ();
00201   key_to_remove = 0;
00202   value_to_remove = 0;
00203   // Found the minimum entry to be purged?
00204   int found = 0;
00205 
00206   // The iterator moves thru the container searching for the entry
00207   // with the lowest ATTRIBUTES.
00208   for (;
00209        iter != end;
00210        ++iter)
00211     {
00212       // If the <min> entry isnt IDLE_AND_PURGABLE continue until you reach
00213       // the first entry which can be purged. This is the minimum with
00214       // which you will compare the rest of the purgable entries.
00215       if ((*iter).ext_id_.recycle_state () == ACE_RECYCLABLE_IDLE_AND_PURGABLE ||
00216           (*iter).ext_id_.recycle_state () == ACE_RECYCLABLE_PURGABLE_BUT_NOT_IDLE)
00217         {
00218           if (found == 0)
00219             {
00220               min = (*iter).int_id_.second ();
00221               key_to_remove = &(*iter).ext_id_;
00222               value_to_remove = &(*iter).int_id_;
00223               found = 1;
00224             }
00225           else
00226             {
00227               // Ah! an entry with lower ATTTRIBUTES...
00228               if (min > (*iter).int_id_.second ())
00229                 {
00230                   min = (*iter).int_id_.second ();
00231                   key_to_remove = &(*iter).ext_id_;
00232                   value_to_remove = &(*iter).int_id_;
00233                 }
00234             }
00235         }
00236     }
00237 }
00238 
00239 ////////////////////////////////////////////////////////////////////////////////
00240 
00241 template <class KEY, class VALUE, class CONTAINER, class ITERATOR, class ATTRIBUTES>
00242 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,
00243                                                                                                                                                    int delete_cleanup_strategy)
00244   : cleanup_strategy_ (cleanup_strategy),
00245     delete_cleanup_strategy_ (delete_cleanup_strategy),
00246     marked_as_closed_entries_ (0)
00247 {
00248   if (cleanup_strategy == 0)
00249     {
00250       ACE_NEW (this->cleanup_strategy_,
00251                CLEANUP_STRATEGY);
00252       this->delete_cleanup_strategy_ = 1;
00253     }
00254 }
00255 
00256 template <class KEY, class VALUE, class CONTAINER, class ITERATOR, class ATTRIBUTES>
00257 ACE_Refcounted_Recyclable_Handler_Caching_Utility<KEY, VALUE, CONTAINER, ITERATOR, ATTRIBUTES>::~ACE_Refcounted_Recyclable_Handler_Caching_Utility (void)
00258 {
00259   if (this->delete_cleanup_strategy_)
00260     delete this->cleanup_strategy_;
00261 }
00262 
00263 template <class KEY, class VALUE, class CONTAINER, class ITERATOR, class ATTRIBUTES> int
00264 ACE_Refcounted_Recyclable_Handler_Caching_Utility<KEY, VALUE, CONTAINER, ITERATOR, ATTRIBUTES>::clear_cache (CONTAINER &container,
00265                                                                                                              double purge_percent)
00266 {
00267   // Check that the purge_percent is non-zero.
00268   if (purge_percent == 0)
00269     return 0;
00270 
00271   // Get the number of entries in the container which can be considered for purging.
00272   size_t available_entries = container.current_size () - this->marked_as_closed_entries_;
00273 
00274   // Also whether the number of entries in the cache zero.
00275   // Oops! then there is no way out but exiting.
00276   if (available_entries <= 0)
00277     return 0;
00278 
00279   // Calculate the no of entries to remove from the cache depending
00280   // upon the <purge_percent>.
00281   size_t entries_to_remove
00282     = ACE_MAX (ACE_static_cast (size_t, 1),
00283                ACE_static_cast(size_t,
00284                                ACE_static_cast(double, purge_percent)
00285                                / 100 * available_entries));
00286 
00287   if (entries_to_remove >= available_entries  ||
00288       entries_to_remove == 0)
00289     entries_to_remove = available_entries - 1;
00290 
00291   KEY *key_to_remove = 0;
00292   VALUE *value_to_remove = 0;
00293 
00294   for (size_t i = 0; i < entries_to_remove ; ++i)
00295     {
00296       this->minimum (container,
00297                      key_to_remove,
00298                      value_to_remove);
00299 
00300       // Simply verifying that the key is non-zero.
00301       // This is important for strategies where the minimum
00302       // entry cant be found due to constraints on the type of entry
00303       // to remove.
00304       if (key_to_remove == 0)
00305         return 0;
00306 
00307       if (this->cleanup_strategy_->cleanup (container,
00308                                             key_to_remove,
00309                                             value_to_remove) == -1)
00310         return -1;
00311 
00312       ++this->marked_as_closed_entries_;
00313     }
00314 
00315   return 0;
00316 }
00317 
00318 template <class KEY, class VALUE, class CONTAINER, class ITERATOR, class ATTRIBUTES> void
00319 ACE_Refcounted_Recyclable_Handler_Caching_Utility<KEY, VALUE, CONTAINER, ITERATOR, ATTRIBUTES>::minimum (CONTAINER &container,
00320                                                                                                          KEY *&key_to_remove,
00321                                                                                                          VALUE *&value_to_remove)
00322 {
00323   // Starting values.
00324   ITERATOR end = container.end ();
00325   ITERATOR iter = container.begin ();
00326   ATTRIBUTES min = (*iter).int_id_.second ();
00327   key_to_remove = 0;
00328   value_to_remove = 0;
00329   // Found the minimum entry to be purged?
00330   int found = 0;
00331 
00332   // The iterator moves thru the container searching for the entry
00333   // with the lowest ATTRIBUTES.
00334   for (;
00335        iter != end;
00336        ++iter)
00337     {
00338       // If the <min> entry isnt IDLE_AND_PURGABLE continue until you reach
00339       // the first entry which can be purged. This is the minimum with
00340       // which you will compare the rest of the purgable entries.
00341       if ((*iter).ext_id_.recycle_state () == ACE_RECYCLABLE_IDLE_AND_PURGABLE ||
00342           (*iter).ext_id_.recycle_state () == ACE_RECYCLABLE_PURGABLE_BUT_NOT_IDLE)
00343         {
00344           if (found == 0)
00345             {
00346               min = (*iter).int_id_.second ();
00347               key_to_remove = &(*iter).ext_id_;
00348               value_to_remove = &(*iter).int_id_;
00349               found = 1;
00350             }
00351           else
00352             {
00353               // Ah! an entry with lower ATTTRIBUTES...
00354               if (min > (*iter).int_id_.second ())
00355                 {
00356                   min = (*iter).int_id_.second ();
00357                   key_to_remove = &(*iter).ext_id_;
00358                   value_to_remove = &(*iter).int_id_;
00359                 }
00360             }
00361         }
00362     }
00363 }
00364 
00365 ////////////////////////////////////////////////////////////////////////////////
00366 
00367 template <class KEY, class VALUE, class CONTAINER, class ITERATOR, class ATTRIBUTES>
00368 ACE_Handler_Caching_Utility<KEY, VALUE, CONTAINER, ITERATOR, ATTRIBUTES>::ACE_Handler_Caching_Utility (ACE_Cleanup_Strategy<KEY, VALUE, CONTAINER> *cleanup_strategy,
00369                                                                                                        int delete_cleanup_strategy)
00370   : cleanup_strategy_ (cleanup_strategy),
00371     delete_cleanup_strategy_ (delete_cleanup_strategy)
00372 {
00373   if (cleanup_strategy == 0)
00374     {
00375       ACE_NEW (this->cleanup_strategy_,
00376                CLEANUP_STRATEGY);
00377       this->delete_cleanup_strategy_ = 1;
00378     }
00379 }
00380 
00381 template <class KEY, class VALUE, class CONTAINER, class ITERATOR, class ATTRIBUTES>
00382 ACE_Handler_Caching_Utility<KEY, VALUE, CONTAINER, ITERATOR, ATTRIBUTES>::~ACE_Handler_Caching_Utility (void)
00383 {
00384   if (this->delete_cleanup_strategy_)
00385     delete this->cleanup_strategy_;
00386 }
00387 
00388 template <class KEY, class VALUE, class CONTAINER, class ITERATOR, class ATTRIBUTES> int
00389 ACE_Handler_Caching_Utility<KEY, VALUE, CONTAINER, ITERATOR, ATTRIBUTES>::clear_cache (CONTAINER &container,
00390                                                                                        double purge_percent)
00391 {
00392   // Check that the purge_percent is non-zero.
00393   if (purge_percent == 0)
00394     return 0;
00395 
00396   // Get the number of entries in the container.
00397   size_t current_map_size = container.current_size ();
00398 
00399   // Also whether the number of entries in the cache is just one!
00400   // Oops! then there is no way out but exiting. So return an error.
00401   if (current_map_size == 0)
00402     return 0;
00403 
00404   // Calculate the no of entries to remove from the cache depending
00405   // upon the <purge_percent>.
00406   size_t entries_to_remove
00407     = ACE_MAX (ACE_static_cast (size_t, 1),
00408                ACE_static_cast(size_t,
00409                                ACE_static_cast(double, purge_percent)
00410                                / 100 * current_map_size));
00411 
00412   KEY *key_to_remove = 0;
00413   VALUE *value_to_remove = 0;
00414 
00415   for (size_t i = 0; i < entries_to_remove ; ++i)
00416     {
00417       this->minimum (container,
00418                      key_to_remove,
00419                      value_to_remove);
00420 
00421       if (this->cleanup_strategy_->cleanup (container,
00422                                             key_to_remove,
00423                                             value_to_remove) == -1)
00424         return -1;
00425     }
00426 
00427   return 0;
00428 }
00429 
00430 template <class KEY, class VALUE, class CONTAINER, class ITERATOR, class ATTRIBUTES> void
00431 ACE_Handler_Caching_Utility<KEY, VALUE, CONTAINER, ITERATOR, ATTRIBUTES>::minimum (CONTAINER &container,
00432                                                                                    KEY *&key_to_remove,
00433                                                                                    VALUE *&value_to_remove)
00434 {
00435   // Starting values.
00436   ITERATOR iter = container.begin ();
00437   ITERATOR end = container.end ();
00438   ATTRIBUTES min = (*iter).int_id_->caching_attributes ();
00439   key_to_remove = &(*iter).ext_id_;
00440   value_to_remove = &(*iter).int_id_;
00441 
00442   // The iterator moves thru the container searching for the entry
00443   // with the lowest ATTRIBUTES.
00444   for (++iter;
00445        iter != end;
00446        ++iter)
00447     {
00448       if (min > (*iter).int_id_->caching_attributes () &&
00449           (*iter).int_id_->active () != 1)
00450         {
00451           // Ah! an item with lower ATTTRIBUTES...
00452           min = (*iter).int_id_->caching_attributes ();
00453           key_to_remove = &(*iter).ext_id_;
00454           value_to_remove = &(*iter).int_id_;
00455         }
00456     }
00457 }
00458 
00459 ////////////////////////////////////////////////////////////////////////////////////////////////////////
00460 
00461 template <class KEY, class VALUE, class CONTAINER, class ITERATOR, class ATTRIBUTES>
00462 ACE_Null_Caching_Utility<KEY, VALUE, CONTAINER, ITERATOR, ATTRIBUTES>::ACE_Null_Caching_Utility (ACE_Cleanup_Strategy<KEY, VALUE, CONTAINER> *cleanup_strategy,
00463                                                                                                  int delete_cleanup_strategy)
00464   : cleanup_strategy_ (cleanup_strategy),
00465     delete_cleanup_strategy_ (delete_cleanup_strategy)
00466 {
00467   if (cleanup_strategy == 0)
00468     {
00469       ACE_NEW (this->cleanup_strategy_,
00470                CLEANUP_STRATEGY);
00471       this->delete_cleanup_strategy_ = 1;
00472     }
00473 }
00474 
00475 template <class KEY, class VALUE, class CONTAINER, class ITERATOR, class ATTRIBUTES>
00476 ACE_Null_Caching_Utility<KEY, VALUE, CONTAINER, ITERATOR, ATTRIBUTES>::~ACE_Null_Caching_Utility (void)
00477 {
00478   if (this->delete_cleanup_strategy_)
00479     delete this->cleanup_strategy_;
00480 }
00481 
00482 template <class KEY, class VALUE, class CONTAINER, class ITERATOR, class ATTRIBUTES> int
00483 ACE_Null_Caching_Utility<KEY, VALUE, CONTAINER, ITERATOR, ATTRIBUTES>::clear_cache (CONTAINER &container,
00484                                                                                     double purge_percent)
00485 {
00486   ACE_UNUSED_ARG (container);
00487   ACE_UNUSED_ARG (purge_percent);
00488 
00489   return 0;
00490 }
00491 
00492 template <class KEY, class VALUE, class CONTAINER, class ITERATOR, class ATTRIBUTES> void
00493 ACE_Null_Caching_Utility<KEY, VALUE, CONTAINER, ITERATOR, ATTRIBUTES>::minimum (CONTAINER &container,
00494                                                                                 KEY *&key_to_remove,
00495                                                                                 VALUE *&value_to_remove)
00496 {
00497   ACE_UNUSED_ARG (container);
00498   ACE_UNUSED_ARG (key_to_remove);
00499   ACE_UNUSED_ARG (value_to_remove);
00500 }
00501 
00502 #endif /* CACHING_UTILITY_T_C */

Generated on Mon Jun 16 11:19:18 2003 for ACE by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002