3 #include "cetlib/container_algorithms.h" 4 #include "cetlib/sqlite/Transaction.h" 5 #include "cetlib/sqlite/column.h" 6 #include "cetlib/sqlite/create_table.h" 7 #include "cetlib/sqlite/exec.h" 8 #include "cetlib/sqlite/query_result.h" 9 #include "cetlib/sqlite/select.h" 26 sqlite3* result =
nullptr;
27 sqlite3_open(
":memory:", &result);
30 Transaction txn{result};
33 column<std::string, primary_key>{
"ID"},
34 column<std::string>{
"PSetBlob"});
43 std::string
const msgString{msg ? msg :
""};
45 if (errcode != SQLITE_OK) {
49 << sqlite3_errstr(errcode) <<
" (" << errcode <<
')' 50 << (msgString.empty() ?
"" : (std::string(
": ") + msgString));
61 auto const errcode = sqlite3_errcode(db);
67 sqlite3_finalize(stmt_);
72 std::cerr << e.what() <<
'\n';
78 retcode = sqlite3_close(primaryDB_);
79 }
while (retcode == SQLITE_BUSY);
86 std::lock_guard sentry{mutex_};
90 sqlite3_stmt* oStmt =
nullptr;
91 sqlite3* primaryDB = instance_().primaryDB_;
96 "INSERT OR IGNORE INTO ParameterSets(ID, PSetBlob) VALUES(?, ?);",
103 query_result<std::string, std::string> inputPSes;
104 inputPSes << select(
"*").from(db,
"ParameterSets");
106 for (
auto const& [idString, psBlob] : inputPSes) {
108 oStmt, 1, idString.c_str(), idString.size() + 1, SQLITE_STATIC);
111 oStmt, 2, psBlob.c_str(), psBlob.size() + 1, SQLITE_STATIC);
113 switch (sqlite3_step(oStmt)) {
119 sqlite3_reset(oStmt);
122 sqlite3_finalize(oStmt);
130 std::lock_guard sentry{mutex_};
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);");
138 sqlite3_stmt* oStmt =
nullptr;
141 "INSERT OR IGNORE INTO ParameterSets(ID, PSetBlob) VALUES(?, ?);",
152 while (std::any_of(
get().
begin(),
get().
end(), [oStmt, db](
auto const& p) {
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) {
162 sqlite3_bind_text(oStmt, 1,
id.c_str(),
id.
size() + 1, SQLITE_STATIC);
165 oStmt, 2, psBlob.c_str(), psBlob.size() + 1, SQLITE_STATIC);
167 switch (sqlite3_step(oStmt)) {
169 sqlite3_reset(oStmt);
179 sqlite3*
const primaryDB{instance_().primaryDB_};
181 query_result<std::string, std::string> regPSes;
182 regPSes << select(
"*").from(primaryDB,
"ParameterSets");
184 for (
auto const& [idString, psBlob] : regPSes) {
186 oStmt, 1, idString.c_str(), idString.size() + 1, SQLITE_STATIC);
189 oStmt, 2, psBlob.c_str(), psBlob.size() + 1, SQLITE_STATIC);
191 switch (sqlite3_step(oStmt)) {
193 sqlite3_reset(oStmt);
200 sqlite3_finalize(oStmt);
207 std::lock_guard sentry{mutex_};
209 sqlite3* primaryDB = instance_().primaryDB_;
210 auto& registry = instance_().registry_;
212 query_result<std::string, std::string> entriesToStageIn;
213 entriesToStageIn << select(
"*").from(primaryDB,
"ParameterSets");
215 cet::transform_all(entriesToStageIn,
216 std::inserter(registry,
std::begin(registry)),
217 [](
auto const& row) {
218 auto const& [idString, psBlob] = row;
225 : primaryDB_{openPrimaryDB()}
235 if (
stmt_ ==
nullptr) {
237 "SELECT PSetBlob FROM ParameterSets WHERE ID = ?;",
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_);
252 reinterpret_cast<char const*>(sqlite3_column_text(
stmt_, 0)));
261 sqlite3_reset(
stmt_);
static ParameterSet make(intermediate_table const &tbl)
collection_type::const_iterator const_iterator
decltype(auto) constexpr end(T &&obj)
ADL-aware version of std::end.
decltype(auto) constexpr size(T &&obj)
ADL-aware version of std::size.
collection_type registry_
static std::recursive_mutex mutex_
void throwOnSQLiteFailure(int rc, char *msg=nullptr)
decltype(auto) constexpr begin(T &&obj)
ADL-aware version of std::begin.
const_iterator find_(ParameterSetID const &id)
cet::coded_exception< error, detail::translate > exception
static void exportTo(sqlite3 *db)
static void importFrom(sqlite3 *db)