ATLAS Offline Software
Loading...
Searching...
No Matches
Database/AthenaPOOL/PoolSvc/src/Database.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2026 CERN for the benefit of the ATLAS collaboration
3*/
4
5#include "Database.h"
6#include "SessionTool.h"
7#include "DatabaseHandler.h"
8
10
11#include "StorageSvc/DbType.h"
12
13#include <exception>
14
15static const std::string& emptyString = "";
16
18 const std::string& name,
19 const Specification::NameType nameType ) : AthMessaging("PoolSvc.Database"),
20 m_session( session ),
21 m_catalog( session.fileCatalog() ),
22 m_transactionType( session.openMode() ),
23 m_name( name ),
24 m_nameType( nameType ),
25 m_technology( 0 ),
26 m_technologySet( false ),
29 m_alreadyConnected( false ),
30 m_the_fid( "" ),
31 m_the_pfn( "" ) {
32 this->checkInRegistry();
33}
34
36
38 if( !m_databaseHandler && m_transactionType != Io::INVALID ) {
39 // Check if the database is already connected
40 if( !checkInRegistry() ) {
41 // It is not. Connect !
42 switch( m_nameType ) {
44 if ( this->fid().empty() ) {
45 throw std::runtime_error( "PFN \"" + m_name + "\" is not existing (APR: \" Database::connectForRead \" from \" PersistencySvc \")" );
46 }
47 break;
49 if ( this->pfn().empty() ) {
50 throw std::runtime_error( "FID \"" + m_name + "\" is not existing in the catalog (APR: \" Database::connectForRead \" from \" PersistencySvc \")" );
51 }
52 break;
54 {
55 std::string lfn = m_name;
56 if ( this->fid().empty() ) {
57 throw std::runtime_error( "LFN \"" + m_name + "\" is not existing in the catalog (APR: \" Database::connectForRead \" from \" PersistencySvc \")" );
58 }
59 this->connectForRead();
60 m_session.registerDatabaseHandler( m_databaseHandler, lfn );
61 }
62 return;
63 break;
64 default:
65 throw std::runtime_error( "Unknown database name type (APR: \" Database::connectForRead \" from \" PersistencySvc \")" );
66 };
67
68 // Now we have all the usefull information to open the file.
69 // Check the registry now that we have the FID (in case of ambiguous PFNs)
70 m_databaseHandler = m_session.lookupByFID( m_the_fid );
71 if( !m_databaseHandler ) {
72 // still no luck - make a new connection
74 }
75 if( m_databaseHandler ) {
76 m_openMode = Io::READ;
77 }
78 else {
79 throw std::runtime_error( "Could not connect to the file (APR: \" Database::connectForRead \" from \" PersistencySvc \")" );
80 }
81 }
82 }
83}
84
85void* Database::readObject( const Token& token, void* object ) {
86 if( !m_databaseHandler) {
87 throw std::runtime_error( "Could not open a database for read object. (APR: \" Database::connectForWrite \" from \" PersistencySvc \")" );
88 }
89 return m_databaseHandler->readObject( token, object );
90}
91
93 if( !m_databaseHandler && m_transactionType != Io::INVALID ) {
94 if ( m_transactionType != Io::WRITE && m_transactionType != Io::APPEND ) {
95 throw std::runtime_error( "Could not open a database for write outside an update transaction. (APR: \" Database::connectForWrite \" from \" PersistencySvc \")" );
96 }
97
98 if ( this->checkInRegistry() ) {
99 if ( m_openMode == Io::READ ) {
100 throw std::runtime_error( "Could not open a database for write that is already connected for read. (APR: \" Database::connectForWrite \" from \" PersistencySvc \")" );
101 }
102 } else { // The database is not yet connected.
103 bool dbRegistered = false;
104 switch( m_nameType ) {
107 if ( this->fid().empty() ) {
108 // Check if the technology is already set
109 if( ! m_technologySet ) {
110 throw std::runtime_error( "The back end technology has not been specified (APR: \" Database::connectForWrite \" from \" PersistencySvc \")" );
111 }
112 // register in the catalog
113 DbType dbType( m_technology );
114 DbType dbTypeMajor( dbType.majorType() );
115 FileCatalogUtils::registerPFN( m_catalog, m_the_pfn.substr(0, m_the_pfn.find('?')), dbTypeMajor.storageName(), m_the_fid );
116 ATH_MSG_DEBUG("registered PFN: " << m_the_pfn << " with FID:" << m_the_fid);
117 dbRegistered = true;
118 }
119 break;
122 if ( this->pfn().empty() ) {
123 throw std::runtime_error( "Could not find the FID \"" + m_name + "\" in the file catalog (APR: \" Database::connectForWrite \" from \" PersistencySvc \")" );
124 }
125 break;
127 {
128 std::string lfn = m_name;
129 if ( this->fid().empty() ) {
130 throw std::runtime_error( "Could not find the LFN \"" + m_name + "\" in the file catalog (APR: \" Database::connectForWrite \" from \" PersistencySvc \")" );
131 }
132 this->connectForWrite();
133 m_session.registerDatabaseHandler( m_databaseHandler, lfn );
134 }
135 return;
136 break;
137 default:
138 throw std::runtime_error( "Unknown database name type (APR: \" Database::connectForWrite \" from \" PersistencySvc \")" );
139 };
140
142 if( !m_databaseHandler ) {
143 if( dbRegistered ) {
144 // creation failed, remove entry from the in-memory catalog
145 m_catalog.deleteFID( m_the_fid );
146 }
147 throw std::runtime_error( "Could not connect to the file (APR: \" Database::connectForWrite \" from \" PersistencySvc \")" );
148 }
149 m_openMode = m_transactionType == Io::WRITE ? Io::WRITE : Io::APPEND;
150 } // Connection established
151
152 } // Database handler retrieved
153}
154
155Token* Database::writeObject( const std::string& containerName,
156 long minorTechnology,
157 const void* object,
158 const RootType& type ) {
159 if( !m_databaseHandler) {
160 throw std::runtime_error( "Could not open a database for write object. (APR: \" Database::connectForWrite \" from \" PersistencySvc \")" );
161 }
162 return m_databaseHandler->writeObject( containerName, minorTechnology, object, type );
163}
164
166 if ( m_databaseHandler ) {
167 m_session.disconnect( m_databaseHandler );
168 m_openMode = Io::INVALID;
169 }
170}
171
172Io::IoFlag Database::openMode() const {
173 return m_openMode;
174}
175
176const std::string& Database::fid() {
177 if ( m_databaseHandler ) return m_databaseHandler->fid();
178 else {
179 if ( m_nameType == Specification::FID ) return m_name;
180 else if ( !m_the_fid.empty() ) return m_the_fid;
181 else {
183 std::string technology;
184 FileCatalogUtils::lookupFileByPFN( m_catalog, m_name.substr(0, m_name.find('?')), m_the_fid, technology );
185 ATH_MSG_DEBUG("lookupPFN: " << m_name << " returned FID: '" << m_the_fid << "' tech=" << technology);
186 if ( ! m_the_fid.empty() ) {
187 if( technology.empty() ) {
189 ATH_MSG_DEBUG("Retrying 'connect' using assumed PFN " << m_name << " as LFN (no tech found in PFC)" );
190 return m_the_fid;
191 }
193 m_technology = DbType::getType( technology ).majorType();
194 m_alreadyConnected = true;
195 }
196 else {
197 if( m_transactionType != Io::WRITE && m_transactionType != Io::APPEND ) { // Fetch the FID from the db itself !
198 if( !m_technologySet ) {
199 ATH_MSG_DEBUG("Opening database '" << m_name << "' with no technology set");
201 m_technologySet = true;
202 }
203 m_the_fid = m_session.fidForPfn( m_technology, m_name );
204 if( ! m_the_fid.empty() ) {
205 // sanity check - verify that the FID is not registered in PFC under a different name
206 std::string pfn, tech;
208 if( !pfn.empty() ) {
209 ATH_MSG_WARNING("Opening file '" << m_name << "' which is already registered in the Catalog as '" << pfn
210 <<"' (GUID " << m_the_fid << ") - this is not supported and may even lead to a crash!" );
211 }
213 m_alreadyConnected = true;
214 }
215 }
216 }
217 } /* not PFN */
218 else if ( m_nameType == Specification::LFN ) {
219 m_the_fid = m_catalog.lookupLFN( m_name );
220 }
221 if ( ! m_the_fid.empty() ) {
224 }
225 return m_the_fid;
226 }
227 return emptyString;
228 }
229}
230
231const std::string& Database::pfn() {
232 if( m_databaseHandler ) return m_databaseHandler->pfn();
233 if( m_nameType == Specification::PFN ) return m_name;
234 if( ! m_the_pfn.empty() ) return m_the_pfn;
235
237 m_the_fid = m_catalog.lookupLFN( m_name );
238 if ( ! m_the_fid.empty() ) {
241 // go from FID=>PFN in the next step
242 }
243 }
245 std::string technology;
247 if( !m_the_pfn.empty() ) {
249 m_technology = DbType::getType( technology ).majorType();
250 m_alreadyConnected = true;
251 }
252 return m_the_pfn;
253 }
254 return emptyString;
255}
256
257bool Database::setTechnology( long technology ) {
258 if ( m_alreadyConnected ) return false;
259 else {
260 DbType dbType( technology );
261 m_technology = dbType.majorType();
262 m_technologySet = true;
263 return true;
264 }
265}
266
267std::vector< std::string > Database::containers() {
268 std::vector< std::string > containers;
269 if ( m_databaseHandler ) {
270 containers = m_databaseHandler->containers();
271 }
272 return containers;
273}
274
275Container* Database::containerHandle( const std::string& name ) {
276 Container* container = 0;
277 if ( m_databaseHandler ) {
278 container = m_databaseHandler->container( name );
279 }
280 return container;
281}
282
284 // Check first if the database is already connected.
285 switch( m_nameType ) {
287 m_databaseHandler = m_session.lookupByPFN( m_name );
288 break;
290 m_databaseHandler = m_session.lookupByFID( m_name );
291 break;
293 m_databaseHandler = m_session.lookupByLFN( m_name );
294 break;
295 default:
296 throw std::runtime_error( "Only PFN, LFN and FID database types are currently supported (APR: \" Database::checkInRegistry \" from \" PersistencySvc \")" );
297 };
298 if ( m_databaseHandler ) {
299 m_alreadyConnected = true;
300 m_technology = m_databaseHandler->technology();
301 m_openMode = m_databaseHandler->accessMode();
305 return true;
306 }
307 else return false;
308}
309
311 return m_databaseHandler? &m_databaseHandler->fileDescriptor() : nullptr;
312}
313
314bool Database::attributeOfType( const std::string& attributeName,
315 void* data,
316 const std::type_info& typeInfo,
317 const std::string& option ) {
318 if ( ! m_databaseHandler ) return false;
319 else return m_databaseHandler->attribute( attributeName, data, typeInfo, option );
320}
321
322bool Database::setAttributeOfType( const std::string& attributeName,
323 const void* data,
324 const std::type_info& typeInfo,
325 const std::string& option ) {
326 if ( ! m_databaseHandler ) return false;
327 else return m_databaseHandler->setAttribute( attributeName, data, typeInfo, option );
328}
#define ATH_MSG_DEBUG(x,...)
#define ATH_MSG_WARNING(x,...)
static const long long int INVALID
static const std::string & emptyString
TTypeAdapter RootType
Definition RootType.h:211
static const Attributes_t empty
AthMessaging(IMessageSvc *msgSvc, const std::string &name)
Constructor.
Container * containerHandle(const std::string &name)
Returns a pointer to a container object. The user acquires ownership of that object.
Specification::NameType m_nameType
The database name spacification.
long m_technology
The technology identifier of the database.
Io::IoFlag m_openMode
Current open mode.
bool checkInRegistry()
Checks in the registry if the database handler already exists.
void disconnect()
Disconnects from the database.
Io::IoFlag openMode() const
Returns the opening mode. It can be used to check whether the database is connected.
void connectForRead()
Connects explicitly to the database for read operations.
bool setAttributeOfType(const std::string &attributeName, const void *data, const std::type_info &typeInfo, const std::string &option)
Database(SessionTool &session, const std::string &name, const Specification::NameType nameType)
Constructor.
const std::string & fid()
Returns the file identifier of this database.
const std::string & pfn()
Returns the physical file name of this database.
std::vector< std::string > containers()
Returns the names of the containers in this database.
std::string m_name
The database name.
bool setTechnology(long technology)
Sets the technology identifier for this database.
SessionTool & m_session
Reference to the session.
void connectForWrite()
Connects explicitly to the database for write/update operations.
void * readObject(const Token &token, void *object=0)
Reads an object given a token.
std::string m_the_fid
Other names used.
bool attributeOfType(const std::string &attributeName, void *data, const std::type_info &typeInfo, const std::string &option)
DatabaseHandler * m_databaseHandler
The underlying database handler.
bool m_alreadyConnected
Flag indicating whether a connection has been already made once.
Token * writeObject(const std::string &containerName, long minorTechnology, const void *object, const RootType &type)
Writes an object and returns a token.
Io::IoFlag m_transactionType
Transaction type (read/update).
Gaudi::IFileCatalog & m_catalog
Reference to the file catalog.
bool m_technologySet
Checks if the technology identifier has been set.
Keeps information about storage technology.
Definition DbType.h:15
static DbType getType(const std::string &name)
Access known storage type object by name.
int majorType() const
Access to major type.
Definition DbType.h:52
const std::string storageName() const
Human readable storage type.
Description:
This class provides a token that identifies in a unique way objects on the persistent storage.
Definition Token.h:22
void registerPFN(Gaudi::IFileCatalog &fc, const std::string &pfn, const std::string &ftype, std::string &fid)
Register PFN, assign new FID if not given. Throws if the PFN is already registered.
void lookupFileByPFN(const Gaudi::IFileCatalog &fc, const std::string &pfn, std::string &fid, std::string &tech)
Get FID and filetype for a given PFN.
void getFirstPFN(const Gaudi::IFileCatalog &fc, const std::string &fid, std::string &pfn, std::string &tech)
Get the first PFN + filetype for the given FID.
static const DbType ROOT_StorageType
Definition DbType.h:72
NameType
Enumeration type specifying the database name field, wherever the latter is used in methods accessing...