ACE_Filecache Class Reference

A hash table holding the information about entry point into the Cached Virtual Filesystem. On insertion, the reference count is incremented. On destruction, reference count is decremented. More...

#include <Filecache.h>

Collaboration diagram for ACE_Filecache:

Collaboration graph
[legend]
List of all members.

Public Types

 ACE_DEFAULT_VIRTUAL_FILESYSTEM_TABLE_SIZE = 512
 ACE_DEFAULT_VIRTUAL_FILESYSTEM_CACHE_SIZE = 20
enum  { ACE_DEFAULT_VIRTUAL_FILESYSTEM_TABLE_SIZE = 512, ACE_DEFAULT_VIRTUAL_FILESYSTEM_CACHE_SIZE = 20 }

Public Member Functions

 ~ACE_Filecache (void)
int find (const ACE_TCHAR *filename)
ACE_Filecache_Objectfetch (const ACE_TCHAR *filename, int mapit=1)
ACE_Filecache_Objectremove (const ACE_TCHAR *filename)
 Remove the file associated with ``filename'' from the cache.
ACE_Filecache_Objectcreate (const ACE_TCHAR *filename, int size)
 Create a new Filecache_Object, returns it.
ACE_Filecache_Objectfinish (ACE_Filecache_Object *&new_file)

Static Public Member Functions

static ACE_Filecacheinstance (void)
 Singleton pattern.

Protected Member Functions

ACE_Filecache_Objectinsert_i (const ACE_TCHAR *filename, ACE_SYNCH_RW_MUTEX &filelock, int mapit)
ACE_Filecache_Objectremove_i (const ACE_TCHAR *filename)
ACE_Filecache_Objectupdate_i (const ACE_TCHAR *filename, ACE_SYNCH_RW_MUTEX &filelock, int mapit)
 ACE_Filecache (void)
 Prevent it from being called.

Private Attributes

ACE_OFF_T size_
ACE_Filecache_Hash hash_
 The hash table.
ACE_SYNCH_RW_MUTEX hash_lock_ [ACE_DEFAULT_VIRTUAL_FILESYSTEM_TABLE_SIZE]
ACE_SYNCH_RW_MUTEX file_lock_ [ACE_DEFAULT_VIRTUAL_FILESYSTEM_TABLE_SIZE]

Static Private Attributes

static ACE_Filecachecvf_ = 0
 The reference to the instance.

Detailed Description

A hash table holding the information about entry point into the Cached Virtual Filesystem. On insertion, the reference count is incremented. On destruction, reference count is decremented.

Definition at line 165 of file Filecache.h.


Member Enumeration Documentation

anonymous enum

Enumerator:
ACE_DEFAULT_VIRTUAL_FILESYSTEM_TABLE_SIZE  For this stupid implementation, use an array. Someday, use a balanced search tree, or real hash table.
ACE_DEFAULT_VIRTUAL_FILESYSTEM_CACHE_SIZE  This determines the highwater mark in megabytes for the cache. This will be ignored for now.

Definition at line 202 of file Filecache.h.

00203   {
00204     /// For this stupid implementation, use an array.  Someday, use a
00205     /// balanced search tree, or real hash table.
00206     ACE_DEFAULT_VIRTUAL_FILESYSTEM_TABLE_SIZE = 512,
00207 
00208     /// This determines the highwater mark in megabytes for the cache.
00209     /// This will be ignored for now.
00210     ACE_DEFAULT_VIRTUAL_FILESYSTEM_CACHE_SIZE = 20
00211   };


Constructor & Destructor Documentation

ACE_Filecache::~ACE_Filecache ( void   ) 

Definition at line 236 of file Filecache.cpp.

00237 {
00238 }

ACE_Filecache::ACE_Filecache ( void   )  [protected]

Prevent it from being called.

Definition at line 230 of file Filecache.cpp.

00231   : size_ (ACE_DEFAULT_VIRTUAL_FILESYSTEM_TABLE_SIZE),
00232     hash_ (size_)
00233 {
00234 }


Member Function Documentation

ACE_Filecache_Object * ACE_Filecache::create ( const ACE_TCHAR filename,
int  size 
)

Create a new Filecache_Object, returns it.

