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