StArrayFile.h

Go to the documentation of this file.
00001 //# StArrayFile.h: Read/write array in external format for a storage manager
00002 //# Copyright (C) 1994,1995,1996,1997,1999,2001,2002
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$
00027 
00028 #ifndef TABLES_STARRAYFILE_H
00029 #define TABLES_STARRAYFILE_H
00030 
00031 //# Includes
00032 #include <casacore/casa/aips.h>
00033 #include <casacore/casa/IO/RegularFileIO.h>
00034 #include <casacore/casa/IO/TypeIO.h>
00035 #include <casacore/casa/BasicSL/String.h>
00036 #include <casacore/casa/BasicSL/Complex.h>
00037 
00038 namespace casacore { //# NAMESPACE CASACORE - BEGIN
00039 
00040 //# Forward Declarations
00041 class MultiFileBase;
00042 class IPosition;
00043 
00044 
00045 // <summary>
00046 // Read/write array in external format for a storage manager
00047 // </summary>
00048 
00049 // <use visibility=local>
00050 
00051 // <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="">
00052 // </reviewed>
00053 
00054 // <prerequisite>
00055 //# Classes you should understand before using this one.
00056 //   <li> ToLocal
00057 //   <li> FromLocal
00058 // </prerequisite>
00059 
00060 // <etymology>
00061 // StManArrayFile is a class used by table storage managers
00062 // to store indirect arrays in a file.
00063 // </etymology>
00064 
00065 // <synopsis> 
00066 // StManArrayFile is for use by the table storage manager, in particular
00067 // to read/write indirectly stored arrays.
00068 // Instead of holding the data in memory, they are written directly
00069 // into a file. It also allows to access a part of an array, which
00070 // is needed for the table system to access an array section.
00071 // It does not use a cache of its own, but it is relying on the
00072 // underlying system routines to cache and buffer adequately.
00073 //
00074 // This class could in principle also be used for other array purposes,
00075 // for example, to implement a paged array class for really huge arrays.
00076 //
00077 // An StManArrayFile object is connected to one file. It is possible
00078 // to hold multiple arrays in the file, each with its own shape.
00079 // An array is stored as its shape followed by the actual data
00080 // (all in canonical format). An array of strings is written as
00081 // an array of offsets pointing to the actual strings.
00082 // When a string gets a new value, the new value is written at the
00083 // end of the file and the file space with the old value is lost.
00084 //
00085 // Currently only the basic types are supported, but arbitrary types
00086 // could also be supported by writing/reading an element in the normal
00087 // way into the AipsIO buffer. It would only require that AipsIO
00088 // would contain a function to get its buffers and to restart them.
00089 // </synopsis> 
00090 
00091 // <example>
00092 // <srcblock>
00093 // void writeArray (const Array<Bool>& array)
00094 // {
00095 //     // Construct object and update file StArray.dat.
00096 //     StManArrayFile arrayFile("StArray.dat, ByteIO::New);
00097 //     // Reserve space for an array with the given shape and data type.
00098 //     // This writes the shape at the end of the file and reserves
00099 //     // space the hold the entire Bool array.
00100 //     // It fills in the file offset where the shape is stored
00101 //     // and returns the length of the shape in the file.
00102 //     Int64 offset;
00103 //     uInt shapeLength = arrayFile.putShape (array.shape(), offset, static_cast<Bool*>(0));
00104 //     // Now put the actual array.
00105 //     // This has to be put at the returned file offset plus the length
00106 //     // of the shape in the file.
00107 //     Bool deleteIt;
00108 //     const Bool* dataPtr = array.getStorage (deleteIt);
00109 //     arrayFile.put (offset+shapeLength, 0, array.nelements(), dataPtr);
00110 //     array.freeStorage (dataPtr, deleteIt);
00111 // }
00112 // </srcblock>
00113 // </example>
00114 
00115 // <motivation>
00116 // The AipsIO class was not suitable for indirect table arrays,
00117 // because it uses memory to hold the data. Furthermore it is
00118 // not possible to access part of the data in AipsIO.
00119 // </motivation>
00120 
00121 // <todo asof="$DATE:$">
00122 //   <li> implement long double
00123 //   <li> support arbitrary types
00124 //   <li> when rewriting a string value, use the current file
00125 //          space if it fits
00126 // </todo>
00127 
00128 
00129 class StManArrayFile
00130 {
00131 public:
00132 
00133     // Construct the object and attach it to the give file.
00134     // The OpenOption determines how the file is opened
00135     // (e.g. ByteIO::New for a new file).
00136     // The buffersize is used to allocate a buffer of a proper size
00137     // for the underlying filebuf object (see iostream package).
00138     // A bufferSize 0 means using the default size (currently 65536).
00139     StManArrayFile (const String& name, ByteIO::OpenOption,
00140                     uInt version=0, Bool bigEndian=True,
00141                     uInt bufferSize=0,
00142                     MultiFileBase* mfile=0);
00143 
00144     // Close the possibly opened file.
00145     ~StManArrayFile();
00146 
00147     // Flush and optionally fsync the data.
00148     // It returns True when any data was written since the last flush.
00149     Bool flush (Bool fsync);
00150 
00151     // Reopen the file for read/write access.
00152     void reopenRW();
00153 
00154     // Resync the file (i.e. clear possible cache information).
00155     void resync();
00156 
00157     // Return the current file length (merely a debug tool).
00158     Int64 length()
00159         { return leng_p; }
00160 
00161     // Put the array shape and store its file offset into the offset argument.
00162     // Reserve file space for the associated array.
00163     // The length of the shape part in the file is returned.
00164     // The file offset plus the shape length is the starting offset of the
00165     // actual array data (which can be used by get and put).
00166     // Space is reserved to store the reference count.
00167     // <group>
00168     uInt putShape (const IPosition& shape, Int64& fileOffset,
00169                    const Bool* dummy);
00170     uInt putShape (const IPosition& shape, Int64& fileOffset,
00171                    const Char* dummy);
00172     uInt putShape (const IPosition& shape, Int64& fileOffset,
00173                    const uChar* dummy);
00174     uInt putShape (const IPosition& shape, Int64& fileOffset,
00175                    const Short* dummy);
00176     uInt putShape (const IPosition& shape, Int64& fileOffset,
00177                    const uShort* dummy);
00178     uInt putShape (const IPosition& shape, Int64& fileOffset,
00179                    const Int* dummy);
00180     uInt putShape (const IPosition& shape, Int64& fileOffset,
00181                    const uInt* dummy);
00182     uInt putShape (const IPosition& shape, Int64& fileOffset,
00183                    const Int64* dummy);
00184     uInt putShape (const IPosition& shape, Int64& fileOffset,
00185                    const uInt64* dummy);
00186     uInt putShape (const IPosition& shape, Int64& fileOffset,
00187                    const Float* dummy);
00188     uInt putShape (const IPosition& shape, Int64& fileOffset,
00189                    const Double* dummy);
00190 //#//    uInt putShape (const IPosition& shape, Int64& fileOffset,
00191 //#//                   const long double* dummy);
00192     uInt putShape (const IPosition& shape, Int64& fileOffset,
00193                    const Complex* dummy);
00194     uInt putShape (const IPosition& shape, Int64& fileOffset,
00195                    const DComplex* dummy);
00196     uInt putShape (const IPosition& shape, Int64& fileOffset,
00197                    const String* dummy);
00198     // </group>
00199 
00200     // Get the reference count.
00201     uInt getRefCount (Int64 offset);
00202 
00203     // Put the reference count.
00204     // An exception is thrown if a value other than 1 is put for version 0.
00205     void putRefCount (uInt refCount, Int64 offset);
00206 
00207     // Put nr elements at the given file offset and array offset.
00208     // The file offset of the first array element is the file offset
00209     // of the shape plus the length of the shape in the file.
00210     // The array offset is counted in number of elements. It can be
00211     // used to put only a (contiguous) section of the array.
00212     // <group>
00213     void put (Int64 fileOffset, uInt arrayOffset, uInt nr, const Bool*);
00214     void put (Int64 fileOffset, uInt arrayOffset, uInt nr, const Char*);
00215     void put (Int64 fileOffset, uInt arrayOffset, uInt nr, const uChar*);
00216     void put (Int64 fileOffset, uInt arrayOffset, uInt nr, const Short*);
00217     void put (Int64 fileOffset, uInt arrayOffset, uInt nr, const uShort*);
00218     void put (Int64 fileOffset, uInt arrayOffset, uInt nr, const Int*);
00219     void put (Int64 fileOffset, uInt arrayOffset, uInt nr, const uInt*);
00220     void put (Int64 fileOffset, uInt arrayOffset, uInt nr, const Int64*);
00221     void put (Int64 fileOffset, uInt arrayOffset, uInt nr, const uInt64*);
00222     void put (Int64 fileOffset, uInt arrayOffset, uInt nr, const Float*);
00223     void put (Int64 fileOffset, uInt arrayOffset, uInt nr, const Double*);
00224 //#//    void put (Int64 fileOffset, uInt arrayOffset, uInt nr, const long double*);
00225     void put (Int64 fileOffset, uInt arrayOffset, uInt nr, const Complex*);
00226     void put (Int64 fileOffset, uInt arrayOffset, uInt nr, const DComplex*);
00227     void put (Int64 fileOffset, uInt arrayOffset, uInt nr, const String*);
00228     // </group>
00229 
00230     // Get the shape at the given file offset.
00231     // It will reshape the IPosition vector when needed.
00232     // It returns the length of the shape in the file.
00233     uInt getShape (Int64 fileOffset, IPosition& shape);
00234 
00235     // Get nr elements at the given file offset and array offset.
00236     // The file offset of the first array element is the file offset
00237     // of the shape plus the length of the shape in the file.
00238     // The array offset is counted in number of elements. It can be
00239     // used to get only a (contiguous) section of the array.
00240     // <group>
00241     void get (Int64 fileOffset, uInt arrayOffset, uInt nr, Bool*);
00242     void get (Int64 fileOffset, uInt arrayOffset, uInt nr, Char*);
00243     void get (Int64 fileOffset, uInt arrayOffset, uInt nr, uChar*);
00244     void get (Int64 fileOffset, uInt arrayOffset, uInt nr, Short*);
00245     void get (Int64 fileOffset, uInt arrayOffset, uInt nr, uShort*);
00246     void get (Int64 fileOffset, uInt arrayOffset, uInt nr, Int*);
00247     void get (Int64 fileOffset, uInt arrayOffset, uInt nr, uInt*);
00248     void get (Int64 fileOffset, uInt arrayOffset, uInt nr, Int64*);
00249     void get (Int64 fileOffset, uInt arrayOffset, uInt nr, uInt64*);
00250     void get (Int64 fileOffset, uInt arrayOffset, uInt nr, Float*);
00251     void get (Int64 fileOffset, uInt arrayOffset, uInt nr, Double*);
00252 //#//    void get (Int64 fileOffset, uInt arrayOffset, uInt nr, long double*);
00253     void get (Int64 fileOffset, uInt arrayOffset, uInt nr, Complex*);
00254     void get (Int64 fileOffset, uInt arrayOffset, uInt nr, DComplex*);
00255     void get (Int64 fileOffset, uInt arrayOffset, uInt nr, String*);
00256     // </group>
00257 
00258     // Copy the array with <src>nr</src> elements from one file offset
00259     // to another.
00260     // <group>
00261     void copyArrayBool     (Int64 to, Int64 from, uInt nr);
00262     void copyArrayChar     (Int64 to, Int64 from, uInt nr);
00263     void copyArrayuChar    (Int64 to, Int64 from, uInt nr);
00264     void copyArrayShort    (Int64 to, Int64 from, uInt nr);
00265     void copyArrayuShort   (Int64 to, Int64 from, uInt nr);
00266     void copyArrayInt      (Int64 to, Int64 from, uInt nr);
00267     void copyArrayuInt     (Int64 to, Int64 from, uInt nr);
00268     void copyArrayInt64    (Int64 to, Int64 from, uInt nr);
00269     void copyArrayuInt64   (Int64 to, Int64 from, uInt nr);
00270     void copyArrayFloat    (Int64 to, Int64 from, uInt nr);
00271     void copyArrayDouble   (Int64 to, Int64 from, uInt nr);
00272 //#//    void copyArrayLDouble  (Int64 to, Int64 from, uInt nr);
00273     void copyArrayComplex  (Int64 to, Int64 from, uInt nr);
00274     void copyArrayDComplex (Int64 to, Int64 from, uInt nr);
00275     void copyArrayString   (Int64 to, Int64 from, uInt nr);
00276     // </group>
00277 
00278 private:
00279     ByteIO* file_p;                //# File object
00280     TypeIO* iofil_p;               //# IO object
00281     Int64   leng_p;                //# File length
00282     uInt    version_p;             //# Version of StArrayFile file
00283     Bool    swput_p;               //# True = put is possible
00284     Bool    hasPut_p;              //# True = put since last flush
00285     uInt    sizeChar_p;
00286     uInt    sizeuChar_p;
00287     uInt    sizeShort_p;
00288     uInt    sizeuShort_p;
00289     uInt    sizeInt_p;
00290     uInt    sizeuInt_p;
00291     uInt    sizeInt64_p;
00292     uInt    sizeuInt64_p;
00293     uInt    sizeFloat_p;
00294     uInt    sizeDouble_p;
00295 
00296     // Put a single value at the current file offset.
00297     // It returns the length of the value in the file.
00298     // <group>
00299     uInt put (const Int&);
00300     uInt put (const uInt&);
00301     // </group>
00302 
00303     // Put the array shape at the end of the file and reserve
00304     // space for nr elements (each lenElem bytes long).
00305     // It fills the file offset of the shape.
00306     // It returns the length of the shape in the file.
00307     uInt putRes (const IPosition& shape, Int64& fileOffset, float lenElem);
00308 
00309     // Get a single value at the current file offset.
00310     // It returns the length of the value in the file.
00311     // <group>
00312     uInt get (Int&);
00313     uInt get (uInt&);
00314     // </group>
00315 
00316     // Copy data with the given length from one file offset to another.
00317     void copyData (Int64 to, Int64 from, uInt length);
00318 
00319     // Position the file on the given offset.
00320     void setpos (Int64 offset);
00321 };
00322     
00323 
00324 inline void StManArrayFile::reopenRW()
00325 {
00326     file_p->reopenRW();
00327 }
00328 inline uInt StManArrayFile::put (const Int& value)
00329 {
00330     hasPut_p = True;
00331     return iofil_p->write (1, &value);
00332 }
00333 inline uInt StManArrayFile::put (const uInt& value)
00334 {
00335     hasPut_p = True;
00336     return iofil_p->write (1, &value);
00337 }
00338 inline uInt StManArrayFile::get (Int& value)
00339 {
00340     return iofil_p->read (1, &value);
00341 }
00342 inline uInt StManArrayFile::get (uInt& value)
00343 {
00344     return iofil_p->read (1, &value);
00345 }
00346 
00347 
00348 
00349 } //# NAMESPACE CASACORE - END
00350 
00351 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated on 31 Aug 2016 for casa by  doxygen 1.6.1