PerFileManager Class Reference

#include <PerFileManager.h>

List of all members.

Public Types

typedef map< string, PerFile
* >::iterator 
FileMapItr
typedef map< string, PerFile
* >::const_iterator 
FileMapConstItr

Public Member Functions

virtual ~PerFileManager ()
UInt_t GetNumFile () const
Per::EErrorCode GetErrorCode () const
const PerFileGetOpenedFile (string fullfilepathname) const
std::ostream & Print (std::ostream &s) const
const PerFileChangeFile (std::string oldfilepathname, std::string newfilepathname, TFile *newfileptr)
const PerFileOpenFile (string fullfilepathname, Per::EAccessMode accessmode)
void CloseFile (string fullfilepathname)
void CloseAll ()

Static Public Member Functions

static PerFileManagerInstance ()

Protected Member Functions

 PerFileManager ()

Private Member Functions

 PerFileManager (PerFileManager &)
PerFileManageroperator= (const PerFileManager &)

Private Attributes

map< string, PerFile * > fFileMap
Per::EErrorCode fErrorCode

Static Private Attributes

static PerFileManagerfgInstance = 0

Friends

struct Cleaner

Classes

struct  Cleaner


Detailed Description

Definition at line 32 of file PerFileManager.h.


Member Typedef Documentation

typedef map<string,PerFile*>::const_iterator PerFileManager::FileMapConstItr

Definition at line 38 of file PerFileManager.h.

typedef map<string,PerFile*>::iterator PerFileManager::FileMapItr

Definition at line 37 of file PerFileManager.h.


Constructor & Destructor Documentation

PerFileManager::~PerFileManager (  )  [virtual]

Definition at line 270 of file PerFileManager.cxx.

References CloseAll().

00270                                 {
00271   //  Purpose:  Destructor.
00272   //
00273   //  Arguments: none.
00274   //
00275   //  Return:  none.
00276   //
00277   //  Contact:   S. Kasahara
00278   //
00279   
00280   // Close all managed files and retrieve allocated memory
00281   CloseAll();
00282 
00283 }

PerFileManager::PerFileManager (  )  [protected]

Definition at line 252 of file PerFileManager.cxx.

Referenced by Instance().

00252                                : fErrorCode(Per::kErrSuccess) {
00253   // Purpose: Default constructor.
00254   //
00255   // Arguments: none.
00256   //
00257   // Return: none.
00258   //
00259   // Contact:  S. Kasahara
00260   //
00261   // Notes: Not called directly, use PerFileManager::Instance() instead.
00262 
00263   // Initialize environment variable to force use of TNetFile in TFile::Open 
00264   // when path to file is prefaced by "root(s):", even when file path host
00265   // indicates a local file.
00266   gEnv -> SetValue("TFile.ForceRemote",1);
00267 
00268 }

PerFileManager::PerFileManager ( PerFileManager  )  [inline, private]

Definition at line 64 of file PerFileManager.h.

00064 { abort(); }


Member Function Documentation

const PerFile * PerFileManager::ChangeFile ( std::string  oldfilepathname,
std::string  newfilepathname,
TFile *  newfileptr 
)

Definition at line 41 of file PerFileManager.cxx.

References Msg::kWarning, and MSG.

Referenced by PerOutputStream::ChangeFile().

00042                                                                             {
00043   //  Purpose:  This method is useful when TTree::Fill has closed the current
00044   //            file and opened a new one with extension _1 because it has
00045   //            detected that the file limit of 2 GByte is near.  Its purpose
00046   //            is to swap the new file name and file pointer for the old
00047   //            filename and pointer in its file map, without actually
00048   //            closing the old file or opening the new file, since this
00049   //            has already been done by root.
00050   //
00051   //  Arguments:oldfilepathname  full file path name of old file.
00052   //            newfilepathname  full file path name of new file.
00053   //            newfileptr       pointer to new file opened by root.
00054   //
00055   //  Return:  pointer to new PerFile.
00056   //
00057   //  Contact:   S. Kasahara
00058   //
00059   //  Notes: Caution: this method is only meant to be used under the
00060   //         special circumstances described above. 
00061   //
00062 
00063   FileMapItr itr = fFileMap.find(oldfilepathname);
00064 
00065   if ( itr == fFileMap.end() ) {
00066     // Nothing to be done if conversion already made
00067     FileMapItr newItr = fFileMap.find(newfilepathname);
00068     if ( newItr == fFileMap.end() ) {
00069       MSG("Per",Msg::kWarning) << "ChangeFile unable to find old file "
00070          << oldfilepathname << " or new file " << newfilepathname 
00071                                << " in file map." << endl; 
00072       return (PerFile*)0;
00073     }
00074     else return itr->second;
00075   }
00076 
00077   // Else convert PerFile attributes and replace entry in map
00078   PerFile* perFile = itr->second;
00079   perFile -> ChangeFile(newfilepathname,newfileptr);
00080 
00081   fFileMap.erase(itr);  // erase old entry
00082   fFileMap[newfilepathname] = perFile;  // replace it with new
00083 
00084   return perFile;
00085 
00086 }