Definition at line 383 of file Filecache.cpp.

References ACE_NEW_RETURN, ACE_SYNCH_RW_MUTEX, ACE_Filecache_Object::acquire(), file_lock_, ACE::hash_pjw(), and size_.

Referenced by ACE_Filecache_Handle::ACE_Filecache_Handle().

00384 {
00385   ACE_Filecache_Object *handle = 0;
00386 
00387   ACE_OFF_T loc = ACE::hash_pjw (filename) % this->size_;
00388   ACE_SYNCH_RW_MUTEX &filelock = this->file_lock_[loc];
00389 
00390   ACE_NEW_RETURN (handle,
00391                   ACE_Filecache_Object (filename, size, filelock),
00392                   0);
00393   handle->acquire ();
00394 
00395   return handle;
00396 }

ACE_Filecache_Object * ACE_Filecache::fetch ( const ACE_TCHAR filename,
int  mapit = 1 
)

Return the file associated with ``filename'' if it is in the cache, or create if not.

Definition at line 335 of file Filecache.cpp.

References ACE_SYNCH_RW_MUTEX, ACE_WRITE_GUARD_RETURN, file_lock_, hash_lock_, ACE::hash_pjw(), insert_i(), size_, ACE_Filecache_Object::update(), and update_i().

Referenced by ACE_Filecache_Handle::ACE_Filecache_Handle().

00336 {
00337   ACE_Filecache_Object *handle = 0;
00338 
00339   ACE_OFF_T loc = ACE::hash_pjw (filename) % this->size_;
00340   ACE_SYNCH_RW_MUTEX &hashlock = this->hash_lock_[loc];
00341   ACE_SYNCH_RW_MUTEX &filelock = this->file_lock_[loc];
00342 
00343   filelock.acquire_read ();
00344 
00345   if (this->hash_.find (filename, handle) == -1)
00346     {
00347       ACE_WRITE_GUARD_RETURN (ACE_SYNCH_RW_MUTEX,
00348                               ace_mon,
00349                               hashlock,
00350                               0);
00351 
00352       // Second check in the method call
00353       handle = this->insert_i (filename, filelock, mapit);
00354 
00355       if (handle == 0)
00356         filelock.release ();
00357     }
00358   else
00359     {
00360       if (handle->update ())
00361         {
00362           {
00363             // Double check locking pattern
00364             ACE_WRITE_GUARD_RETURN (ACE_SYNCH_RW_MUTEX,
00365                                     ace_mon,
00366                                     hashlock,
00367                                     0);
00368 
00369             // Second check in the method call
00370             handle = this->update_i (filename, filelock, mapit);
00371 
00372             if (handle == 0)
00373               filelock.release ();
00374           }
00375         }
00376       //      ACE_DEBUG ((LM_DEBUG,  ACE_TEXT ("   (%t) CVF: found %s\n"), filename));
00377     }
00378 
00379   return handle;
00380 }

int ACE_Filecache::find ( const ACE_TCHAR filename  ) 

Returns 0 if the file associated with ``filename'' is in the cache, or -1 if not.

Definition at line 305 of file Filecache.cpp.

References ACE_Hash_Map_Manager_Ex< EXT_ID, INT_ID, HASH_KEY, COMPARE_KEYS, ACE_LOCK >::find(), and hash_.

00306 {
00307   return this->hash_.find (filename);
00308 }

ACE_Filecache_Object * ACE_Filecache::finish ( ACE_Filecache_Object *&  new_file  ) 

Release an acquired Filecache_Object, returns it again or NULL if it was deleted.

Definition at line 399 of file Filecache.cpp.

References ACE_SYNCH_RW_MUTEX, ACE_WRITE_GUARD_RETURN, ACE_Filecache_Object::ACE_WRITING, ACE_Filecache_Object::acquire(), ACE_Filecache_Object::action_, ACE_Hash_Map_Manager_Ex< EXT_ID, INT_ID, HASH_KEY, COMPARE_KEYS, ACE_LOCK >::bind(), ACE_Filecache_Object::filename(), ACE_Filecache_Object::filename_, hash_, hash_lock_, ACE::hash_pjw(), ACE_Filecache_Object::lock_, ACE_Filecache_Object::release(), remove_i(), size_, and ACE_Filecache_Object::stale_.

