ATLAS Offline Software
Loading...
Searching...
No Matches
AthenaPoolConverter.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
3*/
4
9
12
13#include "SGTools/DataProxy.h"
14
19#include "StorageSvc/DbType.h"
21
22#include <format>
23#include "CxxUtils/HexString.h"
24
25//__________________________________________________________________________
28//__________________________________________________________________________
30 ATH_CHECK(::Converter::initialize());
31
32 // We do not retrieve m_detStore as that store may not always be available!
33
34 // Retrieve AthenaPoolCnvSvc
35 ATH_CHECK( m_athenaPoolCnvSvc.retrieve() );
36
37 return StatusCode::SUCCESS;
38}
39//__________________________________________________________________________
41 // Release AthenaPoolCnvSvc
42 if (!m_athenaPoolCnvSvc.release().isSuccess()) {
43 ATH_MSG_WARNING("Cannot release AthenaPoolCnvSvc.");
44 }
45 return(::Converter::finalize());
46}
47//__________________________________________________________________________
49 return pool::POOL_StorageType.type();
51//__________________________________________________________________________
52StatusCode AthenaPoolConverter::createObj(IOpaqueAddress* pAddr, DataObject*& pObj) {
53 TokenAddress* tokAddr = dynamic_cast<TokenAddress*>(pAddr);
54
55 bool ownTokAddr = false;
56 if (tokAddr == nullptr || tokAddr->getToken() == nullptr) {
57 ownTokAddr = true;
58 auto token = std::make_unique<Token>();
59 token->fromString(*(pAddr->par()));
60 GenericAddress* genAddr = dynamic_cast<GenericAddress*>(pAddr);
61 if (not genAddr){
62 ATH_MSG_ERROR("Dynamic cast failed in AthenaPoolConverter::createObj");
63 //clean up
64 return StatusCode::FAILURE;
65 }
66 tokAddr = new TokenAddress(*genAddr, std::move(token));
67 }
68 if( tokAddr->ipar()[0] > 0 and tokAddr->getToken()->auxString().empty() ) {
69 tokAddr->getToken()->setAuxString(CxxUtils::HexString<"[CTXT={}]">(static_cast<int>(*(pAddr->ipar()))));
70 }
71 ATH_MSG_VERBOSE("createObj: " << tokAddr->getToken()->toString() << ", CTX=" << tokAddr->ipar()[0]
72 << ", auxStr=" << tokAddr->getToken()->auxString() );
73 try {
74 std::string key = pAddr->par()[1];
75 if (!PoolToDataObject(pObj, tokAddr->getToken(), key).isSuccess()) {
76 ATH_MSG_ERROR("createObj PoolToDataObject() failed, Token = " << (tokAddr->getToken() ? tokAddr->getToken()->toString() : "NULL"));
77 pObj = nullptr;
78 }
79 } catch (std::exception& e) {
80 ATH_MSG_ERROR("createObj - caught exception: " << e.what());
81 pObj = nullptr;
82 }
83 if (pObj == nullptr) {
84 ATH_MSG_ERROR("createObj failed to get DataObject, Token = " << (tokAddr->getToken() ? tokAddr->getToken()->toString() : "NULL"));
85 }
86 if (ownTokAddr) {
87 delete tokAddr; tokAddr = nullptr;
88 }
89 if (pObj == nullptr) {
90 return StatusCode::FAILURE;
91 }
92 return StatusCode::SUCCESS;
93}
94//__________________________________________________________________________
95StatusCode AthenaPoolConverter::createRep(DataObject* pObj, IOpaqueAddress*& pAddr) {
96 const SG::DataProxy* proxy = dynamic_cast<SG::DataProxy*>(pObj->registry());
97 if (proxy == nullptr) {
98 ATH_MSG_ERROR("AthenaPoolConverter CreateRep failed to cast DataProxy, key = " << pObj->name());
99 return StatusCode::FAILURE;
101 const CLID clid = proxy->clID();
102 if (pAddr == nullptr) {
103 // Create a IOpaqueAddress for this object.
104 pAddr = new TokenAddress(this->storageType(), clid, "", "", 0, 0);
105 } else {
106 GenericAddress* gAddr = dynamic_cast<GenericAddress*>(pAddr);
107 if (gAddr != nullptr) {
108 gAddr->setSvcType(this->storageType());
109 }
110 }
111 return StatusCode::SUCCESS;
112}
113//__________________________________________________________________________
114StatusCode AthenaPoolConverter::fillRepRefs(IOpaqueAddress* pAddr, DataObject* pObj) {
115 try {
116 if (!DataObjectToPool(pAddr, pObj).isSuccess()) {
117 ATH_MSG_ERROR("FillRepRefs failed, key = " << pObj->name());
118 return StatusCode::FAILURE;
119 }
120 } catch (std::exception& e) {
121 ATH_MSG_ERROR("fillRepRefs - caught exception: " << e.what());
122 return StatusCode::FAILURE;
123 }
124 return StatusCode::SUCCESS;
125}
126//__________________________________________________________________________
130//__________________________________________________________________________
131AthenaPoolConverter::AthenaPoolConverter(const CLID& myCLID, ISvcLocator* pSvcLocator,
132 const char* name /*= nullptr*/) :
133 ::Converter(storageType(), myCLID, pSvcLocator),
134 ::AthMessaging((pSvcLocator != nullptr ? msgSvc() : nullptr),
135 name ? name : "AthenaPoolConverter"),
136 m_detStore("DetectorStore", name ? name : "AthenaPoolConverter"),
137 m_athenaPoolCnvSvc(pSvcLocator && pSvcLocator->existsService("AthenaPoolSharedIOCnvSvc") ? "AthenaPoolSharedIOCnvSvc" : "AthenaPoolCnvSvc", name ? name : "AthenaPoolConverter"){
138}
139//__________________________________________________________________________
140Placement AthenaPoolConverter::setPlacementWithType(const std::string& tname, const std::string& key, const std::string& output) {
141 // Resulting placement
142 Placement placement;
143
144 // Extract the file name and global technology (if available)
145 std::string::size_type pos1 = output.find('[');
146 std::string outputConnectionSpec = output.substr(0, pos1);
147 placement.setFileName(outputConnectionSpec);
148
149 // Override streaming parameters from StreamTool if requested.
150 std::string containerPrefix{APRDefaults::WriteConfig::getEventDataName()};
151 std::string dhContainerPrefix{APRDefaults::WriteConfig::getDataHeaderName()};
152 std::string containerName{""};
153 std::string containerNameHint{""};
154 std::string branchNameHint{""};
155 std::string containerFriendPostfix{""};
156 while (pos1 != std::string::npos) {
157 const std::string::size_type pos2 = output.find('=', pos1);
158 const std::string thisKey = output.substr(pos1 + 1, pos2 - pos1 - 1);
159 const std::string::size_type pos3 = output.find(']', pos2);
160 const std::string value = output.substr(pos2 + 1, pos3 - pos2 - 1);
161 if (thisKey == "OutputCollection") {
162 dhContainerPrefix = std::move(value);
163 } else if (thisKey == "PoolContainerPrefix") {
164 containerPrefix = std::move(value);
165 } else if (thisKey == "TopLevelContainerName") {
166 containerNameHint = std::move(value);
167 } else if (thisKey == "SubLevelBranchName") {
168 branchNameHint = std::move(value);
169 } else if (thisKey == "PoolContainerFriendPostfix") {
170 containerFriendPostfix = std::move(value);
171 }
172 pos1 = output.find('[', pos3);
173 }
174
175 // Extract the technology from the container prefix (if available)
176 if (auto colonPost = containerPrefix.find(':'); colonPost != std::string::npos) {
177 placement.setTechnology(pool::DbType::getType(containerPrefix.substr(0, colonPost)).type());
178 containerPrefix.erase(0, colonPost + 1); // Note that DataHeader and EventTag bypass this...
179 }
180
181 // --- Special types: DataHeader & Form
182 if ( tname.starts_with(APRDefaults::DataHeaderTypeName) ) {
183 containerName = std::format("{}{}({}{})",
184 dhContainerPrefix,
185 tname.starts_with(APRDefaults::DataHeaderFormTypeName) ? "Form" : "",
186 key.back() == '/' ? key : "",
187 tname);
188 }
189 // AttributeList - writing attributes separately to EventTag container group
190 else if ( tname.starts_with(APRDefaults::EventTagTypeName) ) {
191 containerName = std::format("{}({})",
193 key);
194 }
195 // all other object types
196 else {
197 constexpr std::string_view typeTok = "<type>", keyTok = "<key>";
198 containerName = std::format("{}{}{}{}",
199 containerPrefix,
200 containerFriendPostfix,
201 containerNameHint,
202 branchNameHint.empty() ? "" : std::format("({})", branchNameHint));
203 if (auto pos = containerName.find(typeTok); pos != std::string::npos) {
204 containerName.replace(pos, typeTok.size(), tname);
205 }
206 if (auto pos = containerName.find(keyTok); pos != std::string::npos) {
207 containerName.replace(pos, keyTok.size(), key.empty() ? tname : key);
208 }
209 }
210
211 // Set the container name
212 placement.setContainerName(containerName);
213 return(placement);
214}
215//__________________________________________________________________________
216bool AthenaPoolConverter::compareClassGuid(const Token* token, const Guid &guid) const {
217 return(token ? (guid == token->classID()) : false);
218}
219//__________________________________________________________________________
220StatusCode AthenaPoolConverter::cleanUp(const std::string& /*output*/) {
221 ATH_MSG_DEBUG("AthenaPoolConverter cleanUp called for base class.");
222 return StatusCode::SUCCESS;
223}
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_ERROR(x)
#define ATH_MSG_VERBOSE(x)
#define ATH_MSG_WARNING(x)
#define ATH_MSG_DEBUG(x)
This file contains the class definition for the AthenaPoolConverter class.
uint32_t CLID
The Class ID type.
This file contains the class definition for the Guid class (migrated from POOL).
Provides a utility class to format integral types as hexadecimal strings at compile time or with VERY...
This file contains the class definition for the IAthenaPoolCnvSvc interface class.
This file contains the class definition for the Placement class (migrated from POOL).
This file contains the class definition for the TokenAddress class.
This file contains the class definition for the Token class (migrated from POOL).
AthMessaging(IMessageSvc *msgSvc, const std::string &name)
Constructor.
bool compareClassGuid(const Token *token, const Guid &guid) const
virtual StatusCode initialize() override
Gaudi Service Interface method implementations:
ServiceHandle< StoreGateSvc > m_detStore
virtual ~AthenaPoolConverter()
Destructor.
ServiceHandle< IAthenaPoolCnvSvc > m_athenaPoolCnvSvc
virtual Placement setPlacementWithType(const std::string &tname, const std::string &key, const std::string &output)
Set POOL placement hint for a given type.
virtual long repSvcType() const override
virtual StatusCode PoolToDataObject(DataObject *&pObj, const Token *token, const std::string &key)=0
Read an object from POOL.
virtual StatusCode DataObjectToPool(IOpaqueAddress *pAddr, DataObject *pObj)=0
Write an object into POOL.
AthenaPoolConverter(const CLID &id, ISvcLocator *pSvcLocator, const char *name=nullptr)
Standard Service Constructor.
virtual StatusCode fillRepRefs(IOpaqueAddress *pAddr, DataObject *pObj) override
Create a POOL persistent representation for a transient object.
virtual StatusCode createObj(IOpaqueAddress *pAddr, DataObject *&pObj) override
Create a transient object from a POOL persistent representation.
virtual StatusCode cleanUp(const std::string &output) override
Implement cleanUp for AthenaPoolConverter to do nothing.
virtual StatusCode createRep(DataObject *pObj, IOpaqueAddress *&pAddr) override
Create a POOL persistent representation for a transient object.
virtual StatusCode finalize() override
This class provides a encapsulation of a GUID/UUID/CLSID/IID data structure (128 bit number).
Definition Guid.h:25
This class holds all the necessary information to guide the writing of an object in a physical place.
Definition Placement.h:20
Placement & setContainerName(const std::string &containerName)
Set container name.
Definition Placement.h:35
Placement & setFileName(const std::string &fileName)
Set file name.
Definition Placement.h:31
Placement & setTechnology(int technology)
Set technology type.
Definition Placement.h:39
This class provides a Generic Transient Address for POOL tokens.
Token * getToken()
This class provides a token that identifies in a unique way objects on the persistent storage.
Definition Token.h:22
const std::string & auxString() const
Access auxiliary string.
Definition Token.h:92
const Guid & classID() const
Access database identifier.
Definition Token.h:74
virtual const std::string toString() const
Retrieve the string representation of the token.
Definition Token.cxx:135
Token & setAuxString(std::string &&auxString)
Set auxiliary string.
Definition Token.h:94
static DbType getType(const std::string &name)
Access known storage type object by name.
const char * getEventDataName()
const char * getDataHeaderName()
const char * getEventTagName()
static constexpr const char * DataHeaderTypeName
static constexpr const char * EventTagTypeName
static constexpr const char * DataHeaderFormTypeName
static const DbType POOL_StorageType
Definition DbType.h:84