MultiFile.h

Go to the documentation of this file.
00001 //# MultiFile.h: Class to combine multiple files in a single one
00002 //# Copyright (C) 2014
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: RegularFileIO.h 20551 2009-03-25 00:11:33Z Malte.Marquarding $
00027 
00028 #ifndef CASA_MULTIFILE_H
00029 #define CASA_MULTIFILE_H
00030 
00031 //# Includes
00032 #include <casacore/casa/aips.h>
00033 #include <casacore/casa/IO/MultiFileBase.h>
00034 #include <casacore/casa/IO/FiledesIO.h>
00035 
00036 
00037 namespace casacore { //# NAMESPACE CASACORE - BEGIN
00038 
00039   // <summary> 
00040   // Class to combine multiple files in a single one.
00041   // </summary>
00042 
00043   // <use visibility=export>
00044 
00045   // <reviewed reviewer="" date="" tests="tMultiFile" demos="">
00046   // </reviewed>
00047 
00048   // <synopsis> 
00049   // This class is a container file holding multiple virtual files. It is
00050   // primarily meant as a container file for the storage manager files of a
00051   // table to reduce the number of files used (especially for Lustre) and to
00052   // reduce the number of open files (especially when concatenating tables).
00053   // <br>A secondary goal is offering the ability to use an IO buffer size
00054   // that matches the file system well (large buffer size for e.g. ZFS).
00055   //
00056   // The SetupNewTable constructor has a StorageOption argument to define
00057   // if a MultiFile has to be used and if so, the buffer size to use.
00058   // It is also possible to specify that through aipsrc variables.
00059   //
00060 
00061   // A virtual file is spread over multiple (fixed size) data blocks in the
00062   // MultiFile. A data block is never shared by multiple files.
00063   // For each virtual file MultiFile keeps a MultiFileInfo object telling
00064   // the file size and the blocks numbers used for the file. When flushing
00065   // the MultiFile, this meta info is written into the header block. If it
00066   // does not fit in the header block, the rest is written in a separate "-ext"
00067   // file.
00068   // if needed, continuation blocks. On open and resync, it is read back.
00069   // <br>
00070   //
00071   // A virtual file is represented by an MFFileIO object, which is derived
00072   // from ByteIO and as such part of the casacore IO framework. It makes it
00073   // possible for applications to access a virtual file in the same way as
00074   // a regular file.
00075   //
00076   // It is possible to delete a virtual file. Its blocks will be added to
00077   // the free block list (which is also stored in the meta info).
00078   // </synopsis>
00079 
00080   // <example>
00081   // In principle it is possible to use the MultiFile functions directly.
00082   // However, in general it is much easier to use an MFFileIO object
00083   // per virtual file as shown below.
00084   // <srcblock>
00085   //    // Create a new MultiFile using a block size of 1 MB.
00086   //    MultiFile mfile("file.mf', ByteIO::New, 1048576);
00087   //    // Create a virtual file in it.
00088   //    MFFileIO mf1(mfile, "mf1", ByteIO::New);
00089   //    // Use it (for example) as the sink of AipsIO.
00090   //    AipsIO stream (&mf1);
00091   //    // Write values.
00092   //    stream << (Int)10;
00093   //    stream << True;
00094   //    // Seek to beginning of file and read data in.
00095   //    stream.setpos (0);
00096   //    Int vali;
00097   //    Bool valb;
00098   //    stream >> vali >> valb;
00099   // </srcblock>
00100   // </example>
00101 
00102   // <todo>
00103   //  <li> write headers at alternating file positions (for robustness)
00104   //  <li> possibly write headers entirely at the end if larger than blocksize
00105   // </todo>
00106 
00107 
00108   class MultiFile: public MultiFileBase
00109   {
00110   public:
00111     // Open or create a MultiFile with the given name.
00112     // Upon creation the block size can be given. If 0, it uses the block size
00113     // of the file system the file is on.
00114     MultiFile (const String& name, ByteIO::OpenOption, Int blockSize=0);
00115 
00116     // The destructor flushes and closes the file.
00117     virtual ~MultiFile();
00118 
00119     // Reopen the underlying file for read/write access.
00120     // Nothing will be done if the file is writable already.
00121     // Otherwise it will be reopened and an exception will be thrown
00122     // if it is not possible to reopen it for read/write access.
00123     virtual void reopenRW();
00124 
00125     // Fsync the file (i.e., force the data to be physically written).
00126     virtual void fsync();
00127 
00128   private:
00129     // Do the class-specific actions on adding a file.
00130     virtual void doAddFile (MultiFileInfo&);
00131     // Do the class-specific actions on deleting a file.
00132     virtual void doDeleteFile (MultiFileInfo&);
00133     // Flush the file itself.
00134     virtual void flushFile();
00135     // Flush and close the file.
00136     virtual void close();
00137     // Write the header info.
00138     virtual void writeHeader();
00139     // Read the header info. If always==False, the info is only read if the
00140     // header counter has changed.
00141     virtual void readHeader (Bool always=True);
00142     // Extend the virtual file to fit lastblk.
00143     virtual void extend (MultiFileInfo& info, Int64 lastblk);
00144     // Write a data block.
00145     virtual void writeBlock (MultiFileInfo& info, Int64 blknr,
00146                              const void* buffer);
00147     // Read a data block.
00148     virtual void readBlock (MultiFileInfo& info, Int64 blknr,
00149                             void* buffer);
00150 
00151   private:
00152     //# Data members
00153     FiledesIO itsIO;
00154     int       itsFD;
00155   };
00156 
00157 
00158 } //# NAMESPACE CASACORE - END
00159 
00160 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated on 31 Aug 2016 for casa by  doxygen 1.6.1