LArSoft  v06_85_00
Liquid Argon Software toolkit - http://larsoft.org/
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< typename std::iterator_traits< FwdIt >::value_type, mapped_type >::value >
 

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< typename std::iterator_traits< FwdIt >::value_type, mapped_type >::value > put (FwdIt begin, FwdIt end)
 
template<class FwdIt >
static std::enable_if_t< std::is_same< typename std::iterator_traits< FwdIt >::value_type, value_type >::value > 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 37 of file ParameterSetRegistry.h.

Member Typedef Documentation

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

Definition at line 52 of file ParameterSetRegistry.h.

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

Definition at line 48 of file ParameterSetRegistry.h.

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

Definition at line 49 of file ParameterSetRegistry.h.

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

Definition at line 51 of file ParameterSetRegistry.h.

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

Definition at line 50 of file ParameterSetRegistry.h.

Constructor & Destructor Documentation

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

Definition at line 50 of file ParameterSetRegistry.cc.

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

51 {
52  sqlite3_finalize(stmt_);
53  try {
55  }
56  catch (fhicl::exception const& e) {
57  std::cerr << e.what() << '\n';
58  }
59  catch (...) {
60  }
61  int retcode;
62  do {
63  retcode = sqlite3_close(primaryDB_);
64  } while (retcode == SQLITE_BUSY);
65 }
Float_t e
Definition: plot.C:34
void throwOnSQLiteFailure(sqlite3 *db, char *msg=nullptr)
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33
fhicl::ParameterSetRegistry::ParameterSetRegistry ( )
private

Definition at line 204 of file ParameterSetRegistry.cc.

References mutex_.

205  : primaryDB_{openPrimaryDB()}
206 {}

Member Function Documentation

bool fhicl::ParameterSetRegistry::empty ( void  )
inlinestatic

Definition at line 101 of file ParameterSetRegistry.h.

102 {
103  std::lock_guard<decltype(mutex_)> lock{mutex_};
104  return instance_().registry_.empty();
105 }
static std::recursive_mutex mutex_
static ParameterSetRegistry & instance_()
void fhicl::ParameterSetRegistry::exportTo ( sqlite3 *  db)
static

Definition at line 116 of file ParameterSetRegistry.cc.

References fhicl::detail::throwOnSQLiteFailure().

Referenced by fhicl::parse_file_and_fill_db(), and art::RootOutputFile::writeParameterSetRegistry().

117 {
118  assert(db);
119  std::lock_guard<decltype(mutex_)> lock{mutex_};
120 
121  cet::sqlite::Transaction txn{db};
122  cet::sqlite::exec(db,
123  "DROP TABLE IF EXISTS ParameterSets;"
124  "CREATE TABLE ParameterSets(ID PRIMARY KEY, PSetBlob);");
125  txn.commit();
126 
127  sqlite3_stmt* oStmt = nullptr;
128  sqlite3_prepare_v2(
129  db,
130  "INSERT OR IGNORE INTO ParameterSets(ID, PSetBlob) VALUES(?, ?);",
131  -1,
132  &oStmt,
133  nullptr);
135  for (auto const& p : instance_().registry_) {
136  std::string id(p.first.to_string());
137  std::string psBlob(p.second.to_compact_string());
138  sqlite3_bind_text(oStmt, 1, id.c_str(), id.size() + 1, SQLITE_STATIC);
140  sqlite3_bind_text(
141  oStmt, 2, psBlob.c_str(), psBlob.size() + 1, SQLITE_STATIC);
143  switch (sqlite3_step(oStmt)) {
144  case SQLITE_DONE:
145  sqlite3_reset(oStmt);
147  break; // OK
148  default:
150  }
151  }
152 
153  sqlite3* const primaryDB{instance_().primaryDB_};
154  using namespace cet::sqlite;
155  query_result<std::string, std::string> regPSes;
156  regPSes << select("*").from(primaryDB, "ParameterSets");
157 
158  for (auto const& row : regPSes) {
159  std::string idString;
160  std::string psBlob;
161  std::tie(idString, psBlob) = row;
162  sqlite3_bind_text(
163  oStmt, 1, idString.c_str(), idString.size() + 1, SQLITE_STATIC);
165  sqlite3_bind_text(
166  oStmt, 2, psBlob.c_str(), psBlob.size() + 1, SQLITE_STATIC);
168  switch (sqlite3_step(oStmt)) {
169  case SQLITE_DONE:
170  sqlite3_reset(oStmt);
172  break; // OK
173  default:
175  }
176  }
177  sqlite3_finalize(oStmt);
179 }
static std::recursive_mutex mutex_
void throwOnSQLiteFailure(sqlite3 *db, char *msg=nullptr)
static ParameterSetRegistry & instance_()
auto fhicl::ParameterSetRegistry::find_ ( ParameterSetID const &  id)
private

Definition at line 211 of file ParameterSetRegistry.cc.

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

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

Definition at line 164 of file ParameterSetRegistry.h.

References fhicl::cant_find.

166 {
167  std::lock_guard<decltype(mutex_)> lock{mutex_};
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 }
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 177 of file ParameterSetRegistry.h.

178 {
179  std::lock_guard<decltype(mutex_)> lock{mutex_};
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 }
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 190 of file ParameterSetRegistry.h.

191 {
192  std::lock_guard<decltype(mutex_)> lock{mutex_};
193  auto const& reg = instance_().registry_;
194  return reg.find(id) != reg.cend();
195 }
static std::recursive_mutex mutex_
static ParameterSetRegistry & instance_()
void fhicl::ParameterSetRegistry::importFrom ( sqlite3 *  db)
static

Definition at line 68 of file ParameterSetRegistry.cc.

