Public Types | Public Member Functions | Static Public Member Functions | Private Member Functions | Static Private Member Functions | Private Attributes | Static Private Attributes | List of all members
fhicl::ParameterSetRegistry Class Reference

#include <ParameterSetRegistry.h>

Public Types

using collection_type = std::unordered_map< ParameterSetID, ParameterSet, detail::HashParameterSetID >
 
using key_type = typename collection_type::key_type
 
using mapped_type = typename collection_type::mapped_type
 
using value_type = typename collection_type::value_type
 
using size_type = typename collection_type::size_type
 
using const_iterator = typename collection_type::const_iterator
 

Public Member Functions

 ParameterSetRegistry (ParameterSet const &)=delete
 
 ParameterSetRegistry (ParameterSet &&)=delete
 
ParameterSetRegistryoperator= (ParameterSet const &)=delete
 
ParameterSetRegistryoperator= (ParameterSet &&)=delete
 
 ~ParameterSetRegistry ()
 
template<class FwdIt >
auto put (FwdIt b, FwdIt const e) -> std::enable_if_t< std::is_same_v< typename std::iterator_traits< FwdIt >::value_type, mapped_type >>
 

Static Public Member Functions

static void importFrom (sqlite3 *db)
 
static void exportTo (sqlite3 *db)
 
static void stageIn ()
 
static bool empty ()
 
static size_type size ()
 
static ParameterSetID const & put (ParameterSet const &ps)
 
template<class FwdIt >
static std::enable_if_t< std::is_same_v< typename std::iterator_traits< FwdIt >::value_type, mapped_type > > put (FwdIt begin, FwdIt end)
 
template<class FwdIt >
static std::enable_if_t< std::is_same_v< typename std::iterator_traits< FwdIt >::value_type, value_type > > put (FwdIt begin, FwdIt end)
 
static void put (collection_type const &c)
 
static collection_type const & get () noexcept
 
static ParameterSet const & get (ParameterSetID const &id)
 
static bool get (ParameterSetID const &id, ParameterSet &ps)
 
static bool has (ParameterSetID const &id)
 

Private Member Functions

 ParameterSetRegistry ()
 
const_iterator find_ (ParameterSetID const &id)
 

Static Private Member Functions

static ParameterSetRegistryinstance_ ()
 

Private Attributes

sqlite3primaryDB_
 
sqlite3_stmtstmt_ {nullptr}
 
collection_type registry_ {}
 

Static Private Attributes

static std::recursive_mutex mutex_ {}
 

Detailed Description

Definition at line 46 of file ParameterSetRegistry.h.

Member Typedef Documentation

Definition at line 56 of file ParameterSetRegistry.h.

using fhicl::ParameterSetRegistry::const_iterator = typename collection_type::const_iterator

Definition at line 61 of file ParameterSetRegistry.h.

using fhicl::ParameterSetRegistry::key_type = typename collection_type::key_type

Definition at line 57 of file ParameterSetRegistry.h.

using fhicl::ParameterSetRegistry::mapped_type = typename collection_type::mapped_type

Definition at line 58 of file ParameterSetRegistry.h.

using fhicl::ParameterSetRegistry::size_type = typename collection_type::size_type

Definition at line 60 of file ParameterSetRegistry.h.

using fhicl::ParameterSetRegistry::value_type = typename collection_type::value_type

Definition at line 59 of file ParameterSetRegistry.h.

Constructor & Destructor Documentation

fhicl::ParameterSetRegistry::ParameterSetRegistry ( ParameterSet const &  )
delete
fhicl::ParameterSetRegistry::ParameterSetRegistry ( ParameterSet &&  )
delete
fhicl::ParameterSetRegistry::~ParameterSetRegistry ( )

Definition at line 65 of file ParameterSetRegistry.cc.

66 {
67  sqlite3_finalize(stmt_);
68  try {
70  }
71  catch (fhicl::exception const& e) {
72  std::cerr << e.what() << '\n';
73  }
74  catch (...) {
75  }
76  int retcode;
77  do {
78  retcode = sqlite3_close(primaryDB_);
79  } while (retcode == SQLITE_BUSY);
80 }
const double e
void throwOnSQLiteFailure(int rc, char *msg=nullptr)
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33
fhicl::ParameterSetRegistry::ParameterSetRegistry ( )
private

Definition at line 209 of file ParameterSetRegistry.cc.