Referenced by ACE_Filecache_Handle::~ACE_Filecache_Handle().

00400 {
00401   if (file == 0)
00402     return file;
00403 
00404   ACE_OFF_T loc = ACE::hash_pjw (file->filename_) % this->size_;
00405   ACE_SYNCH_RW_MUTEX &hashlock = this->hash_lock_[loc];
00406 
00407   if (file != 0)
00408     switch (file->action_)
00409       {
00410       case ACE_Filecache_Object::ACE_WRITING:
00411         {
00412           ACE_WRITE_GUARD_RETURN (ACE_SYNCH_RW_MUTEX,
00413                                   ace_mon,
00414                                   hashlock,
00415                                   0);
00416 
00417           file->release ();
00418 
00419           this->remove_i (file->filename_);
00420 #if 0
00421           int result = this->hash_.bind (file->filename (), file);
00422 
00423           if (result == 0)
00424             file->acquire ();
00425 #else
00426         // Last one using a stale file is resposible for deleting it.
00427         if (file->stale_)
00428           {
00429             // Try a lock.  If it succeds, we can delete it now.
00430             // Otherwise, it will clean itself up later.
00431             if (file->lock_.tryacquire_write () == 0)
00432               {
00433                 delete file;
00434                 file = 0;
00435               }
00436           }
00437 #endif
00438         }
00439 
00440         break;
00441       default:
00442         file->release ();
00443 
00444         // Last one using a stale file is resposible for deleting it.
00445         if (file->stale_)
00446           {
00447             // Try a lock.  If it succeds, we can delete it now.
00448             // Otherwise, it will clean itself up later.
00449             if (file->lock_.tryacquire_write () == 0)
00450               {
00451                 delete file;
00452                 file = 0;
00453               }
00454           }
00455 
00456         break;
00457       }
00458 
00459   return file;
00460 }

ACE_Filecache_Object * ACE_Filecache::insert_i ( const ACE_TCHAR filename,
ACE_SYNCH_RW_MUTEX &  filelock,
int  mapit 
) [protected]

Definition at line 241 of file Filecache.cpp.

References ACE_NEW_RETURN.

Referenced by fetch(), and update_i().

00244 {
00245   ACE_Filecache_Object *handle = 0;
00246 
00247   if (this->hash_.find (filename, handle) == -1)
00248     {
00249       ACE_NEW_RETURN (handle,
00250                       ACE_Filecache_Object (filename, filelock, 0, mapit),
00251                       0);
00252 
00253       //      ACE_DEBUG ((LM_DEBUG,  ACE_TEXT ("   (%t) CVF: creating %s\n"), filename));
00254 
00255       if (this->hash_.bind (filename, handle) == -1)
00256         {
00257           delete handle;
00258           handle = 0;
00259         }
00260     }
00261   else
00262     handle = 0;
00263 
00264   return handle;
00265 }

ACE_Filecache * ACE_Filecache::instance ( void   )  [static]

Singleton pattern.

Definition at line 209 of file Filecache.cpp.

References ACE_Object_Manager::ACE_FILECACHE_LOCK, ACE_GUARD_RETURN, ACE_NEW_RETURN, ACE_SYNCH_RW_MUTEX, and ACE_Managed_Object< TYPE >::get_preallocated_object().

Referenced by ACE_Filecache_Handle::ACE_Filecache_Handle(), and ACE_Filecache_Handle::~ACE_Filecache_Handle().

00210 {
00211   // Double check locking pattern.
00212   if (ACE_Filecache::cvf_ == 0)
00213     {
00214       ACE_SYNCH_RW_MUTEX &lock =
00215         *ACE_Managed_Object<ACE_SYNCH_RW_MUTEX>::get_preallocated_object
00216           (ACE_Object_Manager::ACE_FILECACHE_LOCK);
00217       ACE_GUARD_RETURN (ACE_SYNCH_RW_MUTEX, ace_mon, lock, 0);
00218 
00219       // @@ James, please check each of the ACE_NEW_RETURN calls to
00220       // make sure that it is safe to return if allocation fails.
00221       if (ACE_Filecache::cvf_ == 0)
00222         ACE_NEW_RETURN (ACE_Filecache::cvf_,
00223                         ACE_Filecache,
00224                         0);
00225     }
00226 
00227   return ACE_Filecache::cvf_;
00228 }

