Public Types | Public Member Functions | Static Public Attributes | Private Member Functions | Private Attributes | Static Private Attributes | List of all members
cet::sqlite::Ntuple< Args > Class Template Reference

#include <Ntuple.h>

Public Types

template<typename T >
using element_t = std::unique_ptr< typename sqlite::permissive_column< T >::element_type >
 
using row_t = std::tuple< element_t< Args >... >
 
using name_array = sqlite::name_array< nColumns >
 

Public Member Functions

 ~Ntuple () noexcept
 
 Ntuple (Connection &connection, std::string const &name, name_array const &columns, bool overwriteContents=false, std::size_t bufsize=1000ull)
 
 Ntuple (Ntuple const &)=delete
 
Ntupleoperator= (Ntuple const &)=delete
 
std::string const & name () const
 
void insert (Args const ...)
 
void flush ()
 

Static Public Attributes

static constexpr auto nColumns = std::tuple_size_v<row_t>
 

Private Member Functions

template<std::size_t... I>
 Ntuple (Connection &db, std::string const &name, name_array const &columns, bool overwriteContents, std::size_t bufsize, std::index_sequence< I... >)
 
int flush_no_throw ()
 

Private Attributes

std::recursive_mutex mutex_ {}
 
Connectionconnection_
 
std::string const name_
 
std::size_t const max_
 
std::vector< row_tbuffer_ {}
 
sqlite3_stmtinsert_statement_ {nullptr}
 

Static Private Attributes

static constexpr auto iSequence = std::make_index_sequence<nColumns>()
 

Detailed Description

template<typename... Args>
class cet::sqlite::Ntuple< Args >

Definition at line 121 of file Ntuple.h.

Member Typedef Documentation

template<typename... Args>
template<typename T >
using cet::sqlite::Ntuple< Args >::element_t = std::unique_ptr<typename sqlite::permissive_column<T>::element_type>

Definition at line 128 of file Ntuple.h.

template<typename... Args>
using cet::sqlite::Ntuple< Args >::name_array = sqlite::name_array<nColumns>

Definition at line 131 of file Ntuple.h.

template<typename... Args>
using cet::sqlite::Ntuple< Args >::row_t = std::tuple<element_t<Args>...>

Definition at line 129 of file Ntuple.h.

Constructor & Destructor Documentation

template<typename... Args>
cet::sqlite::Ntuple< Args >::~Ntuple ( )
noexcept

Definition at line 221 of file Ntuple.h.

222 {
223  if (flush_no_throw() != SQLITE_OK) {
224  std::cerr << "SQLite step failure while flushing.\n";
225  }
226  sqlite3_finalize(insert_statement_);
227 }
int flush_no_throw()
Definition: Ntuple.h:242
sqlite3_stmt * insert_statement_
Definition: Ntuple.h:172
template<typename... Args>
cet::sqlite::Ntuple< Args >::Ntuple ( Connection connection,
std::string const &  name,
name_array const &  columns,
bool  overwriteContents = false,
std::size_t  bufsize = 1000ull 
)

Definition at line 212 of file Ntuple.h.

217  : Ntuple{connection, name, cnames, overwriteContents, bufsize, iSequence}
218 {}
std::string const & name() const
Definition: Ntuple.h:145
static constexpr auto iSequence
Definition: Ntuple.h:153
Ntuple(Connection &connection, std::string const &name, name_array const &columns, bool overwriteContents=false, std::size_t bufsize=1000ull)
Definition: Ntuple.h:212
template<typename... Args>
cet::sqlite::Ntuple< Args >::Ntuple ( Ntuple< Args > const &  )
delete
template<typename... Args>
template<std::size_t... I>
cet::sqlite::Ntuple< Args >::Ntuple ( Connection db,
std::string const &  name,
name_array const &  columns,
bool  overwriteContents,
std::size_t  bufsize,
std::index_sequence< I... >   
)
private

Definition at line 179 of file Ntuple.h.

