ATLAS Offline Software
T_AthenaPoolCustomCnv.icc
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2021 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 /** @file T_AthenaPoolCustomCnv.icc
6  * @brief This file contains the implementation for the templated T_AthenaPoolCustomCnv class.
7  * @author Marcin.Nowak@cern.ch
8  **/
9 
10 #include "GaudiKernel/StatusCode.h"
11 #include "GaudiKernel/DataObject.h"
12 #include "GaudiKernel/IOpaqueAddress.h"
13 #include "GaudiKernel/IRegistry.h"
14 
15 #include "StorageSvc/DbReflex.h"
16 
17 #include "AthenaKernel/StorableConversions.h"
18 
19 #include "AthenaPoolCnvSvc/exceptions.h"
20 #include "AthenaPoolCnvTPExtension.h"
21 
22 #include <sstream>
23 #include <stdexcept>
24 
25 template <class TRANS, class PERS>
26 T_AthenaPoolCustomCnvWithKey<TRANS, PERS>::T_AthenaPoolCustomCnvWithKey(ISvcLocator* pSvcLocator,
27  const char* name /*= nullptr*/) : BaseType(pSvcLocator, name) {
28 }
29 
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);
34 }
35 
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());
39  TRANS* obj = nullptr;
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);
45  }
46  try {
47  persObj = createPersistentWithKey(obj, pObj->name());
48  }
49  catch (const std::exception& ex) {
50  AthenaPoolCnvSvc::throwExcCaughtException (__PRETTY_FUNCTION__,
51  "creating persistent object",
52  ex,
53  typeid(TRANS),
54  pObj->name());
55  }
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);
62  }
63  std::size_t cpos = pAddr->par()->find(':');
64  std::size_t bpos = pAddr->par()->find('[');
65  if (cpos == std::string::npos) {
66  cpos = 0;
67  } else {
68  cpos++;
69  }
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;
76  } else {
77  delete token; token = nullptr;
78  return(StatusCode::FAILURE);
79  }
80  return(status);
81 }
82 
83 template <class TRANS, class PERS>
84 StatusCode
85 T_AthenaPoolCustomCnvWithKey<TRANS, PERS>::PoolToDataObject(DataObject*& pObj,
86  const Token* token,
87  const std::string& key)
88 {
89  if (token != nullptr) {
90  this->m_classID = token->classID();
91  }
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();
97  }
98  try {
99  transObj = createTransientWithKey(key);
100  }
101  catch(const std::exception& ex) {
102  AthenaPoolCnvSvc::throwExcCaughtException (__PRETTY_FUNCTION__,
103  "creating transient object",
104  ex,
105  typeid(TRANS),
106  key);
107  }
108  if (extCnv != nullptr) {
109  extCnv->deletePersistentObjects();
110  }
111  pObj = SG::asStorable(transObj);
112  return(StatusCode::SUCCESS);
113 }
114 
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>
118 template <class P>
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() : ""));
124  }
125  AthenaConverterTLPExtension *extCnv = dynamic_cast<AthenaConverterTLPExtension*>(this);
126  if (extCnv != nullptr) {
127  extCnv->readExtendingObjects(persObj);
128  }
129  return(persObj);
130 }
131 
132 // Read object of type P (plus all extending objects)
133 // using the indicated top-level TP converter
134 template <class TRANS, class PERS>
135 template <class P>
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);
142  }
143  // read the object
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()!
149 }
150 
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);
158 }
159 
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);
168 }
169 
170 
171 template <class TRANS, class PERS>
172 PERS* T_AthenaPoolCustomCnv<TRANS, PERS>::createPersistentWithKey
173  (TRANS* obj, const std::string& /*key*/)
174 {
175  return createPersistent(obj);
176 }
177 
178 
179 template <class TRANS, class PERS>
180 TRANS* T_AthenaPoolCustomCnv<TRANS, PERS>::createTransientWithKey
181  (const std::string& /*key*/)
182 {
183  return createTransient();
184 }