Belle II Software  release-05-02-19
RelationIndexManager.h
1 /**************************************************************************
2  * BASF2 (Belle Analysis Framework 2) *
3  * Copyright(C) 2010-2011 Belle II Collaboration *
4  * *
5  * Author: The Belle II Collaboration *
6  * Contributors: Martin Ritter *
7  * *
8  **************************************************************************/
9 
10 #pragma once
11 
12 #include <framework/datastore/RelationIndexContainer.h>
13 
14 #include <array>
15 #include <memory>
16 #include <map>
17 
18 namespace Belle2 {
31  class RelationIndexManager {
32  public:
35 
46  template<class FROM, class TO> std::shared_ptr<RelationIndexContainer<FROM, TO>> get(const RelationArray& relation)
47  {
48  const static bool doTypeCheck = (FROM::Class() != TObject::Class() or TO::Class() != TObject::Class());
49  if (doTypeCheck)
50  relation.isValid();
51 
52  const std::string& name = relation.getName();
53  DataStore::EDurability durability = relation.getDurability();
54  RelationMap& relations = m_cache[durability];
55  std::shared_ptr<RelationIndexContainer<FROM, TO>> indexContainer;
56  RelationMap::iterator it = relations.find(name);
57  if (it != relations.end()) {
58  //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.
59  indexContainer = std::dynamic_pointer_cast<RelationIndexContainer<FROM, TO>>(it->second);
60  }
61  if (!indexContainer) {
62  indexContainer.reset(new RelationIndexContainer<FROM, TO>(relation));
63  relations[name] = indexContainer;
64  } else {
65  indexContainer->rebuild(false);
66  }
67  return indexContainer;
68  }
69 
76 
82  void reset()
83  {
84  for (int i = 0; i < DataStore::c_NDurabilityTypes; i++)
85  m_cache[i].clear();
86  }
87 
88  protected:
90  RelationIndexManager() = default;
97 
102  template<class FROM, class TO> std::shared_ptr<RelationIndexContainer<FROM, TO>> getIndexIfExists(const std::string& name,
103  DataStore::EDurability durability) const
104  {
105  const RelationMap& relations = m_cache[durability];
106  RelationMap::const_iterator it = relations.find(name);
107  if (it != relations.end()) {
108  return std::dynamic_pointer_cast<RelationIndexContainer<FROM, TO>>(it->second);
109  } else {
110  return nullptr;
111  }
112  }
113 
116  {
117  reset();
118  }
119 
121  typedef std::map<std::string, std::shared_ptr<RelationIndexBase>> RelationMap;
123  typedef std::array<RelationMap, DataStore::c_NDurabilityTypes> RelationCache;
126 
128  friend class DataStore;
129  };
130 
132 } // end namespace Belle2
Belle2::RelationIndexManager::Instance
static RelationIndexManager & Instance()
Returns the singleton instance.
Definition: RelationIndexManager.cc:14
Belle2::RelationIndexManager::RelationIndexManager
RelationIndexManager()=default
No Constructor hidden.
Belle2::RelationIndexManager::RelationCache
std::array< RelationMap, DataStore::c_NDurabilityTypes > RelationCache
Cachetype for all Containers.
Definition: RelationIndexManager.h:130
Belle2::RelationIndexManager::RelationMap
std::map< std::string, std::shared_ptr< RelationIndexBase > > RelationMap
Maptype to keep track of all Containers of one durability.
Definition: RelationIndexManager.h:128
Belle2::RelationIndexManager::reset
void reset()
Reset the cache completely, that is clear all caches and don't even keep the Index objects around.
Definition: RelationIndexManager.h:89
Belle2::RelationIndexContainer::rebuild
void rebuild(bool force=false)
Rebuild the index.
Definition: RelationIndexContainer.h:164
Belle2::RelationIndexManager::get
std::shared_ptr< RelationIndexContainer< FROM, TO > > get(const RelationArray &relation)
Get a RelationIndexContainer.
Definition: RelationIndexManager.h:53
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::RelationIndexManager::operator=
RelationIndexManager & operator=(const RelationIndexManager &)=delete
Also no assignment.
Belle2::DataStore::c_NDurabilityTypes
const static int c_NDurabilityTypes
Number of Durability Types.
Definition: DataStore.h:65
Belle2::RelationIndexManager::getIndexIfExists
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.
Definition: RelationIndexManager.h:109
Belle2::RelationIndexManager::clear
void clear(DataStore::EDurability durability=DataStore::c_Event)
Clear the cache of RelationIndexContainers with the given durability.
Definition: RelationIndexManager.cc:19
Belle2::RelationIndexContainer
Class to store a bidirectional index between two StoreArrays.
Definition: RelationIndexContainer.h:58
Belle2::RelationIndexManager::~RelationIndexManager
~RelationIndexManager()
Clean cache on exit.
Definition: RelationIndexManager.h:122
Belle2::DataStore::c_Event
@ c_Event
Different object in each event, all objects/arrays are invalidated after event() function has been ca...
Definition: DataStore.h:61
Belle2::DataStore
In the store you can park objects that have to be accessed by various modules.
Definition: DataStore.h:53
Belle2::RelationIndexManager::m_cache
RelationCache m_cache
Cache for all Containers.
Definition: RelationIndexManager.h:132
Belle2::DataStore::EDurability
EDurability
Durability types.
Definition: DataStore.h:60