References fhicl::detail::throwOnSQLiteFailure().

Referenced by read_all_parameter_sets().

69 {
70  assert(db);
71  std::lock_guard<decltype(mutex_)> lock{mutex_};
72 
73  // This does *not* cause anything new to be imported into the
74  // registry itself, just its backing DB.
75  sqlite3_stmt* oStmt = nullptr;
76  sqlite3* primaryDB = instance_().primaryDB_;
77 
78  // Index constraint on ID will prevent duplicates via INSERT OR IGNORE.
79  sqlite3_prepare_v2(
80  primaryDB,
81  "INSERT OR IGNORE INTO ParameterSets(ID, PSetBlob) VALUES(?, ?);",
82  -1,
83  &oStmt,
84  nullptr);
85  throwOnSQLiteFailure(primaryDB);
86 
87  using namespace cet::sqlite;
88  query_result<std::string, std::string> inputPSes;
89  inputPSes << select("*").from(db, "ParameterSets");
90 
91  for (auto const& row : inputPSes) {
92  std::string idString;
93  std::string psBlob;
94  std::tie(idString, psBlob) = row;
95 
96  sqlite3_bind_text(
97  oStmt, 1, idString.c_str(), idString.size() + 1, SQLITE_STATIC);
98  throwOnSQLiteFailure(primaryDB);
99  sqlite3_bind_text(
100  oStmt, 2, psBlob.c_str(), psBlob.size() + 1, SQLITE_STATIC);
101  throwOnSQLiteFailure(primaryDB);
102  switch (sqlite3_step(oStmt)) {
103  case SQLITE_DONE:
104  break; // OK
105  default:
106  throwOnSQLiteFailure(primaryDB);
107  }
108  sqlite3_reset(oStmt);
109  throwOnSQLiteFailure(primaryDB);
110  }
111  sqlite3_finalize(oStmt);
112  throwOnSQLiteFailure(primaryDB);
113 }
static std::recursive_mutex mutex_
void throwOnSQLiteFailure(sqlite3 *db, char *msg=nullptr)
static ParameterSetRegistry & instance_()
auto fhicl::ParameterSetRegistry::instance_ ( )
inlinestaticprivate

Definition at line 198 of file ParameterSetRegistry.h.

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

Definition at line 116 of file ParameterSetRegistry.h.

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

118 {
119  std::lock_guard<decltype(mutex_)> lock{mutex_};
120  return instance_().registry_.emplace(ps.id(), ps).first->first;
121 }
static std::recursive_mutex mutex_
static ParameterSetRegistry & instance_()
template<class FwdIt >
static std::enable_if_t< std::is_same<typename std::iterator_traits<FwdIt>::value_type, mapped_type>::value> fhicl::ParameterSetRegistry::put ( FwdIt  begin,
FwdIt  end 
)
static
template<class FwdIt >
static std::enable_if_t< std::is_same<typename std::iterator_traits<FwdIt>::value_type, value_type>::value> fhicl::ParameterSetRegistry::put ( FwdIt  begin,
FwdIt  end 
)
static
void fhicl::ParameterSetRegistry::put ( collection_type const &  c)
inlinestatic

Definition at line 150 of file ParameterSetRegistry.h.

151 {
152  // No lock here -- it will be acquired by 3.
153  put(c.cbegin(), c.cend());
154 }
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<typename std::iterator_traits<FwdIt>::value_type, mapped_type>::value>
inline

Definition at line 126 of file ParameterSetRegistry.h.

References e, put(), and fhicl::detail::atom::value().

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

Definition at line 108 of file ParameterSetRegistry.h.

109 {
110  std::lock_guard<decltype(mutex_)> lock{mutex_};
111  return instance_().registry_.size();
112 }
static std::recursive_mutex mutex_
static ParameterSetRegistry & instance_()
void fhicl::ParameterSetRegistry::stageIn ( )
static

Definition at line 182 of file ParameterSetRegistry.cc.

References evd::details::begin(), and fhicl::make_ParameterSet().

Referenced by read_all_parameter_sets().

183 {
184  std::lock_guard<decltype(mutex_)> lock{mutex_};
185 
186  sqlite3* primaryDB = instance_().primaryDB_;
187  auto& registry = instance_().registry_;
188  using namespace cet::sqlite;
189  query_result<std::string, std::string> entriesToStageIn;
190  entriesToStageIn << select("*").from(primaryDB, "ParameterSets");
191 
192  cet::transform_all(entriesToStageIn,
193  std::inserter(registry, std::begin(registry)),
194  [](auto const& row) {
195  std::string idString;
196  std::string psBlob;
197  std::tie(idString, psBlob) = row;
198  ParameterSet pset;
199  fhicl::make_ParameterSet(psBlob, pset);
200  return std::make_pair(ParameterSetID{idString}, pset);
201  });
202 }
void make_ParameterSet(intermediate_table const &tbl, ParameterSet &ps)
std::vector< evd::details::RawDigitInfo_t >::const_iterator begin(RawDigitCacheDataClass const &cache)
static std::recursive_mutex mutex_
static ParameterSetRegistry & instance_()

Member Data Documentation

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

Definition at line 97 of file ParameterSetRegistry.h.

Referenced by ParameterSetRegistry().

sqlite3* fhicl::ParameterSetRegistry::primaryDB_
private

Definition at line 94 of file ParameterSetRegistry.h.

Referenced by find_().

collection_type fhicl::ParameterSetRegistry::registry_ {}
private

Definition at line 96 of file ParameterSetRegistry.h.

Referenced by find_().

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

Definition at line 95 of file ParameterSetRegistry.h.

Referenced by find_().


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