casa  5.7.0-16
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
StArrayFile.h
Go to the documentation of this file.
1 //# StArrayFile.h: Read/write array in external format for a storage manager
2 //# Copyright (C) 1994,1995,1996,1997,1999,2001,2002
3 //# Associated Universities, Inc. Washington DC, USA.
4 //#
5 //# This library is free software; you can redistribute it and/or modify it
6 //# under the terms of the GNU Library General Public License as published by
7 //# the Free Software Foundation; either version 2 of the License, or (at your
8 //# option) any later version.
9 //#
10 //# This library is distributed in the hope that it will be useful, but WITHOUT
11 //# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 //# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
13 //# License for more details.
14 //#
15 //# You should have received a copy of the GNU Library General Public License
16 //# along with this library; if not, write to the Free Software Foundation,
17 //# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
18 //#
19 //# Correspondence concerning AIPS++ should be addressed as follows:
20 //# Internet email: aips2-request@nrao.edu.
21 //# Postal address: AIPS++ Project Office
22 //# National Radio Astronomy Observatory
23 //# 520 Edgemont Road
24 //# Charlottesville, VA 22903-2475 USA
25 //#
26 //# $Id$
27 
28 #ifndef TABLES_STARRAYFILE_H
29 #define TABLES_STARRAYFILE_H
30 
31 //# Includes
32 #include <casacore/casa/aips.h>
37 
38 namespace casacore { //# NAMESPACE CASACORE - BEGIN
39 
40 //# Forward Declarations
41 class MultiFileBase;
42 class IPosition;
43 
44 
45 // <summary>
46 // Read/write array in external format for a storage manager
47 // </summary>
48 
49 // <use visibility=local>
50 
51 // <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="">
52 // </reviewed>
53 
54 // <prerequisite>
55 //# Classes you should understand before using this one.
56 // <li> ToLocal
57 // <li> FromLocal
58 // </prerequisite>
59 
60 // <etymology>
61 // StManArrayFile is a class used by table storage managers
62 // to store indirect arrays in a file.
63 // </etymology>
64 
65 // <synopsis>
66 // StManArrayFile is for use by the table storage manager, in particular
67 // to read/write indirectly stored arrays.
68 // Instead of holding the data in memory, they are written directly
69 // into a file. It also allows to access a part of an array, which
70 // is needed for the table system to access an array section.
71 // It does not use a cache of its own, but it is relying on the
72 // underlying system routines to cache and buffer adequately.
73 //
74 // This class could in principle also be used for other array purposes,
75 // for example, to implement a paged array class for really huge arrays.
76 //
77 // An StManArrayFile object is connected to one file. It is possible
78 // to hold multiple arrays in the file, each with its own shape.
79 // An array is stored as its shape followed by the actual data
80 // (all in canonical format). An array of strings is written as
81 // an array of offsets pointing to the actual strings.
82 // When a string gets a new value, the new value is written at the
83 // end of the file and the file space with the old value is lost.
84 //
85 // Currently only the basic types are supported, but arbitrary types
86 // could also be supported by writing/reading an element in the normal
87 // way into the AipsIO buffer. It would only require that AipsIO
88 // would contain a function to get its buffers and to restart them.
89 // </synopsis>
90 
91 // <example>
92 // <srcblock>
93 // void writeArray (const Array<Bool>& array)
94 // {
95 // // Construct object and update file StArray.dat.
96 // StManArrayFile arrayFile("StArray.dat, ByteIO::New);
97 // // Reserve space for an array with the given shape and data type.
98 // // This writes the shape at the end of the file and reserves
99 // // space the hold the entire Bool array.
100 // // It fills in the file offset where the shape is stored
101 // // and returns the length of the shape in the file.
102 // Int64 offset;
103 // uInt shapeLength = arrayFile.putShape (array.shape(), offset, static_cast<Bool*>(0));
104 // // Now put the actual array.
105 // // This has to be put at the returned file offset plus the length
106 // // of the shape in the file.
107 // Bool deleteIt;
108 // const Bool* dataPtr = array.getStorage (deleteIt);
109 // arrayFile.put (offset+shapeLength, 0, array.nelements(), dataPtr);
110 // array.freeStorage (dataPtr, deleteIt);
111 // }
112 // </srcblock>
113 // </example>
114 
115 // <motivation>
116 // The AipsIO class was not suitable for indirect table arrays,
117 // because it uses memory to hold the data. Furthermore it is
118 // not possible to access part of the data in AipsIO.
119 // </motivation>
120 
121 // <todo asof="$DATE:$">
122 // <li> implement long double
123 // <li> support arbitrary types
124 // <li> when rewriting a string value, use the current file
125 // space if it fits
126 // </todo>
127 
128 
130 {
131 public:
132 
133  // Construct the object and attach it to the give file.
134  // The OpenOption determines how the file is opened
135  // (e.g. ByteIO::New for a new file).
136  // The buffersize is used to allocate a buffer of a proper size
137  // for the underlying filebuf object (see iostream package).
138  // A bufferSize 0 means using the default size (currently 65536).
140  uInt version=0, Bool bigEndian=True,
141  uInt bufferSize=0,
142  MultiFileBase* mfile=0);
143 
144  // Close the possibly opened file.
145  ~StManArrayFile();
146 
147  // Flush and optionally fsync the data.
148  // It returns True when any data was written since the last flush.
149  Bool flush (Bool fsync);
150 
151  // Reopen the file for read/write access.
152  void reopenRW();
153 
154  // Resync the file (i.e. clear possible cache information).
155  void resync();
156 
157  // Return the current file length (merely a debug tool).
159  { return leng_p; }
160 
161  // Put the array shape and store its file offset into the offset argument.
162  // Reserve file space for the associated array.
163  // The length of the shape part in the file is returned.
164  // The file offset plus the shape length is the starting offset of the
165  // actual array data (which can be used by get and put).
166  // Space is reserved to store the reference count.
167  // <group>
168  uInt putShape (const IPosition& shape, Int64& fileOffset,
169  const Bool* dummy);
170  uInt putShape (const IPosition& shape, Int64& fileOffset,
171  const Char* dummy);
172  uInt putShape (const IPosition& shape, Int64& fileOffset,
173  const uChar* dummy);
174  uInt putShape (const IPosition& shape, Int64& fileOffset,
175  const Short* dummy);
176  uInt putShape (const IPosition& shape, Int64& fileOffset,
177  const uShort* dummy);
178  uInt putShape (const IPosition& shape, Int64& fileOffset,
179  const Int* dummy);
180  uInt putShape (const IPosition& shape, Int64& fileOffset,
181  const uInt* dummy);
182  uInt putShape (const IPosition& shape, Int64& fileOffset,
183  const Int64* dummy);
184  uInt putShape (const IPosition& shape, Int64& fileOffset,
185  const uInt64* dummy);
186  uInt putShape (const IPosition& shape, Int64& fileOffset,
187  const Float* dummy);
188  uInt putShape (const IPosition& shape, Int64& fileOffset,
189  const Double* dummy);
190 //#// uInt putShape (const IPosition& shape, Int64& fileOffset,
191 //#// const long double* dummy);
192  uInt putShape (const IPosition& shape, Int64& fileOffset,
193  const Complex* dummy);
194  uInt putShape (const IPosition& shape, Int64& fileOffset,
195  const DComplex* dummy);
196  uInt putShape (const IPosition& shape, Int64& fileOffset,
197  const String* dummy);
198  // </group>
199 
200  // Get the reference count.
201  uInt getRefCount (Int64 offset);
202 
203  // Put the reference count.
204  // An exception is thrown if a value other than 1 is put for version 0.
205  void putRefCount (uInt refCount, Int64 offset);
206 
207  // Put nr elements at the given file offset and array offset.
208  // The file offset of the first array element is the file offset
209  // of the shape plus the length of the shape in the file.
210  // The array offset is counted in number of elements. It can be
211  // used to put only a (contiguous) section of the array.
212  // <group>
213  void put (Int64 fileOffset, uInt arrayOffset, uInt nr, const Bool*);
214  void put (Int64 fileOffset, uInt arrayOffset, uInt nr, const Char*);
215  void put (Int64 fileOffset, uInt arrayOffset, uInt nr, const uChar*);
216  void put (Int64 fileOffset, uInt arrayOffset, uInt nr, const Short*);
217  void put (Int64 fileOffset, uInt arrayOffset, uInt nr, const uShort*);
218  void put (Int64 fileOffset, uInt arrayOffset, uInt nr, const Int*);
219  void put (Int64 fileOffset, uInt arrayOffset, uInt nr, const uInt*);
220  void put (Int64 fileOffset, uInt arrayOffset, uInt nr, const Int64*);
221  void put (Int64 fileOffset, uInt arrayOffset, uInt nr, const uInt64*);
222  void put (Int64 fileOffset, uInt arrayOffset, uInt nr, const Float*);
223  void put (Int64 fileOffset, uInt arrayOffset, uInt nr, const Double*);
224 //#// void put (Int64 fileOffset, uInt arrayOffset, uInt nr, const long double*);
225  void put (Int64 fileOffset, uInt arrayOffset, uInt nr, const Complex*);
226  void put (Int64 fileOffset, uInt arrayOffset, uInt nr, const DComplex*);
227  void put (Int64 fileOffset, uInt arrayOffset, uInt nr, const String*);
228  // </group>
229 
230  // Get the shape at the given file offset.
231  // It will reshape the IPosition vector when needed.
232  // It returns the length of the shape in the file.
233  uInt getShape (Int64 fileOffset, IPosition& shape);
234 
235  // Get nr elements at the given file offset and array offset.
236  // The file offset of the first array element is the file offset
237  // of the shape plus the length of the shape in the file.
238  // The array offset is counted in number of elements. It can be
239  // used to get only a (contiguous) section of the array.
240  // <group>
241  void get (Int64 fileOffset, uInt arrayOffset, uInt nr, Bool*);
242  void get (Int64 fileOffset, uInt arrayOffset, uInt nr, Char*);
243  void get (Int64 fileOffset, uInt arrayOffset, uInt nr, uChar*);
244  void get (Int64 fileOffset, uInt arrayOffset, uInt nr, Short*);
245  void get (Int64 fileOffset, uInt arrayOffset, uInt nr, uShort*);
246  void get (Int64 fileOffset, uInt arrayOffset, uInt nr, Int*);
247  void get (Int64 fileOffset, uInt arrayOffset, uInt nr, uInt*);
248  void get (Int64 fileOffset, uInt arrayOffset, uInt nr, Int64*);
249  void get (Int64 fileOffset, uInt arrayOffset, uInt nr, uInt64*);
250  void get (Int64 fileOffset, uInt arrayOffset, uInt nr, Float*);
251  void get (Int64 fileOffset, uInt arrayOffset, uInt nr, Double*);
252 //#// void get (Int64 fileOffset, uInt arrayOffset, uInt nr, long double*);
253  void get (Int64 fileOffset, uInt arrayOffset, uInt nr, Complex*);
254  void get (Int64 fileOffset, uInt arrayOffset, uInt nr, DComplex*);
255  void get (Int64 fileOffset, uInt arrayOffset, uInt nr, String*);
256  // </group>
257 
258  // Copy the array with <src>nr</src> elements from one file offset
259  // to another.
260  // <group>
261  void copyArrayBool (Int64 to, Int64 from, uInt nr);
262  void copyArrayChar (Int64 to, Int64 from, uInt nr);
263  void copyArrayuChar (Int64 to, Int64 from, uInt nr);
264  void copyArrayShort (Int64 to, Int64 from, uInt nr);
265  void copyArrayuShort (Int64 to, Int64 from, uInt nr);
266  void copyArrayInt (Int64 to, Int64 from, uInt nr);
267  void copyArrayuInt (Int64 to, Int64 from, uInt nr);
268  void copyArrayInt64 (Int64 to, Int64 from, uInt nr);
269  void copyArrayuInt64 (Int64 to, Int64 from, uInt nr);
270  void copyArrayFloat (Int64 to, Int64 from, uInt nr);
271  void copyArrayDouble (Int64 to, Int64 from, uInt nr);
272 //#// void copyArrayLDouble (Int64 to, Int64 from, uInt nr);
273  void copyArrayComplex (Int64 to, Int64 from, uInt nr);
274  void copyArrayDComplex (Int64 to, Int64 from, uInt nr);
275  void copyArrayString (Int64 to, Int64 from, uInt nr);
276  // </group>
277 
278 private:
279  ByteIO* file_p; //# File object
280  TypeIO* iofil_p; //# IO object
281  Int64 leng_p; //# File length
282  uInt version_p; //# Version of StArrayFile file
283  Bool swput_p; //# True = put is possible
284  Bool hasPut_p; //# True = put since last flush
295 
296  // Put a single value at the current file offset.
297  // It returns the length of the value in the file.
298  // <group>
299  uInt put (const Int&);
300  uInt put (const uInt&);
301  // </group>
302 
303  // Put the array shape at the end of the file and reserve
304  // space for nr elements (each lenElem bytes long).
305  // It fills the file offset of the shape.
306  // It returns the length of the shape in the file.
307  uInt putRes (const IPosition& shape, Int64& fileOffset, float lenElem);
308 
309  // Get a single value at the current file offset.
310  // It returns the length of the value in the file.
311  // <group>
312  uInt get (Int&);
313  uInt get (uInt&);
314  // </group>
315 
316  // Copy data with the given length from one file offset to another.
317  void copyData (Int64 to, Int64 from, uInt length);
318 
319  // Position the file on the given offset.
320  void setpos (Int64 offset);
321 };
322 
323 
325 {
326  file_p->reopenRW();
327 }
329 {
330  hasPut_p = True;
331  return iofil_p->write (1, &value);
332 }
334 {
335  hasPut_p = True;
336  return iofil_p->write (1, &value);
337 }
339 {
340  return iofil_p->read (1, &value);
341 }
343 {
344  return iofil_p->read (1, &value);
345 }
346 
347 
348 
349 } //# NAMESPACE CASACORE - END
350 
351 #endif
void copyArrayComplex(Int64 to, Int64 from, uInt nr)
A Vector of integers, for indexing into Array&lt;T&gt; objects.
Definition: IPosition.h:119
long long Int64
Define the extra non-standard types used by Casacore (like proposed uSize, Size)
Definition: aipsxtype.h:38
void copyArrayBool(Int64 to, Int64 from, uInt nr)
Copy the array with nr elements from one file offset to another.
uInt getRefCount(Int64 offset)
Get the reference count.
int Int
Definition: aipstype.h:50
void copyArrayString(Int64 to, Int64 from, uInt nr)
~StManArrayFile()
Close the possibly opened file.
Abstract base class to combine multiple files in a single one.
Int64 length()
Return the current file length (merely a debug tool).
Definition: StArrayFile.h:158
void copyArrayuChar(Int64 to, Int64 from, uInt nr)
unsigned long long uInt64
Definition: aipsxtype.h:39
unsigned char uChar
Definition: aipstype.h:47
void put(Int64 fileOffset, uInt arrayOffset, uInt nr, const Bool *)
Put nr elements at the given file offset and array offset.
char Char
Definition: aipstype.h:46
void copyArrayInt(Int64 to, Int64 from, uInt nr)
ABSTRACT CLASSES Abstract class for colors Any implementation of color should be able to provide a hexadecimal form of the if a human readable name(i.e."black").In many places throughout the plotter
virtual size_t read(size_t nvalues, Bool *value)
Read the values from the ByteIO object and convert them.
void putRefCount(uInt refCount, Int64 offset)
Put the reference count.
short Short
Definition: aipstype.h:48
void copyArrayuInt(Int64 to, Int64 from, uInt nr)
void copyArrayChar(Int64 to, Int64 from, uInt nr)
void resync()
Resync the file (i.e.
void copyArrayuInt64(Int64 to, Int64 from, uInt nr)
virtual void reopenRW()
Reopen the underlying IO stream for read/write access.
uInt putRes(const IPosition &shape, Int64 &fileOffset, float lenElem)
Put the array shape at the end of the file and reserve space for nr elements (each lenElem bytes long...
uInt putShape(const IPosition &shape, Int64 &fileOffset, const Bool *dummy)
Put the array shape and store its file offset into the offset argument.
double Double
Definition: aipstype.h:55
Abstract base class for IO on a byte stream.
Definition: ByteIO.h:61
void copyArrayFloat(Int64 to, Int64 from, uInt nr)
Abstract base class for IO of data in a type-dependent format.
Definition: TypeIO.h:79
void copyArrayDouble(Int64 to, Int64 from, uInt nr)
virtual size_t write(size_t nvalues, const Bool *value)
Convert the values and write them to the ByteIO object.
void get(Int64 fileOffset, uInt arrayOffset, uInt nr, Bool *)
Get nr elements at the given file offset and array offset.
bool Bool
Define the standard types used by Casacore.
Definition: aipstype.h:42
float Float
Definition: aipstype.h:54
void reopenRW()
Reopen the file for read/write access.
Definition: StArrayFile.h:324
TableExprNode shape(const TableExprNode &array)
Function operating on any scalar or array resulting in a Double array containing the shape...
Definition: ExprNode.h:1944
void copyArrayShort(Int64 to, Int64 from, uInt nr)
OpenOption
Define the possible ByteIO open options.
Definition: ByteIO.h:65
StManArrayFile(const String &name, ByteIO::OpenOption, uInt version=0, Bool bigEndian=True, uInt bufferSize=0, MultiFileBase *mfile=0)
Construct the object and attach it to the give file.
void setpos(Int64 offset)
Position the file on the given offset.
uInt getShape(Int64 fileOffset, IPosition &shape)
Get the shape at the given file offset.
String: the storage and methods of handling collections of characters.
Definition: String.h:223
Bool flush(Bool fsync)
Flush and optionally fsync the data.
void copyArrayInt64(Int64 to, Int64 from, uInt nr)
Read/write array in external format for a storage manager.
Definition: StArrayFile.h:129
const Bool True
Definition: aipstype.h:43
LatticeExprNode value(const LatticeExprNode &expr)
This function returns the value of the expression without a mask.
void copyData(Int64 to, Int64 from, uInt length)
Copy data with the given length from one file offset to another.
void copyArrayDComplex(Int64 to, Int64 from, uInt nr)
unsigned int uInt
Definition: aipstype.h:51
void copyArrayuShort(Int64 to, Int64 from, uInt nr)
unsigned short uShort
Definition: aipstype.h:49
#define casacore
&lt;X11/Intrinsic.h&gt; #defines true, false, casacore::Bool, and String.
Definition: X11Intrinsic.h:42