casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
BucketFile.h
Go to the documentation of this file.
00001 //# BucketFile.h: File object for Tiled hypercube Storage Manager
00002 //# Copyright (C) 1995,1996,1999,2001
00003 //# Associated Universities, Inc. Washington DC, USA.
00004 //#
00005 //# This library is free software; you can redistribute it and/or modify it
00006 //# under the terms of the GNU Library General Public License as published by
00007 //# the Free Software Foundation; either version 2 of the License, or (at your
00008 //# option) any later version.
00009 //#
00010 //# This library is distributed in the hope that it will be useful, but WITHOUT
00011 //# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00012 //# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
00013 //# License for more details.
00014 //#
00015 //# You should have received a copy of the GNU Library General Public License
00016 //# along with this library; if not, write to the Free Software Foundation,
00017 //# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
00018 //#
00019 //# Correspondence concerning AIPS++ should be addressed as follows:
00020 //#        Internet email: aips2-request@nrao.edu.
00021 //#        Postal address: AIPS++ Project Office
00022 //#                        National Radio Astronomy Observatory
00023 //#                        520 Edgemont Road
00024 //#                        Charlottesville, VA 22903-2475 USA
00025 //#
00026 //# $Id: BucketFile.h 20859 2010-02-03 13:14:15Z gervandiepen $
00027 
00028 #ifndef CASA_BUCKETFILE_H
00029 #define CASA_BUCKETFILE_H
00030 
00031 //# Includes
00032 #include <casa/aips.h>
00033 #include <casa/IO/MMapfdIO.h>
00034 #include <casa/IO/LargeFilebufIO.h>
00035 #include <casa/BasicSL/String.h>
00036 #include <unistd.h>
00037 
00038 
00039 namespace casa { //# NAMESPACE CASA - BEGIN
00040 
00041 // Forward Declarations.
00042 class MMapfdIO;
00043 class LargeFilebufIO;
00044 
00045 
00046 // <summary>
00047 // File object for the bucket cache.
00048 // </summary>
00049 
00050 // <use visibility=local>
00051 
00052 // <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="">
00053 // </reviewed>
00054 
00055 //# <prerequisite>
00056 //# Classes you should understand before using this one.
00057 //# </prerequisite>
00058 
00059 // <etymology>
00060 // BucketFile represents a data file for the BucketCache class.
00061 // </etymology>
00062 
00063 // <synopsis>
00064 // A BucketFile object represents a data file. Currently it is used
00065 // by the Table system, but it can easily be turned into
00066 // a more general storage manager file class.
00067 // <br>
00068 // Creation of a BucketFile object does not open the file yet.
00069 // An explicit open call has to be given before the file can be used.
00070 // <p>
00071 // Underneath it uses a file descriptor to access the file.
00072 // It is straightforward to replace this by a mapped file or a filebuf.
00073 // </synopsis> 
00074 
00075 // <motivation>
00076 // Encapsulate the file creation and access into a single class
00077 // to hide the file IO details.
00078 // </motivation>
00079 
00080 // <example>
00081 // <srcblock>
00082 //     // Create the file for the given storage manager.
00083 //     BucketFile file ("file.name");
00084 //     // Open the file and write into it.
00085 //     file.open();
00086 //     file.write (someBuffer, someLength);
00087 //     // Get the length of the file.
00088 //     uInt size = file.fileSize();
00089 // </srcblock>
00090 // </example>
00091 
00092 // <todo asof="$DATE:$">
00093 //  <li> Use the ByteIO classes when they are ready.
00094 // </todo>
00095 
00096 
00097 class BucketFile
00098 {
00099 public:
00100     // Create a BucketFile object for a new file.
00101     // The file with the given name will be created.
00102     // It can be indicated if a MMapfdIO and/or LargeFilebufIO object must be
00103     // created for the file.
00104     explicit BucketFile (const String& fileName,
00105                          uInt bufSizeFile=0, Bool mappedFile=False);
00106 
00107     // Create a BucketFile object for an existing file.
00108     // The file should be opened by the <src>open</src>.
00109     // Tell if the file must be opened writable.
00110     // It can be indicated if a MMapfdIO and/or LargeFilebufIO object must be
00111     // created for the file.
00112     BucketFile (const String& fileName, Bool writable,
00113                 uInt bufSizeFile=0, Bool mappedFile=False);
00114 
00115     // The destructor closes the file (if open).
00116     ~BucketFile();
00117 
00118     // Get the mapped file object.
00119     MMapfdIO* mappedFile()
00120       { return mappedFile_p; }
00121 
00122     // Get the buffered file object.
00123     LargeFilebufIO* bufferedFile()
00124       { return bufferedFile_p; }
00125 
00126     // Open the file if not open yet.
00127     void open();
00128 
00129     // Close the file (if open).
00130     void close();
00131 
00132     // Remove the file (and close it if needed).
00133     void remove();
00134 
00135     // Fsync the file (i.e. force the data to be physically written).
00136     void fsync();
00137 
00138     // Set the file to read/write access. It is reopened if not writable.
00139     // It does nothing if the file is already writable.
00140     void setRW();
00141 
00142     // Get the file name.
00143     const String& name() const;
00144     
00145     // Has the file logically been indicated as writable?
00146     Bool isWritable() const;
00147 
00148     // Read bytes from the file.
00149     uInt read (void* buffer, uInt length) const;
00150 
00151     // Write bytes into the file.
00152     uInt write (const void* buffer, uInt length);
00153 
00154     // Seek in the file.
00155     // <group>
00156     void seek (Int64 offset) const;
00157     void seek (Int offset) const;
00158     // </group>
00159 
00160     // Get the (physical) size of the file.
00161     // This is doing a seek and sets the file pointer to end-of-file.
00162     Int64 fileSize() const;
00163 
00164     // Get the file descriptor of the internal file.
00165     int fd();
00166 
00167     // Is the file cached, mapped, or buffered?
00168     // <group>
00169     Bool isCached() const;
00170     Bool isMapped() const;
00171     Bool isBuffered() const;
00172     // </group>
00173 
00174 private:
00175     // The file name.
00176     String name_p;
00177     // The (logical) writability of the file.
00178     Bool isWritable_p;
00179     Bool isMapped_p;
00180     uInt bufSize_p;
00181     // The file descriptor.
00182     int fd_p;
00183     // The optional mapped file.
00184     MMapfdIO* mappedFile_p;
00185     // The optional buffered file.
00186     LargeFilebufIO* bufferedFile_p;
00187             
00188 
00189     // Forbid copy constructor.
00190     BucketFile (const BucketFile&);
00191 
00192     // Forbid assignment.
00193     BucketFile& operator= (const BucketFile&);
00194 
00195     // Create the mapped or buffered file object.
00196     void createMapBuf();
00197 
00198     // Delete the possible mapped or buffered file object.
00199     void deleteMapBuf();
00200 };
00201 
00202 
00203 inline const String& BucketFile::name() const
00204     { return name_p; }
00205 
00206 inline Bool BucketFile::isWritable() const
00207     { return isWritable_p; }
00208 
00209 inline int BucketFile::fd()
00210     { return fd_p; }
00211 
00212 inline void BucketFile::seek (Int offset) const
00213     { seek (Int64(offset)); }
00214 
00215 inline Bool BucketFile::isCached() const
00216     { return !isMapped_p && bufSize_p==0; }
00217 inline Bool BucketFile::isMapped() const
00218     { return isMapped_p; }
00219 inline Bool BucketFile::isBuffered() const
00220     { return bufSize_p>0; }
00221 
00222 
00223 } //# NAMESPACE CASA - END
00224 
00225 #endif