185  : connection_{connection}, name_{name}, max_{bufsize}
186 {
187  std::lock_guard sentry{mutex_};
188  assert(connection);
189  sqlite::createTableIfNeeded(connection,
190  overwriteContents,
191  name,
192  sqlite::permissive_column<Args>{cnames[I]}...);
193  std::string sql{"INSERT INTO "};
194  sql += name;
195  sql += " VALUES (?";
196  for (std::size_t i = 1; i < nColumns; ++i) {
197  sql += ",?";
198  }
199  sql += ")";
200  int const rc{sqlite3_prepare_v2(
201  connection_.get(), sql.c_str(), sql.size(), &insert_statement_, nullptr)};
202  if (rc != SQLITE_OK) {
203  auto const ec = sqlite3_step(insert_statement_);
205  << "Failed to prepare insertion statement.\n"
206  << "Return code: " << ec << '\n';
207  }
208  buffer_.reserve(bufsize);
209 }
void createTableIfNeeded(sqlite3 *db, bool const delete_contents, std::string const &tablename, permissive_column< Args > const &...cols)
Definition: helpers.h:46
std::recursive_mutex mutex_
Definition: Ntuple.h:167
std::string string
Definition: nybbler.cc:12
cet::coded_exception< errors::ErrorCodes, ExceptionDetail::translate > Exception
Definition: Exception.h:27
sqlite3 * get() const
Definition: Connection.h:48
std::string const name_
Definition: Ntuple.h:169
std::string const & name() const
Definition: Ntuple.h:145
Connection & connection_
Definition: Ntuple.h:168
std::vector< row_t > buffer_
Definition: Ntuple.h:171
static constexpr auto nColumns
Definition: Ntuple.h:130
sqlite3_stmt * insert_statement_
Definition: Ntuple.h:172
std::size_t const max_
Definition: Ntuple.h:170

Member Function Documentation

template<typename... Args>
void cet::sqlite::Ntuple< Args >::flush ( )

Definition at line 258 of file Ntuple.h.

259 {
260  // No lock here -- lock held by flush_no_throw;
261  if (flush_no_throw() != SQLITE_OK) {
263  << "SQLite step failure while flushing.";
264  }
265 }
int flush_no_throw()
Definition: Ntuple.h:242
cet::coded_exception< errors::ErrorCodes, ExceptionDetail::translate > Exception
Definition: Exception.h:27
template<typename... Args>
int cet::sqlite::Ntuple< Args >::flush_no_throw ( )
private

Definition at line 242 of file Ntuple.h.

243 {
244  // Guard against any modifications to the buffer, which is about to
245  // be flushed to the database.
246  std::lock_guard sentry{mutex_};
247  int const rc{
249  if (rc != SQLITE_DONE) {
250  return rc;
251  }
252  buffer_.clear();
253  return SQLITE_OK;
254 }
std::recursive_mutex mutex_
Definition: Ntuple.h:167
Connection & connection_
Definition: Ntuple.h:168
std::vector< row_t > buffer_
Definition: Ntuple.h:171
static constexpr auto nColumns
Definition: Ntuple.h:130
sqlite3_stmt * insert_statement_
Definition: Ntuple.h:172
int flush_no_throw(std::vector< Row > const &buffer, sqlite3_stmt *&insertStmt)
Definition: Connection.h:82
template<typename... Args>
void cet::sqlite::Ntuple< Args >::insert ( Args  const ...)

Definition at line 231 of file Ntuple.h.

232 {
233  std::lock_guard sentry{mutex_};
234  if (buffer_.size() == max_) {
235  flush();
236  }
237  buffer_.emplace_back(std::make_unique<Args>(args)...);
238 }
std::recursive_mutex mutex_
Definition: Ntuple.h:167
static QCString args
Definition: declinfo.cpp:674
std::vector< row_t > buffer_
Definition: Ntuple.h:171
std::size_t const max_
Definition: Ntuple.h:170
template<typename... Args>
std::string const& cet::sqlite::Ntuple< Args >::name ( ) const
inline

Definition at line 145 of file Ntuple.h.

146  {
147  return name_;
148  }
std::string const name_
Definition: Ntuple.h:169
template<typename... Args>
Ntuple& cet::sqlite::Ntuple< Args >::operator= ( Ntuple< Args > const &  )
delete

Member Data Documentation

template<typename... Args>
std::vector<row_t> cet::sqlite::Ntuple< Args >::buffer_ {}
private

Definition at line 171 of file Ntuple.h.

template<typename... Args>
Connection& cet::sqlite::Ntuple< Args >::connection_
private

Definition at line 168 of file Ntuple.h.

template<typename... Args>
sqlite3_stmt* cet::sqlite::Ntuple< Args >::insert_statement_ {nullptr}
private

Definition at line 172 of file Ntuple.h.

template<typename... Args>
constexpr auto cet::sqlite::Ntuple< Args >::iSequence = std::make_index_sequence<nColumns>()
staticprivate

Definition at line 153 of file Ntuple.h.

template<typename... Args>
std::size_t const cet::sqlite::Ntuple< Args >::max_
private

Definition at line 170 of file Ntuple.h.

template<typename... Args>
std::recursive_mutex cet::sqlite::Ntuple< Args >::mutex_ {}
private

Definition at line 167 of file Ntuple.h.

template<typename... Args>
std::string const cet::sqlite::Ntuple< Args >::name_
private

Definition at line 169 of file Ntuple.h.

template<typename... Args>
constexpr auto cet::sqlite::Ntuple< Args >::nColumns = std::tuple_size_v<row_t>
static

Definition at line 130 of file Ntuple.h.


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