void PerFileManager::CloseAll (  ) 

Definition at line 88 of file PerFileManager.cxx.

References fFileMap, and Munits::second.

Referenced by ~PerFileManager().

00088                               {
00089   //  Purpose:  Close all files managed by PerFileManager.
00090   //
00091   //  Argument: none.
00092   //
00093   //  Return:  none.
00094   //
00095   //  Contact:   S. Kasahara
00096   //
00097   //  Notes: Files are closed regardless of remaining number of clients.
00098   //
00099 
00100 
00101   for (FileMapItr itr = fFileMap.begin(); itr != fFileMap.end(); ++itr) {
00102     PerFile *file = itr -> second;
00103     file -> Close(true); // close file 
00104     delete file; // retrieve file memory
00105   }
00106   fFileMap.clear(); // clear all map entries
00107 
00108 }

void PerFileManager::CloseFile ( string  fullfilepathname  ) 

Definition at line 110 of file PerFileManager.cxx.

References fFileMap, Msg::kInfo, MSG, and Munits::second.

Referenced by IoFileListItem::BuildDefStreamList(), and PerStream::CloseFile().

00110                                                       {
00111   //  Purpose:  Close file with name fullfilepathname.  If file has more
00112   //            than one client, number of clients is just decremented by
00113   //            one and the file is left open.
00114   //
00115   //  Argument: fullfilepathname string containing name of file of interest.
00116   //
00117   //  Return:  none
00118   //
00119   //  Contact:   S. Kasahara
00120   //
00121 
00122   FileMapItr itr = fFileMap.find(fullfilepathname);
00123   if (itr != fFileMap.end()) {
00124     UInt_t clientsleft = (itr -> second) -> Close();
00125     if(clientsleft <= 0) {
00126       // Retrieve file memory and clear entry from map 
00127       delete (itr->second); 
00128       fFileMap.erase(itr);
00129     }
00130   }
00131   else {
00132     MSG("Per",Msg::kInfo) << "Unable to close file " << fullfilepathname <<
00133       ". File was not opened by PerFileManager. " << endl;
00134   }
00135   
00136 }

Per::EErrorCode PerFileManager::GetErrorCode (  )  const [inline]

Definition at line 45 of file PerFileManager.h.

References fErrorCode.

Referenced by OpenFile(), and PerInputStream::SetFile().

00045 {return fErrorCode;}

UInt_t PerFileManager::GetNumFile (  )  const [inline]

Definition at line 44 of file PerFileManager.h.

References fFileMap.

Referenced by Print().

00044 {return fFileMap.size();}

const PerFile * PerFileManager::GetOpenedFile ( string  fullfilepathname  )  const

Definition at line 139 of file PerFileManager.cxx.

References fFileMap, and Munits::second.

Referenced by DDSChildServer::Next(), and PerInputStream::UpdateTree().

00139                                                                           {
00140   //  Purpose:  Retrieve file corresponding to fullfilepathname.
00141   //            File must have been previously opened with OpenFile method.
00142   //
00143   //  Argument: fullfilepathname string containing name of file of interest.
00144   //
00145   //  Return:  pointer to a constant PerFile. If file member with name
00146   //           fullfilepathname does not exist, returns (PerFile*)0.  
00147   //
00148   //  Contact:   S. Kasahara
00149   //
00150   //  Notes: User may not modify PerFile data directly.  All
00151   //         modifications should be handled through the PerFileManager. 
00152 
00153   FileMapConstItr citr = fFileMap.find(fullfilepathname);
00154   return (citr != fFileMap.end()) ? citr -> second : (PerFile*)0;
00155 
00156 }

PerFileManager & PerFileManager::Instance (  )  [static]

Definition at line 158 of file PerFileManager.cxx.

References fgInstance, PerFileManager(), and PerFileManager::Cleaner::UseMe().

Referenced by IoFileListItem::BuildDefStreamList(), PerOutputStream::ChangeFile(), PerStream::CloseFile(), DDSChildServer::Next(), PerInputStream::SetFile(), and PerInputStream::UpdateTree().

00158                                          {
00159   //  Purpose:  Return unique instance of PerFileManager, creating it
00160   //            if necessary.
00161   //
00162   //  Arguments: none.
00163   //
00164   //  Return:  reference to PerFileManager.
00165   //
00166   //  Contact:   S. Kasahara
00167   //
00168 
00169   static Cleaner cleaner; // Cleaner destructor provides cleanup of singleton
00170   if (fgInstance == 0) {
00171     cleaner.UseMe(); // dummy call to quiet compiler warnings
00172     fgInstance = new PerFileManager();
00173   }
00174   return *fgInstance;
00175 
00176 }

const PerFile * PerFileManager::OpenFile ( string  fullfilepathname,
Per::EAccessMode  accessmode 
)

Definition at line 179 of file PerFileManager.cxx.

