Belle II Software development
MetadataProvider.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/database/PayloadMetadata.h>
12#include <set>
13#include <string>
14#include <unordered_map>
15#include <vector>
16
17namespace Belle2::Conditions {
28 public:
30 using PayloadMap = std::unordered_map<std::string, PayloadMetadata>;
31
41 public:
43 struct CacheEntry {
44 int exp{ -1};
45 int run{ -1};
47 };
55 std::tuple<bool, PayloadMap*> get(int exp, int run)
56 {
57 using std::get;
58 // if run mismatch swap current and last entry
59 if (current.exp != exp || current.run != run) std::swap(current, previous);
60 // still mismatch? if so clear, otherwise just return
61 const bool found = current.exp == exp and current.run == run;
62 if (!found) {
63 current.exp = exp;
64 current.run = run;
65 current.map.clear();
66 }
67 return {found, &current.map};
68 }
69 private:
72 };
73
75 MetadataProvider() = default;
77 explicit MetadataProvider(const std::set<std::string>& usableTagStates): m_payloads{nullptr}, m_usableTagStates(usableTagStates) {}
79 virtual ~MetadataProvider() = default;
86 bool setTags(const std::vector<std::string>& tags);
120 bool getPayloads(int exp, int run, std::vector<PayloadMetadata>& info);
122 std::set<std::string> getUsableTagStates() { return m_usableTagStates; }
128 void setUsableTagStates(const std::set<std::string>& states)
129 {
130 m_usableTagStates = states;
131 m_usableTagStates.erase("INVALID");
132 }
133 protected:
136 virtual std::string getGlobaltagStatus(const std::string& name) = 0;
152 virtual bool updatePayloads(const std::string& globaltag, int exp, int run) = 0;
162 void addPayload(PayloadMetadata&& payload, const std::string& messagePrefix = "");
167 void printInfoMessage(const std::string& provider);
168
170 std::vector<std::string> m_tags;
172 std::unordered_map<std::string, PayloadMetadataCache> m_cache;
176 std::set<std::string> m_usableTagStates{"TESTING", "VALIDATED", "RUNNING", "PUBLISHED"};
177 };
178
184 bool updatePayloads([[maybe_unused]] const std::string& globaltag, [[maybe_unused]] int exp,
185 [[maybe_unused]] int run) override
186 {
187 return false;
188 }
190 std::string getGlobaltagStatus(const std::string& name) override;
192 bool m_errorShown{false};
193 };
194} // namespace Belle2::Conditions
Simple caching structure to keep the payload information for this and the last exp/run.
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:
Base class for a payload metadata provider.
std::unordered_map< std::string, PayloadMetadataCache > m_cache
Map of globaltag->known metadata.
std::set< std::string > getUsableTagStates()
Get the valid tag states when checking globaltag status.
bool getPayloads(int exp, int run, std::vector< PayloadMetadata > &info)
Update the information in the vector of metadata instances with the actual values.
bool setTags(const std::vector< std::string > &tags)
Set the list of globaltag names to be considered for payloads.
virtual ~MetadataProvider()=default
Default destructible.
std::set< std::string > m_usableTagStates
Set of global tag states to consider valid (except for 'INVALID' which is always considered invalid)
virtual std::string getGlobaltagStatus(const std::string &name)=0
Check the status of a global tag with the given name.
std::vector< std::string > m_tags
List of globaltags to consider.
MetadataProvider()=default
Default constructible.
void printInfoMessage(const std::string &provider)
Print an INFO message about the used metadata provider.
void addPayload(PayloadMetadata &&payload, const std::string &messagePrefix="")
Add a payload information to the internal list.
MetadataProvider(const std::set< std::string > &usableTagStates)
Construct with a known set of usable global tag states.
std::unordered_map< std::string, PayloadMetadata > PayloadMap
Type for the internal list of payloads.
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.
PayloadMap * m_payloads
Map of known payloads for current conditions.
void setUsableTagStates(const std::set< std::string > &states)
Set the valid tag states for this provider when checking globaltag status.
Fallback provider if no providers are given: Will raise an error if used but allows processing if no ...
std::string getGlobaltagStatus(const std::string &name) override
When we get asked to check a global tag we raise an error ... but only once.
bool m_errorShown
Have we already shown the error?
bool updatePayloads(const std::string &globaltag, int exp, int run) override
Nope, no update.
PayloadMap map
Map of all known name -> PayloadMetadata entries.
Simple struct to group all information necessary for a single payload.