Loading...
Searching...
No Matches
casacore::ColumnsIndexArray Class Reference
tables package (libcasa_tables) » Tables_module

More...

`#include <ColumnsIndexArray.h>`

## Public Member Functions

| ColumnsIndexArray (const Table &, const String &columnName)
| Create an index on the given table for the given column.
| ColumnsIndexArray (const ColumnsIndexArray &that)
| Copy constructor (copy semantics).
| ~ColumnsIndexArray ()
ColumnsIndexArray & | operator= (const ColumnsIndexArray &that)
| Assignment (copy semantics).
Bool | isUnique () const
| Are all keys in the index unique?
const String & | columnName () const
| Return the names of the columns forming the index.
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 | setChanged (const String &columnName)
Record & | accessKey ()
| Access the key values.
Record & | accessLowerKey ()
Record & | accessUpperKey ()
rownr_t | getRowNumber (Bool &found)
| Find the row number matching the key.
rownr_t | getRowNumber (Bool &found, const Record &key)
RowNumbers | getRowNumbers (Bool unique=False)
| Find the row numbers matching the key.
RowNumbers | getRowNumbers (const Record &key, Bool unique=False)
RowNumbers | getRowNumbers (Bool lowerInclusive, Bool upperInclusive, Bool unique=False)
| Find the row numbers matching the key range.
RowNumbers | getRowNumbers (const Record &lower, const Record &upper, Bool lowerInclusive, Bool upperInclusive, Bool unique=False)

## Protected Member Functions

void | copy (const ColumnsIndexArray &that)
| Copy that object to this.
void | deleteObjects ()
| Delete all data in the object.
void | addColumnToDesc (RecordDesc &description, const TableColumn &column)
| Add a column to the record description for the keys.
void | makeObjects (const RecordDesc &description)
| Make the various internal RecordFieldPtr objects.
void | readData ()
| Read the data of the columns forming the index, sort them and form the index.
rownr_t | bsearch (Bool &found, void *fieldPtr) const
| Do a binary search on itsUniqueIndexArray for the key in fieldPtrs.
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 not inclusive).
void | getArray (Vector< uChar > &result, const String &name)
| Get the data if the column is an array.
void | getArray (Vector< Short > &result, const String &name)
void | getArray (Vector< Int > &result, const String &name)
void | getArray (Vector< uInt > &result, const String &name)
void | getArray (Vector< Int64 > &result, const String &name)
void | getArray (Vector< String > &result, const String &name)
void | fillRownrs (rownr_t npts, const Block< rownr_t > &nrel)
| Fill the rownrs belonging to each array value.

## Static Protected Member Functions

static Int | compare (void *fieldPtr, void *dataPtr, Int dataType, rownr_t index)
| Compare the key in fieldPtr with the given index entry.

## Private Attributes

Table | itsTable
rownr_t | itsNrrow
Record * | itsLowerKeyPtr
Record * | itsUpperKeyPtr
Int | itsDataType
void * | itsDataVector
void * | itsData
void * | itsLowerField
void * | itsUpperField
Bool | itsChanged
Vector< rownr_t > | itsDataIndex
Vector< rownr_t > | itsUniqueIndex
Block< rownr_t > | itsRownrs
rownr_t * | itsDataInx
rownr_t * | itsUniqueInx

## Detailed Description

Index to an array column in a table.

### Intended use:

Public interface

### Review Status

Reviewed By:
  UNKNOWN
Date Reviewed:
  before2004/08/25
Test programs:
  tColumnsIndexArray

### Prerequisite

- Table
- Record
- RecordFieldPtr

### Synopsis

This class makes it possible to use transient indices on top of an array column in a table in order to speed up the process of finding rows based on a given key or key range. It is similar to class ColumnsIndex which is meant for one or more scalar columns.

When constructing a ColumnsIndexArray object, one has to define which column forms the key for this index on the given table object. Not every data type is supported; only uChar, Short, Int, uInt, Int64 and String array columns are supported. The column can contain arrays of any shape and it can also contain empty cells. The class will probably mostly be used for vectors, as they seem to be the most logical way to hold multiple keys.
The data in the given column will be read, sorted, and stored in memory. When looking up a key or key range, the class will use a fast binary search on the data held in memory.

The ColumnsIndexArray object contains a Record object which can be used to define the key to be looked up. The record contains a field for the column in the index (with the same name and data type). The fastest way to fill the key is by creating a RecordFieldPtr object for the field in the record (see the example) and fill it as needed. However, one can also use the Record::define function, but that is slower.
A second record is available to define the upper key in case a key range has to be looked up. The keys can be accessed using the various accessKey functions.

When a key is defined, the getRowNumbers function can be used to find the table rows containing the given key (range). Function getRowNumber can be used to lookup a single key if all keys in the index are unique (which can be tested with the isUnique function).

