2 Copyright (C) 2002-2021 CERN for the benefit of the ATLAS collaboration
5 /** @file T_AthenaPoolCustomCnv.icc
6 * @brief This file contains the implementation for the templated T_AthenaPoolCustomCnv class.
7 * @author Marcin.Nowak@cern.ch
10 #include "GaudiKernel/StatusCode.h"
11 #include "GaudiKernel/DataObject.h"
12 #include "GaudiKernel/IOpaqueAddress.h"
13 #include "GaudiKernel/IRegistry.h"
15 #include "StorageSvc/DbReflex.h"
17 #include "AthenaKernel/StorableConversions.h"
19 #include "AthenaPoolCnvSvc/exceptions.h"
20 #include "AthenaPoolCnvTPExtension.h"
25 template <class TRANS, class PERS>
26 T_AthenaPoolCustomCnvWithKey<TRANS, PERS>::T_AthenaPoolCustomCnvWithKey(ISvcLocator* pSvcLocator,
27 const char* name /*= nullptr*/) : BaseType(pSvcLocator, name) {
30 template <class TRANS, class PERS>
31 StatusCode T_AthenaPoolCustomCnvWithKey<TRANS, PERS>::DataObjectToPers(DataObject* pObj, IOpaqueAddress*& /*pAddr*/) {
32 ATH_MSG_VERBOSE("In DataObjectToPers() for key = " << pObj->name());
33 return(StatusCode::SUCCESS);
36 template <class TRANS, class PERS>
37 StatusCode T_AthenaPoolCustomCnvWithKey<TRANS, PERS>::DataObjectToPool(IOpaqueAddress* pAddr, DataObject* pObj) {
38 ATH_MSG_VERBOSE("In DataObjectToPool() for key = " << pObj->name());
40 PERS* persObj = nullptr;
41 bool success = SG::fromStorable(pObj, obj);
42 if (!success || obj == nullptr) {
43 ATH_MSG_ERROR("Failed to cast DataObject to transient type");
44 return(StatusCode::FAILURE);
47 persObj = createPersistentWithKey(obj, pObj->name());
49 catch (const std::exception& ex) {
50 AthenaPoolCnvSvc::throwExcCaughtException (__PRETTY_FUNCTION__,
51 "creating persistent object",
56 Token* token = nullptr;
57 StatusCode status = this->objectToPool(persObj, token, pObj->name(), *pAddr->par());
58 // Null/empty token means ERROR
59 if (token == nullptr || token->classID() == Guid::null()) {
60 ATH_MSG_ERROR("failed to get Token for " << pObj->name());
61 return(StatusCode::FAILURE);
63 std::size_t cpos = pAddr->par()->find(':');
64 std::size_t bpos = pAddr->par()->find('[');
65 if (cpos == std::string::npos) {
70 if (bpos != std::string::npos) bpos = bpos - cpos;
71 keepPoolObj(persObj, pAddr->par()->substr(cpos, bpos));
72 // Update IOpaqueAddress for this object.
73 TokenAddress* tokAddr = dynamic_cast<TokenAddress*>(pAddr);
74 if (tokAddr != nullptr) {
75 tokAddr->setToken(token); token = nullptr;
77 delete token; token = nullptr;
78 return(StatusCode::FAILURE);
83 template <class TRANS, class PERS>
85 T_AthenaPoolCustomCnvWithKey<TRANS, PERS>::PoolToDataObject(DataObject*& pObj,
87 const std::string& key)
89 if (token != nullptr) {
90 this->m_classID = token->classID();
92 TRANS* transObj = nullptr;
93 AthenaConverterTLPExtension *extCnv = dynamic_cast<AthenaConverterTLPExtension*>(this);
94 // reset the TP converter used for reading, so old value is not used
95 if (extCnv != nullptr) {
96 extCnv->resetTPCnvForReading();
99 transObj = createTransientWithKey(key);
101 catch(const std::exception& ex) {
102 AthenaPoolCnvSvc::throwExcCaughtException (__PRETTY_FUNCTION__,
103 "creating transient object",
108 if (extCnv != nullptr) {
109 extCnv->deletePersistentObjects();
111 pObj = SG::asStorable(transObj);
112 return(StatusCode::SUCCESS);
115 // Read object of type P. This is an exception-throwing version of poolToObject()
116 // plus reading of all extending objects
117 template <class TRANS, class PERS>
119 inline P* T_AthenaPoolCustomCnvWithKey<TRANS, PERS>::poolReadObject() {
120 P* persObj = nullptr;
121 if (this->poolToObject(this->m_i_poolToken, persObj).isFailure()) {
122 std::string error("POOL read failed. Token = ");
123 throw std::runtime_error(error + (this->m_i_poolToken != nullptr ? this->m_i_poolToken->toString() : ""));
125 AthenaConverterTLPExtension *extCnv = dynamic_cast<AthenaConverterTLPExtension*>(this);
126 if (extCnv != nullptr) {
127 extCnv->readExtendingObjects(persObj);
132 // Read object of type P (plus all extending objects)
133 // using the indicated top-level TP converter
134 template <class TRANS, class PERS>
136 inline void T_AthenaPoolCustomCnvWithKey<TRANS, PERS>::poolReadObject(TopLevelTPCnvBase& tlp_converter) {
137 AthenaPoolCnvTPExtension *extCnv = dynamic_cast<AthenaPoolCnvTPExtension*>(this);
138 // set which Top level TP concerter will by used for reading
139 // only matters for extended converters
140 if (extCnv != nullptr) {
141 extCnv->usingTPCnvForReading(tlp_converter);
144 P* persObj = poolReadObject<P>();
145 // remember the object we just read
146 tlp_converter.setTLPersObject(persObj);
147 // not returning the object - the TLP converter owns it now
148 // and it will delete it automatically in createTransient()!
151 // Remember the POOL object until commit (then delete it)
152 template <class TRANS, class PERS>
153 inline void T_AthenaPoolCustomCnvWithKey<TRANS, PERS>::keepPoolObj(PERS* obj, const std::string& output) {
154 std::ostringstream oss;
155 oss << Gaudi::Hive::currentContext().slot();
156 std::lock_guard lock(m_pListMutex);
157 m_persObjLists[output + ":" + oss.str()].emplace_back(obj);
160 // callback from the CleanupSvc to delete persistent objects after commit
161 template <class TRANS, class PERS>
162 StatusCode T_AthenaPoolCustomCnvWithKey<TRANS, PERS>::cleanUp(const std::string& output) {
163 std::ostringstream oss;
164 oss << Gaudi::Hive::currentContext().slot();
165 std::lock_guard lock(m_pListMutex);
166 m_persObjLists[output + ":" + oss.str()].clear();
167 return(StatusCode::SUCCESS);
171 template <class TRANS, class PERS>
172 PERS* T_AthenaPoolCustomCnv<TRANS, PERS>::createPersistentWithKey
173 (TRANS* obj, const std::string& /*key*/)
175 return createPersistent(obj);
179 template <class TRANS, class PERS>
180 TRANS* T_AthenaPoolCustomCnv<TRANS, PERS>::createTransientWithKey
181 (const std::string& /*key*/)
183 return createTransient();