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