LArSoft  v09_90_00
Liquid Argon Software toolkit - https://larsoft.org/
fhicl::ParameterSetRegistry Class Reference

#include "ParameterSetRegistry.h"

Public Types

using collection_type = std::unordered_map< ParameterSetID, ParameterSet, detail::HashParameterSetID >
 
using key_type = collection_type::key_type
 
using mapped_type = collection_type::mapped_type
 
using value_type = collection_type::value_type
 
using size_type = collection_type::size_type
 
using const_iterator = 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

sqlite3 * primaryDB_
 
sqlite3_stmt * stmt_ {nullptr}
 
collection_type registry_ {}
 

Static Private Attributes

static std::recursive_mutex mutex_ {}
 

Detailed Description

Definition at line 40 of file ParameterSetRegistry.h.

Member Typedef Documentation

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

Definition at line 55 of file ParameterSetRegistry.h.

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

Definition at line 51 of file ParameterSetRegistry.h.

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

Definition at line 52 of file ParameterSetRegistry.h.

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

Definition at line 54 of file ParameterSetRegistry.h.

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

Definition at line 53 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.

References e, and fhicl::detail::throwOnSQLiteFailure().

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 }
void throwOnSQLiteFailure(int rc, char *msg=nullptr)
Float_t e
Definition: plot.C:35
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33
fhicl::ParameterSetRegistry::ParameterSetRegistry ( )
private

Definition at line 224 of file ParameterSetRegistry.cc.

225  : primaryDB_{openPrimaryDB()}
226 {}

Member Function Documentation

bool fhicl::ParameterSetRegistry::empty ( )
inlinestatic

Definition at line 104 of file ParameterSetRegistry.h.

105 {
106  std::lock_guard sentry{mutex_};
107  return instance_().registry_.empty();
108 }
static std::recursive_mutex mutex_
static ParameterSetRegistry & instance_()
void fhicl::ParameterSetRegistry::exportTo ( sqlite3 *  db)
static

Definition at line 127 of file ParameterSetRegistry.cc.

References util::begin(), util::end(), util::size(), and fhicl::detail::throwOnSQLiteFailure().

Referenced by fhicl::parse_file_and_fill_db().

128 {
129  assert(db);
130  std::lock_guard sentry{mutex_};
131 
132  cet::sqlite::Transaction txn{db};
133  cet::sqlite::exec(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 
147  // Calling to_compact_string() can insert entries into the registry
148  // container. If that insertion causes a rehash, the iterators are
149  // invalidated. We therefore need to start over in that case and
150  // make sure we can get through the entire list without causing a
151  // rehash.
152  while (std::any_of(get().begin(), get().end(), [oStmt, db](auto const& p) {
153  // The rehash threshold is defined by the STL
154  // [container.requirements.unord.req.general]
155  auto const current_rehash_threshold =
156  get().max_load_factor() * get().bucket_count();
157  std::string id(p.first.to_string());
158  std::string psBlob(p.second.to_compact_string());
159  if (get().size() > current_rehash_threshold) {
160  return true;
161  }
162  sqlite3_bind_text(oStmt, 1, id.c_str(), id.size() + 1, SQLITE_STATIC);
164  sqlite3_bind_text(
165  oStmt, 2, psBlob.c_str(), psBlob.size() + 1, SQLITE_STATIC);
167  switch (sqlite3_step(oStmt)) {
168  case SQLITE_DONE:
169  sqlite3_reset(oStmt);
171  break; // OK
172  default:
174  }
175  return false;
176  })) {
177  }
178 
179  sqlite3* const primaryDB{instance_().primaryDB_};
180  using namespace cet::sqlite;
181  query_result<std::string, std::string> regPSes;
182  regPSes << select("*").from(primaryDB, "ParameterSets");
183 
184  for (auto const& [idString, psBlob] : regPSes) {
185  sqlite3_bind_text(
186  oStmt, 1, idString.c_str(), idString.size() + 1, SQLITE_STATIC);
188  sqlite3_bind_text(
189  oStmt, 2, psBlob.c_str(), psBlob.size() + 1, SQLITE_STATIC);
191  switch (sqlite3_step(oStmt)) {
192  case SQLITE_DONE:
193  sqlite3_reset(oStmt);
195  break; // OK
196  default:
198  }
199  }
200  sqlite3_finalize(oStmt);
202 }
decltype(auto) constexpr end(T &&obj)
ADL-aware version of std::end.
Definition: StdUtils.h:77
static std::recursive_mutex mutex_
void throwOnSQLiteFailure(int rc, char *msg=nullptr)
decltype(auto) constexpr begin(T &&obj)
ADL-aware version of std::begin.
Definition: StdUtils.h:69
static ParameterSetRegistry & instance_()
auto fhicl::ParameterSetRegistry::find_ ( ParameterSetID const &  id)
private

Definition at line 229 of file ParameterSetRegistry.cc.

References fhicl::ParameterSet::make(), primaryDB_, registry_, stmt_, and fhicl::detail::throwOnSQLiteFailure().

230 {
231  // No lock here -- it was already acquired by get(...).
232  auto it = registry_.find(id);
233  if (it == registry_.cend()) {
234  // Look in primary DB for this ID and its contained IDs.
235  if (stmt_ == nullptr) {
236  sqlite3_prepare_v2(primaryDB_,
237  "SELECT PSetBlob FROM ParameterSets WHERE ID = ?;",
238  -1,
239  &stmt_,
240  nullptr);
242  }
243  auto idString = id.to_string();
244  auto result = sqlite3_bind_text(
245  stmt_, 1, idString.c_str(), idString.size() + 1, SQLITE_STATIC);
247  result = sqlite3_step(stmt_);
248  switch (result) {
249  case SQLITE_ROW: // Found the ID in the DB.
250  {
251  auto const pset = ParameterSet::make(
252  reinterpret_cast<char const*>(sqlite3_column_text(stmt_, 0)));
253  // Put into the registry without triggering ParameterSet::id().
254  it = registry_.emplace(id, pset).first;
255  } break;
256  case SQLITE_DONE:
257  break; // Not here.
258  default:
260  }
261  sqlite3_reset(stmt_);
262  }
263  return it;
264 }
static ParameterSet make(intermediate_table const &tbl)
Definition: ParameterSet.cc:68
void throwOnSQLiteFailure(int rc, char *msg=nullptr)
auto fhicl::ParameterSetRegistry::get ( )
inlinestaticnoexcept
auto fhicl::ParameterSetRegistry::get ( ParameterSetID const &  id)
inlinestatic

Definition at line 166 of file ParameterSetRegistry.h.

References fhicl::cant_find.

168 {
169  std::lock_guard sentry{mutex_};
170  auto it = instance_().find_(id);
171  if (it == instance_().registry_.cend()) {
172  throw exception(error::cant_find, "Can't find ParameterSet")
173  << "with ID " << id.to_string() << " in the registry.";
174  }
175  return it->second;
176 }
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 179 of file ParameterSetRegistry.h.

180 {
181  std::lock_guard sentry{mutex_};
182  bool result{false};
183  auto it = instance_().find_(id);
184  if (it != instance_().registry_.cend()) {
185  ps = it->second;
186  result = true;
187  }
188  return result;
189 }
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 192 of file ParameterSetRegistry.h.

193 {
194  std::lock_guard sentry{mutex_};
195  auto const& reg = instance_().registry_;
196  return reg.find(id) != reg.cend();
197 }
static std::recursive_mutex mutex_
static ParameterSetRegistry & instance_()
void fhicl::ParameterSetRegistry::importFrom ( sqlite3 *  db)
static

Definition at line 83 of file ParameterSetRegistry.cc.

References fhicl::detail::throwOnSQLiteFailure().

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;
103  query_result<std::string, std::string> inputPSes;
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 }
static std::recursive_mutex mutex_
void throwOnSQLiteFailure(int rc, char *msg=nullptr)
static ParameterSetRegistry & instance_()
auto fhicl::ParameterSetRegistry::instance_ ( )
inlinestaticprivate

