casacore
Loading...
Searching...
No Matches
ColumnSet.h
Go to the documentation of this file.
1// # ColumnSet.h: Class to manage a set of table columns
2// # Copyright (C) 1994,1995,1996,1997,1998,1999,2000,2001,2002,2003
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_COLUMNSET_H
27#define TABLES_COLUMNSET_H
28
29// # Includes
30#include <casacore/casa/aips.h>
31#include <casacore/tables/Tables/TableLockData.h>
32#include <casacore/tables/Tables/BaseTable.h>
33#include <casacore/tables/Tables/StorageOption.h>
34#include <casacore/casa/BasicSL/String.h>
35#include <casacore/casa/Arrays/ArrayFwd.h>
36
37#include <map>
38
39namespace casacore { // # NAMESPACE CASACORE - BEGIN
40
41// # Forward Declarations
42class SetupNewTable;
43class Table;
44class TableDesc;
45class TSMOption;
46class BaseTable;
47class TableAttr;
48class ColumnDesc;
49class PlainColumn;
50class DataManager;
51class MultiFile;
52class Record;
53class IPosition;
54class AipsIO;
55
56// <summary>
57// Class to manage a set of table columns
58// </summary>
59
60// <use visibility=local>
61
62// <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="">
63// </reviewed>
64
65// <prerequisite>
66// # Classes you should understand before using this one.
67// <li> PlainTable
68// <li> DataManager
69// </prerequisite>
70
71// <etymology>
72// ColumnSet represent the set of columns in a table.
73// </etymology>
74
75// <synopsis>
76// ColumnSet contains all columns in a plain table (thus not in a RefTable).
77// Furthermore it contains the set of data managers used by the columns
78// in the table.
79//
80// The main purpose of the class is to deal with constructing, writing
81// and reading the column objects. It is used by classes SetupNewTable
82// and Table.
83// </synopsis>
84
85// <todo asof="$DATE:$">
86// # A List of bugs, limitations, extensions or planned refinements.
87// </todo>
88
89class ColumnSet {
90 public:
91 // Construct from the table description.
92 // This creates all underlying filled and virtual column objects.
94
96
97 // Reopen the data managers for read/write.
98 void reopenRW();
99
100 // Rename the necessary subtables in the column keywords.
101 void renameTables(const String& newName, const String& oldName);
102
103 // Get the storage option.
104 const StorageOption& storageOption() const { return storageOpt_p; }
105
106 // Get the possible MultiFileBase object used to combine files.
107 std::shared_ptr<MultiFileBase> getMultiFile() const { return multiFile_p; }
108
109 // Are subtables used in other processes.
111
112 // Get a column by name.
113 PlainColumn* getColumn(const String& columnName) const;
114
115 // Get a column by index.
116 PlainColumn* getColumn(uInt columnIndex) const;
117
118 // Add a data manager.
119 // It increments seqCount_p and returns that as a unique sequence number.
120 // This can, for instance, be used to create a unique file name.
122
123 // Initialize the data managers for a new table.
124 // It creates the data manager column objects for each column
125 // and it allows the data managers to link themselves to the
126 // Table object and to initialize themselves.
127 void initDataManagers(rownr_t nrrow, Bool bigEndian, const TSMOption& tsmOption, Table& tab);
128
129 // Link the ColumnSet object to the BaseTable object.
130 void linkToTable(BaseTable* baseTableObject);
131
132 // Link the ColumnSet object to the TableLockData object.
133 void linkToLockObject(TableLockData* lockObject);
134
135 // Check if the table is locked for read or write.
136 // If manual or permanent locking is in effect, it checks if the
137 // table is properly locked.
138 // If autolocking is in effect, it locks the table when needed.
139 // <group>
140 void checkReadLock(Bool wait);
141 void checkWriteLock(Bool wait);
142 // </group>
143
144 // Inspect the auto lock when the inspection interval has expired and
145 // release it when another process needs the lock.
146 void autoReleaseLock();
147
148 // If needed, get a temporary user lock.
149 // It returns False if the lock was already there.
151
152 // Release a temporary user lock if the given release flag is True.
153 void userUnlock(Bool releaseFlag);
154
155 // Do all data managers and engines allow to add rows?
157
158 // Do all data managers and engines allow to remove rows?
160
161 // Can the given columns be removed from the data manager?
162 Bool canRemoveColumn(const Vector<String>& columnNames) const;
163
164 // Can a column be renamed in the data manager?
165 Bool canRenameColumn(const String& columnName) const;
166
167 // Add rows to all data managers.
168 void addRow(rownr_t nrrow);
169
170 // Remove a row from all data managers.
171 // It will throw an exception if not possible.
172 void removeRow(rownr_t rownr);
173
174 // Remove the columns from the map and the data manager.
175 void removeColumn(const Vector<String>& columnNames);
176
177 // Rename the column in the map.
178 void renameColumn(const String& newName, const String& oldName);
179
180 // Add a column to the table.
181 // The default implementation throws an "invalid operation" exception.
182 // <group>
183 void addColumn(const ColumnDesc& columnDesc, Bool bigEndian, const TSMOption& tsmOption,
184 Table& tab);
185 void addColumn(const ColumnDesc& columnDesc, const String& dataManager, Bool byName,
186 Bool bigEndian, const TSMOption& tsmOption, Table& tab);
187 void addColumn(const ColumnDesc& columnDesc, const DataManager& dataManager, Bool bigEndian,
188 const TSMOption& tsmOption, Table& tab);
189 void addColumn(const TableDesc& tableDesc, const DataManager& dataManager, Bool bigEndian,
190 const TSMOption& tsmOption, Table& tab);
191 // </group>
192
193 // Get nr of rows.
194 rownr_t nrow() const;
195
196 // Get the actual table description.
198
199 // Get the data manager info.
200 // Optionally only the virtual engines are retrieved.
201 Record dataManagerInfo(Bool virtualOnly = False) const;
202
203 // Get the trace-id of the table.
204 int traceId() const { return baseTablePtr_p->traceId(); }
205
206 // Initialize rows startRownr till endRownr (inclusive).
207 void initialize(rownr_t startRownr, rownr_t endRownr);
208
209 // Write all the data and let the data managers flush their data.
210 // This function is called when a table gets written (i.e. flushed).
211 // It returns True if any data manager wrote something.
212 Bool putFile(Bool writeTable, AipsIO&, const TableAttr&, Bool fsync);
213
214 // Read the data, reconstruct the data managers, and link those to
215 // the table object.
216 // This function gets called when an existing table is read back.
217 // It returns the number of rows in case a data manager thinks there are
218 // more. That is in particular used by LofarStMan.
219 rownr_t getFile(AipsIO&, Table& tab, rownr_t nrrow, Bool bigEndian, const TSMOption& tsmOption);
220
221 // Set the table to being changed.
222 void setTableChanged();
223
224 // Get the data manager change flags (used by PlainTable).
226
227 // Synchronize the data managers when data in them have changed.
228 // It returns the number of rows it think it has, which is needed for
229 // storage managers like LofarStMan.
230 // <src>forceSync=True</src> means that the data managers are forced
231 // to do a sync. Otherwise the contents of the lock file tell if a data
232 // manager has to sync.
233 rownr_t resync(rownr_t nrrow, Bool forceSync);
234
235 // Invalidate the column caches for all columns.
237
238 // Get the correct data manager.
239 // This is used by the column objects to link themselves to the
240 // correct datamanagers when they are read back.
242
243 // Check if no double data manager names have been given.
244 void checkDataManagerNames(const String& tableName) const;
245
246 // Find the data manager with the given name or for the given column.
247 // If the data manager or column is unknown, an exception is thrown.
248 // A blank name means the data manager is unknown.
249 DataManager* findDataManager(const String& name, Bool byColumn = False) const;
250
251 // Make a unique data manager name by appending a suffix _n if needed
252 // where n is a number that makes the name unique.
253 String uniqueDataManagerName(const std::string& name) const;
254
255 // Synchronize the columns after it appeared that data in the
256 // main table file have changed.
257 // It cannot deal with changes in number of columns, so it throws an
258 // exception when they have changed.
259 // Keywords in all columns are updated.
260 // The other ColumnSet gives the new data.
261 void syncColumns(const ColumnSet& other, const TableAttr& defaultAttr);
262
263 private:
264 // Remove the last data manager (used by addColumn after an exception).
265 // It does the opposite of addDataManager.
267
268 // Let the data managers (from the given index on) initialize themselves.
270
271 // Let the data managers (from the given index on) prepare themselves.
273
274 // Open or create the MultiFile if needed.
276
277 // Check if a data manager name has not already been used.
278 // Start checking at the given index in the array.
279 // It returns False if the name has already been used.
280 // By default an exception is thrown if the name has already been used.
281 Bool checkDataManagerName(const String& name, uInt from, const String& tableName,
282 Bool doTthrow = True) const;
283
284 // Do the actual addition of a column.
285 void doAddColumn(const ColumnDesc& columnDesc, DataManager* dataManPtr);
286
287 // Check if columns to be removed can be removed.
288 // It returns a map of DataManager* telling how many columns for
289 // a data manager have to be removed. A count of -1 means that all
290 // columns have to be removed. For such columns the flag in the
291 // returned Block is False, otherwise True.
292 std::map<void*, Int> checkRemoveColumn(const Vector<String>& columnNames);
293
294 // Check if the table is locked for read or write.
295 // If manual or permanent locking is in effect, it checks if the
296 // table is properly locked.
297 // If autolocking is in effect, it locks the table when needed.
299
300 // # Declare the variables.
303 std::shared_ptr<MultiFileBase> multiFile_p;
304 rownr_t nrrow_p; // # #rows
306 TableLockData* lockPtr_p; // # lock object
307 std::map<String, void*> colMap_p; // # list of PlainColumns
308 uInt seqCount_p; // # sequence number count
309 // # (used for unique seqnr)
310 Block<void*> blockDataMan_p; // # list of data managers
311 Block<Bool> dataManChanged_p; // # data has changed
312};
313
314inline rownr_t ColumnSet::nrow() const { return nrrow_p; }
315inline void ColumnSet::linkToTable(BaseTable* baseTableObject) { baseTablePtr_p = baseTableObject; }
316inline void ColumnSet::setTableChanged() { baseTablePtr_p->setTableChanged(); }
317inline void ColumnSet::linkToLockObject(TableLockData* lockObject) { lockPtr_p = lockObject; }
319 if (lockPtr_p->readLocking() && !lockPtr_p->hasLock(FileLocker::Read)) {
321 }
322}
324 if (!lockPtr_p->hasLock(FileLocker::Write)) {
326 }
327}
328inline void ColumnSet::userUnlock(Bool releaseFlag) {
329 if (releaseFlag) {
330 lockPtr_p->release();
331 }
332}
333inline void ColumnSet::autoReleaseLock() { lockPtr_p->autoRelease(); }
335
336} // namespace casacore
337
338#endif
OpenOption
Define the possible ByteIO open options.
Definition ByteIO.h:60
void renameTables(const String &newName, const String &oldName)
Rename the necessary subtables in the column keywords.
void removeRow(rownr_t rownr)
Remove a row from all data managers.
Bool canRenameColumn(const String &columnName) const
Can a column be renamed in the data manager?
void autoReleaseLock()
Inspect the auto lock when the inspection interval has expired and release it when another process ne...
Definition ColumnSet.h:333
Bool canAddRow() const
Do all data managers and engines allow to add rows?
BaseTable * baseTablePtr_p
Definition ColumnSet.h:305
void initSomeDataManagers(uInt from, Table &tab)
Let the data managers (from the given index on) initialize themselves.
void linkToTable(BaseTable *baseTableObject)
Link the ColumnSet object to the BaseTable object.
Definition ColumnSet.h:315
void linkToLockObject(TableLockData *lockObject)
Link the ColumnSet object to the TableLockData object.
Definition ColumnSet.h:317
void prepareSomeDataManagers(uInt from)
Let the data managers (from the given index on) prepare themselves.
DataManager * findDataManager(const String &name, Bool byColumn=False) const
Find the data manager with the given name or for the given column.
void removeLastDataManager()
Remove the last data manager (used by addColumn after an exception).
PlainColumn * getColumn(uInt columnIndex) const
Get a column by index.
DataManager * getDataManager(uInt seqnr) const
Get the correct data manager.
Bool userLock(FileLocker::LockType, Bool wait)
If needed, get a temporary user lock.
void addColumn(const TableDesc &tableDesc, const DataManager &dataManager, Bool bigEndian, const TSMOption &tsmOption, Table &tab)
void doAddColumn(const ColumnDesc &columnDesc, DataManager *dataManPtr)
Do the actual addition of a column.
void syncColumns(const ColumnSet &other, const TableAttr &defaultAttr)
Synchronize the columns after it appeared that data in the main table file have changed.
std::map< void *, Int > checkRemoveColumn(const Vector< String > &columnNames)
Check if columns to be removed can be removed.
Bool canRemoveColumn(const Vector< String > &columnNames) const
Can the given columns be removed from the data manager?
void invalidateColumnCaches()
Invalidate the column caches for all columns.
void addColumn(const ColumnDesc &columnDesc, const String &dataManager, Bool byName, Bool bigEndian, const TSMOption &tsmOption, Table &tab)
void removeColumn(const Vector< String > &columnNames)
Remove the columns from the map and the data manager.
void setTableChanged()
Set the table to being changed.
Definition ColumnSet.h:316
std::shared_ptr< MultiFileBase > multiFile_p
Definition ColumnSet.h:303
void checkReadLock(Bool wait)
Check if the table is locked for read or write.
Definition ColumnSet.h:318
TableLockData * lockPtr_p
Definition ColumnSet.h:306
PlainColumn * getColumn(const String &columnName) const
Get a column by name.
Bool putFile(Bool writeTable, AipsIO &, const TableAttr &, Bool fsync)
Write all the data and let the data managers flush their data.
void initDataManagers(rownr_t nrrow, Bool bigEndian, const TSMOption &tsmOption, Table &tab)
Initialize the data managers for a new table.
ColumnSet(TableDesc *, const StorageOption &=StorageOption())
Construct from the table description.
void renameColumn(const String &newName, const String &oldName)
Rename the column in the map.
std::map< String, void * > colMap_p
Definition ColumnSet.h:307
void checkDataManagerNames(const String &tableName) const
Check if no double data manager names have been given.
void openMultiFile(uInt from, const Table &tab, ByteIO::OpenOption)
Open or create the MultiFile if needed.
rownr_t nrow() const
Get nr of rows.
Definition ColumnSet.h:314
StorageOption storageOpt_p
Definition ColumnSet.h:302
int traceId() const
Get the trace-id of the table.
Definition ColumnSet.h:204
TableDesc actualTableDesc() const
Get the actual table description.
void addColumn(const ColumnDesc &columnDesc, Bool bigEndian, const TSMOption &tsmOption, Table &tab)
Add a column to the table.
void addDataManager(DataManager *)
Add a data manager.
void checkWriteLock(Bool wait)
Definition ColumnSet.h:323
Bool checkDataManagerName(const String &name, uInt from, const String &tableName, Bool doTthrow=True) const
Check if a data manager name has not already been used.
void initialize(rownr_t startRownr, rownr_t endRownr)
Initialize rows startRownr till endRownr (inclusive).
std::shared_ptr< MultiFileBase > getMultiFile() const
Get the possible MultiFileBase object used to combine files.
Definition ColumnSet.h:107
Record dataManagerInfo(Bool virtualOnly=False) const
Get the data manager info.
const StorageOption & storageOption() const
Get the storage option.
Definition ColumnSet.h:104
void addColumn(const ColumnDesc &columnDesc, const DataManager &dataManager, Bool bigEndian, const TSMOption &tsmOption, Table &tab)
Block< void * > blockDataMan_p
Definition ColumnSet.h:310
Bool canRemoveRow() const
Do all data managers and engines allow to remove rows?
Block< Bool > & dataManChanged()
Get the data manager change flags (used by PlainTable).
Definition ColumnSet.h:334
Bool areTablesMultiUsed() const
Are subtables used in other processes.
void doLock(FileLocker::LockType, Bool wait)
Check if the table is locked for read or write.
void reopenRW()
Reopen the data managers for read/write.
Block< Bool > dataManChanged_p
Definition ColumnSet.h:311
String uniqueDataManagerName(const std::string &name) const
Make a unique data manager name by appending a suffix _n if needed where n is a number that makes the...
void addRow(rownr_t nrrow)
Add rows to all data managers.
rownr_t getFile(AipsIO &, Table &tab, rownr_t nrrow, Bool bigEndian, const TSMOption &tsmOption)
Read the data, reconstruct the data managers, and link those to the table object.
TableDesc * tdescPtr_p
Definition ColumnSet.h:301
rownr_t resync(rownr_t nrrow, Bool forceSync)
Synchronize the data managers when data in them have changed.
void userUnlock(Bool releaseFlag)
Release a temporary user lock if the given release flag is True.
Definition ColumnSet.h:328
Abstract base class for a data manager.
LockType
Define the possible lock types.
Definition FileLocker.h:89
@ Write
Acquire a write lock.
Definition FileLocker.h:93
@ Read
Acquire a read lock.
Definition FileLocker.h:91
Create a new table - define shapes, data managers, etc.
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
const Bool False
Definition aipstype.h:42
unsigned int uInt
Definition aipstype.h:49
String name() const
Return the name of the field.
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