A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
ns3::SQLiteOutput Class Reference

A C++ interface towards an SQLITE database. More...

#include "sqlite-output.h"

+ Inheritance diagram for ns3::SQLiteOutput:
+ Collaboration diagram for ns3::SQLiteOutput:

Public Member Functions

 SQLiteOutput (const std::string &name)
 SQLiteOutput constructor.
 
 ~SQLiteOutput ()
 Destructor.
 
template<>
bool Bind (sqlite3_stmt *stmt, int pos, const double &value) const
 Bind a value to a sqlite statement.
 
template<>
bool Bind (sqlite3_stmt *stmt, int pos, const int &value) const
 Bind a value to a sqlite statement.
 
template<>
bool Bind (sqlite3_stmt *stmt, int pos, const long &value) const
 Bind a value to a sqlite statement.
 
template<>
bool Bind (sqlite3_stmt *stmt, int pos, const long long &value) const
 Bind a value to a sqlite statement.
 
template<>
bool Bind (sqlite3_stmt *stmt, int pos, const std::string &value) const
 Bind a value to a sqlite statement.
 
template<typename T >
bool Bind (sqlite3_stmt *stmt, int pos, const T &value) const
 Bind a value to a sqlite statement.
 
template<>
bool Bind (sqlite3_stmt *stmt, int pos, const Time &value) const
 Bind a value to a sqlite statement.
 
template<>
bool Bind (sqlite3_stmt *stmt, int pos, const uint16_t &value) const
 Bind a value to a sqlite statement.
 
template<>
bool Bind (sqlite3_stmt *stmt, int pos, const uint32_t &value) const
 Bind a value to a sqlite statement.
 
template<>
bool Bind (sqlite3_stmt *stmt, int pos, const uint8_t &value) const
 Bind a value to a sqlite statement.
 
template<>
int RetrieveColumn (sqlite3_stmt *stmt, int pos) const
 Retrieve a value from an executed statement.
 
template<typename T >
RetrieveColumn (sqlite3_stmt *stmt, int pos) const
 Retrieve a value from an executed statement.
 
void SetJournalInMemory ()
 Instruct SQLite to store the journal in memory.
 
bool SpinExec (const std::string &cmd) const
 Execute a command until the return value is OK or an ERROR.
 
bool SpinExec (sqlite3_stmt *stmt) const
 Execute a command until the return value is OK or an ERROR.
 
bool SpinPrepare (sqlite3_stmt **stmt, const std::string &cmd) const
 Prepare a statement.
 
bool WaitExec (const std::string &cmd) const
 Execute a command, waiting on a mutex.
 
bool WaitExec (sqlite3_stmt *stmt) const
 Execute a command, waiting on a mutex.
 
bool WaitPrepare (sqlite3_stmt **stmt, const std::string &cmd) const
 Prepare a statement, waiting on a mutex.
 
- Public Member Functions inherited from ns3::SimpleRefCount< SQLiteOutput >
 SimpleRefCount ()
 Default constructor.
 
 SimpleRefCount (const SimpleRefCount &o)
 Copy constructor.
 
uint32_t GetReferenceCount () const
 Get the reference count of the object.
 
SimpleRefCountoperator= (const SimpleRefCount &o)
 Assignment operator.
 
void Ref () const
 Increment the reference count.
 
void Unref () const
 Decrement the reference count.
 

Static Public Member Functions

static int SpinFinalize (sqlite3_stmt *stmt)
 Finalize a statement until the result is ok or an error.
 
static int SpinReset (sqlite3_stmt *stmt)
 Reset a statement until the result is ok or an error.
 
static int SpinStep (sqlite3_stmt *stmt)
 Execute a step operation on a statement until the result is ok or an error.
 

Protected Member Functions

int WaitExec (sqlite3 *db, const std::string &cmd) const
 Execute a command, waiting on a mutex.
 
