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// # Includes
30#include <casacore/casa/aips.h>
31#include <casacore/tables/DataMan/TSMShape.h>
32#include <casacore/casa/Containers/Record.h>
33#include <casacore/casa/Arrays/IPosition.h>
34#include <casacore/casa/OS/Conversion.h>
35#include <casacore/casa/iosfwd.h>
36
37namespace casacore { // # NAMESPACE CASACORE - BEGIN
38
39// # Forward declarations
40class TiledStMan;
41class TSMFile;
42class TSMColumn;
43class BucketCache;
44template <class T>
45class 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
102class TSMCube {
103 public:
104 // Define the possible access types for TSMDataColumn.
106
107 // Construct the hypercube using the given file with the given shape.
108 // The record contains the id and possible coordinate values.
109 // <br>If the cubeshape is empty, the hypercube is still undefined and
110 // can be added later with setShape. That is only used by TiledCellStMan.
111 // <br> The fileOffset argument is meant for class TiledFileAccess.
113 const Record& values, Int64 fileOffset, Bool useDerived = False);
114
115 // Reconstruct the hypercube by reading its data from the AipsIO stream.
116 // It will link itself to the correct TSMFile. The TSMFile objects
117 // must have been reconstructed in advance.
118 TSMCube(TiledStMan* stman, AipsIO& ios, Bool useDerived = False);
119
120 virtual ~TSMCube();
121
122 // Forbid copy constructor.
123 TSMCube(const TSMCube&) = delete;
124
125 // Forbid assignment.
126 TSMCube& operator=(const TSMCube&) = delete;
127
128 // Flush the data in the cache.
129 virtual void flushCache();
130
131 // Clear the cache, so data will be reread.
132 // If wanted, the data is flushed before the cache is cleared.
133 void clearCache(Bool doFlush = True);
134
135 // Empty the cache.
136 // It will flush the cache as needed and remove all buckets from it
137 // resulting in a possibly large drop in memory used.
138 // It'll also clear the <src>userSetCache_p</src> flag.
140
141 // Show the cache statistics.
142 virtual void showCacheStatistics(ostream& os) const;
143
144 // Put the data of the object into the AipsIO stream.
145 void putObject(AipsIO& ios);
146
147 // Get the data of the object from the AipsIO stream.
148 // It returns the data manager sequence number, which is -1 if
149 // no file is attached to the cube (for cells without a value).
151
152 // Resync the object with the data file.
153 // It reads the object, and adjusts the cache.
154 virtual void resync(AipsIO& ios);
155
156 // Is the hypercube extensible?
158
159 // Get the bucket size (bytes).
160 // It is the length of a tile in external format.
161 uInt bucketSize() const;
162
163 // Get the length of a tile (in bytes) in local format.
164 uInt localTileLength() const;
165
166 // Set the hypercube shape.
167 // This is only possible if the shape was not defined yet.
168 virtual void setShape(const IPosition& cubeShape, const IPosition& tileShape);
169
170 // Get the shape of the hypercube.
171 const IPosition& cubeShape() const;
172
173 // Get the shape of the tiles.
174 const IPosition& tileShape() const;
175
176 // Get the shape of the data cells in the cube.
178
179 // Get the size of a coordinate (i.e. the number of values in it).
180 // If not defined, it returns zero.
181 uInt coordinateSize(const String& coordinateName) const;
182
183 // Get the record containing the id and coordinate values.
184 // It is used by TSMIdColumn and TSMCoordColumn.
185 // <group>
186 const Record& valueRecord() const;
188 // </group>
189
190 // Test if the id values match.
191 Bool matches(const Block<TSMColumn*>& idColSet, const Record& idValues);
192
193 // Extend the last dimension of the cube with the given number.
194 // The record can contain the coordinates of the elements added.
195 virtual void extend(uInt64 nr, const Record& coordValues, const TSMColumn* lastCoordColumn);
196
197 // Extend the coordinates vector for the given coordinate
198 // to the given length with the given coordValues.
199 // It will be initialized to zero if no coordValues are given.
200 // If the coordinate vector does not exist yet, it will be created.
201 void extendCoordinates(const Record& coordValues, const String& coordName, uInt length);
202
203 // Read or write a section in the cube.
204 // It is assumed that the section buffer is long enough.
205 virtual void accessSection(const IPosition& start, const IPosition& end, char* section,
206 uInt colnr, uInt localPixelSize, uInt externalPixelSize,
207 Bool writeFlag);
208
209 // Read or write a section in a strided way.
210 // It is assumed that the section buffer is long enough.
211 virtual void accessStrided(const IPosition& start, const IPosition& end, const IPosition& stride,
212 char* section, uInt colnr, uInt localPixelSize, uInt externalPixelSize,
213 Bool writeFlag);
214
215 // Get the current cache size (in buckets).
217
218 // Calculate the cache size (in buckets) for the given slice
219 // and access path.
220 // <group>
221 uInt calcCacheSize(const IPosition& sliceShape, const IPosition& windowStart,
222 const IPosition& windowLength, const IPosition& axisPath) const;
223 static uInt calcCacheSize(const IPosition& cubeShape, const IPosition& tileShape, Bool extensible,
224 const IPosition& sliceShape, const IPosition& windowStart,
225 const IPosition& windowLength, const IPosition& axisPath,
226 uInt maxCacheSizeMiB, uInt bucketSize);
227 // </group>
228
229 // Set the cache size for the given slice and access path.
230 virtual void setCacheSize(const IPosition& sliceShape, const IPosition& windowStart,
231 const IPosition& windowLength, const IPosition& axisPath,
232 Bool forceSmaller, Bool userSet);
233
234 // Resize the cache object.
235 // If forceSmaller is False, the cache will only be resized when it grows.
236 // If the given size exceeds the maximum size with more
237 // than 10%, the maximum size will be used.
238 // The cacheSize has to be given in buckets.
239 // <br>The flag <src>userSet</src> inidicates if the cache size is set by
240 // the user (by an Accessor object) or automatically (by TSMDataColumn).
241 virtual void setCacheSize(uInt cacheSize, Bool forceSmaller, Bool userSet);
242
243 // Validate the cache size (in buckets).
244 // This means it will return the given cache size (in buckets) if
245 // smaller than the maximum cache size (given in MiB).
246 // Otherwise the maximum is returned.
247 // <group>
250 // </group>
251
252 // Determine if the user set the cache size (using setCacheSize).
253 Bool userSetCache() const;
254
255 // Functions for TSMDataColumn to keep track of the last type of
256 // access to a hypercube. It uses it to determine if the cache
257 // has to be reset.
258 // <group>
260 const IPosition& getLastColSlice() const;
262 void setLastColSlice(const IPosition& slice);
263 // </group>
264
265 protected:
266 // Initialize the various variables.
267 // <group>
268 void setup();
270 // </group>
271
272 // Adjust the tile shape to the hypercube shape.
273 // A size of 0 gets set to 1.
274 // A tile size > cube size gets set to the cube size.
276
277 // Resize the IPosition member variables used in accessSection()
278 // if nrdim_p changes value.
280
281 private:
282 // Get the cache object.
283 // This will construct the cache object if not present yet.
285
286 // Construct the cache object (if not constructed yet).
287 virtual void makeCache();
288
289 // Resync the cache object.
290 virtual void resyncCache();
291
292 // Delete the cache object.
293 virtual void deleteCache();
294
295 // Access a line in a more optimized way.
296 void accessLine(char* section, uInt pixelOffset, uInt localPixelSize, Bool writeFlag,
297 BucketCache* cachePtr, const IPosition& startTile, uInt endTile,
298 const IPosition& startPixelInFirstTile, uInt endPixelInLastTile, uInt lineIndex);
299
300 // Define the callback functions for the BucketCache.
301 // <group>
302 static char* readCallBack(void* owner, const char* external);
303 static void writeCallBack(void* owner, char* external, const char* local);
304 static char* initCallBack(void* owner);
305 static void deleteCallBack(void* owner, char* buffer);
306 // </group>
307
308 // Define the functions doing the actual read and write of the
309 // data in the tile and converting it to/from local format.
310 // <group>
311 char* readTile(const char* external);
312 void writeTile(char* external, const char* local);
313 // </group>
314
315 protected:
316 // # Declare member variables.
317
318 char* cachedTile_p; // optimization to hold one tile chunk
319
320 // Pointer to the parent storage manager.
322 // Is the class used directly or only by a derived class only?
324 // The values of the possible id and coordinate columns.
326 // Is the hypercube extensible?
328 // Dimensionality of the hypercube.
330 // Number of tiles in the hypercube.
332 // The shape of the hypercube.
334 // The shape of the tiles in the hypercube.
336 // The number of tiles in each hypercube dimension.
338 // Precomputed tileShape information.
340 // Precomputed tilesPerDim information.
342 // Number of tiles in all but last dimension (used when extending).
344 // The tilesize in bytes.
346 // Pointer to the TSMFile object holding the data.
348 // Offset in the TSMFile object where the data of this hypercube starts.
350 // Offset for each data column in a tile (in external format).
352 // Offset for each data column in a tile (in local format).
354 // The bucket size in bytes (is equal to tile size in bytes).
356 // The tile size in bytes in local format.
358 // The bucket cache.
360 // Did the user set the cache size?
362 // Was the last column access to a cell, slice, or column?
364 // The slice shape of the last column access to a slice.
366
367 // IPosition variables used in accessSection(); declared here
368 // as member variables to avoid significant construction and
369 // desctruction overhead if they are local to accessSection()
370 // #tiles needed for the section
372 // First tile needed
374 // Last tile needed
376 // First pixel in first tile
378 // Last pixel in first tile
380 // Last pixel in last tile
382};
383
385 if (cache_p == 0) {
386 makeCache();
387 }
388 return cache_p;
389}
390inline uInt TSMCube::bucketSize() const { return bucketSize_p; }
392inline const IPosition& TSMCube::cubeShape() const { return cubeShape_p; }
393inline const IPosition& TSMCube::tileShape() const { return tileShape_p; }
394inline const Record& TSMCube::valueRecord() const { return values_p; }
396inline Bool TSMCube::userSetCache() const { return userSetCache_p; }
398inline const IPosition& TSMCube::getLastColSlice() const { return lastColSlice_p; }
400inline void TSMCube::setLastColSlice(const IPosition& slice) {
401 lastColSlice_p.resize(slice.nelements());
402 lastColSlice_p = slice;
403}
404
405} // namespace casacore
406
407#endif
Cache for buckets in a part of a file.
size_t nelements() const
The number of elements in this IPosition.
Definition IPosition.h:551
String: the storage and methods of handling collections of characters.
Definition String.h:355
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:347
void emptyCache()
Empty the cache.
const Record & valueRecord() const
Get the record containing the id and coordinate values.
Definition TSMCube.h:394
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:365
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:337
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)
Bool matches(const Block< TSMColumn * > &idColSet, const Record &idValues)
Test if the id values match.
IPosition tileShape_p
The shape of the tiles in the hypercube.
Definition TSMCube.h:335
IPosition endPixelInFirstTile_p
Last pixel in first tile.
Definition TSMCube.h:379
Bool userSetCache() const
Determine if the user set the cache size (using setCacheSize).
Definition TSMCube.h:396
Block< uInt > localOffset_p
Offset for each data column in a tile (in local format).
Definition TSMCube.h:353
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:391
void setLastColAccess(AccessType type)
Definition TSMCube.h:399
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:393
uInt tileSize_p
The tilesize in bytes.
Definition TSMCube.h:345
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:351
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:333
uInt nrTiles_p
Number of tiles in the hypercube.
Definition TSMCube.h:331
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:343
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:341
IPosition startPixelInFirstTile_p
First pixel in first tile.
Definition TSMCube.h:377
void setLastColSlice(const IPosition &slice)
Definition TSMCube.h:400
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:363
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:381
Bool useDerived_p
Is the class used directly or only by a derived class only?
Definition TSMCube.h:323
BucketCache * getCache()
Get the cache object.
Definition TSMCube.h:384
uInt nrdim_p
Dimensionality of the hypercube.
Definition TSMCube.h:329
Int64 fileOffset_p
Offset in the TSMFile object where the data of this hypercube starts.
Definition TSMCube.h:349
IPosition endTile_p
Last tile needed.
Definition TSMCube.h:375
const IPosition & cubeShape() const
Get the shape of the hypercube.
Definition TSMCube.h:392
Bool extensible_p
Is the hypercube extensible?
Definition TSMCube.h:327
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:355
TSMCube & operator=(const TSMCube &)=delete
Forbid assignment.
Record values_p
The values of the possible id and coordinate columns.
Definition TSMCube.h:325
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:339
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:105
TiledStMan * stmanPtr_p
Pointer to the parent storage manager.
Definition TSMCube.h:321
Bool userSetCache_p
Did the user set the cache size?
Definition TSMCube.h:361
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:395
IPosition startTile_p
First tile needed.
Definition TSMCube.h:373
BucketCache * cache_p
The bucket cache.
Definition TSMCube.h:359
AccessType getLastColAccess() const
Functions for TSMDataColumn to keep track of the last type of access to a hypercube.
Definition TSMCube.h:397
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:357
const IPosition & getLastColSlice() const
Definition TSMCube.h:398
char * cachedTile_p
Definition TSMCube.h:318
IPosition nrTileSection_p
IPosition variables used in accessSection(); declared here as member variables to avoid significant c...
Definition TSMCube.h:371
uInt bucketSize() const
Get the bucket size (bytes).
Definition TSMCube.h:390
For temporary backward namespace compatibility, use casa as alias for casacore.
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
iterator end()
Definition Block.h:601
unsigned long long uInt64
Definition aipsxtype.h:37