Belle II Software development
RelationIndexManager.h
1/**************************************************************************
2 * basf2 (Belle II Analysis Software Framework) *
3 * Author: The Belle II Collaboration *
4 * *
5 * See git log for contributors and copyright holders. *
6 * This file is licensed under LGPL-3.0, see LICENSE.md. *
7 **************************************************************************/
8
9#pragma once
10
11#include <framework/datastore/RelationIndexContainer.h>
12
13#include <array>
14#include <memory>
15#include <map>
16
17namespace Belle2 {
31 public:
34
45 template<class FROM, class TO> std::shared_ptr<RelationIndexContainer<FROM, TO>> get(const RelationArray& relation)
46 {
47 const static bool doTypeCheck = (FROM::Class() != TObject::Class() or TO::Class() != TObject::Class());
48 if (doTypeCheck)
49 relation.isValid();
50
51 const std::string& name = relation.getName();
52 DataStore::EDurability durability = relation.getDurability();
53 RelationMap& relations = m_cache[durability];
54 std::shared_ptr<RelationIndexContainer<FROM, TO>> indexContainer;
55 RelationMap::iterator it = relations.find(name);
56 if (it != relations.end()) {
57 //if existing array is of wrong type, we'll overwrite the shared_ptr here, but the index will live on with any RelationIndex objects that use it.
58 indexContainer = std::dynamic_pointer_cast<RelationIndexContainer<FROM, TO>>(it->second);
59 }
60 if (!indexContainer) {
61 indexContainer.reset(new RelationIndexContainer<FROM, TO>(relation));
62 relations[name] = indexContainer;
63 } else {
64 indexContainer->rebuild(false);
65 }
66 return indexContainer;
67 }
68
75
81 void reset()
82 {
83 for (int i = 0; i < DataStore::c_NDurabilityTypes; i++)
84 m_cache[i].clear();
85 }
86
87 protected:
96
101 template<class FROM, class TO> std::shared_ptr<RelationIndexContainer<FROM, TO>> getIndexIfExists(const std::string& name,
102 DataStore::EDurability durability) const
103 {
104 const RelationMap& relations = m_cache[durability];
105 RelationMap::const_iterator it = relations.find(name);
106 if (it != relations.end()) {
107 return std::dynamic_pointer_cast<RelationIndexContainer<FROM, TO>>(it->second);
108 } else {
109 return nullptr;
110 }
111 }
112
115 {
116 reset();
117 }
118
120 typedef std::map<std::string, std::shared_ptr<RelationIndexBase>> RelationMap;
122 typedef std::array<RelationMap, DataStore::c_NDurabilityTypes> RelationCache;
125
127 friend class DataStore;
128 };
129
131} // end namespace Belle2
In the store you can park objects that have to be accessed by various modules.
Definition: DataStore.h:51
static const int c_NDurabilityTypes
Number of Durability Types.
Definition: DataStore.h:63
EDurability
Durability types.
Definition: DataStore.h:58
@ c_Event
Different object in each event, all objects/arrays are invalidated after event() function has been ca...
Definition: DataStore.h:59
Low-level class to create/modify relations between StoreArrays.
Definition: RelationArray.h:62
bool isValid() const
Check whether the object was created.
Class to store a bidirectional index between two StoreArrays.
Manager to keep a cache of existing RelationIndexContainers.
std::array< RelationMap, DataStore::c_NDurabilityTypes > RelationCache
Cachetype for all Containers.
std::shared_ptr< RelationIndexContainer< FROM, TO > > getIndexIfExists(const std::string &name, DataStore::EDurability durability) const
if the index exists in the cache, it is returned; otherwise NULL.
RelationCache m_cache
Cache for all Containers.
RelationIndexManager(const RelationIndexManager &)=delete
No copy-constructor.
RelationIndexManager(RelationIndexManager &&)=delete
No move constructor.
std::shared_ptr< RelationIndexContainer< FROM, TO > > get(const RelationArray &relation)
Get a RelationIndexContainer.
~RelationIndexManager()
Clean cache on exit.
static RelationIndexManager & Instance()
Returns the singleton instance.
RelationIndexManager & operator=(const RelationIndexManager &)=delete
Also no assignment.
std::map< std::string, std::shared_ptr< RelationIndexBase > > RelationMap
Maptype to keep track of all Containers of one durability.
void clear(DataStore::EDurability durability=DataStore::c_Event)
Clear the cache of RelationIndexContainers with the given durability.
void reset()
Reset the cache completely, that is clear all caches and don't even keep the Index objects around.
RelationIndexManager()=default
No Constructor hidden.
DataStore::EDurability getDurability() const
Return durability with which the object is saved in the DataStore.
const std::string & getName() const
Return name under which the object is saved in the DataStore.
Abstract base class for different kinds of events.