int WaitExec (sqlite3 *db, sqlite3_stmt *stmt) const
 Execute a statement, waiting on a mutex.
 
int WaitPrepare (sqlite3 *db, sqlite3_stmt **stmt, const std::string &cmd) const
 Prepare a statement, waiting on a mutex.
 

Static Protected Member Functions

static bool CheckError (sqlite3 *db, int rc, const std::string &cmd, bool hardExit)
 Check any error in the db.
 
static void Error (sqlite3 *db, const std::string &cmd)
 Fail, printing an error message from sqlite.
 
static int SpinExec (sqlite3 *db, const std::string &cmd)
 Execute a command ignoring concurrency problems, retrying instead.
 
static int SpinExec (sqlite3 *db, sqlite3_stmt *stmt)
 Execute a Prepared Statement Object ignoring concurrency problems, retrying instead.
 
static int SpinPrepare (sqlite3 *db, sqlite3_stmt **stmt, const std::string &cmd)
 Preparing a command ignoring concurrency problems, retrying instead.
 

Private Attributes

sqlite3 * m_db {nullptr}
 Database pointer.
 
std::string m_dBname
 Database name.
 
std::mutex m_mutex
 Mutex.
 

Detailed Description

A C++ interface towards an SQLITE database.

The class is able to execute commands, and retrieve results, from an SQLITE database. The methods with the "Spin" prefix, in case of concurrent access to the database, will spin until the operation is applied. The methods with the "Wait" prefix will wait on a mutex.

If you run multiple simulations that write on the same database, it is recommended to use the "Wait" prefixed methods. Otherwise, if the access to the database is unique, using "Spin" methods will speed up database access.

The database is opened in the constructor, and closed in the deconstructor.

Definition at line 46 of file sqlite-output.h.

Constructor & Destructor Documentation

◆ SQLiteOutput()

ns3::SQLiteOutput::SQLiteOutput ( const std::string &  name)

SQLiteOutput constructor.

Parameters
namedatabase name

Definition at line 32 of file sqlite-output.cc.

References m_db, and NS_ABORT_MSG_UNLESS.

◆ ~SQLiteOutput()

ns3::SQLiteOutput::~SQLiteOutput ( )

Destructor.

Definition at line 38 of file sqlite-output.cc.

References m_db, and NS_ABORT_MSG_UNLESS.

Member Function Documentation

◆ Bind() [1/10]

template<>
bool ns3::SQLiteOutput::Bind ( sqlite3_stmt *  stmt,
int  pos,
const double value 
) const

Bind a value to a sqlite statement.

Parameters
stmtSqlite statement
posPosition of the bind argument inside the statement
valueValue to bind
Returns
true on success, false otherwise

Definition at line 141 of file sqlite-output.cc.

◆ Bind() [2/10]

template<>
bool ns3::SQLiteOutput::Bind ( sqlite3_stmt *  stmt,
int  pos,
const int &  value 
) const

Bind a value to a sqlite statement.

Parameters
stmtSqlite statement
posPosition of the bind argument inside the statement
valueValue to bind
Returns
true on success, false otherwise

Definition at line 189 of file sqlite-output.cc.

◆ Bind() [3/10]

template<>
bool ns3::SQLiteOutput::Bind ( sqlite3_stmt *  stmt,
int  pos,
const long &  value 
) const

Bind a value to a sqlite statement.

Parameters
stmtSqlite statement
posPosition of the bind argument inside the statement
valueValue to bind
Returns
true on success, false otherwise

Definition at line 157 of file sqlite-output.cc.

◆ Bind() [4/10]

template<>
bool ns3::SQLiteOutput::Bind ( sqlite3_stmt *  stmt,
int  pos,
const long long &  value 
) const

Bind a value to a sqlite statement.

Parameters
stmtSqlite statement
posPosition of the bind argument inside the statement
valueValue to bind
Returns
true on success, false otherwise

Definition at line 165 of file sqlite-output.cc.

