casacore
Loading...
Searching...
No Matches
ColumnsIndexArray.h
Go to the documentation of this file.
1// # ColumnsIndexArray.h: Index to an array column in a table
2// # Copyright (C) 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_COLUMNSINDEXARRAY_H
27#define TABLES_COLUMNSINDEXARRAY_H
28
29// # Includes
30#include <casacore/casa/aips.h>
31#include <casacore/tables/Tables/Table.h>
32#include <casacore/casa/Arrays/Vector.h>
33#include <casacore/casa/Containers/Block.h>
34#include <casacore/casa/Containers/Record.h>
35
36namespace casacore { // # NAMESPACE CASACORE - BEGIN
37
38// # Forward Declarations
39class String;
40class TableColumn;
41
42// <summary>
43// Index to an array column in a table.
44// </summary>
45
46// <use visibility=export>
47
48// <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="tColumnsIndexArray.cc" demos="">
49// </reviewed>
50
51// <prerequisite>
52// <li> <linkto class=Table>Table</linkto>
53// <li> <linkto class=Record>Record</linkto>
54// <li> <linkto class=RecordFieldPtr>RecordFieldPtr</linkto>
55// </prerequisite>
56
57// <synopsis>
58// This class makes it possible to use transient indices on top
59// of an array column in a table in order to speed up the process of
60// finding rows based on a given key or key range.
61// It is similar to class <linkto class=ColumnsIndex>ColumnsIndex</linkto>
62// which is meant for one or more scalar columns.
63// <p>
64// When constructing a <src>ColumnsIndexArray</src> object, one has to define
65// which column forms the key for this index on the given
66// <src>table</src> object.
67// Not every data type is supported; only uChar, Short, Int, uInt, Int64 and
68// String array columns are supported.
69// The column can contain arrays of any shape and it can also contain
70// empty cells. The class will probably mostly be used for vectors, as
71// they seem to be the most logical way to hold multiple keys.
72// <br>The data in the given column will be read, sorted,
73// and stored in memory. When looking up a key or key range, the class
74// will use a fast binary search on the data held in memory.
75// <p>
76// The <src>ColumnsIndexArray</src> object contains a
77// <linkto class=Record>Record</linkto> object which can be used
78// to define the key to be looked up. The record contains a field for
79// the column in the index (with the same name and data type).
80// The fastest way to fill the key is by creating a
81// <linkto class=RecordFieldPtr>RecordFieldPtr</linkto> object for
82// the field in the record (see the example) and fill it as needed.
83// However, one can also use the <src>Record::define</src> function,
84// but that is slower.
85// <br>
86// A second record is available to define the upper key
87// in case a key range has to be looked up. The keys can be accessed
88// using the various <src>accessKey</src> functions.
89// <p>
90// When a key is defined, the <src>getRowNumbers</src> function can be
91// used to find the table rows containing the given key (range).
92// Function <src>getRowNumber</src> can be used to lookup a single key
93// if all keys in the index are unique (which can be tested with the
94// <src>isUnique</src> function).
95// <p>
96// Instead of using the internal records holding the keys, one can also
97// pass its own Record object to <src>getRowNumbers</src>.
98// However, it will be slower.
99// <p>
100// After an index is created, it is possible to change the data
101// in the underlying columns. However, the <src>ColumnsIndexArray</src> can
102// not detect if the column data have changed. It can only detect if
103// the number of rows has changed. If the column data have changed,
104// the user has to use the <src>setChanged</src> function to indicate
105// that the column has changed.
106// <br>If data have changed, the entire index will be recreated by
107// rereading and resorting the data. This will be deferred
108// until the next key lookup.
109// </synopsis>
110
111// <example>
112// Suppose one has table with a column NAME containing vectors.
113// <srcblock>
114// // Open the table and make an index for the column.
115// Table tab("my.tab")
116// ColumnsIndexArray colInx(tab, "NAME");
117// // Make a RecordFieldPtr for the NAME field in the index key record.
118// // Its data type has to match the data type of the column.
119// RecordFieldPtr<String> nameFld(colInx.accessKey(), "NAME");
120// // Find the row for a given name.
121// Bool found;
122// // Fill the key field and get the row number.
123// // NAME is a unique key, so only one row number matches.
124// // Otherwise function getRowNumbers had to be used.
125// *nameFld = "MYNAME";
126// rownr_t rownr = colInx.getRowNumber (found);
127// if (!found) {
128// cout << "Name MYNAME is unknown" << endl;
129// }
130// // Now get a range of names and return the row numbers in ascending order.
131// // This uses the fact that the 'unique' argument also sorts the data.
132// RecordFieldPtr<String> nameUpp(colInx.accessUpperKey(), "NAME");
133// *nameFld = "LOWER";
134// *nameUpp = "UPPER";
135// RowNumbers rownrs = colInx.getRowNumbers (True, True, True);
136// </srcblock>
137
138// <motivation>
139// Bob Garwood needed such a class.
140// </motivation>
141
143 public:
144 // Create an index on the given table for the given column.
145 // The column can be a scalar or an array column.
146 // If <src>noSort==True</src>, the table is already in order of that
147 // column and the sort step will not be done.
148 // It only supports String and integer columns.
150
151 // Copy constructor (copy semantics).
153
155
156 // Assignment (copy semantics).
158
159 // Are all keys in the index unique?
160 Bool isUnique() const;
161
162 // Return the names of the columns forming the index.
163 const String& columnName() const;
164
165 // Get the table for which this index is created.
166 const Table& table() const;
167
168 // Something has changed in the table, so the index has to be recreated.
169 // The 2nd version indicates that a specific column has changed,
170 // so only that column might need to be reread. If that column is not
171 // part of the index, nothing will be done.
172 // <br>Note that the class itself is keeping track if the number of
173 // rows in the table changes.
174 // <group>
177 // </group>
178
179 // Access the key values.
180 // These functions allow you to create RecordFieldPtr<T> objects
181 // for each field in the key. In this way you can quickly fill in
182 // the key.
183 // <br>The records have a fixed type, so you cannot add or delete fields.
184 // <br>Note that <src>accessKey</src> and <src>accessLowerKey</src>
185 // are synonyms; they return the same underlying record.
186 // <group>
187 Record& accessKey();
190 // </group>
191
192 // Find the row number matching the key. All keys have to be unique,
193 // otherwise an exception is thrown.
194 // If no match is found, <src>found</src> is set to False.
195 // The 2nd version makes it possible to pass in your own Record
196 // instead of using the internal record via the <src>accessKey</src>
197 // functions. Note that the given Record will be copied to the internal
198 // record, thus overwrites it.
199 // <group>
201 rownr_t getRowNumber(Bool& found, const Record& key);
202 // </group>
203
204 // Find the row numbers matching the key. It should be used instead
205 // of <src>getRowNumber</src> if the same key can exist multiple times.
206 // The 2nd version makes it possible to pass in your own Record
207 // instead of using the internal record via the <src>accessKey</src>
208 // functions. Note that the given Record will be copied to the internal
209 // record, thus overwrites it.
210 // <br>A row can contain multiple equal values. In such a case the
211 // same row number can occur multiple times in the output vector,
212 // unless <src>unique</src> is set to True. Note that making the row
213 // numbers unique implies a sort, so it can also be used to get the
214 // row numbers in ascending order.
215 // <group>
217 RowNumbers getRowNumbers(const Record& key, Bool unique = False);
218 // </group>
219
220 // Find the row numbers matching the key range. The boolean arguments
221 // tell if the lower and upper key are part of the range.
222 // The 2nd version makes it possible to pass in your own Records
223 // instead of using the internal records via the
224 // <src>accessLower/UpperKey</src> functions.
225 // Note that the given Records will be copied to the internal
226 // records, thus overwrite them.
227 // <br>A row can contain multiple matching values. In such a case the
228 // same row number can occur multiple times in the output vector,
229 // unless <src>unique</src> is set to True. Note that making the row
230 // numbers unique implies a sort, so it can also be used to get the
231 // row numbers in ascending order.
232 // <group>
233 RowNumbers getRowNumbers(Bool lowerInclusive, Bool upperInclusive, Bool unique = False);
234 RowNumbers getRowNumbers(const Record& lower, const Record& upper, Bool lowerInclusive,
235 Bool upperInclusive, Bool unique = False);
236 // </group>
237
238 protected:
239 // Copy that object to this.
240 void copy(const ColumnsIndexArray& that);
241
242 // Delete all data in the object.
244
245 // Add a column to the record description for the keys.
246 // If the switch <src>arrayPossible</src> is True, the column can
247 // be an array. Otherwise it has to be a scalar.
249
250 // Make the various internal <src>RecordFieldPtr</src> objects.
252
253 // Read the data of the columns forming the index, sort them and
254 // form the index.
255 void readData();
256
257 // Do a binary search on <src>itsUniqueIndexArray</src> for the key in
258 // <src>fieldPtrs</src>.
259 // If the key is found, <src>found</src> is set to True and the index
260 // in <src>itsUniqueIndexArray</src> is returned.
261 // If not found, <src>found</src> is set to False and the index
262 // of the next higher key is returned.
263 rownr_t bsearch(Bool& found, void* fieldPtr) const;
264
265 // Compare the key in <src>fieldPtr</src> with the given index entry.
266 // -1 is returned when less, 0 when equal, 1 when greater.
267 static Int compare(void* fieldPtr, void* dataPtr, Int dataType, rownr_t index);
268
269 // Fill the row numbers vector for the given start till end in the
270 // <src>itsUniqueIndexArray</src> vector (end is not inclusive).
271 // If <src>unique</src> is True, the row numbers will be made unique.
272 void fillRowNumbers(Vector<rownr_t>& rows, rownr_t start, rownr_t end, Bool unique) const;
273
274 // Get the data if the column is an array.
275 // <group>
276 void getArray(Vector<uChar>& result, const String& name);
277 void getArray(Vector<Short>& result, const String& name);
278 void getArray(Vector<Int>& result, const String& name);
279 void getArray(Vector<uInt>& result, const String& name);
280 void getArray(Vector<Int64>& result, const String& name);
281 void getArray(Vector<String>& result, const String& name);
282 // </group>
283
284 // Fill the rownrs belonging to each array value.
285 void fillRownrs(rownr_t npts, const Block<rownr_t>& nrel);
286
287 private:
294 void* itsData; // # pointer to data in itsDataVector
295 // # The following 2 blocks are actually blocks of RecordFieldPtr<T>*.
296 // # They are used for fast access to the records.
300 Vector<rownr_t> itsDataIndex; // # Row numbers of all keys
301 // # Indices in itsDataIndex for each unique key
303 Block<rownr_t> itsRownrs; // # rownr for each value
304 rownr_t* itsDataInx; // # pointer to data in itsDataIndex
305 rownr_t* itsUniqueInx; // # pointer to data in itsUniqueIndex
306};
307
309 return (itsDataIndex.nelements() == itsUniqueIndex.nelements());
310}
311inline const Table& ColumnsIndexArray::table() const { return itsTable; }
315
316} // namespace casacore
317
318#endif
ColumnsIndexArray(const Table &, const String &columnName)
Create an index on the given table for the given column.
const Table & table() const
Get the table for which this index is created.
void setChanged()
Something has changed in the table, so the index has to be recreated.
void getArray(Vector< Int > &result, const String &name)
Bool isUnique() const
Are all keys in the index unique?
ColumnsIndexArray & operator=(const ColumnsIndexArray &that)
Assignment (copy semantics).
void addColumnToDesc(RecordDesc &description, const TableColumn &column)
Add a column to the record description for the keys.
ColumnsIndexArray(const ColumnsIndexArray &that)
Copy constructor (copy semantics).
void readData()
Read the data of the columns forming the index, sort them and form the index.
void getArray(Vector< String > &result, const String &name)
void makeObjects(const RecordDesc &description)
Make the various internal RecordFieldPtr objects.
void copy(const ColumnsIndexArray &that)
Copy that object to this.
RowNumbers getRowNumbers(const Record &lower, const Record &upper, Bool lowerInclusive, Bool upperInclusive, Bool unique=False)
void fillRownrs(rownr_t npts, const Block< rownr_t > &nrel)
Fill the rownrs belonging to each array value.
rownr_t bsearch(Bool &found, void *fieldPtr) const
Do a binary search on itsUniqueIndexArray for the key in fieldPtrs.
RowNumbers getRowNumbers(Bool unique=False)
Find the row numbers matching the key.
static Int compare(void *fieldPtr, void *dataPtr, Int dataType, rownr_t index)
Compare the key in fieldPtr with the given index entry.
void getArray(Vector< uInt > &result, const String &name)
RowNumbers getRowNumbers(const Record &key, Bool unique=False)
void setChanged(const String &columnName)
rownr_t getRowNumber(Bool &found)
Find the row number matching the key.
void fillRowNumbers(Vector< rownr_t > &rows, rownr_t start, rownr_t end, Bool unique) const
Fill the row numbers vector for the given start till end in the itsUniqueIndexArray vector (end is no...
void getArray(Vector< uChar > &result, const String &name)
Get the data if the column is an array.
Record & accessKey()
Access the key values.
void deleteObjects()
Delete all data in the object.
void getArray(Vector< Short > &result, const String &name)
const String & columnName() const
Return the names of the columns forming the index.
RowNumbers getRowNumbers(Bool lowerInclusive, Bool upperInclusive, Bool unique=False)
Find the row numbers matching the key range.
rownr_t getRowNumber(Bool &found, const Record &key)
void getArray(Vector< Int64 > &result, const String &name)
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
const RecordDesc & description() const
Describes the current structure of this Record.
String name() const
Return the name of the field.
int Int
Definition aipstype.h:48
bool Bool
Define the standard types used by Casacore.
Definition aipstype.h:40
iterator end()
Definition Block.h:601
uInt64 rownr_t
Define the type of a row number in a table.
Definition aipsxtype.h:44
DataType dataType(const RecordFieldId &) const