casacore
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
TableCache.h
Go to the documentation of this file.
1 //# TableCache.h: Cache of open tables
2 //# Copyright (C) 1994,1995,1997,1999
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: aips2-request@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 //# $Id$
27 
28 #ifndef TABLES_TABLECACHE_H
29 #define TABLES_TABLECACHE_H
30 
31 //# Includes
32 #include <casacore/casa/aips.h>
35 
36 #include <map>
37 #include <mutex>
38 
39 namespace casacore { //# NAMESPACE CASACORE - BEGIN
40 
41 //# Forward Declarations
42 class PlainTable;
43 class TableLock;
44 
45 // <summary>
46 // Cache of open tables
47 // </summary>
48 
49 // <use visibility=local>
50 
51 // <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="">
52 // </reviewed>
53 
54 // <prerequisite>
55 //# Classes you should understand before using this one.
56 // </prerequisite>
57 
58 // <etymology>
59 // TableCache represents a cache of open tables.
60 // </etymology>
61 
62 // <synopsis>
63 // A TableCache object keeps track of the tables which have already
64 // been opened in a program. It maps the name of a table to its
65 // PlainTable object.
66 // In principle only one TableCache object (statically defined in
67 // class PlainTable) exists in a process.
68 // The cache is used to prevent a table from being opened more than
69 // once, which is not only a waste of space, but more importantly,
70 // may give rise to synchronization problems.
71 // Synchronization between the same table in multiple processes must
72 // be done by a locking mechanism.
73 //
74 // TableCache is used by class Table and PlainTable.
75 // Before opening a table, Table will first look in the cache.
76 // Newly opened or created tables will be added to the cache.
77 // When a table is actually closed, it will be removed from the cache.
78 // </synopsis>
79 
80 // <motivation>
81 // When a RefTable is read back, it will also read back the table it
82 // references. However, that table may have been opened before and
83 // it is bad to have a table open more than once in the same program.
84 // The TableCache class catches this and will not reopen the table.
85 // </motivation>
86 
87 // <todo asof="$DATE:$">
88 //# A List of bugs, limitations, extensions or planned refinements.
89 // <li> Currently only PlainTables are taken into account.
90 // Maybe RefTables should be too.
91 // </todo>
92 
93 
95 {
96 public:
97 
98  // Construct an empty cache of open tables.
99  TableCache();
100 
101  ~TableCache();
102 
103  // Try to find a table with the given name in the cache.
104  // Return a pointer to a table if found (thus if already open).
105  // Return a zero pointer if not found.
106  PlainTable* operator() (const String& tableName) const;
107 
108  // Add an open table to the cache.
109  void define (const String& tableName, PlainTable*);
110 
111  // Remove an open table.
112  void remove (const String& tableName);
113 
114  // Rename an open table.
115  // If oldName is not in the cache, nothing will be done.
116  void rename (const String& newName, const String& oldName);
117 
118  // Determine the number of locked tables opened with the AutoLock option
119  // (Locked table means locked for read and/or write).
120  uInt nAutoLocks();
121 
122  // Unlock locked tables opened with the AutoLock option.
123  // If <src>all=True</src> all such tables will be unlocked.
124  // If <src>all=False</src> only tables requested by another process
125  // will be unlocked.
127 
128  // Get the names of the tables in the cache.
130 
131  // Get the names of tables locked in this process.
132  // By default all locked tables are given (note that a write lock
133  // implies a read lock), but it is possible to select on lock type
134  // FileLocker::Write and on option (TableLock::AutoLocking,
135  // TableLock::ReadLocking, or TableLock::PermanentLocking).
137  int lockOption);
138 
139  // Flush a possibly cached Table.
140  void flushTable (const String& tableName,
141  Bool fsync, Bool recursive);
142 
143  // Look in the cache if the table is already open.
144  // If so, check if table option matches.
145  // If needed reopen the table for read/write and merge the lock options.
146  PlainTable* lookCache (const String& name, int tableOption,
147  const TableLock& tableInfo);
148 
149 private:
150  // The copy constructor is forbidden.
151  TableCache (const TableCache&);
152  // The assignment operator is forbidden.
154 
155  // Get the table without doing a mutex lock (for operator()).
156  PlainTable* getTable (const String& tableName) const;
157 
158  //# void* iso. PlainTable* is used in the map declaration
159  //# to reduce the number of template instantiations.
160  //# The .cc file will use (fully safe) casts.
161  std::map<String,void*> tableMap_p;
162  //# A mutex to synchronize access to the cache.
163  mutable std::mutex itsMutex;
164 };
165 
166 
167 
168 } //# NAMESPACE CASACORE - END
169 
170 #endif
std::map< String, void * > tableMap_p
Definition: TableCache.h:161
A 1-D Specialization of the Array class.
Definition: ArrayFwd.h:9
Class defining a plain regular table.
Definition: PlainTable.h:87
std::mutex itsMutex
Definition: TableCache.h:163
uInt nAutoLocks()
Determine the number of locked tables opened with the AutoLock option (Locked table means locked for ...
Vector< String > getLockedTables(FileLocker::LockType, int lockOption)
Get the names of tables locked in this process.
TableInfo tableInfo(const String &tableName)
Get the table info of the table with the given name.
PlainTable * getTable(const String &tableName) const
Get the table without doing a mutex lock (for operator()).
PlainTable * lookCache(const String &name, int tableOption, const TableLock &tableInfo)
Look in the cache if the table is already open.
Vector< String > getTableNames() const
Get the names of the tables in the cache.
void define(const String &tableName, PlainTable *)
Add an open table to the cache.
Cache of open tables.
Definition: TableCache.h:94
void flushTable(const String &tableName, Bool fsync, Bool recursive)
Flush a possibly cached Table.
bool Bool
Define the standard types used by Casacore.
Definition: aipstype.h:42
Class to hold table lock options.
Definition: TableLock.h:68
TableCache & operator=(const TableCache &)
The assignment operator is forbidden.
void rename(const String &newName, const String &oldName)
Rename an open table.
PlainTable * operator()(const String &tableName) const
Try to find a table with the given name in the cache.
TableCache()
Construct an empty cache of open tables.
String: the storage and methods of handling collections of characters.
Definition: String.h:225
LockType
Define the possible lock types.
Definition: FileLocker.h:95
LatticeExprNode all(const LatticeExprNode &expr)
unsigned int uInt
Definition: aipstype.h:51
void relinquishAutoLocks(Bool all)
Unlock locked tables opened with the AutoLock option.