210  : primaryDB_{openPrimaryDB()}
211 {}

Member Function Documentation

bool fhicl::ParameterSetRegistry::empty ( )
inlinestatic

Definition at line 110 of file ParameterSetRegistry.h.

111 {
112  std::lock_guard sentry{mutex_};
113  return instance_().registry_.empty();
114 }
static std::recursive_mutex mutex_
static ParameterSetRegistry & instance_()
void fhicl::ParameterSetRegistry::exportTo ( sqlite3 db)
static

Definition at line 127 of file ParameterSetRegistry.cc.

128 {
129  assert(db);
130  std::lock_guard sentry{mutex_};
131 
132  cet::sqlite::Transaction txn{db};
134  "DROP TABLE IF EXISTS ParameterSets;"
135  "CREATE TABLE ParameterSets(ID PRIMARY KEY, PSetBlob);");
136  txn.commit();
137 
138  sqlite3_stmt* oStmt = nullptr;
139  sqlite3_prepare_v2(
140  db,
141  "INSERT OR IGNORE INTO ParameterSets(ID, PSetBlob) VALUES(?, ?);",
142  -1,
143  &oStmt,
144  nullptr);
146  for (auto const& p : instance_().registry_) {
147  std::string id(p.first.to_string());
148  std::string psBlob(p.second.to_compact_string());
149  sqlite3_bind_text(oStmt, 1, id.c_str(), id.size() + 1, SQLITE_STATIC);
151  sqlite3_bind_text(
152  oStmt, 2, psBlob.c_str(), psBlob.size() + 1, SQLITE_STATIC);
154  switch (sqlite3_step(oStmt)) {
155  case SQLITE_DONE:
156  sqlite3_reset(oStmt);
158  break; // OK
159  default:
161  }
162  }
163 
164  sqlite3* const primaryDB{instance_().primaryDB_};
165  using namespace cet::sqlite;
167  regPSes << select("*").from(primaryDB, "ParameterSets");
168 
169  for (auto const& [idString, psBlob] : regPSes) {
170  sqlite3_bind_text(
171  oStmt, 1, idString.c_str(), idString.size() + 1, SQLITE_STATIC);
173  sqlite3_bind_text(
174  oStmt, 2, psBlob.c_str(), psBlob.size() + 1, SQLITE_STATIC);
176  switch (sqlite3_step(oStmt)) {
177  case SQLITE_DONE:
178  sqlite3_reset(oStmt);
180  break; // OK
181  default:
183  }
184  }
185  sqlite3_finalize(oStmt);
187 }
std::string string
Definition: nybbler.cc:12
struct sqlite3_stmt sqlite3_stmt
p
Definition: test.py:223
auto select(T const &...t)
Definition: select.h:146
static std::recursive_mutex mutex_
struct sqlite3 sqlite3
void throwOnSQLiteFailure(int rc, char *msg=nullptr)
void exec(sqlite3 *db, std::string const &ddl)
Definition: exec.cc:5
static ParameterSetRegistry & instance_()
auto fhicl::ParameterSetRegistry::find_ ( ParameterSetID const &  id)
private

Definition at line 214 of file ParameterSetRegistry.cc.

215 {
216  // No lock here -- it was already acquired by get(...).
217  auto it = registry_.find(id);
218  if (it == registry_.cend()) {
219  // Look in primary DB for this ID and its contained IDs.
220  if (stmt_ == nullptr) {
221  sqlite3_prepare_v2(primaryDB_,
222  "SELECT PSetBlob FROM ParameterSets WHERE ID = ?;",
223  -1,
224  &stmt_,
225  nullptr);
227  }
228  auto idString = id.to_string();
229  auto result = sqlite3_bind_text(
230  stmt_, 1, idString.c_str(), idString.size() + 1, SQLITE_STATIC);
232  result = sqlite3_step(stmt_);
233  switch (result) {
234  case SQLITE_ROW: // Found the ID in the DB.
235  {
236  auto const pset = ParameterSet::make(
237  reinterpret_cast<char const*>(sqlite3_column_text(stmt_, 0)));
238  // Put into the registry without triggering ParameterSet::id().
239  it = registry_.emplace(id, pset).first;
240  } break;
241  case SQLITE_DONE:
242  break; // Not here.
243  default:
245  }
246  sqlite3_reset(stmt_);
247  }
248  return it;
249 }
static QCString result
static ParameterSet make(intermediate_table const &tbl)
Definition: ParameterSet.cc:68
void throwOnSQLiteFailure(int rc, char *msg=nullptr)
auto fhicl::ParameterSetRegistry::get ( )
inlinestaticnoexcept

