casacore
Loading...
Searching...
No Matches
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: casa-feedback@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#ifndef TABLES_STARRAYFILE_H
27#define TABLES_STARRAYFILE_H
28
29//# Includes
30#include <casacore/casa/aips.h>
31#include <casacore/casa/IO/RegularFileIO.h>
32#include <casacore/casa/IO/TypeIO.h>
33#include <casacore/casa/BasicSL/String.h>
34#include <casacore/casa/BasicSL/Complex.h>
35#include <memory>
36
37namespace casacore { //# NAMESPACE CASACORE - BEGIN
38
39//# Forward Declarations
40class MultiFileBase;
41class IPosition;
42
43
44// <summary>
45// Read/write array in external format for a storage manager
46// </summary>
47
48// <use visibility=local>
49
50// <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="">
51// </reviewed>
52
53// <prerequisite>
54//# Classes you should understand before using this one.
55// <li> ToLocal
56// <li> FromLocal
57// </prerequisite>
58
59// <etymology>
60// StManArrayFile is a class used by table storage managers
61// to store indirect arrays in a file.
62// </etymology>
63
64// <synopsis>
65// StManArrayFile is for use by the table storage manager, in particular
66// to read/write indirectly stored arrays.
67// Instead of holding the data in memory, they are written directly
68// into a file. It also allows to access a part of an array, which
69// is needed for the table system to access an array section.
70// It does not use a cache of its own, but it is relying on the
71// underlying system routines to cache and buffer adequately.
72//
73// This class could in principle also be used for other array purposes,
74// for example, to implement a paged array class for really huge arrays.
75//
76// An StManArrayFile object is connected to one file. It is possible
77// to hold multiple arrays in the file, each with its own shape.
78// An array is stored as its shape followed by the actual data
79// (all in little or big endian format). An array of strings is written as
80// an array of offsets pointing to the actual strings.
81// When a string gets a new value, the new value is written at the
82// end of the file and the file space with the old value is lost.
83//
84// Currently only the basic types are supported, but arbitrary types
85// could also be supported by writing/reading an element in the normal
86// way into the AipsIO buffer. It would only require that AipsIO
87// would contain a function to get its buffers and to restart them.
88// </synopsis>
89
90// <example>
91// <srcblock>
92// void writeArray (const Array<Bool>& array)
93// {
94// // Construct object and update file StArray.dat.
95// StManArrayFile arrayFile("StArray.dat, ByteIO::New);
96// // Reserve space for an array with the given shape and data type.
97// // This writes the shape at the end of the file and reserves
98// // space the hold the entire Bool array.
99// // It fills in the file offset where the shape is stored
100// // and returns the length of the shape in the file.
101// Int64 offset;
102// uInt shapeLength = arrayFile.putShape (array.shape(), offset, static_cast<Bool*>(0));
103// // Now put the actual array.
104// // This has to be put at the returned file offset plus the length
105// // of the shape in the file.
106// Bool deleteIt;
107// const Bool* dataPtr = array.getStorage (deleteIt);
108// arrayFile.put (offset+shapeLength, 0, array.nelements(), dataPtr);
109// array.freeStorage (dataPtr, deleteIt);
110// }
111// </srcblock>
112// </example>
113
114// <motivation>
115// The AipsIO class was not suitable for indirect table arrays,
116// because it uses memory to hold the data. Furthermore it is
117// not possible to access part of the data in AipsIO.
118// </motivation>
119
120// <todo asof="$DATE:$">
121// <li> implement long double
122// <li> support arbitrary types
123// <li> when rewriting a string value, use the current file
124// space if it fits
125// </todo>
126
127
129{
130public:
131
132 // Construct the object and attach it to the give file.
133 // The OpenOption determines how the file is opened
134 // (e.g. ByteIO::New for a new file).
135 // The buffersize is used to allocate a buffer of a proper size
136 // for the underlying filebuf object (see iostream package).
137 // A bufferSize 0 means using the default size (currently 65536).
139 uInt version=0, Bool bigEndian=True,
140 uInt bufferSize=0,
141 const std::shared_ptr<MultiFileBase>& = std::shared_ptr<MultiFileBase>());
142
143 // Close the possibly opened file.
145
146 // Flush and optionally fsync the data.
147 // It returns True when any data was written since the last flush.
148 Bool flush (Bool fsync);
149
150 // Reopen the file for read/write access.
151 void reopenRW();
152
153 // Resync the file (i.e. clear possible cache information).
154 void resync();
155
156 // Return the current file length (merely a debug tool).
158 { return leng_p; }
159
160 // Put the array shape and store its file offset into the offset argument.
161 // Reserve file space for the associated array.
162 // The length of the shape part in the file is returned.
163 // The file offset plus the shape length is the starting offset of the
164 // actual array data (which can be used by get and put).
165 // Space is reserved to store the reference count.
166 // <group>
167 uInt putShape (const IPosition& shape, Int64& fileOffset,
168 const Bool* dummy);
169 uInt putShape (const IPosition& shape, Int64& fileOffset,
170 const Char* dummy);
171 uInt putShape (const IPosition& shape, Int64& fileOffset,
172 const uChar* dummy);
173 uInt putShape (const IPosition& shape, Int64& fileOffset,
174 const Short* dummy);
175 uInt putShape (const IPosition& shape, Int64& fileOffset,
176 const uShort* dummy);
177 uInt putShape (const IPosition& shape, Int64& fileOffset,
178 const Int* dummy);
179 uInt putShape (const IPosition& shape, Int64& fileOffset,
180 const uInt* dummy);
181 uInt putShape (const IPosition& shape, Int64& fileOffset,
182 const Int64* dummy);
183 uInt putShape (const IPosition& shape, Int64& fileOffset,
184 const uInt64* dummy);
185 uInt putShape (const IPosition& shape, Int64& fileOffset,
186 const Float* dummy);
187 uInt putShape (const IPosition& shape, Int64& fileOffset,
188 const Double* dummy);
189 uInt putShape (const IPosition& shape, Int64& fileOffset,
190 const Complex* dummy);
191 uInt putShape (const IPosition& shape, Int64& fileOffset,
192 const DComplex* dummy);
193 uInt putShape (const IPosition& shape, Int64& fileOffset,
194 const String* dummy);
195 // </group>
196
197 // Get the reference count.
199
200 // Put the reference count.
201 // An exception is thrown if a value other than 1 is put for version 0.
202 void putRefCount (uInt refCount, Int64 offset);
203
204 // Put nr elements at the given file offset and array offset.
205 // The file offset of the first array element is the file offset
206 // of the shape plus the length of the shape in the file.
207 // The array offset is counted in number of elements. It can be
208 // used to put only a (contiguous) section of the array.
209 // <group>
210 void put (Int64 fileOffset, Int64 arrayOffset, uInt64 nr, const Bool*);
211 void put (Int64 fileOffset, Int64 arrayOffset, uInt64 nr, const Char*);
212 void put (Int64 fileOffset, Int64 arrayOffset, uInt64 nr, const uChar*);
213 void put (Int64 fileOffset, Int64 arrayOffset, uInt64 nr, const Short*);
214 void put (Int64 fileOffset, Int64 arrayOffset, uInt64 nr, const uShort*);
215 void put (Int64 fileOffset, Int64 arrayOffset, uInt64 nr, const Int*);
216 void put (Int64 fileOffset, Int64 arrayOffset, uInt64 nr, const uInt*);
217 void put (Int64 fileOffset, Int64 arrayOffset, uInt64 nr, const Int64*);
218 void put (Int64 fileOffset, Int64 arrayOffset, uInt64 nr, const uInt64*);
219 void put (Int64 fileOffset, Int64 arrayOffset, uInt64 nr, const Float*);
220 void put (Int64 fileOffset, Int64 arrayOffset, uInt64 nr, const Double*);
221//#// void put (Int64 fileOffset, Int64 arrayOffset, uInt64 nr, const long double*);
222 void put (Int64 fileOffset, Int64 arrayOffset, uInt64 nr, const Complex*);
223 void put (Int64 fileOffset, Int64 arrayOffset, uInt64 nr, const DComplex*);
224 void put (Int64 fileOffset, Int64 arrayOffset, uInt64 nr, const String*);
225 // </group>
226
227 // Get the shape at the given file offset.
228 // It will reshape the IPosition vector when needed.
229 // It returns the length of the shape in the file.
231
232 // Get nr elements at the given file offset and array offset.
233 // The file offset of the first array element is the file offset
234 // of the shape plus the length of the shape in the file.
235 // The array offset is counted in number of elements. It can be
236 // used to get only a (contiguous) section of the array.
237 // <group>
238 void get (Int64 fileOffset, Int64 arrayOffset, uInt64 nr, Bool*);
239 void get (Int64 fileOffset, Int64 arrayOffset, uInt64 nr, Char*);
240 void get (Int64 fileOffset, Int64 arrayOffset, uInt64 nr, uChar*);
241 void get (Int64 fileOffset, Int64 arrayOffset, uInt64 nr, Short*);
242 void get (Int64 fileOffset, Int64 arrayOffset, uInt64 nr, uShort*);
243 void get (Int64 fileOffset, Int64 arrayOffset, uInt64 nr, Int*);
244 void get (Int64 fileOffset, Int64 arrayOffset, uInt64 nr, uInt*);
245 void get (Int64 fileOffset, Int64 arrayOffset, uInt64 nr, Int64*);
246 void get (Int64 fileOffset, Int64 arrayOffset, uInt64 nr, uInt64*);
247 void get (Int64 fileOffset, Int64 arrayOffset, uInt64 nr, Float*);
248 void get (Int64 fileOffset, Int64 arrayOffset, uInt64 nr, Double*);
249//#// void get (Int64 fileOffset, Int64 arrayOffset, uInt64 nr, long double*);
250 void get (Int64 fileOffset, Int64 arrayOffset, uInt64 nr, Complex*);
251 void get (Int64 fileOffset, Int64 arrayOffset, uInt64 nr, DComplex*);
252 void get (Int64 fileOffset, Int64 arrayOffset, uInt64 nr, String*);
253 // </group>
254
255 // Copy the array with <src>nr</src> elements from one file offset
256 // to another.
257 // <group>
258 void copyArrayBool (Int64 to, Int64 from, uInt64 nr);
259 void copyArrayChar (Int64 to, Int64 from, uInt64 nr);
260 void copyArrayuChar (Int64 to, Int64 from, uInt64 nr);
261 void copyArrayShort (Int64 to, Int64 from, uInt64 nr);
262 void copyArrayuShort (Int64 to, Int64 from, uInt64 nr);
263 void copyArrayInt (Int64 to, Int64 from, uInt64 nr);
264 void copyArrayuInt (Int64 to, Int64 from, uInt64 nr);
265 void copyArrayInt64 (Int64 to, Int64 from, uInt64 nr);
266 void copyArrayuInt64 (Int64 to, Int64 from, uInt64 nr);
267 void copyArrayFloat (Int64 to, Int64 from, uInt64 nr);
268 void copyArrayDouble (Int64 to, Int64 from, uInt64 nr);
269//#// void copyArrayLDouble (Int64 to, Int64 from, uInt64 nr);
270 void copyArrayComplex (Int64 to, Int64 from, uInt64 nr);
271 void copyArrayDComplex (Int64 to, Int64 from, uInt64 nr);
272 void copyArrayString (Int64 to, Int64 from, uInt64 nr);
273 // </group>
274
275private:
276 std::shared_ptr<ByteIO> file_p; //# File object
277 std::shared_ptr<TypeIO> iofil_p; //# IO object
278 Int64 leng_p; //# File length
279 uInt version_p; //# Version of StArrayFile file
280 Bool swput_p; //# True = put is possible
281 Bool hasPut_p; //# True = put since last flush
292
293 // Put a single value at the current file offset.
294 // It returns the length of the value in the file.
295 // <group>
296 uInt put (const Int&);
297 uInt put (const uInt&);
298 // </group>
299
300 // Put the array shape at the end of the file and reserve
301 // space for nr elements (each lenElem bytes long).
302 // It fills the file offset of the shape.
303 // It returns the length of the shape in the file.
304 uInt putRes (const IPosition& shape, Int64& fileOffset, float lenElem);
305
306 // Get a single value at the current file offset.
307 // It returns the length of the value in the file.
308 // <group>
309 uInt get (Int&);
310 uInt get (uInt&);
311 // </group>
312
313 // Copy data with the given length from one file offset to another.
314 void copyData (Int64 to, Int64 from, uInt64 length);
315
316 // Position the file on the given offset.
317 void setpos (Int64 offset);
318};
319
320
322{
323 file_p->reopenRW();
324}
326{
327 hasPut_p = True;
328 return iofil_p->write (1, &value);
329}
331{
332 hasPut_p = True;
333 return iofil_p->write (1, &value);
334}
336{
337 return iofil_p->read (1, &value);
338}
340{
341 return iofil_p->read (1, &value);
342}
343
344
345
346} //# NAMESPACE CASACORE - END
347
348#endif
OpenOption
Define the possible ByteIO open options.
Definition ByteIO.h:63
void copyArrayInt64(Int64 to, Int64 from, uInt64 nr)
void get(Int64 fileOffset, Int64 arrayOffset, uInt64 nr, Char *)
void put(Int64 fileOffset, Int64 arrayOffset, uInt64 nr, const uInt *)
uInt putShape(const IPosition &shape, Int64 &fileOffset, const Char *dummy)
std::shared_ptr< TypeIO > iofil_p
void put(Int64 fileOffset, Int64 arrayOffset, uInt64 nr, const Bool *)
Put nr elements at the given file offset and array offset.
void copyArrayComplex(Int64 to, Int64 from, uInt64 nr)
void get(Int64 fileOffset, Int64 arrayOffset, uInt64 nr, DComplex *)
uInt putShape(const IPosition &shape, Int64 &fileOffset, const String *dummy)
void get(Int64 fileOffset, Int64 arrayOffset, uInt64 nr, uChar *)
uInt putShape(const IPosition &shape, Int64 &fileOffset, const Bool *dummy)
Put the array shape and store its file offset into the offset argument.
void put(Int64 fileOffset, Int64 arrayOffset, uInt64 nr, const Short *)
void get(Int64 fileOffset, Int64 arrayOffset, uInt64 nr, Bool *)
Get nr elements at the given file offset and array offset.
void copyArrayChar(Int64 to, Int64 from, uInt64 nr)
void put(Int64 fileOffset, Int64 arrayOffset, uInt64 nr, const Float *)
void put(Int64 fileOffset, Int64 arrayOffset, uInt64 nr, const Int64 *)
uInt putShape(const IPosition &shape, Int64 &fileOffset, const Complex *dummy)
uInt putShape(const IPosition &shape, Int64 &fileOffset, const DComplex *dummy)
void put(Int64 fileOffset, Int64 arrayOffset, uInt64 nr, const uShort *)
uInt putShape(const IPosition &shape, Int64 &fileOffset, const Double *dummy)
void copyArrayFloat(Int64 to, Int64 from, uInt64 nr)
void resync()
Resync the file (i.e.
void copyData(Int64 to, Int64 from, uInt64 length)
Copy data with the given length from one file offset to another.
void copyArrayDouble(Int64 to, Int64 from, uInt64 nr)
void get(Int64 fileOffset, Int64 arrayOffset, uInt64 nr, Double *)
uInt putShape(const IPosition &shape, Int64 &fileOffset, const Short *dummy)
StManArrayFile(const String &name, ByteIO::OpenOption, uInt version=0, Bool bigEndian=True, uInt bufferSize=0, const std::shared_ptr< MultiFileBase > &=std::shared_ptr< MultiFileBase >())
Construct the object and attach it to the give file.
void put(Int64 fileOffset, Int64 arrayOffset, uInt64 nr, const Char *)
void get(Int64 fileOffset, Int64 arrayOffset, uInt64 nr, Int *)
void get(Int64 fileOffset, Int64 arrayOffset, uInt64 nr, Complex *)
void putRefCount(uInt refCount, Int64 offset)
Put the reference count.
void put(Int64 fileOffset, Int64 arrayOffset, uInt64 nr, const Complex *)
void copyArrayInt(Int64 to, Int64 from, uInt64 nr)
uInt putShape(const IPosition &shape, Int64 &fileOffset, const uChar *dummy)
void put(Int64 fileOffset, Int64 arrayOffset, uInt64 nr, const Int *)
void get(Int64 fileOffset, Int64 arrayOffset, uInt64 nr, uInt *)
uInt putShape(const IPosition &shape, Int64 &fileOffset, const Float *dummy)
void setpos(Int64 offset)
Position the file on the given offset.
void put(Int64 fileOffset, Int64 arrayOffset, uInt64 nr, const String *)
void copyArrayBool(Int64 to, Int64 from, uInt64 nr)
Copy the array with nr elements from one file offset to another.
void get(Int64 fileOffset, Int64 arrayOffset, uInt64 nr, Short *)
uInt putShape(const IPosition &shape, Int64 &fileOffset, const Int *dummy)
void get(Int64 fileOffset, Int64 arrayOffset, uInt64 nr, uShort *)
void put(Int64 fileOffset, Int64 arrayOffset, uInt64 nr, const uChar *)
Bool flush(Bool fsync)
Flush and optionally fsync the data.
uInt putShape(const IPosition &shape, Int64 &fileOffset, const uInt *dummy)
uInt putShape(const IPosition &shape, Int64 &fileOffset, const Int64 *dummy)
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 uShort *dummy)
void copyArrayuInt(Int64 to, Int64 from, uInt64 nr)
std::shared_ptr< ByteIO > file_p
void put(Int64 fileOffset, Int64 arrayOffset, uInt64 nr, const Double *)
~StManArrayFile()
Close the possibly opened file.
uInt getShape(Int64 fileOffset, IPosition &shape)
Get the shape at the given file offset.
uInt getRefCount(Int64 offset)
Get the reference count.
void get(Int64 fileOffset, Int64 arrayOffset, uInt64 nr, uInt64 *)
void reopenRW()
Reopen the file for read/write access.
Int64 length()
Return the current file length (merely a debug tool).
void copyArrayuInt64(Int64 to, Int64 from, uInt64 nr)
void copyArrayuChar(Int64 to, Int64 from, uInt64 nr)
void copyArrayShort(Int64 to, Int64 from, uInt64 nr)
uInt putShape(const IPosition &shape, Int64 &fileOffset, const uInt64 *dummy)
void put(Int64 fileOffset, Int64 arrayOffset, uInt64 nr, const uInt64 *)
void copyArrayDComplex(Int64 to, Int64 from, uInt64 nr)
void get(Int64 fileOffset, Int64 arrayOffset, uInt64 nr, Float *)
void get(Int64 fileOffset, Int64 arrayOffset, uInt64 nr, Int64 *)
void get(Int64 fileOffset, Int64 arrayOffset, uInt64 nr, String *)
void copyArrayString(Int64 to, Int64 from, uInt64 nr)
void copyArrayuShort(Int64 to, Int64 from, uInt64 nr)
void put(Int64 fileOffset, Int64 arrayOffset, uInt64 nr, const DComplex *)
String: the storage and methods of handling collections of characters.
Definition String.h:223
this file contains all the compiler specific defines
Definition mainpage.dox:28
unsigned char uChar
Definition aipstype.h:45
short Short
Definition aipstype.h:46
unsigned int uInt
Definition aipstype.h:49
unsigned short uShort
Definition aipstype.h:47
TableExprNode shape(const TableExprNode &array)
Function operating on any scalar or array resulting in a Double array containing the shape.
Definition ExprNode.h:1991
long long Int64
Define the extra non-standard types used by Casacore (like proposed uSize, Size)
Definition aipsxtype.h:36
float Float
Definition aipstype.h:52
int Int
Definition aipstype.h:48
bool Bool
Define the standard types used by Casacore.
Definition aipstype.h:40
LatticeExprNode value(const LatticeExprNode &expr)
This function returns the value of the expression without a mask.
const Bool True
Definition aipstype.h:41
double Double
Definition aipstype.h:53
char Char
Definition aipstype.h:44
unsigned long long uInt64
Definition aipsxtype.h:37