◆ Bind() [5/10]

template<>
bool ns3::SQLiteOutput::Bind ( sqlite3_stmt *  stmt,
int  pos,
const std::string &  value 
) const

Bind a value to a sqlite statement.

Parameters
stmtSqlite statement
posPosition of the bind argument inside the statement
valueValue to bind
Returns
true on success, false otherwise

Definition at line 197 of file sqlite-output.cc.

◆ Bind() [6/10]

template<typename T >
bool ns3::SQLiteOutput::Bind ( sqlite3_stmt *  stmt,
int  pos,
const T &  value 
) const

Bind a value to a sqlite statement.

Parameters
stmtSqlite statement
posPosition of the bind argument inside the statement
valueValue to bind
Returns
true on success, false otherwise

Definition at line 124 of file sqlite-output.cc.

References NS_FATAL_ERROR.

◆ Bind() [7/10]

template<>
bool ns3::SQLiteOutput::Bind ( sqlite3_stmt *  stmt,
int  pos,
const Time value 
) const

Bind a value to a sqlite statement.

Parameters
stmtSqlite statement
posPosition of the bind argument inside the statement
valueValue to bind
Returns
true on success, false otherwise

Definition at line 133 of file sqlite-output.cc.

◆ Bind() [8/10]

template<>
bool ns3::SQLiteOutput::Bind ( sqlite3_stmt *  stmt,
int  pos,
const uint16_t &  value 
) const

Bind a value to a sqlite statement.

Parameters
stmtSqlite statement
posPosition of the bind argument inside the statement
valueValue to bind
Returns
true on success, false otherwise

Definition at line 173 of file sqlite-output.cc.

◆ Bind() [9/10]

template<>
bool ns3::SQLiteOutput::Bind ( sqlite3_stmt *  stmt,
int  pos,
const uint32_t value 
) const

Bind a value to a sqlite statement.

Parameters
stmtSqlite statement
posPosition of the bind argument inside the statement
valueValue to bind
Returns
true on success, false otherwise

Definition at line 149 of file sqlite-output.cc.

◆ Bind() [10/10]

template<>
bool ns3::SQLiteOutput::Bind ( sqlite3_stmt *  stmt,
int  pos,
const uint8_t &  value 
) const

Bind a value to a sqlite statement.

Parameters
stmtSqlite statement
posPosition of the bind argument inside the statement
valueValue to bind
Returns
true on success, false otherwise

Definition at line 181 of file sqlite-output.cc.

◆ CheckError()

bool ns3::SQLiteOutput::CheckError ( sqlite3 *  db,
int  rc,
const std::string &  cmd,
bool  hardExit 
)
staticprotected

Check any error in the db.

Parameters
dbDatabase
rcSqlite return code
cmdCommand
hardExitif true, will exit the program
Returns
true in case of error, false otherwise

Definition at line 277 of file sqlite-output.cc.

References Error().

Referenced by SpinExec(), WaitExec(), and WaitPrepare().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ Error()

void ns3::SQLiteOutput::Error ( sqlite3 *  db,
const std::string &  cmd 
)
staticprotected

Fail, printing an error message from sqlite.

Parameters
dbDatabase
cmdCommand

Definition at line 271 of file sqlite-output.cc.

References NS_ABORT_MSG.

Referenced by CheckError().

+ Here is the caller graph for this function:

◆ RetrieveColumn() [1/2]

template<>
double ns3::SQLiteOutput::RetrieveColumn ( sqlite3_stmt *  stmt,
int  pos 
) const

Retrieve a value from an executed statement.

Parameters
stmtsqlite statement
posColumn position
Returns
the value from the executed statement

Definition at line 101 of file sqlite-output.cc.

◆ RetrieveColumn() [2/2]

template<typename T >
T ns3::SQLiteOutput::RetrieveColumn ( sqlite3_stmt *  stmt,
int  pos 
) const

