ParameterSetRegistry.h
Go to the documentation of this file.
1 #ifndef fhiclcpp_ParameterSetRegistry_h
2 #define fhiclcpp_ParameterSetRegistry_h
3 
4 // ======================================================================
5 //
6 // ParameterSetRegistry
7 //
8 // ======================================================================
9 
10 #include "fhiclcpp/ParameterSet.h"
12 #include "fhiclcpp/fwd.h"
13 #include "hep_concurrency/RecursiveMutex.h"
14 
15 #include "sqlite3.h"
16 
17 #include <unordered_map>
18 
19 namespace fhicl {
20 
21  class ParameterSetRegistry;
22 
23  namespace detail {
24  class HashParameterSetID;
25  void throwOnSQLiteFailure(int rc, char* msg = nullptr);
26  void throwOnSQLiteFailure(sqlite3* db, char* msg = nullptr);
27  }
28 }
29 
31 public:
32  size_t operator()(ParameterSetID const& id) const;
33 
34 private:
35  std::hash<std::string> hash_;
36 };
37 
39 public:
40  ParameterSetRegistry(ParameterSet const&) = delete;
42  ParameterSetRegistry& operator=(ParameterSet const&) = delete;
43  ParameterSetRegistry& operator=(ParameterSet&&) = delete;
45 
46  // Typedefs.
47  using collection_type = std::
48  unordered_map<ParameterSetID, ParameterSet, detail::HashParameterSetID>;
49  using key_type = typename collection_type::key_type;
50  using mapped_type = typename collection_type::mapped_type;
51  using value_type = typename collection_type::value_type;
52  using size_type = typename collection_type::size_type;
53  using const_iterator = typename collection_type::const_iterator;
54 
55  // DB interaction.
56  static void importFrom(sqlite3* db);
57  static void exportTo(sqlite3* db);
58  static void stageIn();
59 
60  // Observers.
61  static bool empty();
62  static size_type size();
63 
64  // Put:
65  // 1. A single ParameterSet.
66  static ParameterSetID const& put(ParameterSet const& ps);
67  // 2. A range of iterator to ParameterSet.
68  template <class FwdIt>
69  static std::enable_if_t<
70  std::is_same_v<typename std::iterator_traits<FwdIt>::value_type,
71  mapped_type>>
72  put(FwdIt begin, FwdIt end);
73  // 3. A range of iterator to pair<ParameterSetID, ParameterSet>. For
74  // each pair, first == second.id() is a prerequisite.
75  template <class FwdIt>
76  static std::enable_if_t<
77  std::is_same_v<typename std::iterator_traits<FwdIt>::value_type,
78  value_type>>
79  put(FwdIt begin, FwdIt end);
80  // 4. A collection_type. For each value_type, first == second.id() is
81  // a prerequisite.
82  static void put(collection_type const& c);
83 
84  // Accessors.
85  static collection_type const& get() noexcept;
86  static ParameterSet const& get(ParameterSetID const& id);
87  static bool get(ParameterSetID const& id, ParameterSet& ps);
88  static bool has(ParameterSetID const& id);
89 
90 private:
92  static ParameterSetRegistry& instance_();
93  const_iterator find_(ParameterSetID const& id);
94 
95  sqlite3* primaryDB_;
96  sqlite3_stmt* stmt_{nullptr};
97  collection_type registry_{};
98  static hep::concurrency::RecursiveMutex mutex_;
99 };
100 
101 inline bool
103 {
104  hep::concurrency::RecursiveMutexSentry sentry{mutex_, __func__};
105  return instance_().registry_.empty();
106 }
107 
108 inline auto
110 {
111  hep::concurrency::RecursiveMutexSentry sentry{mutex_, __func__};
112  return instance_().registry_.size();
113 }
114 
115 // 1.
116 inline auto
118  -> ParameterSetID const&
119 {
120  hep::concurrency::RecursiveMutexSentry sentry{mutex_, __func__};
121  return instance_().registry_.emplace(ps.id(), ps).first->first;
122 }
123 
124 // 2.
125 template <class FwdIt>
126 inline auto
127 fhicl::ParameterSetRegistry::put(FwdIt b, FwdIt const e) -> std::enable_if_t<
128  std::is_same_v<typename std::iterator_traits<FwdIt>::value_type, mapped_type>>
129 {
130  // No lock here -- it will be acquired by 3.
131  for (; b != e; ++b) {
132  (void)put(*b);
133  }
134 }
135 
136 // 3.
137 template <class FwdIt>
138 inline auto
139 fhicl::ParameterSetRegistry::put(FwdIt const b, FwdIt const e)
140  -> std::enable_if_t<
141  std::is_same_v<typename std::iterator_traits<FwdIt>::value_type,
142  value_type>>
143 {
144  hep::concurrency::RecursiveMutexSentry sentry{mutex_, __func__};
145  instance_().registry_.insert(b, e);
146 }
147 
148 // 4.
149 inline void
151 {
152  // No lock here -- it will be acquired by 3.
153  put(c.cbegin(), c.cend());
154 }
155 
156 inline auto
158 {
159  hep::concurrency::RecursiveMutexSentry sentry{mutex_, __func__};
160  return instance_().registry_;
161 }
162 
163 inline auto
165  -> ParameterSet const&
166 {
167  hep::concurrency::RecursiveMutexSentry sentry{mutex_, __func__};
168  auto it = instance_().find_(id);
169  if (it == instance_().registry_.cend()) {
170  throw exception(error::cant_find, "Can't find ParameterSet")
171  << "with ID " << id.to_string() << " in the registry.";
172  }
173  return it->second;
174 }
175 
176 inline bool
178 {
179  hep::concurrency::RecursiveMutexSentry sentry{mutex_, __func__};
180  bool result{false};
181  auto it = instance_().find_(id);
182  if (it != instance_().registry_.cend()) {
183  ps = it->second;
184  result = true;
185  }
186  return result;
187 }
188 
189 inline bool
191 {
192  hep::concurrency::RecursiveMutexSentry sentry{mutex_, __func__};
193  auto const& reg = instance_().registry_;
194  return reg.find(id) != reg.cend();
195 }
196 
197 inline auto
199 {
200  static ParameterSetRegistry s_registry;
201  return s_registry;
202 }
203 
204 inline size_t
206 {
207  return hash_(id.to_string());
208 }
209 
210 #endif /* fhiclcpp_ParameterSetRegistry_h */
211 
212 // Local Variables:
213 // mode: c++
214 // End:
::xsd::cxx::tree::id< char, ncname > id
Definition: Database.h:165
typename collection_type::size_type size_type
static ParameterSetID const & put(ParameterSet const &ps)
set< int >::iterator it
static collection_type const & get() noexcept
std::string to_string(Protection p)
typename collection_type::mapped_type mapped_type
size_t operator()(ParameterSetID const &id) const
static bool has(ParameterSetID const &id)
static hep::concurrency::RecursiveMutex mutex_
typename collection_type::const_iterator const_iterator
const hit & b
Definition: hits.cxx:21
void throwOnSQLiteFailure(int rc, char *msg=nullptr)
typename collection_type::value_type value_type
Float_t e
Definition: plot.C:35
typedef void(XMLCALL *XML_ElementDeclHandler)(void *userData
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33
typename collection_type::key_type key_type
static ParameterSetRegistry & instance_()
std::unordered_map< ParameterSetID, ParameterSet, detail::HashParameterSetID > collection_type