Instead of using the internal records holding the keys, one can also pass its own Record object to getRowNumbers. However, it will be slower.

After an index is created, it is possible to change the data in the underlying columns. However, the ColumnsIndexArray can not detect if the column data have changed. It can only detect if the number of rows has changed. If the column data have changed, the user has to use the setChanged function to indicate that the column has changed.
If data have changed, the entire index will be recreated by rereading and resorting the data. This will be deferred until the next key lookup.

### Example

Suppose one has table with a column NAME containing vectors.

// Open the table and make an index for the column.
Table tab("my.tab")
ColumnsIndexArray colInx(tab, "NAME");
// Make a RecordFieldPtr for the NAME field in the index key record.
// Its data type has to match the data type of the column.
RecordFieldPtr<String> nameFld(colInx.accessKey(), "NAME");
// Find the row for a given name.
Bool found;
// Fill the key field and get the row number.
// NAME is a unique key, so only one row number matches.
// Otherwise function getRowNumbers had to be used.
*nameFld = "MYNAME";
rownr_t rownr = colInx.getRowNumber (found);
if (!found) {
cout << "Name MYNAME is unknown" << endl;
}
// Now get a range of names and return the row numbers in ascending order.
// This uses the fact that the 'unique' argument also sorts the data.
RecordFieldPtr<String> nameUpp(colInx.accessUpperKey(), "NAME");
*nameFld = "LOWER";
*nameUpp = "UPPER";
RowNumbers rownrs = colInx.getRowNumbers (True, True, True);
casacore::ColumnsIndexArray::ColumnsIndexArray
ColumnsIndexArray(const Table &, const String &columnName)
Create an index on the given table for the given column.
casacore::ColumnsIndexArray::getRowNumber
rownr_t getRowNumber(Bool &found)
Find the row number matching the key.
casacore::ColumnsIndexArray::accessKey
Record & accessKey()
Access the key values.
Definition ColumnsIndexArray.h:328
casacore::RecordFieldPtr
Definition ColumnsIndex.h:42
casacore::RowNumbers
Definition RowNumbers.h:36
casacore::String
String: the storage and methods of handling collections of characters.
Definition String.h:223
casacore::Table
Definition Table.h:157
casacore::Bool
bool Bool
Define the standard types used by Casacore.
Definition aipstype.h:40
casacore::True
const Bool True
Definition aipstype.h:41
casacore::rownr_t
uInt64 rownr_t
Define the type of a row number in a table.
Definition aipsxtype.h:44

### Motivation

Bob Garwood needed such a class.

Definition at line 145 of file ColumnsIndexArray.h.

## Constructor & Destructor Documentation

## ColumnsIndexArray() [1/2]

casacore::ColumnsIndexArray::ColumnsIndexArray | ( | const Table & | ,
|  | const String & | columnName )

Create an index on the given table for the given column.

The column can be a scalar or an array column. If noSort==True, the table is already in order of that column and the sort step will not be done. It only supports String and integer columns.

References columnName().

Referenced by ColumnsIndexArray(), copy(), and operator=().

## ColumnsIndexArray() [2/2]

casacore::ColumnsIndexArray::ColumnsIndexArray | ( | const ColumnsIndexArray & | that | ) |

Copy constructor (copy semantics).

References ColumnsIndexArray().

## ~ColumnsIndexArray()

casacore::ColumnsIndexArray::~ColumnsIndexArray | ( |  | ) |

## Member Function Documentation

## accessKey()

Record & casacore::ColumnsIndexArray::accessKey | ( |  | ) |

inline

Access the key values.

These functions allow you to create RecordFieldPtr<T> objects for each field in the key. In this way you can quickly fill in the key.
The records have a fixed type, so you cannot add or delete fields.
Note that accessKey and accessLowerKey are synonyms; they return the same underlying record.

Definition at line 328 of file ColumnsIndexArray.h.

References itsLowerKeyPtr, and casacore::Record().

## accessLowerKey()

Record & casacore::ColumnsIndexArray::accessLowerKey | ( |  | ) |

inline

Definition at line 332 of file ColumnsIndexArray.h.

References itsLowerKeyPtr, and casacore::Record().

## accessUpperKey()

Record & casacore::ColumnsIndexArray::accessUpperKey | ( |  | ) |

inline

Definition at line 336 of file ColumnsIndexArray.h.

References itsUpperKeyPtr, and casacore::Record().

## addColumnToDesc()

void casacore::ColumnsIndexArray::addColumnToDesc | ( | RecordDesc & | description,
|  | const TableColumn & | column )

protected

Add a column to the record description for the keys.

If the switch arrayPossible is True, the column can be an array. Otherwise it has to be a scalar.