ACE_Filecache_Object * ACE_Filecache::remove ( const ACE_TCHAR filename  ) 

Remove the file associated with ``filename'' from the cache.

Definition at line 312 of file Filecache.cpp.

References ACE_SYNCH_RW_MUTEX, ACE_WRITE_GUARD_RETURN, hash_lock_, ACE::hash_pjw(), remove_i(), and size_.

Referenced by ACE_Filecache_Handle::ACE_Filecache_Handle().

00313 {
00314   ACE_Filecache_Object *handle = 0;
00315 
00316   ACE_OFF_T loc = ACE::hash_pjw (filename) % this->size_;
00317   ACE_SYNCH_RW_MUTEX &hashlock = this->hash_lock_[loc];
00318   // ACE_SYNCH_RW_MUTEX &filelock = this->file_lock_[loc];
00319 
00320   if (this->hash_.find (filename, handle) != -1)
00321     {
00322       ACE_WRITE_GUARD_RETURN (ACE_SYNCH_RW_MUTEX,
00323                               ace_mon,
00324                               hashlock,
00325                               0);
00326 
00327       return this->remove_i (filename);
00328     }
00329 
00330   return 0;
00331 }

ACE_Filecache_Object * ACE_Filecache::remove_i ( const ACE_TCHAR filename  )  [protected]

Definition at line 268 of file Filecache.cpp.

References ACE_Filecache_Object::lock_, and ACE_Filecache_Object::stale_.

Referenced by finish(), remove(), and update_i().

00269 {
00270   ACE_Filecache_Object *handle = 0;
00271 
00272   // Disassociate file from the cache.
00273   if (this->hash_.unbind (filename, handle) == 0)
00274     {
00275       handle->stale_ = 1;
00276 
00277       // Try a lock.  If it succeeds, we can delete it now.
00278       // Otherwise, it will clean itself up later.
00279       if (handle->lock_.tryacquire_write () == 0)
00280         {
00281           delete handle;
00282           handle = 0;
00283         }
00284     }
00285   else
00286     handle = 0;
00287 
00288   return handle;
00289 }

ACE_Filecache_Object * ACE_Filecache::update_i ( const ACE_TCHAR filename,
ACE_SYNCH_RW_MUTEX &  filelock,
int  mapit 
) [protected]

Definition at line 292 of file Filecache.cpp.

References insert_i(), and remove_i().

Referenced by fetch().

00295 {
00296   ACE_Filecache_Object *handle = 0;
00297 
00298   handle = this->remove_i (filename);
00299   handle = this->insert_i (filename, filelock, mapit);
00300 
00301   return handle;
00302 }


Member Data Documentation

ACE_BEGIN_VERSIONED_NAMESPACE_DECL ACE_Filecache * ACE_Filecache::cvf_ = 0 [static, private]

The reference to the instance.

Definition at line 224 of file Filecache.h.

ACE_SYNCH_RW_MUTEX ACE_Filecache::file_lock_[ACE_DEFAULT_VIRTUAL_FILESYSTEM_TABLE_SIZE] [private]

Definition at line 228 of file Filecache.h.

Referenced by create(), and fetch().

ACE_Filecache_Hash ACE_Filecache::hash_ [private]

The hash table.

Definition at line 221 of file Filecache.h.

Referenced by find(), and finish().

ACE_SYNCH_RW_MUTEX ACE_Filecache::hash_lock_[ACE_DEFAULT_VIRTUAL_FILESYSTEM_TABLE_SIZE] [private]

Definition at line 227 of file Filecache.h.

Referenced by fetch(), finish(), and remove().

ACE_OFF_T ACE_Filecache::size_ [private]

Definition at line 218 of file Filecache.h.

Referenced by create(), fetch(), finish(), and remove().


The documentation for this class was generated from the following files:
Generated on Tue Feb 2 17:35:08 2010 for ACE by  doxygen 1.4.7