Belle II Software  release-05-02-19
MetadataProvider.h
1 /**************************************************************************
2  * BASF2 (Belle Analysis Framework 2) *
3  * Copyright(C) 2019 - Belle II Collaboration *
4  * *
5  * Author: The Belle II Collaboration *
6  * Contributors: Martin Ritter *
7  * *
8  * This software is provided "as is" without any warranty. *
9  **************************************************************************/
10 
11 #pragma once
12 
13 #include <framework/database/PayloadMetadata.h>
14 #include <set>
15 #include <string>
16 #include <unordered_map>
17 #include <vector>
18 
19 namespace Belle2::Conditions {
29  class MetadataProvider {
30  public:
32  using PayloadMap = std::unordered_map<std::string, PayloadMetadata>;
33 
42  class PayloadMetadataCache {
43  public:
45  struct CacheEntry {
46  int exp{ -1};
47  int run{ -1};
49  };
57  std::tuple<bool, PayloadMap*> get(int exp, int run)
58  {
59  using std::get;
60  // if run mismatch swap current and last entry
61  if (current.exp != exp || current.run != run) std::swap(current, previous);
62  // still mismatch? if so clear, otherwise just return
63  const bool found = current.exp == exp and current.run == run;
64  if (!found) {
65  current.exp = exp;
66  current.run = run;
67  current.map.clear();
68  }
69  return {found, &current.map};
70  }
71  private:
72  CacheEntry current;
74  };
75 
77  MetadataProvider() = default;
79  explicit MetadataProvider(const std::set<std::string>& usableTagStates): m_payloads{nullptr}, m_usableTagStates(usableTagStates) {}
81  virtual ~MetadataProvider() = default;
88  bool setTags(const std::vector<std::string>& tags);
122  bool getPayloads(int exp, int run, std::vector<PayloadMetadata>& info);
124  std::set<std::string> getUsableTagStates() { return m_usableTagStates; }
130  void setUsableTagStates(const std::set<std::string>& states)
131  {
132  m_usableTagStates = states;
133  m_usableTagStates.erase("INVALID");
134  }
135  protected:
138  virtual std::string getGlobaltagStatus(const std::string& name) = 0;
154  virtual bool updatePayloads(const std::string& globaltag, int exp, int run) = 0;
164  void addPayload(PayloadMetadata&& payload, const std::string& messagePrefix = "");
165 
167  std::vector<std::string> m_tags;
169  std::unordered_map<std::string, PayloadMetadataCache> m_cache;
171  PayloadMap* m_payloads{nullptr};
173  std::set<std::string> m_usableTagStates{"TESTING", "VALIDATED", "RUNNING", "PUBLISHED"};
174  };
175 
179  class NullMetadataProvider: public MetadataProvider {
181  bool updatePayloads([[maybe_unused]] const std::string& globaltag, [[maybe_unused]] int exp,
182  [[maybe_unused]] int run) override
183  {
184  return false;
185  }
187  std::string getGlobaltagStatus(const std::string& name) override;
189  bool m_errorShown{false};
190  };
191 } // namespace Belle2::Conditions
Belle2::Conditions::MetadataProvider::PayloadMap
std::unordered_map< std::string, PayloadMetadata > PayloadMap
Type for the internal list of payloads.
Definition: MetadataProvider.h:48
Belle2::Conditions::MetadataProvider::m_tags
std::vector< std::string > m_tags
List of globaltags to consider.
Definition: MetadataProvider.h:183
Belle2::Conditions::MetadataProvider::setTags
bool setTags(const std::vector< std::string > &tags)
Set the list of globaltag names to be considered for payloads.
Definition: MetadataProvider.cc:25
Belle2::Conditions::MetadataProvider::PayloadMetadataCache::CacheEntry
Each cache entry.
Definition: MetadataProvider.h:61
Belle2::Conditions::MetadataProvider::getPayloads
bool getPayloads(int exp, int run, std::vector< PayloadMetadata > &info)
Update the information in the vector of metadata instances with the actual values.
Definition: MetadataProvider.cc:46
Belle2::Conditions::NullMetadataProvider::m_errorShown
bool m_errorShown
Have we already shown the error?
Definition: MetadataProvider.h:197
Belle2::Conditions::NullMetadataProvider::getGlobaltagStatus
std::string getGlobaltagStatus(const std::string &name) override
When we get asked to check a global tag we raise an error ...
Definition: MetadataProvider.cc:124
Belle2::Conditions::MetadataProvider::updatePayloads
virtual bool updatePayloads(const std::string &globaltag, int exp, int run)=0
Update the list of existing payloads from a given globaltag, exp and run combination.
Belle2::Conditions::MetadataProvider::m_usableTagStates
std::set< std::string > m_usableTagStates
Set of global tag states to consider valid (except for 'INVALID' which is always considered invalid)
Definition: MetadataProvider.h:189
Belle2::Conditions::MetadataProvider::PayloadMetadataCache::current
CacheEntry current
currently valid conditions
Definition: MetadataProvider.h:88
Belle2::Conditions::MetadataProvider::PayloadMetadataCache::get
std::tuple< bool, PayloadMap * > get(int exp, int run)
Get the list of map payloads from the cache This returns a tuple containing two elements:
Definition: MetadataProvider.h:73
Belle2::Conditions::MetadataProvider::m_cache
std::unordered_map< std::string, PayloadMetadataCache > m_cache
Map of globaltag->known metadata.
Definition: MetadataProvider.h:185
Belle2::Conditions::MetadataProvider::addPayload
void addPayload(PayloadMetadata &&payload, const std::string &messagePrefix="")
Add a payload information to the internal list.
Definition: MetadataProvider.cc:101
Belle2::Conditions::MetadataProvider::PayloadMetadataCache::CacheEntry::exp
int exp
experiment number
Definition: MetadataProvider.h:62
Belle2::Conditions::MetadataProvider::getGlobaltagStatus
virtual std::string getGlobaltagStatus(const std::string &name)=0
Check the status of a global tag with the given name.
Belle2::Conditions::MetadataProvider::MetadataProvider
MetadataProvider()=default
Default constructible.
Belle2::Conditions::MetadataProvider::PayloadMetadataCache::CacheEntry::run
int run
run number
Definition: MetadataProvider.h:63
Belle2::Conditions::MetadataProvider::PayloadMetadataCache::previous
CacheEntry previous
previously valid conditions
Definition: MetadataProvider.h:89
Belle2::Conditions::MetadataProvider::PayloadMetadataCache::CacheEntry::map
PayloadMap map
Map of all known name -> PayloadMetadata entries.
Definition: MetadataProvider.h:64
Belle2::Conditions::MetadataProvider::getUsableTagStates
std::set< std::string > getUsableTagStates()
Get the valid tag states when checking globaltag status.
Definition: MetadataProvider.h:140
Belle2::Conditions::PayloadMetadata
Simple struct to group all information necessary for a single payload.
Definition: PayloadMetadata.h:25
Belle2::Conditions::NullMetadataProvider::updatePayloads
bool updatePayloads([[maybe_unused]] const std::string &globaltag, [[maybe_unused]] int exp, [[maybe_unused]] int run) override
Nope, no update.
Definition: MetadataProvider.h:189
Belle2::Conditions::MetadataProvider::setUsableTagStates
void setUsableTagStates(const std::set< std::string > &states)
Set the valid tag states for this provider when checking globaltag status.
Definition: MetadataProvider.h:146
Belle2::Conditions::MetadataProvider::m_payloads
PayloadMap * m_payloads
Map of known payloads for current conditions.
Definition: MetadataProvider.h:187
Belle2::Conditions::MetadataProvider::~MetadataProvider
virtual ~MetadataProvider()=default
Default destructible.