casacore
Loading...
Searching...
No Matches
TSMCube.h
Go to the documentation of this file.
1//# TSMCube.h: Tiled hypercube in a table
2//# Copyright (C) 1995,1996,1997,1999,2000,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_TSMCUBE_H
27#define TABLES_TSMCUBE_H
28
29
30//# Includes
31#include <casacore/casa/aips.h>
32#include <casacore/tables/DataMan/TSMShape.h>
33#include <casacore/casa/Containers/Record.h>
34#include <casacore/casa/Arrays/IPosition.h>
35#include <casacore/casa/OS/Conversion.h>
36#include <casacore/casa/iosfwd.h>
37
38namespace casacore { //# NAMESPACE CASACORE - BEGIN
39
40//# Forward declarations
41class TiledStMan;
42class TSMFile;
43class TSMColumn;
44class BucketCache;
45template<class T> class Block;
46
47// <summary>
48// Tiled hypercube in a table
49// </summary>
50
51// <use visibility=local>
52
53// <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="">
54// </reviewed>
55
56// <prerequisite>
57//# Classes you should understand before using this one.
58// <li> <linkto class=TiledStMan>TiledStMan</linkto>
59// <li> <linkto class=ROTiledStManAccessor>ROTiledStManAccessor</linkto>
60// for a discussion of the maximum cache size
61// <li> <linkto class=TSMFile>TSMFile</linkto>
62// <li> <linkto class=BucketCache>BucketCache</linkto>
63// </prerequisite>
64
65// <etymology>
66// TSMCube represents a hypercube in the Tiled Storage Manager.
67// </etymology>
68
69// <synopsis>
70// TSMCube defines a tiled hypercube. The data is stored in a TSMFile
71// object and accessed using a BucketCache object. The hypercube can
72// be extensible in its last dimension to support tables with a size
73// which is not known in advance.
74// <br>
75// Normally hypercubes share the same TSMFile object, but extensible
76// hypercubes have their own TSMFile object (to be extensible).
77// If the hypercolumn has multiple data columns, their cells share the same
78// tiles. Per tile data column A appears first, thereafter B, etc..
79// <br>
80// The data in the cache is held in external format and is converted
81// when accessed. The alternative would be to hold it in the cache in
82// local format and convert it when read/written from the file. It was
83// felt that the latter approach would generate more needless conversions.
84// <p>
85// The possible id and coordinate values are stored in a Record
86// object. They are written in the main hypercube AipsIO file.
87// <p>
88// TSMCube uses the maximum cache size set for a Tiled Storage manager.
89// The description of class
90// <linkto class=ROTiledStManAccessor>ROTiledStManAccessor</linkto>
91// contains a discussion about the effect of setting the maximum cache size.
92// </synopsis>
93
94// <motivation>
95// TSMCube encapsulates all operations on a hypercube.
96// </motivation>
97
98//# <todo asof="$DATE:$">
99//# A List of bugs, limitations, extensions or planned refinements.
100//# </todo>
101
102
104{
105public:
106 // Define the possible access types for TSMDataColumn.
114
115 // Construct the hypercube using the given file with the given shape.
116 // The record contains the id and possible coordinate values.
117 // <br>If the cubeshape is empty, the hypercube is still undefined and
118 // can be added later with setShape. That is only used by TiledCellStMan.
119 // <br> The fileOffset argument is meant for class TiledFileAccess.
120 TSMCube (TiledStMan* stman, TSMFile* file,
121 const IPosition& cubeShape,
122 const IPosition& tileShape,
123 const Record& values,
124 Int64 fileOffset,
125 Bool useDerived = False);
126
127 // Reconstruct the hypercube by reading its data from the AipsIO stream.
128 // It will link itself to the correct TSMFile. The TSMFile objects
129 // must have been reconstructed in advance.
130 TSMCube (TiledStMan* stman, AipsIO& ios,
131 Bool useDerived = False);
132
133 virtual ~TSMCube();
134
135 // Forbid copy constructor.
136 TSMCube (const TSMCube&) = delete;
137
138 // Forbid assignment.
139 TSMCube& operator= (const TSMCube&) = delete;
140
141 // Flush the data in the cache.
142 virtual void flushCache();
143
144 // Clear the cache, so data will be reread.
145 // If wanted, the data is flushed before the cache is cleared.
146 void clearCache (Bool doFlush = True);
147
148 // Empty the cache.
149 // It will flush the cache as needed and remove all buckets from it
150 // resulting in a possibly large drop in memory used.
151 // It'll also clear the <src>userSetCache_p</src> flag.
153
154 // Show the cache statistics.
155 virtual void showCacheStatistics (ostream& os) const;
156
157 // Put the data of the object into the AipsIO stream.
158 void putObject (AipsIO& ios);
159
160 // Get the data of the object from the AipsIO stream.
161 // It returns the data manager sequence number, which is -1 if
162 // no file is attached to the cube (for cells without a value).
164
165 // Resync the object with the data file.
166 // It reads the object, and adjusts the cache.
167 virtual void resync (AipsIO& ios);
168
169 // Is the hypercube extensible?
171
172 // Get the bucket size (bytes).
173 // It is the length of a tile in external format.
174 uInt bucketSize() const;
175
176 // Get the length of a tile (in bytes) in local format.
177 uInt localTileLength() const;
178
179 // Set the hypercube shape.
180 // This is only possible if the shape was not defined yet.
181 virtual void setShape (const IPosition& cubeShape,
182 const IPosition& tileShape);
183
184 // Get the shape of the hypercube.
185 const IPosition& cubeShape() const;
186
187 // Get the shape of the tiles.
188 const IPosition& tileShape() const;
189
190 // Get the shape of the data cells in the cube.
192
193 // Get the size of a coordinate (i.e. the number of values in it).
194 // If not defined, it returns zero.
195 uInt coordinateSize (const String& coordinateName) const;
196
197 // Get the record containing the id and coordinate values.
198 // It is used by TSMIdColumn and TSMCoordColumn.
199 // <group>
200 const Record& valueRecord() const;
202 // </group>
203
204 // Test if the id values match.
206 const Record& idValues);
207
208 // Extend the last dimension of the cube with the given number.
209 // The record can contain the coordinates of the elements added.
210 virtual void extend (uInt64 nr, const Record& coordValues,
211 const TSMColumn* lastCoordColumn);
212
213 // Extend the coordinates vector for the given coordinate
214 // to the given length with the given coordValues.
215 // It will be initialized to zero if no coordValues are given.
216 // If the coordinate vector does not exist yet, it will be created.
217 void extendCoordinates (const Record& coordValues,
218 const String& coordName, uInt length);
219
220 // Read or write a section in the cube.
221 // It is assumed that the section buffer is long enough.
222 virtual void accessSection (const IPosition& start, const IPosition& end,
223 char* section, uInt colnr,
224 uInt localPixelSize, uInt externalPixelSize,
225 Bool writeFlag);
226
227 // Read or write a section in a strided way.
228 // It is assumed that the section buffer is long enough.
229 virtual void accessStrided (const IPosition& start, const IPosition& end,
230 const IPosition& stride,
231 char* section, uInt colnr,
232 uInt localPixelSize, uInt externalPixelSize,
233 Bool writeFlag);
234
235 // Get the current cache size (in buckets).
237
238 // Calculate the cache size (in buckets) for the given slice
239 // and access path.
240 // <group>
241 uInt calcCacheSize (const IPosition& sliceShape,
242 const IPosition& windowStart,
243 const IPosition& windowLength,
244 const IPosition& axisPath) const;
246 const IPosition& tileShape,
247 Bool extensible,
248 const IPosition& sliceShape,
249 const IPosition& windowStart,
250 const IPosition& windowLength,
251 const IPosition& axisPath,
252 uInt maxCacheSizeMiB, uInt bucketSize);
253 // </group>
254
255 // Set the cache size for the given slice and access path.
256 virtual void setCacheSize (const IPosition& sliceShape,
257 const IPosition& windowStart,
258 const IPosition& windowLength,
259 const IPosition& axisPath,
260 Bool forceSmaller, Bool userSet);
261
262 // Resize the cache object.
263 // If forceSmaller is False, the cache will only be resized when it grows.
264 // If the given size exceeds the maximum size with more
265 // than 10%, the maximum size will be used.
266 // The cacheSize has to be given in buckets.
267 // <br>The flag <src>userSet</src> inidicates if the cache size is set by
268 // the user (by an Accessor object) or automatically (by TSMDataColumn).
269 virtual void setCacheSize (uInt cacheSize, Bool forceSmaller, Bool userSet);
270
271 // Validate the cache size (in buckets).
272 // This means it will return the given cache size (in buckets) if
273 // smaller than the maximum cache size (given in MiB).
274 // Otherwise the maximum is returned.
275 // <group>
279 // </group>
280
281 // Determine if the user set the cache size (using setCacheSize).
282 Bool userSetCache() const;
283
284 // Functions for TSMDataColumn to keep track of the last type of
285 // access to a hypercube. It uses it to determine if the cache
286 // has to be reset.
287 // <group>
289 const IPosition& getLastColSlice() const;
290 void setLastColAccess (AccessType type);
291 void setLastColSlice (const IPosition& slice);
292 // </group>
293
294protected:
295 // Initialize the various variables.
296 // <group>
297 void setup();
299 // </group>
300
301 // Adjust the tile shape to the hypercube shape.
302 // A size of 0 gets set to 1.
303 // A tile size > cube size gets set to the cube size.
305 const IPosition& tileShape) const;
306
307 // Resize the IPosition member variables used in accessSection()
308 // if nrdim_p changes value.
310
311private:
312 // Get the cache object.
313 // This will construct the cache object if not present yet.
315
316 // Construct the cache object (if not constructed yet).
317 virtual void makeCache();
318
319 // Resync the cache object.
320 virtual void resyncCache();
321
322 // Delete the cache object.
323 virtual void deleteCache();
324
325 // Access a line in a more optimized way.
326 void accessLine (char* section, uInt pixelOffset,
327 uInt localPixelSize,
328 Bool writeFlag, BucketCache* cachePtr,
329 const IPosition& startTile, uInt endTile,
330 const IPosition& startPixelInFirstTile,
331 uInt endPixelInLastTile,
332 uInt lineIndex);
333
334 // Define the callback functions for the BucketCache.
335 // <group>
336 static char* readCallBack (void* owner, const char* external);
337 static void writeCallBack (void* owner, char* external,
338 const char* local);
339 static char* initCallBack (void* owner);
340 static void deleteCallBack (void* owner, char* buffer);
341 // </group>
342
343 // Define the functions doing the actual read and write of the
344 // data in the tile and converting it to/from local format.
345 // <group>
346 char* readTile (const char* external);
347 void writeTile (char* external, const char* local);
348 // </group>
349
350protected:
351 //# Declare member variables.
352
353 char * cachedTile_p; // optimization to hold one tile chunk
354
355 // Pointer to the parent storage manager.
357 // Is the class used directly or only by a derived class only?
359 // The values of the possible id and coordinate columns.
361 // Is the hypercube extensible?
363 // Dimensionality of the hypercube.
365 // Number of tiles in the hypercube.
367 // The shape of the hypercube.
369 // The shape of the tiles in the hypercube.
371 // The number of tiles in each hypercube dimension.
373 // Precomputed tileShape information.
375 // Precomputed tilesPerDim information.
377 // Number of tiles in all but last dimension (used when extending).
379 // The tilesize in bytes.
381 // Pointer to the TSMFile object holding the data.
383 // Offset in the TSMFile object where the data of this hypercube starts.
385 // Offset for each data column in a tile (in external format).
387 // Offset for each data column in a tile (in local format).
389 // The bucket size in bytes (is equal to tile size in bytes).
391 // The tile size in bytes in local format.
393 // The bucket cache.
395 // Did the user set the cache size?
397 // Was the last column access to a cell, slice, or column?
399 // The slice shape of the last column access to a slice.
401
402 // IPosition variables used in accessSection(); declared here
403 // as member variables to avoid significant construction and
404 // desctruction overhead if they are local to accessSection()
405 // #tiles needed for the section
407 // First tile needed
409 // Last tile needed
411 // First pixel in first tile
413 // Last pixel in first tile
415 // Last pixel in last tile
417};
418
419
420
422{
423 if (cache_p == 0) {
424 makeCache();
425 }
426 return cache_p;
427}
429{
430 return bucketSize_p;
431}
433{
434 return localTileLength_p;
435}
436inline const IPosition& TSMCube::cubeShape() const
437{
438 return cubeShape_p;
439}
440inline const IPosition& TSMCube::tileShape() const
441{
442 return tileShape_p;
443}
444inline const Record& TSMCube::valueRecord() const
445{
446 return values_p;
447}
449{
450 return values_p;
451}
453{
454 return userSetCache_p;
455}
461{
462 return lastColSlice_p;
463}
465{
466 lastColAccess_p = type;
467}
468inline void TSMCube::setLastColSlice (const IPosition& slice)
469{
471 lastColSlice_p = slice;
472}
473
474
475
476
477} //# NAMESPACE CASACORE - END
478
479#endif
simple 1-D array
Definition Block.h:198
Cache for buckets in a part of a file.
void resize(size_t newSize, bool copy=true)
Old values are copied on resize if copy==true.
size_t nelements() const
The number of elements in this IPosition.
Definition IPosition.h:566
A drop-in replacement for Block<T*>.
Definition Block.h:812
String: the storage and methods of handling collections of characters.
Definition String.h:223
virtual ~TSMCube()
void extendCoordinates(const Record &coordValues, const String &coordName, uInt length)
Extend the coordinates vector for the given coordinate to the given length with the given coordValues...
uInt cacheSize() const
Get the current cache size (in buckets).
TSMFile * filePtr_p
Pointer to the TSMFile object holding the data.
Definition TSMCube.h:382
void emptyCache()
Empty the cache.
const Record & valueRecord() const
Get the record containing the id and coordinate values.
Definition TSMCube.h:444
void putObject(AipsIO &ios)
Put the data of the object into the AipsIO stream.
virtual void showCacheStatistics(ostream &os) const
Show the cache statistics.
IPosition lastColSlice_p
The slice shape of the last column access to a slice.
Definition TSMCube.h:400
uInt validateCacheSize(uInt cacheSize) const
Validate the cache size (in buckets).
virtual void setCacheSize(const IPosition &sliceShape, const IPosition &windowStart, const IPosition &windowLength, const IPosition &axisPath, Bool forceSmaller, Bool userSet)
Set the cache size for the given slice and access path.
virtual void resyncCache()
Resync the cache object.
IPosition tilesPerDim_p
The number of tiles in each hypercube dimension.
Definition TSMCube.h:372
void resizeTileSections()
Resize the IPosition member variables used in accessSection() if nrdim_p changes value.
virtual void makeCache()
Construct the cache object (if not constructed yet).
static void deleteCallBack(void *owner, char *buffer)
IPosition tileShape_p
The shape of the tiles in the hypercube.
Definition TSMCube.h:370
IPosition endPixelInFirstTile_p
Last pixel in first tile.
Definition TSMCube.h:414
Bool userSetCache() const
Determine if the user set the cache size (using setCacheSize).
Definition TSMCube.h:452
Block< uInt > localOffset_p
Offset for each data column in a tile (in local format).
Definition TSMCube.h:388
TSMCube(TiledStMan *stman, AipsIO &ios, Bool useDerived=False)
Reconstruct the hypercube by reading its data from the AipsIO stream.
char * readTile(const char *external)
Define the functions doing the actual read and write of the data in the tile and converting it to/fro...
uInt localTileLength() const
Get the length of a tile (in bytes) in local format.
Definition TSMCube.h:432
void setLastColAccess(AccessType type)
Definition TSMCube.h:464
virtual void resync(AipsIO &ios)
Resync the object with the data file.
const IPosition & tileShape() const
Get the shape of the tiles.
Definition TSMCube.h:440
uInt tileSize_p
The tilesize in bytes.
Definition TSMCube.h:380
IPosition adjustTileShape(const IPosition &cubeShape, const IPosition &tileShape) const
Adjust the tile shape to the hypercube shape.
uInt coordinateSize(const String &coordinateName) const
Get the size of a coordinate (i.e.
virtual void setShape(const IPosition &cubeShape, const IPosition &tileShape)
Set the hypercube shape.
Block< uInt > externalOffset_p
Offset for each data column in a tile (in external format).
Definition TSMCube.h:386
virtual void extend(uInt64 nr, const Record &coordValues, const TSMColumn *lastCoordColumn)
Extend the last dimension of the cube with the given number.
void accessLine(char *section, uInt pixelOffset, uInt localPixelSize, Bool writeFlag, BucketCache *cachePtr, const IPosition &startTile, uInt endTile, const IPosition &startPixelInFirstTile, uInt endPixelInLastTile, uInt lineIndex)
Access a line in a more optimized way.
IPosition cubeShape_p
The shape of the hypercube.
Definition TSMCube.h:368
uInt nrTiles_p
Number of tiles in the hypercube.
Definition TSMCube.h:366
void clearCache(Bool doFlush=True)
Clear the cache, so data will be reread.
uInt nrTilesSubCube_p
Number of tiles in all but last dimension (used when extending).
Definition TSMCube.h:378
static uInt calcCacheSize(const IPosition &cubeShape, const IPosition &tileShape, Bool extensible, const IPosition &sliceShape, const IPosition &windowStart, const IPosition &windowLength, const IPosition &axisPath, uInt maxCacheSizeMiB, uInt bucketSize)
TSMShape expandedTilesPerDim_p
Precomputed tilesPerDim information.
Definition TSMCube.h:376
IPosition startPixelInFirstTile_p
First pixel in first tile.
Definition TSMCube.h:412
void setLastColSlice(const IPosition &slice)
Definition TSMCube.h:468
TSMCube(const TSMCube &)=delete
Forbid copy constructor.
TSMCube(TiledStMan *stman, TSMFile *file, const IPosition &cubeShape, const IPosition &tileShape, const Record &values, Int64 fileOffset, Bool useDerived=False)
Construct the hypercube using the given file with the given shape.
virtual void accessStrided(const IPosition &start, const IPosition &end, const IPosition &stride, char *section, uInt colnr, uInt localPixelSize, uInt externalPixelSize, Bool writeFlag)
Read or write a section in a strided way.
AccessType lastColAccess_p
Was the last column access to a cell, slice, or column?
Definition TSMCube.h:398
static char * readCallBack(void *owner, const char *external)
Define the callback functions for the BucketCache.
IPosition endPixelInLastTile_p
Last pixel in last tile.
Definition TSMCube.h:416
Bool matches(const PtrBlock< TSMColumn * > &idColSet, const Record &idValues)
Test if the id values match.
Bool useDerived_p
Is the class used directly or only by a derived class only?
Definition TSMCube.h:358
BucketCache * getCache()
Get the cache object.
Definition TSMCube.h:421
uInt nrdim_p
Dimensionality of the hypercube.
Definition TSMCube.h:364
Int64 fileOffset_p
Offset in the TSMFile object where the data of this hypercube starts.
Definition TSMCube.h:384
IPosition endTile_p
Last tile needed.
Definition TSMCube.h:410
const IPosition & cubeShape() const
Get the shape of the hypercube.
Definition TSMCube.h:436
Bool extensible_p
Is the hypercube extensible?
Definition TSMCube.h:362
virtual void flushCache()
Flush the data in the cache.
static uInt validateCacheSize(uInt cacheSize, uInt maxSizeMiB, uInt bucketSize)
void writeTile(char *external, const char *local)
uInt bucketSize_p
The bucket size in bytes (is equal to tile size in bytes).
Definition TSMCube.h:390
TSMCube & operator=(const TSMCube &)=delete
Forbid assignment.
Record values_p
The values of the possible id and coordinate columns.
Definition TSMCube.h:360
Int getObject(AipsIO &ios)
Get the data of the object from the AipsIO stream.
void setup()
Initialize the various variables.
uInt calcCacheSize(const IPosition &sliceShape, const IPosition &windowStart, const IPosition &windowLength, const IPosition &axisPath) const
Calculate the cache size (in buckets) for the given slice and access path.
virtual void deleteCache()
Delete the cache object.
TSMShape expandedTileShape_p
Precomputed tileShape information.
Definition TSMCube.h:374
static char * initCallBack(void *owner)
IPosition cellShape() const
Get the shape of the data cells in the cube.
static void writeCallBack(void *owner, char *external, const char *local)
AccessType
Define the possible access types for TSMDataColumn.
Definition TSMCube.h:107
TiledStMan * stmanPtr_p
Pointer to the parent storage manager.
Definition TSMCube.h:356
Bool userSetCache_p
Did the user set the cache size?
Definition TSMCube.h:396
virtual void accessSection(const IPosition &start, const IPosition &end, char *section, uInt colnr, uInt localPixelSize, uInt externalPixelSize, Bool writeFlag)
Read or write a section in the cube.
Record & rwValueRecord()
Definition TSMCube.h:448
IPosition startTile_p
First tile needed.
Definition TSMCube.h:408
BucketCache * cache_p
The bucket cache.
Definition TSMCube.h:394
AccessType getLastColAccess() const
Functions for TSMDataColumn to keep track of the last type of access to a hypercube.
Definition TSMCube.h:456
virtual void setCacheSize(uInt cacheSize, Bool forceSmaller, Bool userSet)
Resize the cache object.
Bool isExtensible() const
Is the hypercube extensible?
uInt localTileLength_p
The tile size in bytes in local format.
Definition TSMCube.h:392
const IPosition & getLastColSlice() const
Definition TSMCube.h:460
char * cachedTile_p
Definition TSMCube.h:353
IPosition nrTileSection_p
IPosition variables used in accessSection(); declared here as member variables to avoid significant c...
Definition TSMCube.h:406
uInt bucketSize() const
Get the bucket size (bytes).
Definition TSMCube.h:428
this file contains all the compiler specific defines
Definition mainpage.dox:28
const Bool False
Definition aipstype.h:42
unsigned int uInt
Definition aipstype.h:49
long long Int64
Define the extra non-standard types used by Casacore (like proposed uSize, Size)
Definition aipsxtype.h:36
LatticeExprNode length(const LatticeExprNode &expr, const LatticeExprNode &axis)
2-argument function to get the length of an axis.
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
unsigned long long uInt64
Definition aipsxtype.h:37