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