Retrieve a value from an executed statement.

Parameters
stmtsqlite statement
posColumn position
Returns
the value from the executed statement

Definition at line 93 of file sqlite-output.cc.

References NS_FATAL_ERROR.

◆ SetJournalInMemory()

void ns3::SQLiteOutput::SetJournalInMemory ( )

Instruct SQLite to store the journal in memory.

May lead to data losses in case of unexpected program exits.

Definition at line 47 of file sqlite-output.cc.

References NS_LOG_FUNCTION, and SpinExec().

+ Here is the call graph for this function:

◆ SpinExec() [1/4]

bool ns3::SQLiteOutput::SpinExec ( const std::string &  cmd) const

Execute a command until the return value is OK or an ERROR.

Ignore errors due to concurrency, the method will repeat the command instead

Parameters
cmdCommand
Returns
true in case of success

Definition at line 54 of file sqlite-output.cc.

References m_db, and SpinExec().

Referenced by SetJournalInMemory(), and SpinExec().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ SpinExec() [2/4]

int ns3::SQLiteOutput::SpinExec ( sqlite3 *  db,
const std::string &  cmd 
)
staticprotected

Execute a command ignoring concurrency problems, retrying instead.

Parameters
dbDatabase
cmdCommand
Returns
Sqlite error code

Definition at line 295 of file sqlite-output.cc.

References CheckError(), SpinFinalize(), SpinPrepare(), and SpinStep().

+ Here is the call graph for this function:

◆ SpinExec() [3/4]

int ns3::SQLiteOutput::SpinExec ( sqlite3 *  db,
sqlite3_stmt *  stmt 
)
staticprotected

Execute a Prepared Statement Object ignoring concurrency problems, retrying instead.

Parameters
dbDatabase
stmtPrepared Statement Object
Returns
Sqlite error code

Definition at line 321 of file sqlite-output.cc.

References CheckError(), SpinFinalize(), and SpinStep().

+ Here is the call graph for this function:

◆ SpinExec() [4/4]

bool ns3::SQLiteOutput::SpinExec ( sqlite3_stmt *  stmt) const

Execute a command until the return value is OK or an ERROR.

Parameters
stmtSQLite statement
Returns
true in case of success

Definition at line 60 of file sqlite-output.cc.

References CheckError(), m_db, and SpinExec().

+ Here is the call graph for this function:

◆ SpinFinalize()

int ns3::SQLiteOutput::SpinFinalize ( sqlite3_stmt *  stmt)
static

Finalize a statement until the result is ok or an error.

Ignores concurrency errors; it will retry instead of failing.

Parameters
stmtStatement
Returns
Sqlite error code

Definition at line 246 of file sqlite-output.cc.

Referenced by SpinExec(), and WaitExec().

+ Here is the caller graph for this function:

◆ SpinPrepare() [1/2]

int ns3::SQLiteOutput::SpinPrepare ( sqlite3 *  db,
sqlite3_stmt **  stmt,
const std::string &  cmd 
)
staticprotected

Preparing a command ignoring concurrency problems, retrying instead.

Parameters
dbDatabase
stmtStatement
cmdCommand to prepare
Returns
Sqlite error code

Definition at line 222 of file sqlite-output.cc.

◆ SpinPrepare() [2/2]

bool ns3::SQLiteOutput::SpinPrepare ( sqlite3_stmt **  stmt,
const std::string &  cmd 
) const

Prepare a statement.

Parameters
stmtSqlite statement
cmdCommand to prepare inside the statement
Returns
true in case of success

Definition at line 86 of file sqlite-output.cc.

References m_db, and SpinPrepare().

Referenced by SpinExec(), SpinPrepare(), and WaitExec().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ SpinReset()

int ns3::SQLiteOutput::SpinReset ( sqlite3_stmt *  stmt)
static

Reset a statement until the result is ok or an error.

