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