casacore
Loading...
Searching...
No Matches
SSMBase.h
Go to the documentation of this file.
1//# SSMBase.h: Base class of the Standard Storage Manager
2//# Copyright (C) 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_SSMBASE_H
27#define TABLES_SSMBASE_H
28
29
30//# Includes
31#include <casacore/casa/aips.h>
32#include <casacore/tables/DataMan/DataManager.h>
33#include <casacore/casa/Containers/Block.h>
34
35namespace casacore { //# NAMESPACE CASACORE - BEGIN
36
37//# Forward declarations
38class BucketCache;
39class BucketFile;
40class StManArrayFile;
41class SSMIndex;
42class SSMColumn;
43class SSMStringHandler;
44
45// <summary>
46// Base class of the Standard Storage Manager
47// </summary>
48
49// <use visibility=local>
50
51// <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="tStandardStMan.cc">
52// </reviewed>
53
54// <prerequisite>
55//# Classes you should understand before using this one.
56// <li> <linkto class=StandardStMan>StandardStMan</linkto>
57// <li> <linkto class=SSMColumn>SSMColumn</linkto>
58// </prerequisite>
59
60// <etymology>
61// SSMBase is the base class of the Standard Storage Manager.
62// </etymology>
63
64// <synopsis>
65// The global principles of this class are described in
66// <linkto class="StandardStMan:description">StandardStMan</linkto>.
67// <p>
68// The Standard Storage Manager divides the data file in equally sized
69// chunks called buckets. There are 3 types of buckets:
70// <ul>
71// <li> Data buckets containing the fixed length data (scalars and
72// direct arrays of data type Int, Float, Bool, etc.).
73// For variable shaped data (strings and indirect arrays) they
74// contain references to the actual data position in the
75// string buckets or in an external file.
76// <li> String buckets containing strings and array of strings.
77// <li> Index buckets containing the index info for the data buckets.
78// </ul>
79// Bucket access is handled by class
80// <linkto class=BucketCache>BucketCache</linkto>.
81// It also keeps a list of free buckets. A bucket is freed when it is
82// not needed anymore (e.g. all data from it are deleted).
83// <p>
84// Data buckets form the main part of the SSM. The data can be viewed as
85// a few streams of buckets, where each stream contains the data of
86// a given number of columns. Each stream has an
87// <linkto class=SSMIndex>SSMIndex</linkto> object describing the
88// number of rows stored in each data bucket of the stream.
89// The SSM starts with a single bucket stream (holding all columns),
90// but when columns are added, new bucket streams might be created.
91// <p>
92// For example, we have an SSM with a bucket size of 100 bytes.
93// There are 5 Int columns (A,B,C,D,E) each taking 4 bytes per row.
94// Column A, B, C, and D are stored in bucket stream 1, while column
95// E is stored in bucket stream 2. So in stream 1 each bucket can hold
96// 6 rows, while in stream 2 each bucket can hold 25 rows.
97// For a 100 row table it will result in 17+4 data buckets.
98// <p>
99// A few classes collaborate to make it work:
100// <ul>
101// <li> Each bucket stream has an <linkto class=SSMIndex>SSMIndex</linkto>
102// object to map row number to bucket number.
103// Note that in principle each bucket in a stream contains the same
104// number of rows. However, when a row is deleted it is removed
105// from its bucket shifting the remainder to the left. Data in the
106// next buckets is not shifted, so that bucket has now one row less.
107// <li> For each column SSMBase knows to which bucket stream it belongs
108// and at which offset the column starts in a bucket.
109// Note that column data in a bucket are adjacent, which is done
110// to make it easier to use the
111// <linkto class=ColumnCache>ColumnCache</linkto> object in SSMColumn
112// and to be able to efficiently store Bool values as bits.
113// <li> Each column has an <linkto class=SSMColumn>SSMColumn</linkto>
114// object knowing how many bits each data cell takes in a bucket.
115// The SSMColumn objects handle all access to data in the columns
116// (using SSMBase and SSMIndex).
117// </ul>
118// <p>
119// String buckets are used by class
120// <linkto class=SSMStringHandler>SSMStringHandler</linkto> to
121// store scalar strings and fixed and variable shaped arrays of strings.
122// The bucketnr, offset, and length of such string (arrays) are stored
123// in the data buckets.
124// <br>
125// Indirect arrays of other data types are also stored indirectly
126// and their offset is stored in the data buckets. Such arrays are
127// handled by class <linkto class=StIndArray>StIndArray</linkto>
128// which uses an extra file to store the arrays.
129// <p>
130// Index buckets are used by SSMBase to make the SSMIndex data persistent.
131// It uses alternately 2 sets of index buckets. In that way there is
132// always an index availanle in case the system crashes.
133// If possible 2 halfs of a single bucket are used alternately, otherwise
134// separate buckets are used.
135// </synopsis>
136
137// <motivation>
138// The public interface of SSMBase is quite large, because the other
139// internal SSM classes need these functions. To have a class with a
140// minimal interface for the normal user, class <src>StandardStMan</src>
141// is derived from it.
142// <br>StandardStMan needs an isA- instead of hasA-relation to be
143// able to bind columns to it in class <linkto class=SetupNewTable>
144// SetupNewTable</linkto>.
145// </motivation>
146
147// <todo asof="$DATE:$">
148//# A List of bugs, limitations, extensions or planned refinements.
149// <li> Remove AipsIO argument from open and close.
150// <li> When only 1 bucket in use addcolumn can check if there's enough
151// room to fit the new column (so rearange the bucket) in the free
152// row space.
153// </todo>
154
155
156class SSMBase: public DataManager
157{
158public:
159 // Create a Standard storage manager with default name SSM.
160 explicit SSMBase (Int aBucketSize=0,
161 uInt aCacheSize=1);
162
163 // Create a Standard storage manager with the given name.
164 explicit SSMBase (const String& aDataManName,
165 Int aBucketSize=0,
166 uInt aCacheSize=1);
167
168 // Create a Standard storage manager with the given name.
169 // The specifications are part of the record (as created by dataManagerSpec).
170 SSMBase (const String& aDataManName,
171 const Record& spec);
172
174
175 // Clone this object.
176 // It does not clone SSMColumn objects possibly used.
177 // The caller has to delete the newly created object.
178 virtual DataManager* clone() const;
179
180 // Get the type name of the data manager (i.e. StandardStMan).
181 virtual String dataManagerType() const;
182
183 // Get the name given to the storage manager (in the constructor).
184 virtual String dataManagerName() const;
185
186 // Record a record containing data manager specifications.
187 virtual Record dataManagerSpec() const;
188
189 // Get data manager properties that can be modified.
190 // It is only ActualCacheSize (the actual cache size in buckets).
191 // It is a subset of the data manager specification.
192 virtual Record getProperties() const;
193
194 // Modify data manager properties.
195 // Only MaxCacheSize can be used. It is similar to function setCacheSize
196 // with <src>canExceedNrBuckets=False</src>.
197 virtual void setProperties (const Record& spec);
198
199 // Get the version of the class.
201
202 // Set the cache size (in buckets).
203 // If <src>canExceedNrBuckets=True</src>, the given cache size can be
204 // larger than the nr of buckets in the file. In this way the cache can
205 // be made large enough for a future file extension.
206 // Otherwise, it is limited to the actual number of buckets. This is useful
207 // if one wants the entire file to be cached.
208 void setCacheSize (uInt aCacheSize, Bool canExceedNrBuckets=True);
209
210 // Get the current cache size (in buckets).
211 uInt getCacheSize() const;
212
213 // Clear the cache used by this storage manager.
214 // It will flush the cache as needed and remove all buckets from it.
216
217 // Show the statistics of all caches used.
218 virtual void showCacheStatistics (ostream& anOs) const;
219
220 // Show statistics of all indices used.
221 void showIndexStatistics (ostream & anOs) const;
222
223 // Show statistics of the Base offsets/index etc.
224 void showBaseStatistics (ostream & anOs) const;
225
226 // Get the bucket size.
227 uInt getBucketSize() const;
228
229 // Get the number of rows in this storage manager.
230 rownr_t getNRow() const;
231
232 // The storage manager can add rows.
233 virtual Bool canAddRow() const;
234
235 // The storage manager can delete rows.
236 virtual Bool canRemoveRow() const;
237
238 // The storage manager can add columns.
239 virtual Bool canAddColumn() const;
240
241 // The storage manager can delete columns.
242 virtual Bool canRemoveColumn() const;
243
244 // Make the object from the type name string.
245 // This function gets registered in the DataManager "constructor" map.
246 // The caller has to delete the object.
247 static DataManager* makeObject (const String& aDataManType,
248 const Record& spec);
249
250 // Get access to the given column.
251 SSMColumn& getColumn (uInt aColNr);
252
253 // Get access to the given Index.
254 SSMIndex& getIndex (uInt anIdxNr);
255
256 // Make the current bucket in the cache dirty (i.e. something has been
257 // changed in it and it needs to be written when removed from the cache).
258 // (used by SSMColumn::putValue).
260
261 // Open (if needed) the file for indirect arrays with the given mode.
262 // Return a pointer to the object.
264
265 // Find the bucket containing the column and row and return the pointer
266 // to the beginning of the column data in that bucket.
267 // It also fills in the start and end row for the column data.
268 char* find (rownr_t aRowNr, uInt aColNr,
269 rownr_t& aStartRow, rownr_t& anEndRow,
270 const String& colName);
271
272 // Add a new bucket and get its bucket number.
274
275 // Read the bucket (if needed) and return the pointer to it.
276 char* getBucket (uInt aBucketNr);
277
278 // Remove a bucket from the bucket cache.
279 void removeBucket (uInt aBucketNr);
280
281 // Get rows per bucket for the given column.
282 uInt getRowsPerBucket (uInt aColumn) const;
283
284 // Return a pointer to the (one and only) StringHandler object.
286
287 // <group>
288 // Callbacks for BucketCache access.
289 static char* readCallBack (void* anOwner, const char* aBucketStorage);
290 static void writeCallBack (void* anOwner, char* aBucketStorage,
291 const char* aBucket);
292 static void deleteCallBack (void*, char* aBucket);
293 static char* initCallBack (void* anOwner);
294 // </group>
295
296private:
297 // Copy constructor (only meant for clone function).
298 SSMBase (const SSMBase& that);
299
300 // Assignment cannot be used.
302
303 // (Re)create the index, file, and cache object.
304 // It is used when all rows are deleted from the table.
305 void recreate();
306
307 // The data manager supports use of MultiFile.
308 virtual Bool hasMultiFileSupport() const;
309
310 // Flush and optionally fsync the data.
311 // It returns a True status if it had to flush (i.e. if data have changed).
312 virtual Bool flush (AipsIO&, Bool doFsync);
313
314 // Let the storage manager create files as needed for a new table.
315 // This allows a column with an indirect array to create its file.
316 virtual void create64 (rownr_t aNrRows);
317
318 // Open the storage manager file for an existing table, read in
319 // the data, and let the SSMColumn objects read their data.
320 virtual rownr_t open64 (rownr_t aRowNr, AipsIO&);
321
322 // Resync the storage manager with the new file contents.
323 // This is done by clearing the cache.
324 virtual rownr_t resync64 (rownr_t aRowNr);
325
326 // Reopen the storage manager files for read/write.
327 virtual void reopenRW();
328
329 // The data manager will be deleted (because all its columns are
330 // requested to be deleted).
331 // So clean up the things needed (e.g. delete files).
332 virtual void deleteManager();
333
334 // Let the storage manager initialize itself (upon creation).
335 // It determines the bucket size and fills the index.
336 void init();
337
338 // Determine and set the bucket size.
339 // It returns the number of rows per bucket.
341
342 // Get the number of indices in use.
343 uInt getNrIndices() const;
344
345 // Add rows to the storage manager.
346 // Per column it extends number of rows.
347 virtual void addRow64 (rownr_t aNrRows);
348
349 // Delete a row from all columns.
350 virtual void removeRow64 (rownr_t aRowNr);
351
352 // Do the final addition of a column.
354
355 // Remove a column from the data file.
357
358 // Create a column in the storage manager on behalf of a table column.
359 // The caller has to delete the newly created object.
360 // <group>
361 // Create a scalar column.
363 int aDataType,
364 const String& aDataTypeID);
365 // Create a direct array column.
367 int aDataType,
368 const String& aDataTypeID);
369 // Create an indirect array column.
371 int aDataType,
372 const String& aDataTypeID);
373 // </group>
374
375 // Get the cache object.
376 // This will construct the cache object if not present yet.
377 // The cache object will be deleted by the destructor.
379
380 // Construct the cache object (if not constructed yet).
381 void makeCache();
382
383 // Read the header.
385
386 // Read the index from its buckets.
388
389 // Write the header and the indices.
391
392
393 //# Declare member variables.
394 // Name of data manager.
396
397 // The file containing the indirect arrays.
399
400 // The number of rows in the columns.
402
403 // Column offset
405
406 // Row Index ID containing all the columns in a bucket
408
409 // Will contain all indices
411
412 // The cache with the SSM buckets.
414
415 // The file containing all data.
417
418 // String handler class
420
421 // The persistent cache size.
423
424 // The actual cache size.
426
427 // The initial number of buckets in the cache.
429
430 // Nr of buckets needed for index.
432
433 // Number of the first index bucket
435
436 // Offset of index in first bucket.
437 // If >0, the index fits in a single bucket.
439
440 // Number of the first String Bucket
442
443 // length of index memoryblock
445
446 // The nr of free buckets.
448
449 // The first free bucket.
451
452 // The bucket size.
455
456 // The assembly of all columns.
458
459 // Has the data changed since the last flush?
461};
462
463
465{
466 return itsPtrIndex.nelements();
467}
468
470{
471 return itsCacheSize;
472}
473
475{
476 return itsNrRows;
477}
478
480{
481 return itsBucketSize;
482}
483
485{
486 if (itsCache == 0) {
487 makeCache();
488 }
489 return *itsCache;
490}
491
493{
494 return *(itsPtrColumn[aColNr]);
495}
496
498{
499 return *(itsPtrIndex[anIdxNr]);
500}
501
506
507
508
509} //# NAMESPACE CASACORE - END
510
511#endif
simple 1-D array
Definition Block.h:198
Cache for buckets in a part of a file.
OpenOption
Define the possible ByteIO open options.
Definition ByteIO.h:63
Abstract base class for a data manager.
A drop-in replacement for Block<T*>.
Definition Block.h:812
SSMIndex & getIndex(uInt anIdxNr)
Get access to the given Index.
Definition SSMBase.h:497
virtual void create64(rownr_t aNrRows)
Let the storage manager create files as needed for a new table.
static char * readCallBack(void *anOwner, const char *aBucketStorage)
Callbacks for BucketCache access.
SSMStringHandler * getStringHandler()
Return a pointer to the (one and only) StringHandler object.
Definition SSMBase.h:502
SSMColumn & getColumn(uInt aColNr)
Get access to the given column.
Definition SSMBase.h:492
void makeCache()
Construct the cache object (if not constructed yet).
uInt itsIndexLength
length of index memoryblock
Definition SSMBase.h:444
static void deleteCallBack(void *, char *aBucket)
uInt getCacheSize() const
Get the current cache size (in buckets).
Definition SSMBase.h:469
Int itsLastStringBucket
Number of the first String Bucket.
Definition SSMBase.h:441
virtual void addRow64(rownr_t aNrRows)
Add rows to the storage manager.
char * find(rownr_t aRowNr, uInt aColNr, rownr_t &aStartRow, rownr_t &anEndRow, const String &colName)
Find the bucket containing the column and row and return the pointer to the beginning of the column d...
uInt itsPersCacheSize
The persistent cache size.
Definition SSMBase.h:422
virtual Record dataManagerSpec() const
Record a record containing data manager specifications.
static char * initCallBack(void *anOwner)
PtrBlock< SSMIndex * > itsPtrIndex
Will contain all indices.
Definition SSMBase.h:410
uInt getRowsPerBucket(uInt aColumn) const
Get rows per bucket for the given column.
SSMBase & operator=(const SSMBase &that)
Assignment cannot be used.
uInt itsBucketSize
The bucket size.
Definition SSMBase.h:453
SSMStringHandler * itsStringHandler
String handler class.
Definition SSMBase.h:419
BucketCache * itsCache
The cache with the SSM buckets.
Definition SSMBase.h:413
uInt getNewBucket()
Add a new bucket and get its bucket number.
uInt itsNrBuckets
The initial number of buckets in the cache.
Definition SSMBase.h:428
void recreate()
(Re)create the index, file, and cache object.
String itsDataManName
Name of data manager.
Definition SSMBase.h:395
void init()
Let the storage manager initialize itself (upon creation).
void readHeader()
Read the header.
void showBaseStatistics(ostream &anOs) const
Show statistics of the Base offsets/index etc.
virtual DataManagerColumn * makeScalarColumn(const String &aName, int aDataType, const String &aDataTypeID)
Create a column in the storage manager on behalf of a table column.
virtual Bool canAddColumn() const
The storage manager can add columns.
virtual DataManagerColumn * makeDirArrColumn(const String &aName, int aDataType, const String &aDataTypeID)
Create a direct array column.
StManArrayFile * openArrayFile(ByteIO::OpenOption anOpt)
Open (if needed) the file for indirect arrays with the given mode.
uInt itsFreeBucketsNr
The nr of free buckets.
Definition SSMBase.h:447
Int itsFirstIdxBucket
Number of the first index bucket.
Definition SSMBase.h:434
BucketFile * itsFile
The file containing all data.
Definition SSMBase.h:416
Block< uInt > itsColIndexMap
Row Index ID containing all the columns in a bucket.
Definition SSMBase.h:407
rownr_t getNRow() const
Get the number of rows in this storage manager.
Definition SSMBase.h:474
Int itsFirstFreeBucket
The first free bucket.
Definition SSMBase.h:450
void setCacheSize(uInt aCacheSize, Bool canExceedNrBuckets=True)
Set the cache size (in buckets).
virtual Bool hasMultiFileSupport() const
The data manager supports use of MultiFile.
SSMBase(const String &aDataManName, Int aBucketSize=0, uInt aCacheSize=1)
Create a Standard storage manager with the given name.
uInt getVersion() const
Get the version of the class.
virtual Record getProperties() const
Get data manager properties that can be modified.
virtual void setProperties(const Record &spec)
Modify data manager properties.
static void writeCallBack(void *anOwner, char *aBucketStorage, const char *aBucket)
uInt itsIdxBucketOffset
Offset of index in first bucket.
Definition SSMBase.h:438
virtual DataManagerColumn * makeIndArrColumn(const String &aName, int aDataType, const String &aDataTypeID)
Create an indirect array column.
void removeBucket(uInt aBucketNr)
Remove a bucket from the bucket cache.
rownr_t itsNrRows
The number of rows in the columns.
Definition SSMBase.h:401
void showIndexStatistics(ostream &anOs) const
Show statistics of all indices used.
SSMBase(const String &aDataManName, const Record &spec)
Create a Standard storage manager with the given name.
BucketCache & getCache()
Get the cache object.
Definition SSMBase.h:484
char * getBucket(uInt aBucketNr)
Read the bucket (if needed) and return the pointer to it.
virtual String dataManagerType() const
Get the type name of the data manager (i.e.
virtual void removeRow64(rownr_t aRowNr)
Delete a row from all columns.
uInt getNrIndices() const
Get the number of indices in use.
Definition SSMBase.h:464
uInt itsCacheSize
The actual cache size.
Definition SSMBase.h:425
void setBucketDirty()
Make the current bucket in the cache dirty (i.e.
SSMBase(Int aBucketSize=0, uInt aCacheSize=1)
Create a Standard storage manager with default name SSM.
virtual Bool flush(AipsIO &, Bool doFsync)
Flush and optionally fsync the data.
virtual Bool canAddRow() const
The storage manager can add rows.
Block< uInt > itsColumnOffset
Column offset.
Definition SSMBase.h:404
virtual void reopenRW()
Reopen the storage manager files for read/write.
PtrBlock< SSMColumn * > itsPtrColumn
The assembly of all columns.
Definition SSMBase.h:457
static DataManager * makeObject(const String &aDataManType, const Record &spec)
Make the object from the type name string.
virtual DataManager * clone() const
Clone this object.
virtual void showCacheStatistics(ostream &anOs) const
Show the statistics of all caches used.
virtual void deleteManager()
The data manager will be deleted (because all its columns are requested to be deleted).
void readIndexBuckets()
Read the index from its buckets.
virtual void removeColumn(DataManagerColumn *)
Remove a column from the data file.
uInt setBucketSize()
Determine and set the bucket size.
virtual Bool canRemoveRow() const
The storage manager can delete rows.
virtual rownr_t resync64(rownr_t aRowNr)
Resync the storage manager with the new file contents.
virtual String dataManagerName() const
Get the name given to the storage manager (in the constructor).
StManArrayFile * itsIosFile
The file containing the indirect arrays.
Definition SSMBase.h:398
virtual Bool canRemoveColumn() const
The storage manager can delete columns.
uInt getBucketSize() const
Get the bucket size.
Definition SSMBase.h:479
SSMBase(const SSMBase &that)
Copy constructor (only meant for clone function).
void writeIndex()
Write the header and the indices.
uInt itsNrIdxBuckets
Nr of buckets needed for index.
Definition SSMBase.h:431
Bool isDataChanged
Has the data changed since the last flush?
Definition SSMBase.h:460
void clearCache()
Clear the cache used by this storage manager.
virtual rownr_t open64(rownr_t aRowNr, AipsIO &)
Open the storage manager file for an existing table, read in the data, and let the SSMColumn objects ...
virtual void addColumn(DataManagerColumn *)
Do the final addition of a column.
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 int uInt
Definition aipstype.h:49
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
uInt64 rownr_t
Define the type of a row number in a table.
Definition aipsxtype.h:44