References fErrorCode, fFileMap, GetErrorCode(), Per::kErrFileConflict, Per::kErrFileError, and Per::kErrSuccess.

Referenced by IoFileListItem::BuildDefStreamList(), and PerInputStream::SetFile().

00179                                                                                           {
00180   //  Purpose:  Open file with name fullfilepathname in specified accessmode.
00181   //            If file is already open, adds a new client to this file if
00182   //            new accessmode is compatible with the accessmode in which the
00183   //            file was initially opened.
00184   //
00185   //  Arguments:fullfilepathname string containing full file path name of
00186   //                             file to be opened.  File may be local
00187   //                             or remote.  See PerFile::OpenFile
00188   //                             for information about the remote filename
00189   //                             format.
00190   //            accessmode       EAccessMode in which file is to be opened
00191   //                             (defined in Per.h).
00192   //                             kRead = open file in read only mode.
00193   //                             kNew = open new file in read/write. If file
00194   //                                    already exists, do not overwrite.
00195   //                             kRecreate = same as kNew, but if file already
00196   //                                         exists overwrite.
00197   //                             kUpdate = open existing file in read/write.
00198   //                                
00199   //
00200   //  Return:  pointer to a constant PerFile. If unable to open
00201   //           file, or if requested accessmode is incompatible with the
00202   //           accessmode in which the file was originally opened, 
00203   //           returns (PerFile*)0.
00204   //
00205   //  Contact:   S. Kasahara
00206   //
00207   //  Notes: User may not modify PerFile data directly.  All
00208   //         modifications should be handled through the PerFileManager.
00209   //         See PerFile::OpenFile for information about specifying
00210   //         the authentication information required for remote access.
00211   //         If file fails to open, PerFileManager::GetErrorCode can
00212   //         be used to determine source of failure.
00213 
00214   bool openok = false;
00215 
00216   fErrorCode = Per::kErrSuccess;
00217 
00218   PerFile* file = fFileMap[fullfilepathname];
00219   if (!file) {
00220     // File not found in map, need to create new one
00221     file = new PerFile(fullfilepathname,accessmode);
00222     fFileMap[fullfilepathname] = file;
00223     if(!file || !file -> IsOpen()) {
00224       fErrorCode = Per::kErrFileError;
00225       if (file) {
00226         fErrorCode = file -> GetErrorCode();
00227         delete file; file = 0; // retrieve memory
00228       }
00229 #ifndef IRIX6
00230       fFileMap.erase(fullfilepathname); // erase entry from map
00231 #else
00232       // erase entry from map -- SGI barfs at fFileMap.erase(fullfilepathname);
00233       FileMapItr itr = fFileMap.find(fullfilepathname);
00234       fFileMap.erase(itr);
00235 #endif
00236     }
00237     else {
00238       openok = true;
00239     }  
00240   }
00241   else {
00242     // File has already been opened, attempt to add new client if 
00243     // accessmodes are compatible
00244     openok = file -> AddNewClient(accessmode);
00245     if (!openok) fErrorCode = Per::kErrFileConflict;
00246   }
00247 
00248   return (openok) ? file : (PerFile*)0;
00249 
00250 }

PerFileManager& PerFileManager::operator= ( const PerFileManager  )  [inline, private]

Definition at line 65 of file PerFileManager.h.

00066       { abort(); return *this; }

std::ostream & PerFileManager::Print ( std::ostream &  s  )  const

Definition at line 285 of file PerFileManager.cxx.

References fFileMap, GetNumFile(), and Munits::second.

Referenced by operator<<().

00285                                                       {
00286   //  Purpose:  Print current map of files maintained by file manager on
00287   //            ostream.
00288   //
00289   //  Arguments: ms ostream to display on.
00290   //
00291   //  Return:  ostream reference.
00292   //
00293   //  Contact:   S. Kasahara
00294   //
00295 
00296   ms << "Number of files currently open = " << GetNumFile() << "." << endl;
00297 
00298   int ifile=0;
00299   for (FileMapConstItr itr = fFileMap.begin(); itr != fFileMap.end(); ++itr) {
00300     ms << ++ifile << ")" << itr -> second;
00301   }
00302 
00303   return ms;
00304 }


Friends And Related Function Documentation

friend struct Cleaner [friend]

Definition at line 80 of file PerFileManager.h.


Member Data Documentation

Per::EErrorCode PerFileManager::fErrorCode [private]

Definition at line 82 of file PerFileManager.h.

Referenced by GetErrorCode(), and OpenFile().

map<string,PerFile*> PerFileManager::fFileMap [private]

Definition at line 69 of file PerFileManager.h.

Referenced by CloseAll(), CloseFile(), GetNumFile(), GetOpenedFile(), OpenFile(), and Print().

PerFileManager * PerFileManager::fgInstance = 0 [static, private]

Definition at line 81 of file PerFileManager.h.

Referenced by Instance(), and PerFileManager::Cleaner::~Cleaner().


The documentation for this class was generated from the following files:
Generated on Fri Oct 10 22:46:18 2014 for loon by  doxygen 1.4.7