Definition at line 165 of file ParameterSetRegistry.h.

166 {
167  std::lock_guard sentry{mutex_};
168  return instance_().registry_;
169 }
static std::recursive_mutex mutex_
static ParameterSetRegistry & instance_()
auto fhicl::ParameterSetRegistry::get ( ParameterSetID const &  id)
inlinestatic

Definition at line 172 of file ParameterSetRegistry.h.

174 {
175  std::lock_guard sentry{mutex_};
176  auto it = instance_().find_(id);
177  if (it == instance_().registry_.cend()) {
178  throw exception(error::cant_find, "Can't find ParameterSet")
179  << "with ID " << id.to_string() << " in the registry.";
180  }
181  return it->second;
182 }
static std::recursive_mutex mutex_
const_iterator find_(ParameterSetID const &id)
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33
static ParameterSetRegistry & instance_()
bool fhicl::ParameterSetRegistry::get ( ParameterSetID const &  id,
ParameterSet ps 
)
inlinestatic

Definition at line 185 of file ParameterSetRegistry.h.

186 {
187  std::lock_guard sentry{mutex_};
188  bool result{false};
189  auto it = instance_().find_(id);
190  if (it != instance_().registry_.cend()) {
191  ps = it->second;
192  result = true;
193  }
194  return result;
195 }
static QCString result
static constexpr double ps
Definition: Units.h:99
static std::recursive_mutex mutex_
const_iterator find_(ParameterSetID const &id)
static ParameterSetRegistry & instance_()
bool fhicl::ParameterSetRegistry::has ( ParameterSetID const &  id)
inlinestatic

Definition at line 198 of file ParameterSetRegistry.h.

199 {
200  std::lock_guard sentry{mutex_};
201  auto const& reg = instance_().registry_;
202  return reg.find(id) != reg.cend();
203 }
static const_iterator cend()
static std::recursive_mutex mutex_
static ParameterSetRegistry & instance_()
void fhicl::ParameterSetRegistry::importFrom ( sqlite3 db)
static

Definition at line 83 of file ParameterSetRegistry.cc.

84 {
85  assert(db);
86  std::lock_guard sentry{mutex_};
87 
88  // This does *not* cause anything new to be imported into the
89  // registry itself, just its backing DB.
90  sqlite3_stmt* oStmt = nullptr;
91  sqlite3* primaryDB = instance_().primaryDB_;
92 
93  // Index constraint on ID will prevent duplicates via INSERT OR IGNORE.
94  sqlite3_prepare_v2(
95  primaryDB,
96  "INSERT OR IGNORE INTO ParameterSets(ID, PSetBlob) VALUES(?, ?);",
97  -1,
98  &oStmt,
99  nullptr);
100  throwOnSQLiteFailure(primaryDB);
101 
102  using namespace cet::sqlite;
104  inputPSes << select("*").from(db, "ParameterSets");
105 
106  for (auto const& [idString, psBlob] : inputPSes) {
107  sqlite3_bind_text(
108  oStmt, 1, idString.c_str(), idString.size() + 1, SQLITE_STATIC);
109  throwOnSQLiteFailure(primaryDB);
110  sqlite3_bind_text(
111  oStmt, 2, psBlob.c_str(), psBlob.size() + 1, SQLITE_STATIC);
112  throwOnSQLiteFailure(primaryDB);
113  switch (sqlite3_step(oStmt)) {
114  case SQLITE_DONE:
115  break; // OK
116  default:
117  throwOnSQLiteFailure(primaryDB);
118  }
119  sqlite3_reset(oStmt);
120  throwOnSQLiteFailure(primaryDB);
121  }
122  sqlite3_finalize(oStmt);
123  throwOnSQLiteFailure(primaryDB);
124 }
struct sqlite3_stmt sqlite3_stmt
auto select(T const &...t)
Definition: select.h:146
static std::recursive_mutex mutex_
struct sqlite3 sqlite3
void throwOnSQLiteFailure(int rc, char *msg=nullptr)
static ParameterSetRegistry & instance_()
auto fhicl::ParameterSetRegistry::instance_ ( )
inlinestaticprivate

