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