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