Definition at line 206 of file ParameterSetRegistry.h.

207 {
208  static ParameterSetRegistry s_registry;
209  return s_registry;
210 }
ParameterSetRegistry& fhicl::ParameterSetRegistry::operator= ( ParameterSet const &  )
delete
ParameterSetRegistry& fhicl::ParameterSetRegistry::operator= ( ParameterSet &&  )
delete
auto fhicl::ParameterSetRegistry::put ( ParameterSet const &  ps)
inlinestatic

Definition at line 125 of file ParameterSetRegistry.h.

127 {
128  std::lock_guard sentry{mutex_};
129  return instance_().registry_.emplace(ps.id(), ps).first->first;
130 }
static constexpr double ps
Definition: Units.h:99
static std::recursive_mutex mutex_
static ParameterSetRegistry & instance_()
template<class FwdIt >
static std::enable_if_t< std::is_same_v<typename std::iterator_traits<FwdIt>::value_type, mapped_type> > fhicl::ParameterSetRegistry::put ( FwdIt  begin,
FwdIt  end 
)
static
template<class FwdIt >
static std::enable_if_t< std::is_same_v<typename std::iterator_traits<FwdIt>::value_type, value_type> > fhicl::ParameterSetRegistry::put ( FwdIt  begin,
FwdIt  end 
)
static
void fhicl::ParameterSetRegistry::put ( collection_type const &  c)
inlinestatic

Definition at line 158 of file ParameterSetRegistry.h.

159 {
160  // No lock here -- it will be acquired by 3.
161  put(c.cbegin(), c.cend());
162 }
static ParameterSetID const & put(ParameterSet const &ps)
template<class FwdIt >
auto fhicl::ParameterSetRegistry::put ( FwdIt  b,
FwdIt const  e 
) -> std::enable_if_t< std::is_same_v<typename std::iterator_traits<FwdIt>::value_type, mapped_type>>
inline

Definition at line 135 of file ParameterSetRegistry.h.

137 {
138  // No lock here -- it will be acquired by 3.
139  for (; b != e; ++b) {
140  (void)put(*b);
141  }
142 }
static ParameterSetID const & put(ParameterSet const &ps)
const double e
static bool * b
Definition: config.cpp:1043
auto fhicl::ParameterSetRegistry::size ( )
inlinestatic

Definition at line 117 of file ParameterSetRegistry.h.

118 {
119  std::lock_guard sentry{mutex_};
120  return instance_().registry_.size();
121 }
static std::recursive_mutex mutex_
static ParameterSetRegistry & instance_()
void fhicl::ParameterSetRegistry::stageIn ( )
static

Definition at line 190 of file ParameterSetRegistry.cc.

191 {
192  std::lock_guard sentry{mutex_};
193 
194  sqlite3* primaryDB = instance_().primaryDB_;
195  auto& registry = instance_().registry_;
196  using namespace cet::sqlite;
198  entriesToStageIn << select("*").from(primaryDB, "ParameterSets");
199 
200  cet::transform_all(entriesToStageIn,
201  std::inserter(registry, std::begin(registry)),
202  [](auto const& row) {
203  auto const& [idString, psBlob] = row;
204  auto const pset = ParameterSet::make(psBlob);
205  return std::make_pair(ParameterSetID{idString}, pset);
206  });
207 }
static ParameterSet make(intermediate_table const &tbl)
Definition: ParameterSet.cc:68
auto transform_all(Container &, OutputIt, UnaryOp)
auto select(T const &...t)
Definition: select.h:146
static std::recursive_mutex mutex_
struct sqlite3 sqlite3
decltype(auto) constexpr begin(T &&obj)
ADL-aware version of std::begin.
Definition: StdUtils.h:72
static ParameterSetRegistry & instance_()

Member Data Documentation

std::recursive_mutex fhicl::ParameterSetRegistry::mutex_ {}
staticprivate

Definition at line 106 of file ParameterSetRegistry.h.

sqlite3* fhicl::ParameterSetRegistry::primaryDB_
private

Definition at line 103 of file ParameterSetRegistry.h.

collection_type fhicl::ParameterSetRegistry::registry_ {}
private

Definition at line 105 of file ParameterSetRegistry.h.

sqlite3_stmt* fhicl::ParameterSetRegistry::stmt_ {nullptr}
private

Definition at line 104 of file ParameterSetRegistry.h.


The documentation for this class was generated from the following files: