casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
SetupNewTab.h
Go to the documentation of this file.
00001 //# SetupNewTab.h: Create a new table - define shapes, data managers, etc.
00002 //# Copyright (C) 1994,1995,1996,1999,2001,2002,2003
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: SetupNewTab.h 20551 2009-03-25 00:11:33Z Malte.Marquarding $
00027 
00028 #ifndef TABLES_SETUPNEWTAB_H
00029 #define TABLES_SETUPNEWTAB_H
00030 
00031 
00032 //# Includes
00033 #include <casa/aips.h>
00034 #include <tables/Tables/Table.h>
00035 #include <casa/Containers/SimOrdMap.h>
00036 #include <casa/BasicSL/String.h>
00037 
00038 namespace casa { //# NAMESPACE CASA - BEGIN
00039 
00040 //# Forward Declarations
00041 class TableDesc;
00042 class ColumnSet;
00043 class VirtualColumnEngine;
00044 class DataManager;
00045 class IPosition;
00046 
00047 
00048 // <summary>
00049 // Representation for handle class SetupNewTable
00050 // </summary>
00051 
00052 // <use visibility=local>
00053 
00054 // <reviewed reviewer="bglenden" date="12AUG94" tests="None">
00055 // </reviewed>
00056 
00057 // <prerequisite>
00058 //   <li> TableDesc and related classes like ArrayColumnDesc
00059 //   <li> DataManager
00060 //   <li> Table
00061 // </prerequisite>
00062 
00063 // <etymology>
00064 // SetupNewTableRep is the representation of class SetupNewTable.
00065 // </etymology>
00066 
00067 // <synopsis> 
00068 // SetupNewTableRep is the representation of class
00069 // <linkto class="SetupNewTable:description">SetupNewTable</linkto>.
00070 // Its functionality is described there.
00071 // </synopsis>
00072 
00073 // <motivation>
00074 // Copying a SetupNewTable object as such is very difficult, if not
00075 // impossible. However, being able to use a SetupNewTable copy constructor
00076 // was required to be able to have (static) functions constructing a
00077 // SetupNewTable object and return it by value (as done for example
00078 // by <src>ForwardColumn::setupNewTable</src>).
00079 // Therefore SetupNewTable is implemented using the handle idiom.
00080 // SetupNewTable is the interface (i.e. the handle) for the user,
00081 // while underneath SetupNewTableRep is doing all the work.
00082 // The SetupNewTable copy constructor can simply copy yhe pointer
00083 // to the underlying SetupNewTableRep object.
00084 // </motivation>
00085 
00086 // <todo asof="$DATE:$">
00087 //# A List of bugs, limitations, extensions or planned refinements.
00088 //   <li> full implementation of tiling
00089 // </todo>
00090 
00091 
00092 class SetupNewTableRep
00093 {
00094 public:
00095     // Create a new table using the table description with the given name.
00096     // The description will be read from a file.
00097     SetupNewTableRep (const String& tableName, const String& tableDescName,
00098                       Table::TableOption);
00099 
00100     // Create a new table using the given table description.
00101     SetupNewTableRep (const String& tableName, const TableDesc&,
00102                       Table::TableOption);
00103 
00104     ~SetupNewTableRep();
00105 
00106     // Get access to the reference count.
00107     uInt& count()
00108         { return count_p; }
00109 
00110     // Get the name of the table.
00111     const String& name() const
00112         { return tabName_p; }
00113 
00114     // Get the table create option.
00115     int option() const
00116         { return option_p; }
00117 
00118     // Test if the table is marked for delete.
00119     Bool isMarkedForDelete() const
00120         { return delete_p; }
00121 
00122     // Get the table description.
00123     const TableDesc& tableDesc() const
00124         { return *tdescPtr_p; }
00125 
00126     // Bind a column to the given data manager.
00127     // If already bound, the binding will be overwritten.
00128     // It cannot be used anymore once the SetupNewTableRep object is used to
00129     // construct a Table object.
00130     void bindColumn (const String& columnName, const DataManager&);
00131 
00132     // Bind a column to the given data manager of the other column.
00133     // If the other column is not bound, nothing will be done.
00134     // If columnName is already bound, the binding will be overwritten.
00135     // It cannot be used anymore once the SetupNewTableRep object is used to
00136     // construct a Table object.
00137     void bindColumn (const String& columnName, const String& otherColumn);
00138 
00139     // Bind a group of columns to the given data manager.
00140     // The flag rebind tells if the binding of an already bound column
00141     // will be overwritten.
00142     // It cannot be used anymore once the SetupNewTableRep object is used to
00143     // construct a Table object.
00144     void bindGroup (const String& columnGroup, const DataManager&,
00145                     Bool rebind=False);
00146 
00147     // Bind all columns to the given data manager.
00148     // The flag rebind tells if the binding of an already bound column
00149     // will be overwritten.
00150     // It cannot be used anymore once the SetupNewTableRep object is used to
00151     // construct a Table object.
00152     void bindAll (const DataManager&, Bool rebind=False);
00153 
00154     // Create data managers and bind the columns using the specifications
00155     // in the given record (which is obtained using Table::dataManagerInfo()).
00156     void bindCreate (const Record& spec);
00157 
00158     // Define the shape of fixed shaped arrays in a column.
00159     // The shape of those arrays has to be known before the table
00160     // can be constructed. It has to be defined via this function,
00161     // if it was not already defined in the column description.
00162     // If only the dimensionality was defined in the column
00163     // description, the shape's dimensionality must match it.
00164     // Calling this function for an non-fixed shaped array results in
00165     // an exception.
00166     // It cannot be used anymore once the SetupNewTableRep object is used to
00167     // construct a Table object.
00168     void setShapeColumn (const String& columnName, const IPosition& shape);
00169 
00170     // Test if object is already in use.
00171     Bool isUsed() const
00172         { return (colSetPtr_p == 0  ?  True : False); }
00173 
00174     // Get pointer to column set.
00175     // This function is used by PlainTable.
00176     ColumnSet* columnSetPtr()
00177         { return colSetPtr_p; }
00178 
00179     // Get pointer to table description.
00180     // This function is used by PlainTable.
00181     TableDesc* tableDescPtr()
00182         { return tdescPtr_p; }
00183 
00184     // Set object to in use by a (Plain)Table object.
00185     // This function is used by PlainTable.
00186     void setInUse()
00187         { colSetPtr_p = 0; }
00188 
00189     // Make a data manager for all unbound columns.
00190     void handleUnbound();
00191 
00192 private:
00193     // Reference count.
00194     uInt        count_p;
00195     // Table name.
00196     String      tabName_p;
00197     // Constructor options.
00198     int         option_p;
00199     // Marked for delete?
00200     Bool        delete_p;
00201     TableDesc*  tdescPtr_p;
00202     ColumnSet*  colSetPtr_p;      //# 0 = object is already used by a Table
00203     SimpleOrderedMap<void*,void*> dataManMap_p;
00204 
00205     // Copy constructor is forbidden, because copying a table requires
00206     // some more knowledge (like table name of result).
00207     // Declaring it private, makes it unusable.
00208     SetupNewTableRep (const SetupNewTableRep&);
00209 
00210     // Assignment is forbidden, because copying a table requires
00211     // some more knowledge (like table name of result).
00212     // Declaring it private, makes it unusable.
00213     SetupNewTableRep& operator= (const SetupNewTableRep&);
00214 
00215     // Setup the new table.
00216     // This checks various things and creates the set of columns.
00217     void setup();
00218 
00219     // Get the internal data manager object for the given data manager.
00220     // If it does not exist yet, it will be cloned and stored internally.
00221     DataManager* getDataManager (const DataManager& dataMan);
00222 };
00223 
00224 
00225 
00226 
00227 
00228 // <summary>
00229 // Create a new table - define shapes, data managers, etc.
00230 // </summary>
00231 
00232 // <use visibility=export>
00233 
00234 // <reviewed reviewer="bglenden" date="12AUG94" tests="None">
00235 // </reviewed>
00236 
00237 // <prerequisite>
00238 //   <li> TableDesc and related classes like ArrayColumnDesc
00239 //   <li> DataManager
00240 //   <li> Table
00241 // </prerequisite>
00242 
00243 // <etymology>
00244 // SetupNewTable is a class to setup a new table.
00245 // </etymology>
00246 
00247 // <synopsis> 
00248 // Constructing a new table is a two stage process.
00249 // First a SetupNewTable object has to be created. Thereafter its columns
00250 // have to be bound defining how they have to be stored or calculated.
00251 // Columns have to be bound to a data manager (e.g. a storage manager
00252 // or a virtual column engine)..
00253 // Once the required columns are bound, the actual Table object can
00254 // be created. At this stage, still unbound columns will be bound
00255 // to the default data managers.
00256 // The Table object can be used to write data, etc.
00257 // 
00258 // The construct options for SetupNewTable are defined in class Table.
00259 // The possible options are:
00260 // <ul>
00261 //   <li> New
00262 //      creates a new table file.
00263 //      The Table destructor will write the table into the file.
00264 //   <li> NewNoReplace
00265 //      as option New, but an exception will be thrown if the table
00266 //      file already exists.
00267 //   <li> Scratch
00268 //      creates a temporary table.
00269 //      It will be lost when the Table object gets destructed.
00270 // </ul>
00271 // More information is provided in the Tables module documentation.
00272 // </synopsis> 
00273 //
00274 // <example>
00275 // <srcblock>
00276 //      Table makeIt(const TableDesc &td) {                            // 1
00277 //            SetupNewTable maker("test.table", td, Table::New);       // 2
00278 //            maker.setShapeColumn("SomeArray", IPosition(2,10,10));   // 3
00279 //            maker.setShapeColumn("AnotherArray", IPosition(1,100));  // 4
00280 //            StManAipsIO sm1;                                         // 5
00281 //            StManKarma  sm2;                                         // 6
00282 //            maker.bindAll(sm1);                                      // 7
00283 //            maker.bindColumn("SomeCol", sm2);                        // 8
00284 //            maker.bindColumn("AnotherCol", sm2);                     // 9
00285 //            return Table(maker, 1000); // 1000 row table             // 10
00286 //      }                                                              // 11
00287 // </srcblock>
00288 // This code illustrates a simple function that creates a Table starting
00289 // from a Table descriptor. I
00290 // <ol>
00291 //    <li> Declare the function makeIt which, given a TableDesc, returns
00292 //           a table.
00293 //    <li> Create the SetupNewTable object "maker". We want the new table
00294 //           to be named "test.table", its rows columns and keywords come
00295 //           from the TableDesc "td", and this table is to be created
00296 //           unconditionally, that is, it will overwrite an existing table
00297 //           of the same name. Alternative options are given in the synopsis.
00298 //    <li>
00299 //    <li> Give direct arrays declared in the table descriptor (but not
00300 //           necessarily given a shape) a defined shape; 10x10 for the first
00301 //           array, 100 long vector for the second. If all direct arrays 
00302 //           do not have a shape, an error will occur when the table is
00303 //           actually constructed.
00304 //    <li>
00305 //    <li> Declare two data (storage) managers. AipsIO keeps a whole column
00306 //           in memory, Karma does I/O to keep a subsection in memory at once.
00307 //           A powerful feature of AIPS++ tables is that different columns
00308 //           may be bound to different data managers, which have different
00309 //           properties.
00310 //    <li> Define the default data manager. AipsIO in this case.
00311 //           Note that this statement and statement 5 are actually not
00312 //           needed. When the Table constructor finds some unbound columns,
00313 //           it will construct the default data manager for them and
00314 //           bind them. A default data manager can be defined in the
00315 //           column description and defaults to AipsIO.
00316 //    <li>
00317 //    <li> Override the default for some particular columns.
00318 //    <li> Create and return a 1000 row table. With the Karma storage manager
00319 //           the table size must be defined at construction since new rows
00320 //           can't be added or deleted. If AipsIO was the only storage manager,
00321 //           the size wouldn't need to be defined since rows can be added with
00322 //           AipsIO.
00323 // </ol>
00324 // </example>
00325 
00326 // <motivation>
00327 // In principle, SetupNewTab isn't necessary as what we are doing is logically
00328 // just constructing a Table, so it could be done in the Table constructor.
00329 // However such a process can be an involved one - binding multiple data
00330 // managers and filling in the shapes of direct arrays - so separating
00331 // the process makes it much clearer what is going on.
00332 // </motivation>
00333 
00334 // <todo asof="$DATE:$">
00335 //# A List of bugs, limitations, extensions or planned refinements.
00336 //   <li> full implementation of tiling
00337 // </todo>
00338 
00339 
00340 class SetupNewTable
00341 {
00342 friend class PlainTable;
00343 friend class MemoryTable;
00344 
00345 public:
00346     // Create a new table using the table description with the given name.
00347     // The description will be read from a file.
00348     SetupNewTable (const String& tableName, const String& tableDescName,
00349                    Table::TableOption);
00350 
00351     // Create a new table using the given table description.
00352     SetupNewTable (const String& tableName, const TableDesc&,
00353                    Table::TableOption);
00354 
00355     // Copy constructor (reference semantics).
00356     SetupNewTable (const SetupNewTable&);
00357 
00358     ~SetupNewTable();
00359 
00360     // Assignment (reference semantics).
00361     SetupNewTable& operator= (const SetupNewTable&);
00362 
00363     // Get the name of the table.
00364     const String& name() const
00365         { return newTable_p->name(); }
00366 
00367     // Get the table create option.
00368     int option() const
00369         { return newTable_p->option(); }
00370 
00371     // Test if the table is marked for delete.
00372     Bool isMarkedForDelete() const
00373         { return newTable_p->isMarkedForDelete(); }
00374 
00375     // Get the table description.
00376     const TableDesc& tableDesc() const
00377         { return newTable_p->tableDesc(); }
00378 
00379     // Adjust the hypercolumn definitions.
00380     // It renames and/or removes columns as necessary.
00381     void adjustHypercolumns (const SimpleOrderedMap<String, String>& old2new,
00382                              Bool keepUnknown)
00383       { newTable_p->tableDescPtr()->adjustHypercolumns(old2new,keepUnknown); }
00384 
00385     // Bind a column to the given data manager.
00386     // If already bound, the binding will be overwritten.
00387     // It cannot be used anymore once the SetupNewTable object is used to
00388     // construct a Table object.
00389     void bindColumn (const String& columnName, const DataManager& dm)
00390         { newTable_p->bindColumn (columnName, dm); }
00391 
00392     // Bind a column to the given data manager of the other column.
00393     // If the other column is not bound, nothing will be done.
00394     // If columnName is already bound, the binding will be overwritten.
00395     // It cannot be used anymore once the SetupNewTableRep object is used to
00396     // construct a Table object.
00397     void bindColumn (const String& columnName, const String& otherColumn)
00398         { newTable_p->bindColumn (columnName, otherColumn); }
00399 
00400     // Bind a group of columns to the given data manager.
00401     // The flag rebind tells if the binding of an already bound column
00402     // will be overwritten.
00403     // It cannot be used anymore once the SetupNewTable object is used to
00404     // construct a Table object.
00405     void bindGroup (const String& columnGroup, const DataManager& dm,
00406                     Bool rebind=False)
00407         { newTable_p->bindGroup (columnGroup, dm, rebind); }
00408 
00409     // Bind all columns to the given data manager.
00410     // The flag rebind tells if the binding of an already bound column
00411     // will be overwritten.
00412     // It cannot be used anymore once the SetupNewTable object is used to
00413     // construct a Table object.
00414     void bindAll (const DataManager& dm, Bool rebind=False)
00415         { newTable_p->bindAll (dm, rebind); }
00416 
00417     // Create data managers and bind the columns using the specifications
00418     // in the given record (which is obtained using Table::dataManagerInfo()).
00419     void bindCreate (const Record& spec)
00420         { newTable_p->bindCreate (spec); }
00421 
00422     // Define the shape of fixed shaped arrays in a column.
00423     // The shape of those arrays has to be known before the table
00424     // can be constructed. It has to be defined via this function,
00425     // if it was not already defined in the column description.
00426     // If only the dimensionality was defined in the column
00427     // description, the shape's dimensionality must match it.
00428     // Calling this function for an non-fixed shaped array results in
00429     // an exception.
00430     // It cannot be used anymore once the SetupNewTable object is used to
00431     // construct a Table object.
00432     void setShapeColumn (const String& columnName, const IPosition& shape)
00433         { newTable_p->setShapeColumn (columnName, shape); }
00434 
00435     // Test if object is already in use.
00436     Bool isUsed() const
00437         { return newTable_p->isUsed(); }
00438 
00439 private:
00440     // Actual object.
00441     SetupNewTableRep* newTable_p;
00442 
00443     // Get pointer to column set.
00444     // This function is used by PlainTable.
00445     ColumnSet* columnSetPtr()
00446         { return newTable_p->columnSetPtr(); }
00447 
00448     // Get pointer to table description.
00449     // This function is used by PlainTable.
00450     TableDesc* tableDescPtr()
00451         { return newTable_p->tableDescPtr(); }
00452 
00453     // Set object to in use by a (Plain)Table object.
00454     // This function is used by PlainTable.
00455     void setInUse()
00456         { newTable_p->setInUse(); }
00457 
00458     // Make a data manager for all unbound columns.
00459     void handleUnbound()
00460         { newTable_p->handleUnbound(); }
00461 };
00462 
00463 
00464 
00465 } //# NAMESPACE CASA - END
00466 
00467 #endif