casacore
Loading...
Searching...
No Matches
RefTable.h
Go to the documentation of this file.
1// # RefTable.h: Class for a table as a view of another table
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_REFTABLE_H
27#define TABLES_REFTABLE_H
28
29// # Includes
30#include <casacore/casa/aips.h>
31#include <casacore/tables/Tables/BaseTable.h>
32#include <casacore/casa/BasicSL/String.h>
33#include <casacore/casa/Arrays/Vector.h>
34#include <map>
35
36namespace casacore { // # NAMESPACE CASACORE - BEGIN
37
38// # Forward Declarations
39class TSMOption;
40class RefColumn;
41class AipsIO;
42
43// <summary>
44// Class for a table as a view of another table
45// </summary>
46
47// <use visibility=local>
48
49// <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="">
50// </reviewed>
51
52// <prerequisite>
53// # Classes you should understand before using this one.
54// <li> BaseTable
55// <li> RefColumn
56// </prerequisite>
57
58// <etymology>
59// RefTable represents a table which is a view on another table,
60// thus which references another table.
61// </etymology>
62
63// <synopsis>
64// RefTable is used to make a view on another table.
65// Usually it is a view on a subset of the table, either in vertical
66// or horizontal direction. Thus a subset of rows and/or columns.
67// It will be the result of a select, sort, project or iterate function.
68//
69// It acts to the user as a normal table. All gets and puts are
70// handled by RefColumn which directs them to the referenced column
71// while (if needed) converting the given row number to the row number
72// in the referenced table. For that purpose RefTable maintains a
73// Vector of the row numbers in the referenced table.
74//
75// The RefTable constructor acts in a way that it will always reference
76// the original table. This means that if a select is done on a RefTable,
77// the resulting RefTable will also reference the original PlainTable.
78// This is done to avoid long chains of RefTables.
79// However, if ever some other kind of table views are introduced
80// (like a join or a concatenation of similar tables), this cannot be
81// used anymore. Most software already anticipates on that. The only
82// exception is the code anding, oring tables (refAnd, etc.).
83// </synopsis>
84
85// <todo asof="$DATE:$">
86// # A List of bugs, limitations, extensions or planned refinements.
87// <li> Maybe not allocating the row number vector for a projection.
88// This saves space and time, but each rownr conversion will
89// take a bit more time because it has to test if there is a vector.
90// <li> Maybe maintain a Vector<String> telling on which columns
91// the table is ordered. This may speed up selection, but
92// it is hard to check if the order is changed by a put.
93// <li> Allow to remove a row or column from the RefTable
94// <li> Allow to rename a column in the RefTable
95// <li> Maybe implement doSort one time for a more efficient sort.
96// (now everything is handled by BaseTable).
97// </todo>
98
99class RefTable : public BaseTable {
100 public:
101 // Create a reference table object referencing the
102 // given BaseTable object.
103 // If the BaseTable is actually another RefTable, it will reference
104 // its referenced table (thus the original table) and it will
105 // take its vector of row numbers and projected column names
106 // into account. Thus if a select is done on a projected table,
107 // the resulting RefTable will have the same projection.
108 // <group>
109 // Construct a RefTable with an empty row number vector.
110 // rowOrder=True indicates that the order of the rows will not
111 // be disturbed (as will be the case for a sort).
112 // A row number vector of the given size is initially allocated.
113 // Later this RefTable will be filled in by the select, etc..
115
116 // A RefTable with the given row numbers is constructed.
118
119 // Create a reference table object out of a mask.
120 // The row number vector will consist of the rows for which the
121 // mask has a True value.
122 // The length of the mask must be the number of rows in the BaseTable.
123 RefTable(BaseTable*, const Vector<Bool>& rowMask);
124
125 // Create a reference table object via projection (i.e. column selection).
126 // The row number vector is a copy of the given table.
127 RefTable(BaseTable*, const Vector<String>& columnNames);
128 // </group>
129
130 // Create a reference table out of a file (written by writeRefTable).
131 // The referenced table will also be created (if not stored in the cache).
132 RefTable(AipsIO&, const String& name, rownr_t nrrow, int option, const TableLock& lockOptions,
133 const TSMOption& tsmOption);
134
135 // The destructor flushes (i.e. writes) the table if it is opened
136 // for output and not marked for delete.
137 virtual ~RefTable();
138
139 // Copy constructor is forbidden, because copying a table requires
140 // some more knowledge (like table name of result).
141 RefTable(const RefTable&) = delete;
142
143 // Assignment is forbidden, because copying a table requires
144 // some more knowledge (like table name of result).
145 RefTable& operator=(const RefTable&) = delete;
146
147 // Return the layout of a table (i.e. description and #rows).
148 // This function has the advantage that only the minimal amount of
149 // information required is read from the table, thus it is much
150 // faster than a normal table open.
151 // <br> The number of rows is returned. The description of the table
152 // is stored in desc (its contents will be overwritten).
153 static void getLayout(TableDesc& desc, AipsIO& ios);
154
155 // Try to reopen the table (the underlying one) for read/write access.
156 // An exception is thrown if the table is not writable.
157 // Nothing is done if the table is already open for read/write.
158 virtual void reopenRW();
159
160 // Is the table stored in big or little endian format?
161 virtual Bool asBigEndian() const;
162
163 // Get the storage option used for the table.
164 virtual const StorageOption& storageOption() const;
165
166 // Is the table in use (i.e. open) in another process?
167 // It always returns False.
168 virtual Bool isMultiUsed(Bool checkSubTable) const;
169
170 // Get the locking info.
171 virtual const TableLock& lockOptions() const;
172
173 // Merge the given lock info with the existing one.
174 virtual void mergeLock(const TableLock& lockOptions);
175
176 // Has this process the read or write lock, thus can the table
177 // be read or written safely?
179
180 // Try to lock the table for read or write access.
181 virtual Bool lock(FileLocker::LockType, uInt nattempts);
182
183 // Unlock the table. This will also synchronize the table data,
184 // thus force the data to be written to disk.
185 virtual void unlock();
186
187 // Flush the table, i.e. write it to disk.
188 // Nothing will be done if the table is not writable.
189 // A flush can be executed at any time.
190 // When a table is marked for delete, the destructor will remove
191 // files written by intermediate flushes.
192 // Note that if necessary the destructor will do an implicit flush,
193 // unless it is executed due to an exception.
194 virtual void flush(Bool fsync, Bool recursive);
195
196 // Resync the Table object with the table file.
197 virtual void resync();
198
199 // Get the modify counter.
200 virtual uInt getModifyCounter() const;
201
202 // Test if the parent table is opened as writable.
203 virtual Bool isWritable() const;
204
205 // Read a reference table from a file.
206 // The referenced table will also be created (if not stored in the cache).
207 void getRef(AipsIO&, int option, const TableLock& lockOptions, const TSMOption& tsmOption);
208
209 // This is doing a shallow copy.
210 // It gives an error if the RefTable has not been stored yet.
211 virtual void copy(const String& newName, int tableOption) const;
212
213 // Copy the table and all its subtables.
214 // It copies the contents of each row to get a real copy.
215 virtual void deepCopy(const String& newName, const Record& dataManagerInfo, const StorageOption&,
216 int tableOption, Bool, int endianFormat, Bool noRows) const;
217
218 // It returns the type of the parent table.
219 virtual int tableType() const;
220
221 // Get the actual table description.
222 virtual TableDesc actualTableDesc() const;
223
224 // Get the data manager info.
225 virtual Record dataManagerInfo() const;
226
227 // Get readonly access to the table keyword set.
229
230 // Get read/write access to the table keyword set.
231 // This requires that the table is locked (or it gets locked
232 // when using AutoLocking mode).
234
235 // Get a column object using its index.
236 virtual BaseColumn* getColumn(uInt columnIndex) const;
237
238 // Get a column object using its name.
239 virtual BaseColumn* getColumn(const String& columnName) const;
240
241 // Test if it is possible to remove a row from this table.
242 virtual Bool canRemoveRow() const;
243
244 // Remove the given row.
245 virtual void removeRow(rownr_t rownr);
246
247 // Remove the given row.
248 virtual void removeAllRow();
249
250 // Add one or more columns to the table.
251 // The column is added to the parent table if told so and if not existing.
252 // <group>
253 virtual void addColumn(const ColumnDesc& columnDesc, Bool addToParent);
254 virtual void addColumn(const ColumnDesc& columnDesc, const String& dataManager, Bool byName,
255 Bool addToParent);
256 virtual void addColumn(const ColumnDesc& columnDesc, const DataManager& dataManager,
257 Bool addToParent);
258 virtual void addColumn(const TableDesc& tableDesc, const DataManager& dataManager,
259 Bool addToParent);
260 // </group>
261
262 // Test if columns can be removed (yes).
263 virtual Bool canRemoveColumn(const Vector<String>& columnNames) const;
264
265 // Remove columns.
266 virtual void removeColumn(const Vector<String>& columnNames);
267
268 // Test if a column can be renamed (yes).
269 virtual Bool canRenameColumn(const String& columnName) const;
270
271 // Rename a column.
272 virtual void renameColumn(const String& newName, const String& oldName);
273
274 // Rename a hypercolumn.
275 virtual void renameHypercolumn(const String& newName, const String& oldName);
276
277 // Find the data manager with the given name or for the given column.
278 virtual DataManager* findDataManager(const String& name, Bool byColumn) const;
279
280 // Get a vector of row numbers.
282
283 // Get parent of this table.
284 virtual BaseTable* root();
285
286 // Get rownr in root table.
287 // This converts the given row number to the row number in the root table.
288 rownr_t rootRownr(rownr_t rownr) const;
289
290 // Get vector of rownrs in root table.
291 // This converts the given row numbers to row numbers in the root table.
293
294 // Tell if the table is in row order.
295 virtual Bool rowOrder() const;
296
297 // Get row number vector.
298 // This is used by the BaseTable logic and sort routines.
300
301 // Add a rownr to reference table.
302 void addRownr(rownr_t rownr);
303
304 void addRownrRange(rownr_t startRownr, rownr_t endRownr);
305
306 // Set the exact number of rows in the table.
307 // An exception is thrown if more than current nrrow.
308 void setNrrow(rownr_t nrrow);
309
310 // Adjust the row numbers to be the actual row numbers in the
311 // root table. This is, for instance, used when a RefTable is sorted.
312 // Optionally it also determines if the resulting rows are in row order.
313 virtual Bool adjustRownrs(rownr_t nrrow, Vector<rownr_t>& rownrs, Bool determineOrder) const;
314
315 // And, or, subtract or xor the row numbers of 2 tables.
316 void refAnd(rownr_t nr1, const rownr_t* rows1, rownr_t nr2, const rownr_t* rows2);
317 void refOr(rownr_t nr1, const rownr_t* rows1, rownr_t nr2, const rownr_t* rows2);
318 void refSub(rownr_t nr1, const rownr_t* rows1, rownr_t nr2, const rownr_t* rows2);
319 void refXor(rownr_t nr1, const rownr_t* rows1, rownr_t nr2, const rownr_t* rows2);
320 void refNot(rownr_t nr1, const rownr_t* rows1, rownr_t nrmain);
321
322 private:
323 std::shared_ptr<BaseTable> baseTabPtr_p; // # pointer to parent table
324 Bool rowOrd_p; // # True = table is in row order
325 Vector<rownr_t> rowStorage_p; // # row numbers in parent table
326 std::map<String, String> nameMap_p; // # map to column name in parent
327 std::map<String, RefColumn*> colMap_p; // # map name to column
328 Bool changed_p; // # True = changed since last write
329
330 // Get the names of the tables this table consists of.
331 virtual void getPartNames(Block<String>& names, Bool recursive) const;
332
333 // Show the extra table structure info (name of root table).
334 void showStructureExtra(std::ostream&) const;
335
336 // Make a table description for the given columns.
337 static void makeDesc(TableDesc& desc, const TableDesc& rootDesc,
338 std::map<String, String>& nameMap, Vector<String>& names);
339
340 // Setup the main parts of the object.
341 // <br>First create the name map (mapping column name in RefTable to
342 // the column in the original table).
343 // If the BaseTable is a RefTable, use its name map.
344 // Otherwise create the initial name map from the table description.
345 // A rename might change the map.
346 // <br>Create the RefColumn objects.
347 // <br>Create the initial TableInfo as a copy of the original BaseTable.
348 void setup(BaseTable* btp, const Vector<String>& columnNames);
349
350 // Create the RefColumn objects for all columns in the description.
352
353 // Write a reference table.
354 void writeRefTable(Bool fsync);
355
356 // Copy a RefTable that is not persistent. It requires some special logic.
357 void copyRefTable(const String& newName, int tableOption);
358
359 // Check if a column can be added. Return True if it can and must be
360 // added to the parent table first.
361 Bool checkAddColumn(const String& name, Bool addToParent);
362
363 // Add a column.
364 void addRefCol(const ColumnDesc& cd);
365 // Add multiple columns.
366 void addRefCol(const TableDesc& tdesc);
367};
368
369inline rownr_t RefTable::rootRownr(rownr_t rnr) const { return rowStorage_p[rnr]; }
370
371} // namespace casacore
372
373#endif
const TableDesc & tableDesc() const
Get the table description.
Definition BaseTable.h:261
BaseTable(const String &tableName, int tableOption, rownr_t nrrow)
Initialize the object.
int tableOption() const
Get the table option.
Definition BaseTable.h:244
Abstract base class for a data manager.
LockType
Define the possible lock types.
Definition FileLocker.h:89
Vector< rownr_t > rowStorage_p
Definition RefTable.h:325
static void getLayout(TableDesc &desc, AipsIO &ios)
Return the layout of a table (i.e.
void refAnd(rownr_t nr1, const rownr_t *rows1, rownr_t nr2, const rownr_t *rows2)
And, or, subtract or xor the row numbers of 2 tables.
virtual Vector< rownr_t > & rowStorage()
Get row number vector.
void addRownrRange(rownr_t startRownr, rownr_t endRownr)
virtual Record dataManagerInfo() const
Get the data manager info.
virtual Bool canRemoveRow() const
Test if it is possible to remove a row from this table.
virtual DataManager * findDataManager(const String &name, Bool byColumn) const
Find the data manager with the given name or for the given column.
virtual Bool adjustRownrs(rownr_t nrrow, Vector< rownr_t > &rownrs, Bool determineOrder) const
Adjust the row numbers to be the actual row numbers in the root table.
virtual void addColumn(const ColumnDesc &columnDesc, const DataManager &dataManager, Bool addToParent)
virtual Bool canRemoveColumn(const Vector< String > &columnNames) const
Test if columns can be removed (yes).
Vector< rownr_t > rootRownr(const Vector< rownr_t > &rownrs) const
Get vector of rownrs in root table.
virtual void unlock()
Unlock the table.
virtual ~RefTable()
The destructor flushes (i.e.
std::shared_ptr< BaseTable > baseTabPtr_p
Definition RefTable.h:323
void refSub(rownr_t nr1, const rownr_t *rows1, rownr_t nr2, const rownr_t *rows2)
void showStructureExtra(std::ostream &) const
Show the extra table structure info (name of root table).
virtual void addColumn(const ColumnDesc &columnDesc, const String &dataManager, Bool byName, Bool addToParent)
virtual void removeAllRow()
Remove the given row.
RefTable(BaseTable *, const Vector< rownr_t > &rowNumbers)
A RefTable with the given row numbers is constructed.
RefTable(AipsIO &, const String &name, rownr_t nrrow, int option, const TableLock &lockOptions, const TSMOption &tsmOption)
Create a reference table out of a file (written by writeRefTable).
void copyRefTable(const String &newName, int tableOption)
Copy a RefTable that is not persistent.
virtual void copy(const String &newName, int tableOption) const
This is doing a shallow copy.
RefTable & operator=(const RefTable &)=delete
Assignment is forbidden, because copying a table requires some more knowledge (like table name of res...
virtual void deepCopy(const String &newName, const Record &dataManagerInfo, const StorageOption &, int tableOption, Bool, int endianFormat, Bool noRows) const
Copy the table and all its subtables.
virtual Vector< rownr_t > rowNumbers() const
Get a vector of row numbers.
void addRownr(rownr_t rownr)
Add a rownr to reference table.
void makeRefCol()
Create the RefColumn objects for all columns in the description.
virtual TableRecord & keywordSet()
Get readonly access to the table keyword set.
RefTable(BaseTable *, Bool rowOrder, rownr_t initialNrrow)
Create a reference table object referencing the given BaseTable object.
virtual void removeRow(rownr_t rownr)
Remove the given row.
virtual uInt getModifyCounter() const
Get the modify counter.
void addRefCol(const TableDesc &tdesc)
Add multiple columns.
virtual Bool lock(FileLocker::LockType, uInt nattempts)
Try to lock the table for read or write access.
virtual Bool canRenameColumn(const String &columnName) const
Test if a column can be renamed (yes).
rownr_t rootRownr(rownr_t rownr) const
Get rownr in root table.
Definition RefTable.h:369
virtual BaseColumn * getColumn(const String &columnName) const
Get a column object using its name.
void writeRefTable(Bool fsync)
Write a reference table.
void setup(BaseTable *btp, const Vector< String > &columnNames)
Setup the main parts of the object.
RefTable(BaseTable *, const Vector< Bool > &rowMask)
Create a reference table object out of a mask.
virtual void renameHypercolumn(const String &newName, const String &oldName)
Rename a hypercolumn.
void refXor(rownr_t nr1, const rownr_t *rows1, rownr_t nr2, const rownr_t *rows2)
virtual const StorageOption & storageOption() const
Get the storage option used for the table.
virtual void getPartNames(Block< String > &names, Bool recursive) const
Get the names of the tables this table consists of.
virtual void addColumn(const ColumnDesc &columnDesc, Bool addToParent)
Add one or more columns to the table.
virtual Bool asBigEndian() const
Is the table stored in big or little endian format?
virtual const TableLock & lockOptions() const
Get the locking info.
virtual TableDesc actualTableDesc() const
Get the actual table description.
virtual TableRecord & rwKeywordSet()
Get read/write access to the table keyword set.
virtual Bool rowOrder() const
Tell if the table is in row order.
void getRef(AipsIO &, int option, const TableLock &lockOptions, const TSMOption &tsmOption)
Read a reference table from a file.
virtual void mergeLock(const TableLock &lockOptions)
Merge the given lock info with the existing one.
virtual void flush(Bool fsync, Bool recursive)
Flush the table, i.e.
virtual BaseTable * root()
Get parent of this table.
void refOr(rownr_t nr1, const rownr_t *rows1, rownr_t nr2, const rownr_t *rows2)
void addRefCol(const ColumnDesc &cd)
Add a column.
RefTable(const RefTable &)=delete
Copy constructor is forbidden, because copying a table requires some more knowledge (like table name ...
void setNrrow(rownr_t nrrow)
Set the exact number of rows in the table.
std::map< String, String > nameMap_p
Definition RefTable.h:326
virtual Bool hasLock(FileLocker::LockType) const
Has this process the read or write lock, thus can the table be read or written safely?
virtual void resync()
Resync the Table object with the table file.
virtual Bool isWritable() const
Test if the parent table is opened as writable.
virtual void renameColumn(const String &newName, const String &oldName)
Rename a column.
static void makeDesc(TableDesc &desc, const TableDesc &rootDesc, std::map< String, String > &nameMap, Vector< String > &names)
Make a table description for the given columns.
virtual Bool isMultiUsed(Bool checkSubTable) const
Is the table in use (i.e.
void refNot(rownr_t nr1, const rownr_t *rows1, rownr_t nrmain)
RefTable(BaseTable *, const Vector< String > &columnNames)
Create a reference table object via projection (i.e.
std::map< String, RefColumn * > colMap_p
Definition RefTable.h:327
virtual BaseColumn * getColumn(uInt columnIndex) const
Get a column object using its index.
virtual void removeColumn(const Vector< String > &columnNames)
Remove columns.
virtual void addColumn(const TableDesc &tableDesc, const DataManager &dataManager, Bool addToParent)
Bool checkAddColumn(const String &name, Bool addToParent)
Check if a column can be added.
virtual void reopenRW()
Try to reopen the table (the underlying one) for read/write access.
virtual int tableType() const
It returns the type of the parent table.
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
String name() const
Return the name of the field.
bool Bool
Define the standard types used by Casacore.
Definition aipstype.h:40
uInt64 rownr_t
Define the type of a row number in a table.
Definition aipsxtype.h:44