Belle II Software  release-08-01-10
Database.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 #pragma once
9 
10 #include <framework/database/IntervalOfValidity.h>
11 #include <framework/database/PayloadMetadata.h>
12 #include <framework/database/TestingPayloadStorage.h>
13 #include <framework/database/DBStore.h>
14 #include <framework/utilities/ScopeGuard.h>
15 #include <TClonesArray.h>
16 
17 #include <string>
18 #include <utility>
19 #include <list>
20 #include <memory>
21 
22 class TObject;
23 
24 
25 namespace Belle2 {
30  class EventMetaData;
31  namespace Conditions {
32  class MetadataProvider;
33  class PayloadProvider;
34  }
35 
42  class Database {
43  public:
47  c_PreInit = 0,
51  c_Ready = 2,
52  };
53 
57  static Database& Instance();
58 
67  static void reset(bool keepConfig = false);
68 
73 
77  struct DBImportQuery {
84  explicit DBImportQuery(const std::string& aName, TObject* aObject = nullptr,
85  const IntervalOfValidity& aIov = IntervalOfValidity()): name(aName), object(aObject), iov(aIov) {};
86  std::string name;
87  TObject* object;
89  };
90 
101  std::pair<TObject*, IntervalOfValidity> getData(const EventMetaData& event, const std::string& name);
102 
103 
110  bool getData(const EventMetaData& event, DBQuery& query)
111  {
112  std::vector<DBQuery> container{query};
113  bool found = getData(event, container);
114  query = container[0];
115  return found;
116  }
117 
126  bool getData(const EventMetaData& event, std::vector<DBQuery>& query);
127 
135  const TObject* getData(const std::string& name, int experiment, int run)
136  {
137  EventMetaData eventMetaData(1, run, experiment);
138  return getData(eventMetaData, name).first;
139  }
140 
146  std::set<std::string> getAllDataNames() { return DBStore::Instance().getAllEntryNames(); };
147 
156  bool storeData(const std::string& name, TObject* object, const IntervalOfValidity& iov);
157 
165  bool storeData(TClonesArray* array, const IntervalOfValidity& iov) {return storeData(DataStore::defaultArrayName(array->ClassName()), array, iov);};
166 
174  template<class T> bool storeData(T* object, const IntervalOfValidity& iov) {return storeData(DBStore::objectName<T>(""), object, iov);};
175 
182  bool storeData(std::list<DBImportQuery>& query);
183 
192  bool addPayload(const std::string& name, const std::string& fileName, const IntervalOfValidity& iov)
193  {
195  return m_payloadCreation->storePayload(name, fileName, iov);
196  }
197 
201  static void exposePythonAPI();
202 
208  std::string getGlobalTags();
209 
216 
218  void initialize(const EDatabaseState target = c_Ready);
219 
220  protected:
222  Database() = default;
224  Database(const Database&) = delete;
228  void nextMetadataProvider();
230  std::vector<std::string> m_metadataConfigurations;
232  std::string m_currentProvider;
234  std::vector<std::string> m_globalTags;
236  std::set<std::string> m_usableTagStates;
238  std::unique_ptr<Conditions::MetadataProvider> m_metadataProvider;
240  std::unique_ptr<Conditions::PayloadProvider> m_payloadProvider;
242  std::unique_ptr<Conditions::TestingPayloadStorage> m_payloadCreation;
244  std::vector<Conditions::TestingPayloadStorage> m_testingPayloads;
247  };
249 } // namespace Belle2
static std::string defaultArrayName()
Return the default storage name for an array of the given type.
Definition: DataStore.h:157
Singleton base class for the low-level interface to the database.
Definition: Database.h:42
std::string m_currentProvider
Name of the currently used metadata provider.
Definition: Database.h:232
Database(const Database &)=delete
No copy constructor, as it is a singleton.
bool getData(const EventMetaData &event, DBQuery &query)
Request an object from the database.
Definition: Database.h:110
std::unique_ptr< Conditions::MetadataProvider > m_metadataProvider
Currently active metadata provider.
Definition: Database.h:238
EDatabaseState m_configState
Current configuration state of the database.
Definition: Database.h:246
bool storeData(T *object, const IntervalOfValidity &iov)
Store an object in the database with the default name.
Definition: Database.h:174
EDatabaseState
State of the database.
Definition: Database.h:45
@ c_PreInit
Before any initialization.
Definition: Database.h:47
@ c_InitGlobaltagList
Globaltag list has been finalized metadata providers not and globaltags are not checked for usability...
Definition: Database.h:49
@ c_Ready
Everything is ready.
Definition: Database.h:51
const TObject * getData(const std::string &name, int experiment, int run)
Convenience function to get an object for an arbitrary experiment and run.
Definition: Database.h:135
std::unique_ptr< Conditions::PayloadProvider > m_payloadProvider
The active payload provider.
Definition: Database.h:240
std::set< std::string > m_usableTagStates
Set of usable globaltag states to be handed to the metadata providers.
Definition: Database.h:236
bool storeData(TClonesArray *array, const IntervalOfValidity &iov)
Store an ClonesArray in the database with the default name.
Definition: Database.h:165
std::unique_ptr< Conditions::TestingPayloadStorage > m_payloadCreation
testing payload storage to create new payloads
Definition: Database.h:242
std::set< std::string > getAllDataNames()
Return the names of all the payloads stored in the database at the time of this function is called.
Definition: Database.h:146
std::vector< std::string > m_globalTags
List of globaltags to be used.
Definition: Database.h:234
bool addPayload(const std::string &name, const std::string &fileName, const IntervalOfValidity &iov)
Add a payload file to the database.
Definition: Database.h:192
std::vector< Conditions::TestingPayloadStorage > m_testingPayloads
optional list of testing payload storages to look for existing payloads
Definition: Database.h:244
std::vector< std::string > m_metadataConfigurations
List of available metadata providers (which haven't been tried yet)
Definition: Database.h:230
Database()=default
Hidden constructor, as it is a singleton.
Store event, run, and experiment numbers.
Definition: EventMetaData.h:33
A class that describes the interval of experiments/runs for which an object in the database is valid.
Simple ScopeGuard to execute a function at the end of the object lifetime.
Definition: ScopeGuard.h:36
std::string getGlobalTags()
Return the global tags used by the database.
Definition: Database.cc:154
void initialize(const EDatabaseState target=c_Ready)
Initialize the database connection settings on first use.
Definition: Database.cc:197
~Database()
Hidden destructor, as it is a singleton.
std::pair< TObject *, IntervalOfValidity > getData(const EventMetaData &event, const std::string &name)
Request an object from the database.
Definition: Database.cc:72
static Database & Instance()
Instance of a singleton Database.
Definition: Database.cc:42
static DBStore & Instance()
Instance of a singleton DBStore.
Definition: DBStore.cc:28
static void exposePythonAPI()
Exposes setGlobalTag function of the Database class to Python.
Definition: Database.cc:272
bool storeData(const std::string &name, TObject *object, const IntervalOfValidity &iov)
Store an object in the database.
Definition: Database.cc:141
void nextMetadataProvider()
Enable the next metadataprovider in the list.
Definition: Database.cc:159
std::set< std::string > getAllEntryNames()
Return the names of all the payloads stored in the database at the time of this function is called.
Definition: DBStore.cc:196
static void reset(bool keepConfig=false)
Reset the database instance.
Definition: Database.cc:50
ScopeGuard createScopedUpdateSession()
Make sure we have efficient http pipelinging during initialize/beginRun but don't keep session alive ...
Definition: Database.cc:62
Abstract base class for different kinds of events.
Simple struct to group all information necessary for a single payload.
Struct for bulk write queries.
Definition: Database.h:77
IntervalOfValidity iov
Interval of validity of the object.
Definition: Database.h:88
TObject * object
Pointer to the object.
Definition: Database.h:87
DBImportQuery(const std::string &aName, TObject *aObject=nullptr, const IntervalOfValidity &aIov=IntervalOfValidity())
Constructor.
Definition: Database.h:84
std::string name
identifier of the object
Definition: Database.h:85