Caching_Utility_T.cpp

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

Generated on Thu Nov 9 09:41:48 2006 for ACE by doxygen 1.3.6