References casacore::description().

## bsearch()

rownr_t casacore::ColumnsIndexArray::bsearch | ( | Bool & | found,
|  | void * | fieldPtr ) const

protected

Do a binary search on itsUniqueIndexArray for the key in fieldPtrs.

If the key is found, found is set to True and the index in itsUniqueIndexArray is returned. If not found, found is set to False and the index of the next higher key is returned.

## columnName()

const String & casacore::ColumnsIndexArray::columnName | ( |  | ) | const

Return the names of the columns forming the index.

Referenced by ColumnsIndexArray(), and setChanged().

## compare()

Int casacore::ColumnsIndexArray::compare | ( | void * | fieldPtr,
|  | void * | dataPtr,
|  | Int | dataType,
|  | rownr_t | index )

static protected

Compare the key in fieldPtr with the given index entry.

-1 is returned when less, 0 when equal, 1 when greater.

References casacore::dataType().

## copy()

void casacore::ColumnsIndexArray::copy | ( | const ColumnsIndexArray & | that | ) |

protected

Copy that object to this.

References ColumnsIndexArray().

## deleteObjects()

void casacore::ColumnsIndexArray::deleteObjects | ( |  | ) |

protected

Delete all data in the object.

## fillRownrs()

void casacore::ColumnsIndexArray::fillRownrs | ( | rownr_t | npts,
|  | const Block< rownr_t > & | nrel )

protected

Fill the rownrs belonging to each array value.

## fillRowNumbers()

void casacore::ColumnsIndexArray::fillRowNumbers | ( | Vector< rownr_t > & | rows,
|  | rownr_t | start,
|  | rownr_t | end,
|  | Bool | unique ) const

protected

Fill the row numbers vector for the given start till end in the itsUniqueIndexArray vector (end is not inclusive).

If unique is True, the row numbers will be made unique.

References casacore::end().

## getArray() [1/6]

void casacore::ColumnsIndexArray::getArray | ( | Vector< Int > & | result,
|  | const String & | name )

protected

References casacore::name().

## getArray() [2/6]

void casacore::ColumnsIndexArray::getArray | ( | Vector< Int64 > & | result,
|  | const String & | name )

protected

References casacore::name().

## getArray() [3/6]

void casacore::ColumnsIndexArray::getArray | ( | Vector< Short > & | result,
|  | const String & | name )

protected

References casacore::name().

## getArray() [4/6]

void casacore::ColumnsIndexArray::getArray | ( | Vector< String > & | result,
|  | const String & | name )

protected

References casacore::name().

## getArray() [5/6]

void casacore::ColumnsIndexArray::getArray | ( | Vector< uChar > & | result,
|  | const String & | name )

protected

Get the data if the column is an array.

References casacore::name().

## getArray() [6/6]

void casacore::ColumnsIndexArray::getArray | ( | Vector< uInt > & | result,
|  | const String & | name )

protected

References casacore::name().

## getRowNumber() [1/2]

rownr_t casacore::ColumnsIndexArray::getRowNumber | ( | Bool & | found | ) |

Find the row number matching the key.

All keys have to be unique, otherwise an exception is thrown. If no match is found, found is set to False. The 2nd version makes it possible to pass in your own Record instead of using the internal record via the accessKey functions. Note that the given Record will be copied to the internal record, thus overwrites it.

## getRowNumber() [2/2]

rownr_t casacore::ColumnsIndexArray::getRowNumber | ( | Bool & | found,
|  | const Record & | key )

References casacore::Record().

## getRowNumbers() [1/4]

RowNumbers casacore::ColumnsIndexArray::getRowNumbers | ( | Bool | lowerInclusive,
|  | Bool | upperInclusive,
|  | Bool | unique = False )

Find the row numbers matching the key range.

The boolean arguments tell if the lower and upper key are part of the range. The 2nd version makes it possible to pass in your own Records instead of using the internal records via the accessLower/UpperKey functions. Note that the given Records will be copied to the internal records, thus overwrite them.
A row can contain multiple matching values. In such a case the same row number can occur multiple times in the output vector, unless unique is set to True. Note that making the row numbers unique implies a sort, so it can also be used to get the row numbers in ascending order.

References casacore::False.

## getRowNumbers() [2/4]

RowNumbers casacore::ColumnsIndexArray::getRowNumbers | ( | Bool | unique = False | ) |

Find the row numbers matching the key.

It should be used instead of getRowNumber if the same key can exist multiple times. The 2nd version makes it possible to pass in your own Record instead of using the internal record via the accessKey functions. Note that the given Record will be copied to the internal record, thus overwrites it.
A row can contain multiple equal values. In such a case the same row number can occur multiple times in the output vector, unless unique is set to True. Note that making the row numbers unique implies a sort, so it can also be used to get the row numbers in ascending order.