Ignores concurrency errors: it will retry instead of failing.

Parameters
stmtStatement
Returns
the return code of reset

Definition at line 258 of file sqlite-output.cc.

◆ SpinStep()

int ns3::SQLiteOutput::SpinStep ( sqlite3_stmt *  stmt)
static

Execute a step operation on a statement until the result is ok or an error.

Ignores concurrency errors; it will retry instead of failing.

Parameters
stmtStatement
Returns
Sqlite error core

Definition at line 234 of file sqlite-output.cc.

Referenced by SpinExec(), and WaitExec().

+ Here is the caller graph for this function:

◆ WaitExec() [1/4]

bool ns3::SQLiteOutput::WaitExec ( const std::string &  cmd) const

Execute a command, waiting on a mutex.

Parameters
cmdCommand to be executed
Returns
true in case of success

Definition at line 67 of file sqlite-output.cc.

References CheckError(), m_db, and WaitExec().

Referenced by WaitExec().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ WaitExec() [2/4]

int ns3::SQLiteOutput::WaitExec ( sqlite3 *  db,
const std::string &  cmd 
) const
protected

Execute a command, waiting on a mutex.

Parameters
dbDatabase
cmdCommand
Returns
Sqlite error code

Definition at line 357 of file sqlite-output.cc.

References CheckError(), m_mutex, SpinFinalize(), SpinPrepare(), and SpinStep().

+ Here is the call graph for this function:

◆ WaitExec() [3/4]

int ns3::SQLiteOutput::WaitExec ( sqlite3 *  db,
sqlite3_stmt *  stmt 
) const
protected

Execute a statement, waiting on a mutex.

Parameters
dbDatabase
stmtStatement
Returns
Sqlite error code

Definition at line 336 of file sqlite-output.cc.

References CheckError(), m_mutex, SpinFinalize(), and SpinStep().

+ Here is the call graph for this function:

◆ WaitExec() [4/4]

bool ns3::SQLiteOutput::WaitExec ( sqlite3_stmt *  stmt) const

Execute a command, waiting on a mutex.

Parameters
stmtSqlite3 statement to be executed
Returns
true in case of success

Definition at line 74 of file sqlite-output.cc.

References m_db, and WaitExec().

+ Here is the call graph for this function:

◆ WaitPrepare() [1/2]

int ns3::SQLiteOutput::WaitPrepare ( sqlite3 *  db,
sqlite3_stmt **  stmt,
const std::string &  cmd 
) const
protected

Prepare a statement, waiting on a mutex.

Parameters
dbDatabase
stmtStatement
cmdCommand to prepare
Returns
Sqlite error code

Definition at line 203 of file sqlite-output.cc.

References CheckError(), and m_mutex.

+ Here is the call graph for this function:

◆ WaitPrepare() [2/2]

bool ns3::SQLiteOutput::WaitPrepare ( sqlite3_stmt **  stmt,
const std::string &  cmd 
) const

Prepare a statement, waiting on a mutex.

Parameters
stmtSqlite statement
cmdCommand to prepare inside the statement
Returns
true in case of success

Definition at line 80 of file sqlite-output.cc.

References m_db, and WaitPrepare().

Referenced by WaitPrepare().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Member Data Documentation

◆ m_db

sqlite3* ns3::SQLiteOutput::m_db {nullptr}
private

Database pointer.

Definition at line 232 of file sqlite-output.h.

Referenced by SQLiteOutput(), ~SQLiteOutput(), SpinExec(), SpinPrepare(), WaitExec(), and WaitPrepare().

◆ m_dBname

std::string ns3::SQLiteOutput::m_dBname
private

Database name.

Definition at line 230 of file sqlite-output.h.

◆ m_mutex

std::mutex ns3::SQLiteOutput::m_mutex
mutableprivate

Mutex.

Definition at line 231 of file sqlite-output.h.

Referenced by WaitExec(), and WaitPrepare().


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