ATLAS Offline Software
Loading...
Searching...
No Matches
DatabaseHandler.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 "DatabaseHandler.h"
6#include "Container.h"
10#include "StorageSvc/DbOption.h"
12
13#include <exception>
14#include <memory>
15
17 long technology,
18 const std::string& fid,
19 const std::string& pfn,
20 Io::IoFlag accessmode ) :
21 m_storageSvc( storageSvc ),
24 m_accessMode( accessmode ) {
25 if ( ! m_storageSvc.connect( m_accessMode, m_fileDescriptor ).isSuccess() ) {
26 throw std::runtime_error( "Could not connect to the database (APR: \" DatabaseHandler::DatabaseHandler \" from \" PersistencySvc \")" );
27 }
28}
29
31 Io::IoFlag mode = Io::INVALID;
32 if( m_storageSvc.openMode(m_fileDescriptor, mode).isSuccess() ) {
33 m_storageSvc.disconnect( m_fileDescriptor ).ignore();
34 }
35}
36
38 return m_storageSvc.endTransaction( m_fileDescriptor, Transaction::TRANSACT_COMMIT ).isSuccess() &&
39 m_storageSvc.endTransaction( m_fileDescriptor, Transaction::TRANSACT_FLUSH ).isSuccess();
40}
41
45
47 return ( m_storageSvc.disconnect( m_fileDescriptor ).isSuccess() );
48}
49
50std::vector< std::string > DatabaseHandler::containers() {
51 std::vector< std::string > result;
52 std::vector<const Token*> containerTokens;
53 DbDatabase dbH( m_fileDescriptor.dbc()->handle() );
54 if( !dbH.containers(containerTokens, false).isSuccess() ) {
55 return result;
56 }
57 for ( std::vector<const Token*>::const_iterator iToken = containerTokens.begin();
58 iToken != containerTokens.end(); ++iToken ) {
59 Token tok (*iToken);
60 result.push_back( m_storageSvc.getContName(m_fileDescriptor, tok) );
61 }
62 return result;
63}
64
65Container* DatabaseHandler::container( const std::string& containerName ) {
66 // Check the existence of a given container.
67 std::vector< std::string > allContainers = this->containers();
68 for ( std::vector< std::string >::const_iterator iName = allContainers.begin();
69 iName != allContainers.end(); ++iName ) {
70 if ( *iName == containerName ) {
71 return new Container( m_fileDescriptor, containerName );
72 }
73 }
74 return 0;
75}
76
77const std::string& DatabaseHandler::pfn() const {
78 return m_fileDescriptor.PFN();
79}
80
81const std::string& DatabaseHandler::fid() const {
82 return m_fileDescriptor.FID();
83}
84
86 return m_technology;
87}
88
89Io::IoFlag DatabaseHandler::accessMode() const {
90 return m_accessMode;
91}
92
93Token* DatabaseHandler::writeObject( const std::string& containerName,
94 long minorTechnology,
95 const void* object,
96 const RootType& type ) {
97 Token* token = nullptr;
99 containerName,
100 minorTechnology,
101 object,
102 type,
103 token ).isSuccess() ) {
104 return nullptr;
105 }
106 return token;
107}
108
109void* DatabaseHandler::readObject( const Token& token, void* object ) {
110 void* result( object );
111 if( !m_storageSvc.read( m_fileDescriptor, token, &result ).isSuccess() ) {
112 return nullptr;
113 }
114 return result;
115}
116
117bool DatabaseHandler::attribute( const std::string& attributeName,
118 void* data,
119 const std::type_info& typeInfo,
120 const std::string& option ) {
121 DbOption databaseOption( attributeName, option );
122 DbDatabase dbH( m_fileDescriptor.dbc()->handle() );
123 if( !dbH.getOption(databaseOption).isSuccess() ) return false;
124 return databaseOption.i_getValue(typeInfo, data).isSuccess();
125}
126
127bool DatabaseHandler::setAttribute( const std::string& attributeName,
128 const void* data,
129 const std::type_info& typeInfo,
130 const std::string& option ) {
131 DbOption databaseOption( attributeName, option );
132 if( !databaseOption.i_setValue( typeInfo, const_cast<void*>( data ) ).isSuccess() ) return false;
133 DbDatabase dbH( m_fileDescriptor.dbc()->handle() );
134 return dbH.setOption(databaseOption).isSuccess();
135}
TTypeAdapter RootType
Definition RootType.h:211
This file contains the class definition for the Token class (migrated from POOL).
bool commitTransaction()
Commits the transaction.
IStorageSvc & m_storageSvc
IStorageSvc reference.
~DatabaseHandler()
Destructor. Disconnects from the database.
Token * writeObject(const std::string &containerName, long minorTechnology, const void *object, const RootType &type)
Writes an object and returns a token.
Container * container(const std::string &containerName)
Returns a container handle.
void * readObject(const Token &token, void *object=0)
Reads an object given a token.
FileDescriptor m_fileDescriptor
File descriptor for this database.
bool setAttribute(const std::string &attributeName, const void *data, const std::type_info &typeInfo, const std::string &option)
Sets an attrtibute.
long technology() const
Returns the technology identifier.
std::vector< std::string > containers()
Gives the list of containers.
long m_technology
Technology identifier.
bool disconnectTransaction()
Disconnects the transaction.
Io::IoFlag accessMode() const
Returns the access mode.
DatabaseHandler(IStorageSvc &storageSvc, long technology, const std::string &fid, const std::string &pfn, Io::IoFlag accessmode)
Constructor. Connects to the database.
Io::IoFlag m_accessMode
Current access mode.
bool attribute(const std::string &attributeName, void *data, const std::type_info &typeInfo, const std::string &option)
Returns an attribute.
bool commitAndHoldTransaction()
Commits and holds the transaction.
const std::string & fid() const
Returns the file identifier.
const std::string & pfn() const
Returns the physical file name.
Description: Handle managing a DbDatabaseObj, a generic Database object.
Definition DbDatabase.h:39
StatusCode containers(std::vector< const Token * > &conts, bool intern=false)
Allow access to all known containers.
StatusCode setOption(const DbOption &refOpt)
Set options.
StatusCode getOption(DbOption &refOpt)
Access options.
Description: Definition an option to be supplied to database objects.
Definition DbOption.h:22
StatusCode i_getValue(const std::type_info &typ, void *value) const
Read the option value.
StatusCode i_setValue(const std::type_info &typ, const void *value)
Set the option value.
The IStorageSvc interface is able to handle user request for.
Definition IStorageSvc.h:39
This class provides a token that identifies in a unique way objects on the persistent storage.
Definition Token.h:22