casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
ColumnsIndexArray.h
Go to the documentation of this file.
00001 //# ColumnsIndexArray.h: Index to an array column in a table
00002 //# Copyright (C) 2001,2002
00003 //# Associated Universities, Inc. Washington DC, USA.
00004 //#
00005 //# This library is free software; you can redistribute it and/or modify it
00006 //# under the terms of the GNU Library General Public License as published by
00007 //# the Free Software Foundation; either version 2 of the License, or (at your
00008 //# option) any later version.
00009 //#
00010 //# This library is distributed in the hope that it will be useful, but WITHOUT
00011 //# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00012 //# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
00013 //# License for more details.
00014 //#
00015 //# You should have received a copy of the GNU Library General Public License
00016 //# along with this library; if not, write to the Free Software Foundation,
00017 //# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
00018 //#
00019 //# Correspondence concerning AIPS++ should be addressed as follows:
00020 //#        Internet email: aips2-request@nrao.edu.
00021 //#        Postal address: AIPS++ Project Office
00022 //#                        National Radio Astronomy Observatory
00023 //#                        520 Edgemont Road
00024 //#                        Charlottesville, VA 22903-2475 USA
00025 //#
00026 //# $Id: ColumnsIndexArray.h 21298 2012-12-07 14:53:03Z gervandiepen $
00027 
00028 #ifndef TABLES_COLUMNSINDEXARRAY_H
00029 #define TABLES_COLUMNSINDEXARRAY_H
00030 
00031 
00032 //# Includes
00033 #include <tables/Tables/Table.h>
00034 #include <casa/Arrays/Vector.h>
00035 #include <casa/Containers/Block.h>
00036 #include <casa/Containers/Record.h>
00037 
00038 namespace casa { //# NAMESPACE CASA - BEGIN
00039 
00040 //# Forward Declarations
00041 class String;
00042 class TableColumn;
00043 
00044 
00045 // <summary>
00046 // Index to an array column in a table.
00047 // </summary>
00048 
00049 // <use visibility=export>
00050 
00051 // <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="tColumnsIndexArray.cc" demos="">
00052 // </reviewed>
00053 
00054 // <prerequisite>
00055 //   <li> <linkto class=Table>Table</linkto>
00056 //   <li> <linkto class=Record>Record</linkto>
00057 //   <li> <linkto class=RecordFieldPtr>RecordFieldPtr</linkto>
00058 // </prerequisite>
00059 
00060 // <synopsis>
00061 // This class makes it possible to use transient indices on top
00062 // of an array column in a table in order to speed up the process of
00063 // finding rows based on a given key or key range.
00064 // It is similar to class <linkto class=ColumnsIndex>ColumnsIndex</linkto>
00065 // which is meant for one or more scalar columns.
00066 // <p>
00067 // When constructing a <src>ColumnsIndexArray</src> object, one has to define
00068 // which column forms the key for this index on the given
00069 // <src>table</src> object. 
00070 // Not every data type is supported; only uChar, Short, Int, uInt, and
00071 // String array columns are supported.
00072 // The column can contain arrays of any shape and it can also contain
00073 // empty cells. The class will probably mostly be used for vectors, as
00074 // they seem to be the most logical way to hold multiple keys.
00075 // <br>The data in the given column will be read, sorted,
00076 // and stored in memory. When looking up a key or key range, the class
00077 // will use a fast binary search on the data held in memory.
00078 // <p>
00079 // The <src>ColumnsIndexArray</src> object contains a
00080 // <linkto class=Record>Record</linkto> object which can be used
00081 // to define the key to be looked up. The record contains a field for
00082 // the column in the index (with the same name and data type).
00083 // The fastest way to fill the key is by creating a
00084 // <linkto class=RecordFieldPtr>RecordFieldPtr</linkto> object for
00085 // the field in the record (see the example) and fill it as needed.
00086 // However, one can also use the <src>Record::define</src> function,
00087 // but that is slower.
00088 // <br>
00089 // A second record is available to define the upper key
00090 // in case a key range has to be looked up. The keys can be accessed
00091 // using the various <src>accessKey</src> functions.
00092 // <p>
00093 // When a key is defined, the <src>getRowNumbers</src> function can be
00094 // used to find the table rows containing the given key (range).
00095 // Function <src>getRowNumber</src> can be used to lookup a single key
00096 // if all keys in the index are unique (which can be tested with the
00097 // <src>isUnique</src> function).
00098 // <p>
00099 // Instead of using the internal records holding the keys, one can also
00100 // pass its own Record object to <src>getRowNumbers</src>.
00101 // However, it will be slower.
00102 // <p>
00103 // After an index is created, it is possible to change the data
00104 // in the underlying columns. However, the <src>ColumnsIndexArray</src> can
00105 // not detect if the column data have changed. It can only detect if
00106 // the number of rows has changed. If the column data have changed,
00107 // the user has to use the <src>setChanged</src> function to indicate
00108 // that the column has changed.
00109 // <br>If data have changed, the entire index will be recreated by
00110 // rereading and resorting the data. This will be deferred
00111 // until the next key lookup.
00112 // </synopsis>
00113 
00114 // <example>
00115 // Suppose one has table with a column NAME containing vectors.
00116 // <srcblock>
00117 // // Open the table and make an index for the column.
00118 // Table tab("my.tab")
00119 // ColumnsIndexArray colInx(tab, "NAME");
00120 // // Make a RecordFieldPtr for the NAME field in the index key record.
00121 // // Its data type has to match the data type of the column.
00122 // RecordFieldPtr<String> nameFld(colInx.accessKey(), "NAME");
00123 // // Find the row for a given name.
00124 // Bool found;
00125 // // Fill the key field and get the row number.
00126 // // NAME is a unique key, so only one row number matches.
00127 // // Otherwise function getRowNumbers had to be used.
00128 // *nameFld = "MYNAME";
00129 // uInt rownr = colInx.getRowNumber (found);
00130 // if (!found) {
00131 //     cout << "Name MYNAME is unknown" << endl;
00132 // }
00133 // // Now get a range of names and return the row numbers in ascending order.
00134 // // This uses the fact that the 'unique' argument also sorts the data.
00135 // RecordFieldPtr<String> nameUpp(colInx.accessUpperKey(), "NAME");
00136 // *nameFld = "LOWER";
00137 // *nameUpp = "UPPER";
00138 // Vector<uInt> rownrs = colInx.getRowNumbers (True, True, True);
00139 // </srcblock>
00140 
00141 // <motivation>
00142 // Bob Garwood needed such a class.
00143 // </motivation>
00144 
00145 
00146 class ColumnsIndexArray
00147 {
00148 public:
00149   // Create an index on the given table for the given column.
00150   // The column can be a scalar or an array column.
00151   // If <src>noSort==True</src>, the table is already in order of that
00152   // column and the sort step will not be done.
00153   // It only supports String and integer columns.
00154   ColumnsIndexArray (const Table&, const String& columnName);
00155 
00156   // Copy constructor (copy semantics).
00157   ColumnsIndexArray (const ColumnsIndexArray& that);
00158 
00159   ~ColumnsIndexArray();
00160 
00161   // Assignment (copy semantics).
00162   ColumnsIndexArray& operator= (const ColumnsIndexArray& that);
00163 
00164   // Are all keys in the index unique?
00165   Bool isUnique() const;
00166 
00167   // Return the names of the columns forming the index.
00168   const String& columnName() const;
00169 
00170   // Get the table for which this index is created.
00171   const Table& table() const;
00172 
00173   // Something has changed in the table, so the index has to be recreated.
00174   // The 2nd version indicates that a specific column has changed,
00175   // so only that column might need to be reread. If that column is not
00176   // part of the index, nothing will be done.
00177   // <br>Note that the class itself is keeping track if the number of
00178   // rows in the table changes.
00179   // <group>
00180   void setChanged();
00181   void setChanged (const String& columnName);
00182   // </group>
00183 
00184   // Access the key values.
00185   // These functions allow you to create RecordFieldPtr<T> objects
00186   // for each field in the key. In this way you can quickly fill in
00187   // the key.
00188   // <br>The records have a fixed type, so you cannot add or delete fields.
00189   // <br>Note that <src>accessKey</src> and <src>accessLowerKey</src>
00190   // are synonyms; they return the same underlying record.
00191   // <group>
00192   Record& accessKey();
00193   Record& accessLowerKey();
00194   Record& accessUpperKey();
00195   // </group>
00196 
00197   // Find the row number matching the key. All keys have to be unique,
00198   // otherwise an exception is thrown.
00199   // If no match is found, <src>found</src> is set to False.
00200   // The 2nd version makes it possible to pass in your own Record
00201   // instead of using the internal record via the <src>accessKey</src>
00202   // functions. Note that the given Record will be copied to the internal
00203   // record, thus overwrites it.
00204   // <group>
00205   uInt getRowNumber (Bool& found);
00206   uInt getRowNumber (Bool& found, const Record& key);
00207   // </group>
00208 
00209   // Find the row numbers matching the key. It should be used instead
00210   // of <src>getRowNumber</src> if the same key can exist multiple times.
00211   // The 2nd version makes it possible to pass in your own Record
00212   // instead of using the internal record via the <src>accessKey</src>
00213   // functions. Note that the given Record will be copied to the internal
00214   // record, thus overwrites it.
00215   // <br>A row can contain multiple equal values. In such a case the
00216   // same row number can occur multiple times in the output vector,
00217   // unless <src>unique</src> is set to True. Note that making the row
00218   // numbers unique implies a sort, so it can also be used to get the
00219   // row numbers in ascending order.
00220   // <group>
00221   Vector<uInt> getRowNumbers (Bool unique=False);
00222   Vector<uInt> getRowNumbers (const Record& key, Bool unique=False);
00223   // </group>
00224 
00225   // Find the row numbers matching the key range. The boolean arguments
00226   // tell if the lower and upper key are part of the range.
00227   // The 2nd version makes it possible to pass in your own Records
00228   // instead of using the internal records via the
00229   // <src>accessLower/UpperKey</src> functions.
00230   // Note that the given Records will be copied to the internal
00231   // records, thus overwrite them.
00232   // <br>A row can contain multiple matching values. In such a case the
00233   // same row number can occur multiple times in the output vector,
00234   // unless <src>unique</src> is set to True. Note that making the row
00235   // numbers unique implies a sort, so it can also be used to get the
00236   // row numbers in ascending order.
00237   // <group>
00238   Vector<uInt> getRowNumbers (Bool lowerInclusive, Bool upperInclusive,
00239                               Bool unique=False);
00240   Vector<uInt> getRowNumbers (const Record& lower, const Record& upper,
00241                               Bool lowerInclusive, Bool upperInclusive,
00242                               Bool unique=False);
00243   // </group>
00244 
00245 protected:
00246   // Copy that object to this.
00247   void copy (const ColumnsIndexArray& that);
00248 
00249   // Delete all data in the object.
00250   void deleteObjects();
00251 
00252   // Add a column to the record description for the keys.
00253   // If the switch <src>arrayPossible</src> is True, the column can
00254   // be an array. Otherwise it has to be a scalar.
00255   void addColumnToDesc (RecordDesc& description,
00256                         const TableColumn& column);
00257 
00258   // Make the various internal <src>RecordFieldPtr</src> objects.
00259   void makeObjects (const RecordDesc& description);
00260 
00261   // Read the data of the columns forming the index, sort them and
00262   // form the index.
00263   void readData();
00264 
00265   // Do a binary search on <src>itsUniqueIndexArray</src> for the key in
00266   // <src>fieldPtrs</src>.
00267   // If the key is found, <src>found</src> is set to True and the index
00268   // in <src>itsUniqueIndexArray</src> is returned.
00269   // If not found, <src>found</src> is set to False and the index
00270   // of the next higher key is returned.
00271   uInt bsearch (Bool& found, void* fieldPtr) const;
00272 
00273   // Compare the key in <src>fieldPtr</src> with the given index entry.
00274   // -1 is returned when less, 0 when equal, 1 when greater.
00275   static Int compare (void* fieldPtr,
00276                       void* dataPtr,
00277                       Int dataType,
00278                       Int index);
00279 
00280   // Fill the row numbers vector for the given start till end in the
00281   // <src>itsUniqueIndexArray</src> vector (end is not inclusive).
00282   // If <src>unique</src> is True, the row numbers will be made unique.
00283   void fillRowNumbers (Vector<uInt>& rows, uInt start, uInt end,
00284                        Bool unique) const;
00285 
00286   // Get the data if the column is an array.
00287   // <group>
00288   void getArray (Vector<uChar>& result, const String& name);
00289   void getArray (Vector<Short>& result, const String& name);
00290   void getArray (Vector<Int>& result, const String& name);
00291   void getArray (Vector<uInt>& result, const String& name);
00292   void getArray (Vector<String>& result, const String& name);
00293   // </group>
00294 
00295   // Fill the rownrs belonging to each array value.
00296   void fillRownrs (uInt npts, const Block<uInt>& nrel);
00297 
00298 private:
00299   Table  itsTable;
00300   uInt   itsNrrow;
00301   Record* itsLowerKeyPtr;
00302   Record* itsUpperKeyPtr;
00303   Int     itsDataType;
00304   void*   itsDataVector;
00305   void*   itsData;              //# pointer to data in itsDataVector
00306   //# The following 2 blocks are actually blocks of RecordFieldPtr<T>*.
00307   //# They are used for fast access to the records.
00308   void*   itsLowerField;
00309   void*   itsUpperField;
00310   Bool         itsChanged;
00311   Vector<uInt> itsDataIndex;         //# Row numbers of all keys
00312   //# Indices in itsDataIndex for each unique key
00313   Vector<uInt> itsUniqueIndex;
00314   Block<uInt>  itsRownrs;            //# rownr for each value
00315   uInt*        itsDataInx;           //# pointer to data in itsDataIndex
00316   uInt*        itsUniqueInx;         //# pointer to data in itsUniqueIndex
00317 };
00318 
00319 
00320 inline Bool ColumnsIndexArray::isUnique() const
00321 {
00322     return (itsDataIndex.nelements() == itsUniqueIndex.nelements());
00323 }
00324 inline const Table& ColumnsIndexArray::table() const
00325 {
00326     return itsTable;
00327 }
00328 inline Record& ColumnsIndexArray::accessKey()
00329 {
00330     return *itsLowerKeyPtr;
00331 }
00332 inline Record& ColumnsIndexArray::accessLowerKey()
00333 {
00334     return *itsLowerKeyPtr;
00335 }
00336 inline Record& ColumnsIndexArray::accessUpperKey()
00337 {
00338     return *itsUpperKeyPtr;
00339 }
00340 
00341 
00342 
00343 } //# NAMESPACE CASA - END
00344 
00345 #endif