00001 //# TableCache.h: Cache of open tables 00002 //# Copyright (C) 1994,1995,1997,1999 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$ 00027 00028 #ifndef TABLES_TABLECACHE_H 00029 #define TABLES_TABLECACHE_H 00030 00031 //# Includes 00032 #include <casacore/casa/aips.h> 00033 #include <casacore/casa/IO/FileLocker.h> 00034 #include <casacore/casa/Containers/SimOrdMap.h> 00035 #include <casacore/casa/OS/Mutex.h> 00036 00037 namespace casacore { //# NAMESPACE CASACORE - BEGIN 00038 00039 //# Forward Declarations 00040 class PlainTable; 00041 class TableLock; 00042 template<class T> class Vector; 00043 00044 00045 // <summary> 00046 // Cache of open tables 00047 // </summary> 00048 00049 // <use visibility=local> 00050 00051 // <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests=""> 00052 // </reviewed> 00053 00054 // <prerequisite> 00055 //# Classes you should understand before using this one. 00056 // </prerequisite> 00057 00058 // <etymology> 00059 // TableCache represents a cache of open tables. 00060 // </etymology> 00061 00062 // <synopsis> 00063 // A TableCache object keeps track of the tables which have already 00064 // been opened in a program. It maps the name of a table to its 00065 // PlainTable object. 00066 // In principle only one TableCache object (statically defined in 00067 // class PlainTable) exists in a process. 00068 // The cache is used to prevent a table from being opened more than 00069 // once, which is not only a waste of space, but more importantly, 00070 // may give rise to synchronization problems. 00071 // Synchronization between the same table in multiple processes must 00072 // be done by a locking mechanism. 00073 // 00074 // TableCache is used by class Table and PlainTable. 00075 // Before opening a table, Table will first look in the cache. 00076 // Newly opened or created tables will be added to the cache. 00077 // When a table is actually closed, it will be removed from the cache. 00078 // </synopsis> 00079 00080 // <motivation> 00081 // When a RefTable is read back, it will also read back the table it 00082 // references. However, that table may have been opened before and 00083 // it is bad to have a table open more than once in the same program. 00084 // The TableCache class catches this and will not reopen the table. 00085 // </motivation> 00086 00087 // <todo asof="$DATE:$"> 00088 //# A List of bugs, limitations, extensions or planned refinements. 00089 // <li> Currently only PlainTables are taken into account. 00090 // Maybe RefTables should be too. 00091 // </todo> 00092 00093 00094 class TableCache 00095 { 00096 public: 00097 00098 // Construct an empty cache of open tables. 00099 TableCache(); 00100 00101 ~TableCache(); 00102 00103 // Try to find a table with the given name in the cache. 00104 // Return a pointer to a table if found (thus if already open). 00105 // Return a zero pointer if not found. 00106 PlainTable* operator() (const String& tableName) const; 00107 00108 // Add an open table to the cache. 00109 void define (const String& tableName, PlainTable*); 00110 00111 // Remove an open table. 00112 void remove (const String& tableName); 00113 00114 // Rename an open table. 00115 // If oldName is not in the cache, nothing will be done. 00116 void rename (const String& newName, const String& oldName); 00117 00118 // Determine the number of locked tables opened with the AutoLock option 00119 // (Locked table means locked for read and/or write). 00120 uInt nAutoLocks(); 00121 00122 // Unlock locked tables opened with the AutoLock option. 00123 // If <src>all=True</src> all such tables will be unlocked. 00124 // If <src>all=False</src> only tables requested by another process 00125 // will be unlocked. 00126 void relinquishAutoLocks (Bool all); 00127 00128 // Get the names of the tables in the cache. 00129 Vector<String> getTableNames() const; 00130 00131 // Get the names of tables locked in this process. 00132 // By default all locked tables are given (note that a write lock 00133 // implies a read lock), but it is possible to select on lock type 00134 // FileLocker::Write and on option (TableLock::AutoLocking, 00135 // TableLock::ReadLocking, or TableLock::PermanentLocking). 00136 Vector<String> getLockedTables (FileLocker::LockType, 00137 int lockOption); 00138 00139 // Flush a possibly cached Table. 00140 void flushTable (const String& tableName, 00141 Bool fsync, Bool recursive); 00142 00143 // Look in the cache if the table is already open. 00144 // If so, check if table option matches. 00145 // If needed reopen the table for read/write and merge the lock options. 00146 PlainTable* lookCache (const String& name, int tableOption, 00147 const TableLock& tableInfo); 00148 00149 private: 00150 // The copy constructor is forbidden. 00151 TableCache (const TableCache&); 00152 // The assignment operator is forbidden. 00153 TableCache& operator= (const TableCache&); 00154 00155 // Get the table without doing a mutex lock (for operator()). 00156 PlainTable* getTable (const String& tableName) const; 00157 00158 //# void* iso. PlainTable* is used in the map declaration 00159 //# to reduce the number of template instantiations. 00160 //# The .cc file will use (fully safe) casts. 00161 SimpleOrderedMap<String,void*> tableMap_p; 00162 //# A mutex to synchronize access to the cache. 00163 mutable Mutex itsMutex; 00164 }; 00165 00166 00167 00168 } //# NAMESPACE CASACORE - END 00169 00170 #endif