References casacore::False.

## getRowNumbers() [3/4]

RowNumbers casacore::ColumnsIndexArray::getRowNumbers | ( | const Record & | key,
|  | Bool | unique = False )

References casacore::False, and casacore::Record().

## getRowNumbers() [4/4]

RowNumbers casacore::ColumnsIndexArray::getRowNumbers | ( | const Record & | lower,
|  | const Record & | upper,
|  | Bool | lowerInclusive,
|  | Bool | upperInclusive,
|  | Bool | unique = False )

References casacore::False, and casacore::Record().

## isUnique()

Bool casacore::ColumnsIndexArray::isUnique | ( |  | ) | const

inline

Are all keys in the index unique?

Definition at line 320 of file ColumnsIndexArray.h.

References itsDataIndex, and itsUniqueIndex.

## makeObjects()

void casacore::ColumnsIndexArray::makeObjects | ( | const RecordDesc & | description | ) |

protected

Make the various internal RecordFieldPtr objects.

References casacore::description().

## operator=()

ColumnsIndexArray & casacore::ColumnsIndexArray::operator= | ( | const ColumnsIndexArray & | that | ) |

Assignment (copy semantics).

References ColumnsIndexArray().

## readData()

void casacore::ColumnsIndexArray::readData | ( |  | ) |

protected

Read the data of the columns forming the index, sort them and form the index.

## setChanged() [1/2]

void casacore::ColumnsIndexArray::setChanged | ( |  | ) |

Something has changed in the table, so the index has to be recreated.

The 2nd version indicates that a specific column has changed, so only that column might need to be reread. If that column is not part of the index, nothing will be done.
Note that the class itself is keeping track if the number of rows in the table changes.

## setChanged() [2/2]

void casacore::ColumnsIndexArray::setChanged | ( | const String & | columnName | ) |

References columnName(), and casacore::Record().

## table()

const Table & casacore::ColumnsIndexArray::table | ( |  | ) | const

inline

Get the table for which this index is created.

Definition at line 324 of file ColumnsIndexArray.h.

References itsTable.

## Member Data Documentation

## itsChanged

Bool casacore::ColumnsIndexArray::itsChanged

private

Definition at line 310 of file ColumnsIndexArray.h.

## itsData

void* casacore::ColumnsIndexArray::itsData

private

Definition at line 305 of file ColumnsIndexArray.h.

## itsDataIndex

Vector<rownr_t> casacore::ColumnsIndexArray::itsDataIndex

private

Definition at line 311 of file ColumnsIndexArray.h.

Referenced by isUnique().

## itsDataInx

rownr_t* casacore::ColumnsIndexArray::itsDataInx

private

Definition at line 315 of file ColumnsIndexArray.h.

## itsDataType

Int casacore::ColumnsIndexArray::itsDataType

private

Definition at line 303 of file ColumnsIndexArray.h.

## itsDataVector

void* casacore::ColumnsIndexArray::itsDataVector

private

Definition at line 304 of file ColumnsIndexArray.h.

## itsLowerField

void* casacore::ColumnsIndexArray::itsLowerField

private

Definition at line 308 of file ColumnsIndexArray.h.

## itsLowerKeyPtr

Record* casacore::ColumnsIndexArray::itsLowerKeyPtr

private

Definition at line 301 of file ColumnsIndexArray.h.

Referenced by accessKey(), and accessLowerKey().

## itsNrrow

rownr_t casacore::ColumnsIndexArray::itsNrrow

private

Definition at line 300 of file ColumnsIndexArray.h.

## itsRownrs

Block<rownr_t> casacore::ColumnsIndexArray::itsRownrs

private

Definition at line 314 of file ColumnsIndexArray.h.

## itsTable

Table casacore::ColumnsIndexArray::itsTable

private

Definition at line 299 of file ColumnsIndexArray.h.

Referenced by table().

## itsUniqueIndex

Vector<rownr_t> casacore::ColumnsIndexArray::itsUniqueIndex

private

Definition at line 313 of file ColumnsIndexArray.h.

Referenced by isUnique().

## itsUniqueInx

rownr_t* casacore::ColumnsIndexArray::itsUniqueInx

private

Definition at line 316 of file ColumnsIndexArray.h.

## itsUpperField

void* casacore::ColumnsIndexArray::itsUpperField

private

Definition at line 309 of file ColumnsIndexArray.h.

## itsUpperKeyPtr

Record* casacore::ColumnsIndexArray::itsUpperKeyPtr

private

Definition at line 302 of file ColumnsIndexArray.h.

Referenced by accessUpperKey().

The documentation for this class was generated from the following file:

- tables/Tables/ColumnsIndexArray.h