Skip to content

Commit

Permalink
Set inMemory as property of DatabaseInterface
Browse files Browse the repository at this point in the history
  • Loading branch information
boutinb committed Dec 18, 2024
1 parent 85b08cb commit b8947d8
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 19 deletions.
31 changes: 16 additions & 15 deletions CommonData/databaseinterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,13 @@ void DatabaseInterface::upgradeDBFromVersion(Version originalVersion)
}

DatabaseInterface::DatabaseInterface(bool createDb, bool inMemory)
: _inMemory{inMemory}
{
assert(!_singleton);
_singleton = this;

if(createDb) create(inMemory);
else load(inMemory);
if(createDb) create();
else load();
}

DatabaseInterface::~DatabaseInterface()
Expand Down Expand Up @@ -1353,14 +1354,14 @@ void DatabaseInterface::labelsWrite(Column *column)
transactionWriteEnd();
}

std::string DatabaseInterface::dbFile(bool onlyName, bool inMemory) const
std::string DatabaseInterface::dbFile(bool onlyName) const
{
JASPTIMER_SCOPE(DatabaseInterface::dbFile);

static std::string fileName = "internal.sqlite";
static std::string memoryName = ":memory:";

if (inMemory)
if (_inMemory)
return memoryName;

return onlyName ? fileName : Utils::osPath(TempFiles::sessionDirName() + "/" + fileName).string();
Expand Down Expand Up @@ -1609,49 +1610,49 @@ void DatabaseInterface::_runStatementsRepeatedly(const std::string & statements,
}
}

void DatabaseInterface::create(bool inMemory)
void DatabaseInterface::create()
{
JASPTIMER_SCOPE(DatabaseInterface::create);
assert(!_db);

if(!inMemory && std::filesystem::exists(dbFile(false, inMemory)))
if(!_inMemory && std::filesystem::exists(dbFile()))
{
Log::log() << "DatabaseInterface::create: Removing existing sqlite internal db at " << dbFile(false, inMemory) << std::endl;
std::filesystem::remove(dbFile(false, inMemory));
Log::log() << "DatabaseInterface::create: Removing existing sqlite internal db at " << dbFile() << std::endl;
std::filesystem::remove(dbFile());
}

int ret = sqlite3_open_v2(dbFile(false, inMemory).c_str(), &_db, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | SQLITE_OPEN_FULLMUTEX, NULL);
int ret = sqlite3_open_v2(dbFile().c_str(), &_db, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | SQLITE_OPEN_FULLMUTEX, NULL);

if(ret != SQLITE_OK)
{
Log::log() << "Couldnt open sqlite internal db, because of: " << (_db ? sqlite3_errmsg(_db) : "not even a broken sqlite3 obj was returned..." ) << std::endl;
throw std::runtime_error("JASP cannot run without an internal database and it cannot be created. Contact the JASP team for help.");
}
else
Log::log() << "Opened internal sqlite database for creation at '" << dbFile(false, inMemory) << "'." << std::endl;
Log::log() << "Opened internal sqlite database for creation at '" << dbFile() << "'." << std::endl;

transactionWriteBegin();
runStatements(_dbConstructionSql);
transactionWriteEnd();
}

void DatabaseInterface::load(bool inMemory)
void DatabaseInterface::load()
{
JASPTIMER_SCOPE(DatabaseInterface::load);
assert(!_db);

if(!std::filesystem::exists(dbFile(false, inMemory)))
throw std::runtime_error("Trying to load '" + dbFile(false, inMemory) + "' but it doesn't exist!");
if(!std::filesystem::exists(dbFile()))
throw std::runtime_error("Trying to load '" + dbFile() + "' but it doesn't exist!");

int ret = sqlite3_open_v2(dbFile(false, inMemory).c_str(), &_db, SQLITE_OPEN_READWRITE | SQLITE_OPEN_FULLMUTEX, NULL);
int ret = sqlite3_open_v2(dbFile().c_str(), &_db, SQLITE_OPEN_READWRITE | SQLITE_OPEN_FULLMUTEX, NULL);

if(ret != SQLITE_OK)
{
Log::log() << "Couldnt open sqlite internal db, because of: " << (_db ? sqlite3_errmsg(_db) : "not even a broken sqlite3 obj was returned..." ) << std::endl;
throw std::runtime_error("JASP cannot run without an internal database and it cannot be created. Contact the JASP team for help.");
}
else
Log::log() << "Opened internal sqlite database for loading at '" << dbFile(false, inMemory) << "'." << std::endl;
Log::log() << "Opened internal sqlite database for loading at '" << dbFile() << "'." << std::endl;

}

Expand Down
7 changes: 4 additions & 3 deletions CommonData/databaseinterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class DatabaseInterface

DatabaseInterface(bool create = false, bool inMemory = false); ///< Creates or loads a sqlite database based on the argument
~DatabaseInterface();
std::string dbFile(bool onlyPostfix, bool inMemory = false) const; ///< Convenience function for getting the filename where sqlite db should be
std::string dbFile(bool onlyPostfix = false) const; ///< Convenience function for getting the filename where sqlite db should be

static DatabaseInterface * singleton() { return _singleton; } ///< There can be only one! https://www.youtube.com/watch?v=sqcLjcSloXs

Expand Down Expand Up @@ -166,15 +166,16 @@ class DatabaseInterface
void _runStatements( const std::string & statements, std::function<void(sqlite3_stmt *stmt)> * bindParameters = nullptr, std::function<void(size_t row, sqlite3_stmt *stmt)> * processRow = nullptr); ///< Runs several sql statements without looking at the results. Unless processRow is not NULL, then this is called for each row.
void _runStatementsRepeatedly( const std::string & statements, std::function<bool( std::function<void(sqlite3_stmt *stmt)> ** bindParameters, size_t row)> bindParameterFactory, std::function<void(size_t row, size_t repetition, sqlite3_stmt *stmt)> * processRow = nullptr);

void create(bool inMemory = false); ///< Creates a new sqlite database in sessiondir and loads it
void load(bool inMemory = false); ///< Loads a sqlite database from sessiondir (after loading a jaspfile)
void create(); ///< Creates a new sqlite database in sessiondir and loads it
void load(); ///< Loads a sqlite database from sessiondir (after loading a jaspfile)
void close(); ///< Closes the loaded database and disconnects
bool tableHasColumn(const std::string & tableName, const std::string & columnName);

int _transactionWriteDepth = 0,
_transactionReadDepth = 0;

sqlite3 * _db = nullptr;
bool _inMemory = false;

static std::string _wrap_sqlite3_column_text(sqlite3_stmt * stmt, int iCol);
static const std::string _dbConstructionSql;
Expand Down
2 changes: 1 addition & 1 deletion Engine/enginebase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ EngineBase::EngineBase(unsigned long sessionID, bool useMemory)
JASPTIMER_STOP(TempFiles Attach);

if(sessionID != 0) //Otherwise we are just running to fix R packages
_db = new DatabaseInterface(useMemory);
_db = new DatabaseInterface(false, useMemory);
}

void EngineBase::provideStateFileName(std::string & root, std::string & relativePath)
Expand Down

0 comments on commit b8947d8

Please sign in to comment.