Definition at line 200 of file ParameterSetRegistry.h.

201 {
202  static ParameterSetRegistry s_registry;
203  return s_registry;
204 }
ParameterSetRegistry& fhicl::ParameterSetRegistry::operator= ( ParameterSet const &  )
delete
ParameterSetRegistry& fhicl::ParameterSetRegistry::operator= ( ParameterSet &&  )
delete
auto fhicl::ParameterSetRegistry::put ( ParameterSet const &  ps)
inlinestatic

Definition at line 119 of file ParameterSetRegistry.h.

Referenced by fhicl::detail::encode(), fhicl::parse_file_and_fill_db(), art::PathManager::PathManager(), put(), and art::run_art().

121 {
122  std::lock_guard sentry{mutex_};
123  return instance_().registry_.emplace(ps.id(), ps).first->first;
124 }
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 152 of file ParameterSetRegistry.h.

153 {
154  // No lock here -- it will be acquired by 3.
155  put(c.cbegin(), c.cend());
156 }
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 129 of file ParameterSetRegistry.h.

References e, and put().

131 {
132  // No lock here -- it will be acquired by 3.
133  for (; b != e; ++b) {
134  (void)put(*b);
135  }
136 }
static ParameterSetID const & put(ParameterSet const &ps)
Float_t e
Definition: plot.C:35
auto fhicl::ParameterSetRegistry::size ( void  )
inlinestatic

Definition at line 111 of file ParameterSetRegistry.h.

112 {
113  std::lock_guard sentry{mutex_};
114  return instance_().registry_.size();
115 }
static std::recursive_mutex mutex_
static ParameterSetRegistry & instance_()
void fhicl::ParameterSetRegistry::stageIn ( )
static

Definition at line 205 of file ParameterSetRegistry.cc.

References util::begin(), and art::InputSourceFactory::make().

206 {
207  std::lock_guard sentry{mutex_};
208 
209  sqlite3* primaryDB = instance_().primaryDB_;
210  auto& registry = instance_().registry_;
211  using namespace cet::sqlite;
212  query_result<std::string, std::string> entriesToStageIn;
213  entriesToStageIn << select("*").from(primaryDB, "ParameterSets");
214 
215  cet::transform_all(entriesToStageIn,
216  std::inserter(registry, std::begin(registry)),
217  [](auto const& row) {
218  auto const& [idString, psBlob] = row;
219  auto const pset = ParameterSet::make(psBlob);
220  return std::make_pair(ParameterSetID{idString}, pset);
221  });
222 }
static ParameterSet make(intermediate_table const &tbl)
Definition: ParameterSet.cc:68
static std::recursive_mutex mutex_
decltype(auto) constexpr begin(T &&obj)
ADL-aware version of std::begin.
Definition: StdUtils.h:69
static ParameterSetRegistry & instance_()

Member Data Documentation

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

Definition at line 100 of file ParameterSetRegistry.h.

sqlite3* fhicl::ParameterSetRegistry::primaryDB_
private

Definition at line 97 of file ParameterSetRegistry.h.

Referenced by find_().

collection_type fhicl::ParameterSetRegistry::registry_ {}
private

Definition at line 99 of file ParameterSetRegistry.h.

Referenced by find_().

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

Definition at line 98 of file ParameterSetRegistry.h.

Referenced by find_().


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