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"
9#include "StorageSvc/Shape.h"
11#include "StorageSvc/DbOption.h"
12#include "StorageSvc/DbReflex.h"
15#include "StorageSvc/DbPrint.h"
16
17#include <exception>
18#include <memory>
19
21 long technology,
22 const std::string& fid,
23 const std::string& pfn,
24 Io::IoFlag accessmode ):
25 m_storageSvc( storageSvc ),
28 m_accessMode( accessmode )
29{
30 if ( ! m_storageSvc.connect( m_accessMode, m_fileDescriptor ).isSuccess() ) {
31 throw std::runtime_error( "Could not connect to the database (APR: \" DatabaseHandler::DatabaseHandler \" from \" PersistencySvc \")" );
32 }
33}
34
36{
37 Io::IoFlag mode = Io::INVALID;
38 if( m_storageSvc.openMode(m_fileDescriptor, mode).isSuccess() ) {
39 m_storageSvc.disconnect( m_fileDescriptor ).ignore();
40 }
41}
42
43
44bool
50
51
52bool
57
58
59bool
61{
62 return ( m_storageSvc.disconnect( m_fileDescriptor ).isSuccess() );
63}
64
65std::vector< std::string >
67{
68 std::vector< std::string > result;
69 std::vector<const Token*> containerTokens;
70 DbDatabase dbH( m_fileDescriptor.dbc()->handle() );
71 if( !dbH.containers(containerTokens, false).isSuccess() ) {
72 DbPrint log( m_fileDescriptor.PFN() );
73 log << MSG::ERROR << "Could not retrieve the list of containers." << endmsg;
74 }
75 for ( std::vector<const Token*>::const_iterator iToken = containerTokens.begin();
76 iToken != containerTokens.end(); ++iToken ) {
77 Token tok (*iToken);
78 result.push_back( m_storageSvc.getContName(m_fileDescriptor, tok) );
79 }
80 return result;
81}
82
84pool::DatabaseHandler::container( const std::string& containerName )
85{
86 // Check the existence of a given container.
87 std::vector< std::string > allContainers = this->containers();
88 for ( std::vector< std::string >::const_iterator iName = allContainers.begin();
89 iName != allContainers.end(); ++iName ) {
90 if ( *iName == containerName ) {
93 containerName );
94 }
95 }
96 return 0;
97}
98
99
100const std::string&
102{
103 return m_fileDescriptor.PFN();
104}
105
106
107const std::string&
109{
110 return m_fileDescriptor.FID();
111}
112
113
114long
119
120
121Io::IoFlag
126
127
128Token*
129pool::DatabaseHandler::writeObject( const std::string& containerName,
130 long minorTechnology,
131 const void* object,
132 const RootType& type )
133{
134 Token* token = 0;
135 if ( ! object ) return token;
136
137 // Get the persistent shape.
138 const pool::Shape* shape = nullptr;
139 Guid guid = DbReflex::guid(type);
140 if( !m_storageSvc.getShape( m_fileDescriptor, guid, shape ).isSuccess() ) {
141 shape = m_storageSvc.createShape( guid );
142 }
143 if ( shape ) {
144 if ( m_storageSvc.allocate( m_fileDescriptor,
145 containerName,
146 minorTechnology,
147 object,
148 shape,
149 token ).isSuccess() ) {
150 token->setClassID( shape->shapeID() );
151 }
152 }
153 return token;
154}
155
156
157void*
158pool::DatabaseHandler::readObject( const Token& token, void* object )
159{
160 void* result( object );
161
162 // Get the persistent shape
163 const pool::Shape* shape = nullptr;
164 if( !m_storageSvc.getShape( m_fileDescriptor, token.classID(), shape ).isSuccess() ) {
165 return result;
166 }
167 if( !m_storageSvc.read( m_fileDescriptor, token, shape, &result ).isSuccess() ) {
168 return nullptr;
169 }
170
171 return result;
172}
173
174
175bool
176pool::DatabaseHandler::attribute( const std::string& attributeName,
177 void* data,
178 const std::type_info& typeInfo,
179 const std::string& option )
180{
181 pool::DbOption databaseOption( attributeName, option );
182 DbDatabase dbH( m_fileDescriptor.dbc()->handle() );
183 if( !dbH.getOption(databaseOption).isSuccess() ) return false;
184 return databaseOption.i_getValue(typeInfo, data).isSuccess();
185}
186
187
188bool
189pool::DatabaseHandler::setAttribute( const std::string& attributeName,
190 const void* data,
191 const std::type_info& typeInfo,
192 const std::string& option )
193{
194 pool::DbOption databaseOption( attributeName, option );
195 if( !databaseOption.i_setValue( typeInfo, const_cast<void*>( data ) ).isSuccess() ) return false;
196 DbDatabase dbH( m_fileDescriptor.dbc()->handle() );
197 return dbH.setOption(databaseOption).isSuccess();
198 }
#define endmsg
TTypeAdapter RootType
Definition RootType.h:211
This file contains the class definition for the Token class (migrated from POOL).
This class provides a encapsulation of a GUID/UUID/CLSID/IID data structure (128 bit number).
Definition Guid.h:25
This class provides a token that identifies in a unique way objects on the persistent storage.
Definition Token.h:22
const Guid & classID() const
Access database identifier.
Definition Token.h:74
Token & setClassID(const Guid &cl_id)
Access database identifier.
Definition Token.h:76
Container is an implementation of the IContainer interface.
Definition Container.h:24
Token * writeObject(const std::string &containerName, long minorTechnology, const void *object, const RootType &type)
Writes an object and returns a token.
FileDescriptor m_fileDescriptor
File descriptor for this database.
bool commitAndHoldTransaction()
Commits and holds the transaction.
void * readObject(const Token &token, void *object=0)
Reads an object given a token.
std::vector< std::string > containers()
Gives the list of containers.
bool attribute(const std::string &attributeName, void *data, const std::type_info &typeInfo, const std::string &option)
Returns an attribute.
bool disconnectTransaction()
Disconnects the transaction.
long m_technology
Technology identifier.
bool commitTransaction()
Commits the transaction.
const std::string & pfn() const
Returns the physical file name.
Io::IoFlag accessMode() const
Returns the access mode.
long technology() const
Returns the technology identifier.
~DatabaseHandler()
Destructor. Disconnects from the database.
Io::IoFlag m_accessMode
Current access mode.
IContainer * container(const std::string &containerName)
Returns a container handle.
bool setAttribute(const std::string &attributeName, const void *data, const std::type_info &typeInfo, const std::string &option)
Sets an attrtibute.
DatabaseHandler(IStorageSvc &storageSvc, long technology, const std::string &fid, const std::string &pfn, Io::IoFlag accessmode)
Constructor. Connects to the database.
const std::string & fid() const
Returns the file identifier.
IStorageSvc & m_storageSvc
IStorageSvc reference.
Description: Handle managing a DbDatabaseObj, a generic Database object.
Definition DbDatabase.h:54
StatusCode getOption(DbOption &refOpt)
Access options.
StatusCode containers(std::vector< const Token * > &conts, bool intern=false)
Allow access to all known containers.
StatusCode setOption(const DbOption &refOpt)
Set options.
Description: Definition an option to be supplied to database objects.
Definition DbOption.h:36
StatusCode i_setValue(const std::type_info &typ, const void *value)
Set the option value.
StatusCode i_getValue(const std::type_info &typ, void *value) const
Read the option value.
static Guid guid(const RootType &id)
Determine Guid (normalized string form) from reflection type.
IContainer is the base class for container objects.
Definition IContainer.h:23
The IStorageSvc interface is able to handle user request for.
Definition IStorageSvc.h:48
Description:
Definition Shape.h:35
const Guid & shapeID() const
Access database identifier.
Definition Shape.h:45