File: TableCache.cc

package info (click to toggle)
casacore 3.5.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 51,680 kB
  • sloc: cpp: 462,815; fortran: 16,372; ansic: 7,403; yacc: 4,626; lex: 2,340; sh: 1,786; python: 629; perl: 531; sed: 499; csh: 34; makefile: 31
file content (202 lines) | stat: -rw-r--r-- 6,329 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
//# TableCache.cc: Cache of open tables
//# Copyright (C) 1994,1995,1997,1999
//# Associated Universities, Inc. Washington DC, USA.
//#
//# This library is free software; you can redistribute it and/or modify it
//# under the terms of the GNU Library General Public License as published by
//# the Free Software Foundation; either version 2 of the License, or (at your
//# option) any later version.
//#
//# This library is distributed in the hope that it will be useful, but WITHOUT
//# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
//# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
//# License for more details.
//#
//# You should have received a copy of the GNU Library General Public License
//# along with this library; if not, write to the Free Software Foundation,
//# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
//#
//# Correspondence concerning AIPS++ should be addressed as follows:
//#        Internet email: aips2-request@nrao.edu.
//#        Postal address: AIPS++ Project Office
//#                        National Radio Astronomy Observatory
//#                        520 Edgemont Road
//#                        Charlottesville, VA 22903-2475 USA
//#
//# $Id$

#include <casacore/tables/Tables/TableCache.h>
#include <casacore/tables/Tables/Table.h>
#include <casacore/tables/Tables/PlainTable.h>
#include <casacore/tables/Tables/TableLock.h>
#include <casacore/tables/Tables/TableError.h>
#include <casacore/casa/Arrays/Vector.h>


namespace casacore { //# NAMESPACE CASACORE - BEGIN

TableCache::TableCache()
{}

TableCache::~TableCache()
{}

PlainTable* TableCache::operator() (const String& tableName) const
{
    std::lock_guard<std::mutex> sc(itsMutex);
    return getTable (tableName);
}

PlainTable* TableCache::getTable (const String& tableName) const
{
    std::map<String,void*>::const_iterator iter = tableMap_p.find (tableName);
    if (iter == tableMap_p.end()) {
        return 0;
    }
    return static_cast<PlainTable*>(iter->second);
}

void TableCache::define (const String& tableName, PlainTable* tab)
{
    std::lock_guard<std::mutex> sc(itsMutex);
    tableMap_p.insert (std::make_pair(tableName, tab));
}

void TableCache::remove (const String& tableName)
{
    // For static Table objects it is possible that the cache is
    // deleted before the Table.
    // Therefore do not delete if the map is already empty
    // (otherwise an exception is thrown).
    std::lock_guard<std::mutex> sc(itsMutex);
    if (tableMap_p.size() > 0) {
      try {
        tableMap_p.erase (tableName);
      } catch (std::exception&) {
	// Something strange has happened.
	// Throwing an exception causes an immediate crash (probably by
	// Table destructors).
	// So write a message on stderr;
	std::cerr << "Cannot remove table " << tableName
                  << " from the table cache; suggest restarting" << std::endl;
      }
    }
}

void TableCache::rename (const String& newName, const String& oldName)
{
    std::lock_guard<std::mutex> sc(itsMutex);
    if (tableMap_p.find (oldName) != tableMap_p.end()) {
        void* ptr = tableMap_p.at(oldName);
        tableMap_p.erase (oldName);
        tableMap_p.insert (std::make_pair(newName, ptr));
    }
}

uInt TableCache::nAutoLocks()
{
    std::lock_guard<std::mutex> sc(itsMutex);
    uInt n=0;
    for (const auto& x : tableMap_p) {
	PlainTable& table = *static_cast<PlainTable*>(x.second);
	if (table.lockOptions().option() == TableLock::AutoLocking) {
	    //# Having a read lock is enough.
	    if (table.hasLock (FileLocker::Read)) {
		n++;
	    }
	}
    }
    return n;
}

void TableCache::relinquishAutoLocks (Bool all)
{
    std::lock_guard<std::mutex> sc(itsMutex);
    for (const auto& x : tableMap_p) {
	PlainTable& table = *static_cast<PlainTable*>(x.second);
	if (table.lockOptions().option() == TableLock::AutoLocking) {
	    //# Having a read lock is enough.
	    if (table.hasLock (FileLocker::Read)) {
		if (all) {
		    table.unlock();
		}else{
		    table.autoReleaseLock (True);
		}
	    }
	}
    }
}

Vector<String> TableCache::getTableNames() const
{
    std::lock_guard<std::mutex> sc(itsMutex);
    uInt ntab = tableMap_p.size();
    Vector<String> names(ntab);
    ntab = 0;
    for (const auto& x : tableMap_p) {
	PlainTable& table = *static_cast<PlainTable*>(x.second);
        names[ntab++] = table.tableName();
    }
    return names;
}

Vector<String> TableCache::getLockedTables (FileLocker::LockType lockType,
                                            int lockOption)
{
    std::lock_guard<std::mutex> sc(itsMutex);
    vector<String> names;
    for (const auto& x : tableMap_p) {
	PlainTable& table = *static_cast<PlainTable*>(x.second);
	if (lockOption < 0  ||  table.lockOptions().option() == lockOption) {
	    if (table.hasLock (lockType)) {
                names.push_back (table.tableName());
	    }
	}
    }
    return Vector<String>(names);
}

void TableCache::flushTable (const String& name,
                             Bool fsync, Bool recursive)
{
  std::lock_guard<std::mutex> sc(itsMutex);
  PlainTable* tab = getTable(name);
  if (tab) {
    tab->flush (fsync, recursive);
  }
}

PlainTable* TableCache::lookCache (const String& name, int tableOption,
                                   const TableLock& lockOptions)
{
    //# Exit if table is not in cache yet.
    PlainTable* btp = this->operator()(name);
    if (btp == 0) {
	return btp;
    }
    //# Check if option matches. It does if equal.
    //# Otherwise it does if option in cached table is "more".
    //# Note that class PlainTable already throws an exception if
    //# a new table is created with the same name as an open table.
    int cachedTableOption = btp->tableOption();
    if ((tableOption == cachedTableOption)
    ||  ((cachedTableOption == Table::New
      ||  cachedTableOption == Table::NewNoReplace
      ||  cachedTableOption == Table::Update)
     &&  (tableOption == Table::Update
      ||  tableOption == Table::Old))) {
	btp->mergeLock (lockOptions);
	return btp;
    }
    if (cachedTableOption == Table::Old  &&  tableOption == Table::Update) {
	btp->mergeLock (lockOptions);
	btp->reopenRW();
	return btp;
    }
    throw (TableInvOper ("Table " + name +
			 " cannot be opened/created (already in cache)"));
}


} //# NAMESPACE CASACORE - END