ATLAS Offline Software
IdentifiableContainerMT.h
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2023 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 #ifndef EVENTCONTAINERS_IDENTIFIABLECONTAINERMT_H
6 #define EVENTCONTAINERS_IDENTIFIABLECONTAINERMT_H
7 
19 #include "Identifier/Identifier.h"
21 #include <memory>
22 #include "GaudiKernel/DataObject.h"
27 
28 template < class T>
30 {
31 
32 public:
33 
35  friend class IdentifiableContainerMT<T>;
38  public:
43  static void Swap(IDC_WriteHandle& a, IDC_WriteHandle& b) noexcept {
44  if(&a == &b) return;
45  std::swap(a.m_IDC_ptr, b.m_IDC_ptr);
46  std::swap(a.m_hashId, b.m_hashId);
47  std::swap(a.m_atomic, b.m_atomic);
48  }
50  Swap(*this, other);
51  }
52 
53  [[nodiscard]] StatusCode addOrDelete(std::unique_ptr<T> ptr){
54  if(ATH_UNLIKELY(m_hashId >= m_IDC_ptr->m_link->fullSize())) return StatusCode::FAILURE;
55  StatusCode sc = m_IDC_ptr->addLock(std::move(ptr), m_hashId);
56  IDC_WriteHandleBase::ReleaseLock();
57  return sc;
58  }
59  bool alreadyPresent() { return m_IDC_ptr->m_link->tryAddFromCache(m_hashId, *this); }
61  bool OnlineAndPresentInAnotherView() { return m_IDC_ptr->m_OnlineMode && m_IDC_ptr->m_link->tryAddFromCache(m_hashId, *this); }
62  };
63 
64 
65 
66  typedef T IDENTIFIABLE;
69  typedef std::vector<std::atomic<const void*> >::size_type size_type;
70  typedef T*& reference;
71  typedef T* const & const_reference;
72  typedef T* value_type;
73  typedef T** pointer;
74  typedef T* const * const_pointer;
75  typedef T base_value_type;
76 
77 
79  {
80 
81  public:
82 
86  const_iterator(const const_iterator&) = default;
87  const_iterator& operator = ( const const_iterator & ) = default; //move operators may be useful for shared_ptr
89  ~const_iterator() = default;
90 
91 
94  ++m_itr;
95  return *this;
96  }
97 
100  const_iterator tmp = *this;
101  ++*this;
102  return tmp;
103  }
104 
105  const T* cptr () const {
106  return reinterpret_cast<const T*>( m_itr->second );
107  }
108 
109  const T* operator * () const {
110  return reinterpret_cast<const T*>( m_itr->second );
111  }
112 
113  const T* operator ->() const { return (operator*()); }
114 
116  bool operator != ( const const_iterator &it ) const {
117  return m_itr!= it.m_itr;
118  }
119 
121  bool operator == ( const const_iterator &it ) const {
122  return m_itr == it.m_itr;
123  }
124 
126  //Calling this on an end iterator is undefined behaviour
128  return m_itr->first;
129  }
130 
131  protected:
132  friend class IdentifiableContainerMT<T>;
133 
135 
136 
138  };
139 
140  friend class IDC_WriteHandle;
141 
144 
146 
149 
151 
152  virtual bool hasExternalCache() const override final { return m_OnlineMode; }
153 
154 
158  virtual const T* indexFindPtr( IdentifierHash hashId ) const override final;
159 
161  return m_link->indexFind(hashId);
162  }
163 
166  virtual StatusCode addCollection(const T* coll, IdentifierHash hashId) override final;
167 
170  virtual StatusCode addOrDelete(std::unique_ptr<T>, IdentifierHash hashId) override final;
171  virtual StatusCode addOrDelete(std::unique_ptr<const T>, IdentifierHash hashId) override final;
172 
174  StatusCode addOrDelete(std::unique_ptr<T>, IdentifierHash hashId, bool &deleted);
175 
177  StatusCode addLock(std::unique_ptr<T> ptr, IdentifierHash hashId);
178 
181  virtual bool tryAddFromCache(IdentifierHash hashId) override final;
182 
186 
187  StatusCode fetchOrCreate(const std::vector<IdentifierHash> &hashId);
188 
189  //This is a method to support bad behaviour in old code. Remove ASAP
190  virtual StatusCode naughtyRetrieve ATLAS_NOT_THREAD_SAFE (IdentifierHash hashId, T* &collToRetrieve) const override final;
191 
192 
196 
198  virtual void cleanup() override final;
199 
201  virtual size_t fullSize() const override final {
202  return m_link->fullSize();
203  }
204 
206  size_t size() const {
207  return m_link->fullSize();
208  }
209 
210  void prepareItr() const { m_link->wait(); }
211 
213  virtual size_t numberOfCollections() const override final{
214  return IdentifiableContainerBase::numberOfCollections();
215  }
216 
217  const std::vector < EventContainers::hashPair<T> >& GetAllHashPtrPair() const{
218  static_assert(sizeof(const T*) == sizeof(const void*) && std::is_pointer<const T*>::value);
219  static_assert(sizeof(EventContainers::hashPair<T>) == sizeof(EventContainers::hashPair<void>));
220  return reinterpret_cast<const std::vector < EventContainers::hashPair<T> >&>
221  (m_link->getAllHashPtrPair());
222  }
223 
228  virtual std::vector<IdentifierHash> GetAllCurrentHashes() const override final {
229  return IdentifiableContainerBase::GetAllCurrentHashes();
230  }
231 
234  return const_iterator(m_link->cbegin());
235  }
236 
237 
239  const_iterator end() const {
240  return const_iterator(m_link->cend());
241  }
242 
244  bool empty() const {
245  return numberOfCollections()==0;
246  }
247 
249  {
250  IDC_WriteHandle lock;
251  lock.m_hashId = hash;
252  lock.m_IDC_ptr = this;
253  return lock;
254  }
255 };
256 
257 template < class T>
258 T* //Please don't do this we want to get rid of this
260 {
261  return reinterpret_cast<T*>(m_link->removeCollection(hashId));
262 }
263 
264 
265 
266 // Constructor for OFFLINE style IDC
267 template < class T>
268 IdentifiableContainerMT<T>::IdentifiableContainerMT(IdentifierHash maxHash) : IdentifiableContainerBase(maxHash)
269 {
270 }
271 
272 template < class T>
274  IdentifiableContainerBase(maxHash, mode)
275 {
276 }
277 
278 // Constructor for ONLINE style IDC
279 template < class T>
280 IdentifiableContainerMT<T>::IdentifiableContainerMT(ICACHE *cache) : IdentifiableContainerBase(cache)
281 {
282 }
283 
287 template < class T>
288 const T*
290 {
291  return reinterpret_cast<const T* > (IdentifiableContainerBase::indexFindPtr(hashId));
292 }
293 
294 // insert collection into container with id hash
295 template < class T>
298 {
299  // update m_hashids
300  if (ATH_UNLIKELY(! IdentifiableContainerBase::insert(hashId, coll))) return StatusCode::FAILURE;
301  return StatusCode::SUCCESS;
302 
303 }
304 
305 
306 
307 template < class T>
308 void
310 {
311  IdentifiableContainerBase::cleanup(void_unique_ptr::Deleter<T>::deleter);
312 }
313 
314 template < class T>
317 {
318  return IdentifiableContainerBase::fetchOrCreate(hashId);
319 }
320 
321 template < class T>
323 IdentifiableContainerMT<T>::fetchOrCreate(const std::vector<IdentifierHash> &hashIds)
324 {
325  return IdentifiableContainerBase::fetchOrCreate(hashIds);
326 }
327 
328 
329 template < class T>
330 bool
332 {
333  return IdentifiableContainerBase::tryAddFromCache(hashId);
334 }
335 
336 template < class T >
337 StatusCode
338 IdentifiableContainerMT<T>::naughtyRetrieve(IdentifierHash hashId, T* &collToRetrieve) const
339 {
340  if(ATH_UNLIKELY(m_OnlineMode)) return StatusCode::FAILURE;//NEVER ALLOW FOR EXTERNAL CACHE
341  else {
342  auto p = reinterpret_cast<const T* > (m_link->findIndexPtr(hashId));//collToRetrieve can be null on success
343  collToRetrieve = const_cast<T*>(p);
344  return StatusCode::SUCCESS;
345  }
346 }
347 
348 template < class T>
351 {
352  if(ATH_UNLIKELY(hashId >= m_link->fullSize())) return StatusCode::FAILURE;
353  auto ptr = uptr.release();
354  bool b = IdentifiableContainerBase::insert(hashId, ptr);
355  if(!b) delete ptr;
356  return StatusCode::SUCCESS;
357 }
358 
359 template < class T>
361 IdentifiableContainerMT<T>::addOrDelete(std::unique_ptr<const T> uptr, IdentifierHash hashId)
362 {
363  if(ATH_UNLIKELY(hashId >= m_link->fullSize())) return StatusCode::FAILURE;
364  auto ptr = uptr.release();
365  bool b = IdentifiableContainerBase::insert(hashId, ptr);
366  if(!b) delete ptr;
367  return StatusCode::SUCCESS;
368 }
369 
370 template < class T>
373 {
374  return m_link->addLock(hashId, ptr.release());
375 }
376 
377 template < class T>
379 IdentifiableContainerMT<T>::addOrDelete(std::unique_ptr<T> uptr, IdentifierHash hashId, bool &deleted)
380 {
381  if(ATH_UNLIKELY(hashId >= m_link->fullSize())) return StatusCode::FAILURE;
382  auto ptr = uptr.release();
383  bool b = IdentifiableContainerBase::insert(hashId, ptr);
384  if(!b) delete ptr;
385  deleted = !b;
386  return StatusCode::SUCCESS;
387 }
388 
389 #endif
390 
IdentifiableContainerMT::IDC_WriteHandle::operator=
IDC_WriteHandle & operator=(const IDC_WriteHandle &other)=delete
IdentifiableContainerMT::~IdentifiableContainerMT
~IdentifiableContainerMT()
Definition: IdentifiableContainerMT.h:150
IdentifiableContainerMT::IDC_WriteHandle::m_hashId
IdentifierHash m_hashId
Definition: IdentifiableContainerMT.h:37
IdentifiableContainerMT::base_value_type
T base_value_type
Definition: IdentifiableContainerMT.h:75
IdentifiableContainerMT::IDC_WriteHandle::m_IDC_ptr
IdentifiableContainerMT< T > * m_IDC_ptr
Definition: IdentifiableContainerMT.h:36
IdentifiableContainerMT::addCollection
virtual StatusCode addCollection(const T *coll, IdentifierHash hashId) override final
insert collection into container with id hash if IDC should not take ownership of collection,...
Definition: IdentifiableContainerMT.h:297
IdentifiableContainerMT::getWriteHandle
IDC_WriteHandle getWriteHandle(IdentifierHash hash)
Definition: IdentifiableContainerMT.h:248
IdentifiableContainerMT::const_iterator::const_iterator
const_iterator()
iterator constructor
Definition: IdentifiableContainerMT.h:84
IdentifiableCache.h
IdentifiableContainerMT::IDC_WriteHandle::operator=
IDC_WriteHandle & operator=(IDC_WriteHandle &&other) noexcept=delete
IdentifiableContainerMT::const_iterator::operator==
bool operator==(const const_iterator &it) const
comparison operator
Definition: IdentifiableContainerMT.h:121
EventContainers::IDC_WriteHandleBase::IDC_WriteHandleBase
IDC_WriteHandleBase()
Definition: IDC_WriteHandleBase.h:17
IdentifiableContainerMT::reference
T *& reference
Definition: IdentifiableContainerMT.h:70
IdentifiableContainerMT::IDC_WriteHandle::OnlineAndPresentInAnotherView
bool OnlineAndPresentInAnotherView()
This method is to avoid calling an expensive operation in the offline case.
Definition: IdentifiableContainerMT.h:61
IIdentifiableCont.h
skel.it
it
Definition: skel.GENtoEVGEN.py:396
IdentifiableContainerMT::size
size_t size() const
Duplicate of fullSize for backwards compatability.
Definition: IdentifiableContainerMT.h:206
IdentifiableContainerMT::const_iterator::const_iterator
const_iterator(const const_iterator &)=default
IdentifiableContainerMT::size_type
std::vector< std::atomic< const void * > >::size_type size_type
Definition: IdentifiableContainerMT.h:69
IdentifiableContainerMT::IDENTIFIABLE
T IDENTIFIABLE
Definition: IdentifiableContainerMT.h:66
athena.value
value
Definition: athena.py:124
IdentifiableContainerMT::MyType
IdentifiableContainerMT< T > MyType
Definition: IdentifiableContainerMT.h:68
ATH_UNLIKELY
#define ATH_UNLIKELY(x)
Definition: AthUnlikelyMacros.h:17
IdentifiableContainerMT::IDC_WriteHandle::alreadyPresent
bool alreadyPresent()
Definition: IdentifiableContainerMT.h:59
dbg::ptr
void * ptr(T *p)
Definition: SGImplSvc.cxx:74
const
bool const RAWDATA *ch2 const
Definition: LArRodBlockPhysicsV0.cxx:560
EventContainers::hashPair
Definition: I_InternalIDC.h:25
IdentifiableContainerMT::const_iterator::operator!=
bool operator!=(const const_iterator &it) const
comparison operator
Definition: IdentifiableContainerMT.h:116
IdentifiableContainerMT::const_iterator::const_iterator
const_iterator(EventContainers::I_InternalIDC::InternalConstItr itr)
Definition: IdentifiableContainerMT.h:134
void_unique_ptr::Deleter
Definition: deleter.h:22
IdentifiableContainerMT::value_type
T * value_type
Definition: IdentifiableContainerMT.h:72
AthenaPoolTestRead.sc
sc
Definition: AthenaPoolTestRead.py:27
EventContainers::IIdentifiableCont
Definition: IIdentifiableCont.h:13
IdentifiableContainerMT::empty
bool empty() const
return true if container is empty
Definition: IdentifiableContainerMT.h:244
IdentifiableContainerMT::const_iterator::operator=
const_iterator & operator=(const const_iterator &)=default
AthUnlikelyMacros.h
IdentifiableContainerMT::hasExternalCache
virtual bool hasExternalCache() const override final
Definition: IdentifiableContainerMT.h:152
IdentifiableContainerMT::IDC_WriteHandle::IDC_WriteHandle
IDC_WriteHandle(IDC_WriteHandle &&other)
Definition: IdentifiableContainerMT.h:49
IdentifiableContainerMT::ICACHE
EventContainers::IdentifiableCache< T > ICACHE
Definition: IdentifiableContainerMT.h:67
IdentifiableContainerMT::prepareItr
void prepareItr() const
Definition: IdentifiableContainerMT.h:210
EventContainers::IdentifiableContainerBase::m_link
std::unique_ptr< I_InternalIDC > m_link
Definition: IdentifiableContainerBase.h:26
python.utils.AtlRunQueryDQUtils.p
p
Definition: AtlRunQueryDQUtils.py:210
IdentifiableContainerMT::numberOfCollections
virtual size_t numberOfCollections() const override final
return number of collections
Definition: IdentifiableContainerMT.h:213
IdentifiableContainerMT::const_iterator::operator*
const T * operator*() const
Definition: IdentifiableContainerMT.h:109
IdentifiableContainerMT::GetAllCurrentHashes
virtual std::vector< IdentifierHash > GetAllCurrentHashes() const override final
Returns a collection of all hashes availiable in this IDC.
Definition: IdentifiableContainerMT.h:228
IdentifiableContainerMT::const_pointer
T *const * const_pointer
Definition: IdentifiableContainerMT.h:74
IdentifiableContainerMT::addOrDelete
virtual StatusCode addOrDelete(std::unique_ptr< T >, IdentifierHash hashId) override final
Tries to add the item to the cache, if the item already exists then it is deleted This is a convenien...
Definition: IdentifiableContainerMT.h:350
IdentifiableContainerMT::addLock
StatusCode addLock(std::unique_ptr< T > ptr, IdentifierHash hashId)
Like the other add methods but optimized for changing from the inprogress state.
Definition: IdentifiableContainerMT.h:372
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
IdentifiableContainerMT::fullSize
virtual size_t fullSize() const override final
return full size of container
Definition: IdentifiableContainerMT.h:201
IdentifiableContainerMT::tryAddFromCache
virtual bool tryAddFromCache(IdentifierHash hashId) override final
Looks in the cache to see if item already exists if not it returns false, If it does exist it incorpo...
Definition: IdentifiableContainerMT.h:331
IdentifiableContainerMT::indexFind
const_iterator indexFind(IdentifierHash hashId) const
Definition: IdentifiableContainerMT.h:160
IdentifiableContainerMT::IDC_WriteHandle
Definition: IdentifiableContainerMT.h:34
IDC_WriteHandleBase.h
IdentifiableContainerMT::end
const_iterator end() const
return const_iterator for end of container
Definition: IdentifiableContainerMT.h:239
Preparation.mode
mode
Definition: Preparation.py:94
IdentifiableContainerMT::removeCollection
T * removeCollection(IdentifierHash hashId)
remove collection from container for id hash, returning it (and ownership) to client
Definition: IdentifiableContainerMT.h:259
IdentifiableContainerMT::const_iterator
Definition: IdentifiableContainerMT.h:79
IdentifiableContainerMT::begin
const_iterator begin() const
return const_iterator for first entry
Definition: IdentifiableContainerMT.h:233
DeMoUpdate.tmp
string tmp
Definition: DeMoUpdate.py:1167
WriteCalibToCool.swap
swap
Definition: WriteCalibToCool.py:94
EventContainers::IdentifiableContainerBase
Definition: IdentifiableContainerBase.h:14
IdentifiableContainerMT::const_iterator::hashId
IdentifierHash hashId() const
hashId of the pointed-to element
Definition: IdentifiableContainerMT.h:127
plotBeamSpotMon.b
b
Definition: plotBeamSpotMon.py:77
EventContainers::Mode
Mode
Definition: IdentifiableContainerBase.h:13
IdentifiableContainerMT::const_iterator::cptr
const T * cptr() const
Definition: IdentifiableContainerMT.h:105
IdentifiableContainerMT::const_reference
T *const & const_reference
Definition: IdentifiableContainerMT.h:71
IdentifiableContainerMT::IDC_WriteHandle::addOrDelete
StatusCode addOrDelete(std::unique_ptr< T > ptr)
Definition: IdentifiableContainerMT.h:53
IdentifiableContainerMT::indexFindPtr
virtual const T * indexFindPtr(IdentifierHash hashId) const override final
return pointer on the found entry or null if out of range using hashed index - fast version,...
Definition: IdentifiableContainerMT.h:289
IdentifiableContainerMT::pointer
T ** pointer
Definition: IdentifiableContainerMT.h:73
EventContainers::IIdentifiableCont::naughtyRetrieve
virtual StatusCode naughtyRetrieve(IdentifierHash hashId, T *&collToRetrieve) const =0
a
TList * a
Definition: liststreamerinfos.cxx:10
InDetDD::other
@ other
Definition: InDetDD_Defs.h:16
CaloCondBlobAlgs_fillNoiseFromASCII.hash
dictionary hash
Definition: CaloCondBlobAlgs_fillNoiseFromASCII.py:109
IdentifiableContainerMT::const_iterator::operator->
const T * operator->() const
Definition: IdentifiableContainerMT.h:113
IdentifiableContainerMT::fetchOrCreate
StatusCode fetchOrCreate(IdentifierHash hashId)
Tries will look for item in cache, if it doesn't exist will call the cache IMAKER If cache doesn't ha...
Definition: IdentifiableContainerMT.h:316
IdentifiableContainerMT::IDC_WriteHandle::IDC_WriteHandle
IDC_WriteHandle(const IDC_WriteHandle &other)=delete
IdentifiableContainerMT::IDC_WriteHandle::Swap
static void Swap(IDC_WriteHandle &a, IDC_WriteHandle &b) noexcept
Definition: IdentifiableContainerMT.h:43
IdentifiableContainerMT::GetAllHashPtrPair
const std::vector< EventContainers::hashPair< T > > & GetAllHashPtrPair() const
Definition: IdentifiableContainerMT.h:217
EventContainers::IDC_WriteHandleBase
Definition: IDC_WriteHandleBase.h:13
IdentifiableContainerMT::const_iterator::m_itr
EventContainers::I_InternalIDC::InternalConstItr m_itr
Definition: IdentifiableContainerMT.h:137
IdentifiableContainerMT::IDC_WriteHandle::IDC_WriteHandle
IDC_WriteHandle()
Definition: IdentifiableContainerMT.h:39
IdentifiableContainerMT::addOrDelete
virtual StatusCode addOrDelete(std::unique_ptr< const T >, IdentifierHash hashId) override final
EventContainers::IdentifiableContainerBase::m_OnlineMode
bool m_OnlineMode
Definition: IdentifiableContainerBase.h:24
IdentifiableContainerMT
Definition: IdentifiableContainerMT.h:30
IdentifiableContainerMT::ATLAS_NOT_THREAD_SAFE
virtual StatusCode naughtyRetrieve ATLAS_NOT_THREAD_SAFE(IdentifierHash hashId, T *&collToRetrieve) const override final
IdentifierHash
This is a "hash" representation of an Identifier. This encodes a 32 bit index which can be used to lo...
Definition: IdentifierHash.h:25
IdentifiableContainerBase.h
IdentifiableContainerMT::IdentifiableContainerMT
IdentifiableContainerMT(IdentifierHash hashMax)
constructor initializes the collection the hashmax, OFFLINE usages pattern
Definition: IdentifiableContainerMT.h:268
IdentifiableContainerMT::const_iterator::operator++
const_iterator & operator++()
increment operator
Definition: IdentifiableContainerMT.h:93
IdentifiableContainerMT::const_iterator::const_iterator
const_iterator(const_iterator &&)=default
EventContainers::I_InternalIDC::InternalConstItr
std::vector< hashPair >::const_iterator InternalConstItr
Definition: I_InternalIDC.h:37
IdentifiableContainerMT::cleanup
virtual void cleanup() override final
reset m_hashids and call IdentifiableCache's cleanup
Definition: IdentifiableContainerMT.h:309
EventContainers::IdentifiableCache
Definition: IdentifiableCache.h:29
TSU::T
unsigned long long T
Definition: L1TopoDataTypes.h:35
IdentifiableContainerMT::const_iterator::~const_iterator
~const_iterator()=default