Rename "mod metadata" to "mod storage" everywhere

This commit is contained in:
Jude Melton-Houghton 2022-11-23 17:25:34 -05:00
parent 8817af07fb
commit 7c21347a40
22 changed files with 480 additions and 474 deletions

@ -134,7 +134,7 @@ Client::Client(
// Make the mod storage database and begin the save for later // Make the mod storage database and begin the save for later
m_mod_storage_database = m_mod_storage_database =
new ModMetadataDatabaseSQLite3(porting::path_user + DIR_DELIM + "client"); new ModStorageDatabaseSQLite3(porting::path_user + DIR_DELIM + "client");
m_mod_storage_database->beginSave(); m_mod_storage_database->beginSave();
if (g_settings->getBool("enable_minimap")) { if (g_settings->getBool("enable_minimap")) {
@ -151,7 +151,7 @@ void Client::migrateModStorage()
if (fs::IsDir(old_mod_storage)) { if (fs::IsDir(old_mod_storage)) {
infostream << "Migrating client mod storage to SQLite3 database" << std::endl; infostream << "Migrating client mod storage to SQLite3 database" << std::endl;
{ {
ModMetadataDatabaseFiles files_db(mod_storage_dir); ModStorageDatabaseFiles files_db(mod_storage_dir);
std::vector<std::string> mod_list; std::vector<std::string> mod_list;
files_db.listMods(&mod_list); files_db.listMods(&mod_list);
for (const std::string &modname : mod_list) { for (const std::string &modname : mod_list) {

@ -381,7 +381,7 @@ public:
{ return checkPrivilege(priv); } { return checkPrivilege(priv); }
virtual scene::IAnimatedMesh* getMesh(const std::string &filename, bool cache = false); virtual scene::IAnimatedMesh* getMesh(const std::string &filename, bool cache = false);
const std::string* getModFile(std::string filename); const std::string* getModFile(std::string filename);
ModMetadataDatabase *getModStorageDatabase() override { return m_mod_storage_database; } ModStorageDatabase *getModStorageDatabase() override { return m_mod_storage_database; }
// Migrates away old files-based mod storage if necessary // Migrates away old files-based mod storage if necessary
void migrateModStorage(); void migrateModStorage();
@ -590,7 +590,7 @@ private:
// Client modding // Client modding
ClientScripting *m_script = nullptr; ClientScripting *m_script = nullptr;
ModMetadataDatabase *m_mod_storage_database = nullptr; ModStorageDatabase *m_mod_storage_database = nullptr;
float m_mod_storage_save_timer = 10.0f; float m_mod_storage_save_timer = 10.0f;
std::vector<ModSpec> m_mods; std::vector<ModSpec> m_mods;
StringMap m_mod_vfs; StringMap m_mod_vfs;

@ -217,22 +217,22 @@ std::vector<ModSpec> flattenMods(const std::map<std::string, ModSpec> &mods)
} }
ModMetadata::ModMetadata(const std::string &mod_name, ModMetadataDatabase *database): ModStorage::ModStorage(const std::string &mod_name, ModStorageDatabase *database):
m_mod_name(mod_name), m_database(database) m_mod_name(mod_name), m_database(database)
{ {
} }
void ModMetadata::clear() void ModStorage::clear()
{ {
m_database->removeModEntries(m_mod_name); m_database->removeModEntries(m_mod_name);
} }
bool ModMetadata::contains(const std::string &name) const bool ModStorage::contains(const std::string &name) const
{ {
return m_database->hasModEntry(m_mod_name, name); return m_database->hasModEntry(m_mod_name, name);
} }
bool ModMetadata::setString(const std::string &name, const std::string &var) bool ModStorage::setString(const std::string &name, const std::string &var)
{ {
if (var.empty()) if (var.empty())
return m_database->removeModEntry(m_mod_name, name); return m_database->removeModEntry(m_mod_name, name);
@ -240,21 +240,21 @@ bool ModMetadata::setString(const std::string &name, const std::string &var)
return m_database->setModEntry(m_mod_name, name, var); return m_database->setModEntry(m_mod_name, name, var);
} }
const StringMap &ModMetadata::getStrings(StringMap *place) const const StringMap &ModStorage::getStrings(StringMap *place) const
{ {
place->clear(); place->clear();
m_database->getModEntries(m_mod_name, place); m_database->getModEntries(m_mod_name, place);
return *place; return *place;
} }
const std::vector<std::string> &ModMetadata::getKeys(std::vector<std::string> *place) const const std::vector<std::string> &ModStorage::getKeys(std::vector<std::string> *place) const
{ {
place->clear(); place->clear();
m_database->getModKeys(m_mod_name, place); m_database->getModKeys(m_mod_name, place);
return *place; return *place;
} }
const std::string *ModMetadata::getStringRaw(const std::string &name, std::string *place) const const std::string *ModStorage::getStringRaw(const std::string &name, std::string *place) const
{ {
return m_database->getModEntry(m_mod_name, name, place) ? place : nullptr; return m_database->getModEntry(m_mod_name, name, place) ? place : nullptr;
} }

@ -32,7 +32,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "metadata.h" #include "metadata.h"
#include "subgames.h" #include "subgames.h"
class ModMetadataDatabase; class ModStorageDatabase;
#define MODNAME_ALLOWED_CHARS "abcdefghijklmnopqrstuvwxyz0123456789_" #define MODNAME_ALLOWED_CHARS "abcdefghijklmnopqrstuvwxyz0123456789_"
@ -110,12 +110,12 @@ std::map<std::string, ModSpec> getModsInPath(const std::string &path,
std::vector<ModSpec> flattenMods(const std::map<std::string, ModSpec> &mods); std::vector<ModSpec> flattenMods(const std::map<std::string, ModSpec> &mods);
class ModMetadata : public IMetadata class ModStorage : public IMetadata
{ {
public: public:
ModMetadata() = delete; ModStorage() = delete;
ModMetadata(const std::string &mod_name, ModMetadataDatabase *database); ModStorage(const std::string &mod_name, ModStorageDatabase *database);
~ModMetadata() = default; ~ModStorage() = default;
const std::string &getModName() const { return m_mod_name; } const std::string &getModName() const { return m_mod_name; }
@ -135,5 +135,5 @@ protected:
private: private:
std::string m_mod_name; std::string m_mod_name;
ModMetadataDatabase *m_database; ModStorageDatabase *m_database;
}; };

@ -83,8 +83,8 @@ void Database_Dummy::listPlayers(std::vector<std::string> &res)
bool Database_Dummy::getModEntries(const std::string &modname, StringMap *storage) bool Database_Dummy::getModEntries(const std::string &modname, StringMap *storage)
{ {
const auto mod_pair = m_mod_meta_database.find(modname); const auto mod_pair = m_mod_storage_database.find(modname);
if (mod_pair != m_mod_meta_database.cend()) { if (mod_pair != m_mod_storage_database.cend()) {
for (const auto &pair : mod_pair->second) { for (const auto &pair : mod_pair->second) {
(*storage)[pair.first] = pair.second; (*storage)[pair.first] = pair.second;
} }
@ -94,8 +94,8 @@ bool Database_Dummy::getModEntries(const std::string &modname, StringMap *storag
bool Database_Dummy::getModKeys(const std::string &modname, std::vector<std::string> *storage) bool Database_Dummy::getModKeys(const std::string &modname, std::vector<std::string> *storage)
{ {
const auto mod_pair = m_mod_meta_database.find(modname); const auto mod_pair = m_mod_storage_database.find(modname);
if (mod_pair != m_mod_meta_database.cend()) { if (mod_pair != m_mod_storage_database.cend()) {
storage->reserve(storage->size() + mod_pair->second.size()); storage->reserve(storage->size() + mod_pair->second.size());
for (const auto &pair : mod_pair->second) for (const auto &pair : mod_pair->second)
storage->push_back(pair.first); storage->push_back(pair.first);
@ -106,8 +106,8 @@ bool Database_Dummy::getModKeys(const std::string &modname, std::vector<std::str
bool Database_Dummy::getModEntry(const std::string &modname, bool Database_Dummy::getModEntry(const std::string &modname,
const std::string &key, std::string *value) const std::string &key, std::string *value)
{ {
auto mod_pair = m_mod_meta_database.find(modname); auto mod_pair = m_mod_storage_database.find(modname);
if (mod_pair == m_mod_meta_database.end()) if (mod_pair == m_mod_storage_database.end())
return false; return false;
const StringMap &meta = mod_pair->second; const StringMap &meta = mod_pair->second;
@ -121,8 +121,8 @@ bool Database_Dummy::getModEntry(const std::string &modname,
bool Database_Dummy::hasModEntry(const std::string &modname, const std::string &key) bool Database_Dummy::hasModEntry(const std::string &modname, const std::string &key)
{ {
auto mod_pair = m_mod_meta_database.find(modname); auto mod_pair = m_mod_storage_database.find(modname);
if (mod_pair == m_mod_meta_database.end()) if (mod_pair == m_mod_storage_database.end())
return false; return false;
const StringMap &meta = mod_pair->second; const StringMap &meta = mod_pair->second;
@ -132,9 +132,9 @@ bool Database_Dummy::hasModEntry(const std::string &modname, const std::string &
bool Database_Dummy::setModEntry(const std::string &modname, bool Database_Dummy::setModEntry(const std::string &modname,
const std::string &key, const std::string &value) const std::string &key, const std::string &value)
{ {
auto mod_pair = m_mod_meta_database.find(modname); auto mod_pair = m_mod_storage_database.find(modname);
if (mod_pair == m_mod_meta_database.end()) { if (mod_pair == m_mod_storage_database.end()) {
m_mod_meta_database[modname] = StringMap({{key, value}}); m_mod_storage_database[modname] = StringMap({{key, value}});
} else { } else {
mod_pair->second[key] = value; mod_pair->second[key] = value;
} }
@ -143,16 +143,16 @@ bool Database_Dummy::setModEntry(const std::string &modname,
bool Database_Dummy::removeModEntry(const std::string &modname, const std::string &key) bool Database_Dummy::removeModEntry(const std::string &modname, const std::string &key)
{ {
auto mod_pair = m_mod_meta_database.find(modname); auto mod_pair = m_mod_storage_database.find(modname);
if (mod_pair != m_mod_meta_database.end()) if (mod_pair != m_mod_storage_database.end())
return mod_pair->second.erase(key) > 0; return mod_pair->second.erase(key) > 0;
return false; return false;
} }
bool Database_Dummy::removeModEntries(const std::string &modname) bool Database_Dummy::removeModEntries(const std::string &modname)
{ {
auto mod_pair = m_mod_meta_database.find(modname); auto mod_pair = m_mod_storage_database.find(modname);
if (mod_pair != m_mod_meta_database.end() && !mod_pair->second.empty()) { if (mod_pair != m_mod_storage_database.end() && !mod_pair->second.empty()) {
mod_pair->second.clear(); mod_pair->second.clear();
return true; return true;
} }
@ -161,7 +161,7 @@ bool Database_Dummy::removeModEntries(const std::string &modname)
void Database_Dummy::listMods(std::vector<std::string> *res) void Database_Dummy::listMods(std::vector<std::string> *res)
{ {
for (const auto &pair : m_mod_meta_database) { for (const auto &pair : m_mod_storage_database) {
res->push_back(pair.first); res->push_back(pair.first);
} }
} }

@ -24,7 +24,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "database.h" #include "database.h"
#include "irrlichttypes.h" #include "irrlichttypes.h"
class Database_Dummy : public MapDatabase, public PlayerDatabase, public ModMetadataDatabase class Database_Dummy : public MapDatabase, public PlayerDatabase, public ModStorageDatabase
{ {
public: public:
bool saveBlock(const v3s16 &pos, const std::string &data); bool saveBlock(const v3s16 &pos, const std::string &data);
@ -54,5 +54,5 @@ public:
private: private:
std::map<s64, std::string> m_database; std::map<s64, std::string> m_database;
std::set<std::string> m_player_database; std::set<std::string> m_player_database;
std::unordered_map<std::string, StringMap> m_mod_meta_database; std::unordered_map<std::string, StringMap> m_mod_storage_database;
}; };

@ -376,12 +376,12 @@ bool AuthDatabaseFiles::writeAuthFile()
return true; return true;
} }
ModMetadataDatabaseFiles::ModMetadataDatabaseFiles(const std::string &savedir): ModStorageDatabaseFiles::ModStorageDatabaseFiles(const std::string &savedir):
m_storage_dir(savedir + DIR_DELIM + "mod_storage") m_storage_dir(savedir + DIR_DELIM + "mod_storage")
{ {
} }
bool ModMetadataDatabaseFiles::getModEntries(const std::string &modname, StringMap *storage) bool ModStorageDatabaseFiles::getModEntries(const std::string &modname, StringMap *storage)
{ {
Json::Value *meta = getOrCreateJson(modname); Json::Value *meta = getOrCreateJson(modname);
if (!meta) if (!meta)
@ -396,7 +396,7 @@ bool ModMetadataDatabaseFiles::getModEntries(const std::string &modname, StringM
return true; return true;
} }
bool ModMetadataDatabaseFiles::getModKeys(const std::string &modname, bool ModStorageDatabaseFiles::getModKeys(const std::string &modname,
std::vector<std::string> *storage) std::vector<std::string> *storage)
{ {
Json::Value *meta = getOrCreateJson(modname); Json::Value *meta = getOrCreateJson(modname);
@ -411,7 +411,7 @@ bool ModMetadataDatabaseFiles::getModKeys(const std::string &modname,
return true; return true;
} }
bool ModMetadataDatabaseFiles::getModEntry(const std::string &modname, bool ModStorageDatabaseFiles::getModEntry(const std::string &modname,
const std::string &key, std::string *value) const std::string &key, std::string *value)
{ {
Json::Value *meta = getOrCreateJson(modname); Json::Value *meta = getOrCreateJson(modname);
@ -425,13 +425,13 @@ bool ModMetadataDatabaseFiles::getModEntry(const std::string &modname,
return false; return false;
} }
bool ModMetadataDatabaseFiles::hasModEntry(const std::string &modname, const std::string &key) bool ModStorageDatabaseFiles::hasModEntry(const std::string &modname, const std::string &key)
{ {
Json::Value *meta = getOrCreateJson(modname); Json::Value *meta = getOrCreateJson(modname);
return meta && meta->isMember(key); return meta && meta->isMember(key);
} }
bool ModMetadataDatabaseFiles::setModEntry(const std::string &modname, bool ModStorageDatabaseFiles::setModEntry(const std::string &modname,
const std::string &key, const std::string &value) const std::string &key, const std::string &value)
{ {
Json::Value *meta = getOrCreateJson(modname); Json::Value *meta = getOrCreateJson(modname);
@ -444,7 +444,7 @@ bool ModMetadataDatabaseFiles::setModEntry(const std::string &modname,
return true; return true;
} }
bool ModMetadataDatabaseFiles::removeModEntry(const std::string &modname, bool ModStorageDatabaseFiles::removeModEntry(const std::string &modname,
const std::string &key) const std::string &key)
{ {
Json::Value *meta = getOrCreateJson(modname); Json::Value *meta = getOrCreateJson(modname);
@ -459,7 +459,7 @@ bool ModMetadataDatabaseFiles::removeModEntry(const std::string &modname,
return false; return false;
} }
bool ModMetadataDatabaseFiles::removeModEntries(const std::string &modname) bool ModStorageDatabaseFiles::removeModEntries(const std::string &modname)
{ {
Json::Value *meta = getOrCreateJson(modname); Json::Value *meta = getOrCreateJson(modname);
if (!meta || meta->empty()) if (!meta || meta->empty())
@ -470,22 +470,22 @@ bool ModMetadataDatabaseFiles::removeModEntries(const std::string &modname)
return true; return true;
} }
void ModMetadataDatabaseFiles::beginSave() void ModStorageDatabaseFiles::beginSave()
{ {
} }
void ModMetadataDatabaseFiles::endSave() void ModStorageDatabaseFiles::endSave()
{ {
if (m_modified.empty()) if (m_modified.empty())
return; return;
if (!fs::CreateAllDirs(m_storage_dir)) { if (!fs::CreateAllDirs(m_storage_dir)) {
errorstream << "ModMetadataDatabaseFiles: Unable to save. '" errorstream << "ModStorageDatabaseFiles: Unable to save. '"
<< m_storage_dir << "' cannot be created." << std::endl; << m_storage_dir << "' cannot be created." << std::endl;
return; return;
} }
if (!fs::IsDir(m_storage_dir)) { if (!fs::IsDir(m_storage_dir)) {
errorstream << "ModMetadataDatabaseFiles: Unable to save. '" errorstream << "ModStorageDatabaseFiles: Unable to save. '"
<< m_storage_dir << "' is not a directory." << std::endl; << m_storage_dir << "' is not a directory." << std::endl;
return; return;
} }
@ -493,10 +493,10 @@ void ModMetadataDatabaseFiles::endSave()
for (auto it = m_modified.begin(); it != m_modified.end();) { for (auto it = m_modified.begin(); it != m_modified.end();) {
const std::string &modname = *it; const std::string &modname = *it;
const Json::Value &json = m_mod_meta[modname]; const Json::Value &json = m_mod_storage[modname];
if (!fs::safeWriteToFile(m_storage_dir + DIR_DELIM + modname, fastWriteJson(json))) { if (!fs::safeWriteToFile(m_storage_dir + DIR_DELIM + modname, fastWriteJson(json))) {
errorstream << "ModMetadataDatabaseFiles[" << modname errorstream << "ModStorageDatabaseFiles[" << modname
<< "]: failed to write file." << std::endl; << "]: failed to write file." << std::endl;
++it; ++it;
continue; continue;
@ -506,24 +506,24 @@ void ModMetadataDatabaseFiles::endSave()
} }
} }
void ModMetadataDatabaseFiles::listMods(std::vector<std::string> *res) void ModStorageDatabaseFiles::listMods(std::vector<std::string> *res)
{ {
// List in-memory metadata first. // List in-memory metadata first.
for (const auto &pair : m_mod_meta) { for (const auto &pair : m_mod_storage) {
res->push_back(pair.first); res->push_back(pair.first);
} }
// List other metadata present in the filesystem. // List other metadata present in the filesystem.
for (const auto &entry : fs::GetDirListing(m_storage_dir)) { for (const auto &entry : fs::GetDirListing(m_storage_dir)) {
if (!entry.dir && m_mod_meta.count(entry.name) == 0) if (!entry.dir && m_mod_storage.count(entry.name) == 0)
res->push_back(entry.name); res->push_back(entry.name);
} }
} }
Json::Value *ModMetadataDatabaseFiles::getOrCreateJson(const std::string &modname) Json::Value *ModStorageDatabaseFiles::getOrCreateJson(const std::string &modname)
{ {
auto found = m_mod_meta.find(modname); auto found = m_mod_storage.find(modname);
if (found != m_mod_meta.end()) if (found != m_mod_storage.end())
return &found->second; return &found->second;
Json::Value meta(Json::objectValue); Json::Value meta(Json::objectValue);
@ -537,11 +537,11 @@ Json::Value *ModMetadataDatabaseFiles::getOrCreateJson(const std::string &modnam
std::string errs; std::string errs;
if (!Json::parseFromStream(builder, is, &meta, &errs)) { if (!Json::parseFromStream(builder, is, &meta, &errs)) {
errorstream << "ModMetadataDatabaseFiles[" << modname errorstream << "ModStorageDatabaseFiles[" << modname
<< "]: failed to decode data: " << errs << std::endl; << "]: failed to decode data: " << errs << std::endl;
return nullptr; return nullptr;
} }
} }
return &(m_mod_meta[modname] = std::move(meta)); return &(m_mod_storage[modname] = std::move(meta));
} }

@ -72,11 +72,11 @@ private:
bool writeAuthFile(); bool writeAuthFile();
}; };
class ModMetadataDatabaseFiles : public ModMetadataDatabase class ModStorageDatabaseFiles : public ModStorageDatabase
{ {
public: public:
ModMetadataDatabaseFiles(const std::string &savedir); ModStorageDatabaseFiles(const std::string &savedir);
virtual ~ModMetadataDatabaseFiles() = default; virtual ~ModStorageDatabaseFiles() = default;
virtual bool getModEntries(const std::string &modname, StringMap *storage); virtual bool getModEntries(const std::string &modname, StringMap *storage);
virtual bool getModKeys(const std::string &modname, std::vector<std::string> *storage); virtual bool getModKeys(const std::string &modname, std::vector<std::string> *storage);
@ -97,6 +97,6 @@ private:
bool writeJson(const std::string &modname, const Json::Value &json); bool writeJson(const std::string &modname, const Json::Value &json);
std::string m_storage_dir; std::string m_storage_dir;
std::unordered_map<std::string, Json::Value> m_mod_meta; std::unordered_map<std::string, Json::Value> m_mod_storage;
std::unordered_set<std::string> m_modified; std::unordered_set<std::string> m_modified;
}; };

@ -813,14 +813,14 @@ void AuthDatabasePostgreSQL::writePrivileges(const AuthEntry &authEntry)
} }
} }
ModMetadataDatabasePostgreSQL::ModMetadataDatabasePostgreSQL(const std::string &connect_string): ModStorageDatabasePostgreSQL::ModStorageDatabasePostgreSQL(const std::string &connect_string):
Database_PostgreSQL(connect_string, "_mod_storage"), Database_PostgreSQL(connect_string, "_mod_storage"),
ModMetadataDatabase() ModStorageDatabase()
{ {
connectToDatabase(); connectToDatabase();
} }
void ModMetadataDatabasePostgreSQL::createDatabase() void ModStorageDatabasePostgreSQL::createDatabase()
{ {
createTableIfNotExists("mod_storage", createTableIfNotExists("mod_storage",
"CREATE TABLE mod_storage (" "CREATE TABLE mod_storage ("
@ -833,7 +833,7 @@ void ModMetadataDatabasePostgreSQL::createDatabase()
infostream << "PostgreSQL: Mod Storage Database was initialized." << std::endl; infostream << "PostgreSQL: Mod Storage Database was initialized." << std::endl;
} }
void ModMetadataDatabasePostgreSQL::initStatements() void ModStorageDatabasePostgreSQL::initStatements()
{ {
prepareStatement("get_all", prepareStatement("get_all",
"SELECT key, value FROM mod_storage WHERE modname = $1"); "SELECT key, value FROM mod_storage WHERE modname = $1");
@ -866,7 +866,7 @@ void ModMetadataDatabasePostgreSQL::initStatements()
"SELECT DISTINCT modname FROM mod_storage"); "SELECT DISTINCT modname FROM mod_storage");
} }
bool ModMetadataDatabasePostgreSQL::getModEntries(const std::string &modname, StringMap *storage) bool ModStorageDatabasePostgreSQL::getModEntries(const std::string &modname, StringMap *storage)
{ {
verifyDatabase(); verifyDatabase();
@ -886,7 +886,7 @@ bool ModMetadataDatabasePostgreSQL::getModEntries(const std::string &modname, St
return true; return true;
} }
bool ModMetadataDatabasePostgreSQL::getModKeys(const std::string &modname, bool ModStorageDatabasePostgreSQL::getModKeys(const std::string &modname,
std::vector<std::string> *storage) std::vector<std::string> *storage)
{ {
verifyDatabase(); verifyDatabase();
@ -908,7 +908,7 @@ bool ModMetadataDatabasePostgreSQL::getModKeys(const std::string &modname,
return true; return true;
} }
bool ModMetadataDatabasePostgreSQL::getModEntry(const std::string &modname, bool ModStorageDatabasePostgreSQL::getModEntry(const std::string &modname,
const std::string &key, std::string *value) const std::string &key, std::string *value)
{ {
verifyDatabase(); verifyDatabase();
@ -929,7 +929,7 @@ bool ModMetadataDatabasePostgreSQL::getModEntry(const std::string &modname,
return found; return found;
} }
bool ModMetadataDatabasePostgreSQL::hasModEntry(const std::string &modname, bool ModStorageDatabasePostgreSQL::hasModEntry(const std::string &modname,
const std::string &key) const std::string &key)
{ {
verifyDatabase(); verifyDatabase();
@ -947,7 +947,7 @@ bool ModMetadataDatabasePostgreSQL::hasModEntry(const std::string &modname,
return found; return found;
} }
bool ModMetadataDatabasePostgreSQL::setModEntry(const std::string &modname, bool ModStorageDatabasePostgreSQL::setModEntry(const std::string &modname,
const std::string &key, const std::string &value) const std::string &key, const std::string &value)
{ {
verifyDatabase(); verifyDatabase();
@ -969,7 +969,7 @@ bool ModMetadataDatabasePostgreSQL::setModEntry(const std::string &modname,
return true; return true;
} }
bool ModMetadataDatabasePostgreSQL::removeModEntry(const std::string &modname, bool ModStorageDatabasePostgreSQL::removeModEntry(const std::string &modname,
const std::string &key) const std::string &key)
{ {
verifyDatabase(); verifyDatabase();
@ -986,7 +986,7 @@ bool ModMetadataDatabasePostgreSQL::removeModEntry(const std::string &modname,
return affected > 0; return affected > 0;
} }
bool ModMetadataDatabasePostgreSQL::removeModEntries(const std::string &modname) bool ModStorageDatabasePostgreSQL::removeModEntries(const std::string &modname)
{ {
verifyDatabase(); verifyDatabase();
@ -1002,7 +1002,7 @@ bool ModMetadataDatabasePostgreSQL::removeModEntries(const std::string &modname)
return affected > 0; return affected > 0;
} }
void ModMetadataDatabasePostgreSQL::listMods(std::vector<std::string> *res) void ModStorageDatabasePostgreSQL::listMods(std::vector<std::string> *res)
{ {
verifyDatabase(); verifyDatabase();

@ -175,11 +175,11 @@ private:
virtual void writePrivileges(const AuthEntry &authEntry); virtual void writePrivileges(const AuthEntry &authEntry);
}; };
class ModMetadataDatabasePostgreSQL : private Database_PostgreSQL, public ModMetadataDatabase class ModStorageDatabasePostgreSQL : private Database_PostgreSQL, public ModStorageDatabase
{ {
public: public:
ModMetadataDatabasePostgreSQL(const std::string &connect_string); ModStorageDatabasePostgreSQL(const std::string &connect_string);
~ModMetadataDatabasePostgreSQL() = default; ~ModStorageDatabasePostgreSQL() = default;
bool getModEntries(const std::string &modname, StringMap *storage); bool getModEntries(const std::string &modname, StringMap *storage);
bool getModKeys(const std::string &modname, std::vector<std::string> *storage); bool getModKeys(const std::string &modname, std::vector<std::string> *storage);

@ -763,12 +763,12 @@ void AuthDatabaseSQLite3::writePrivileges(const AuthEntry &authEntry)
} }
} }
ModMetadataDatabaseSQLite3::ModMetadataDatabaseSQLite3(const std::string &savedir): ModStorageDatabaseSQLite3::ModStorageDatabaseSQLite3(const std::string &savedir):
Database_SQLite3(savedir, "mod_storage"), ModMetadataDatabase() Database_SQLite3(savedir, "mod_storage"), ModStorageDatabase()
{ {
} }
ModMetadataDatabaseSQLite3::~ModMetadataDatabaseSQLite3() ModStorageDatabaseSQLite3::~ModStorageDatabaseSQLite3()
{ {
FINALIZE_STATEMENT(m_stmt_remove_all) FINALIZE_STATEMENT(m_stmt_remove_all)
FINALIZE_STATEMENT(m_stmt_remove) FINALIZE_STATEMENT(m_stmt_remove)
@ -779,7 +779,7 @@ ModMetadataDatabaseSQLite3::~ModMetadataDatabaseSQLite3()
FINALIZE_STATEMENT(m_stmt_get_all) FINALIZE_STATEMENT(m_stmt_get_all)
} }
void ModMetadataDatabaseSQLite3::createDatabase() void ModStorageDatabaseSQLite3::createDatabase()
{ {
assert(m_database); // Pre-condition assert(m_database); // Pre-condition
@ -794,7 +794,7 @@ void ModMetadataDatabaseSQLite3::createDatabase()
"Failed to create database table"); "Failed to create database table");
} }
void ModMetadataDatabaseSQLite3::initStatements() void ModStorageDatabaseSQLite3::initStatements()
{ {
PREPARE_STATEMENT(get_all, "SELECT `key`, `value` FROM `entries` WHERE `modname` = ?"); PREPARE_STATEMENT(get_all, "SELECT `key`, `value` FROM `entries` WHERE `modname` = ?");
PREPARE_STATEMENT(get_keys, "SELECT `key` FROM `entries` WHERE `modname` = ?"); PREPARE_STATEMENT(get_keys, "SELECT `key` FROM `entries` WHERE `modname` = ?");
@ -808,7 +808,7 @@ void ModMetadataDatabaseSQLite3::initStatements()
PREPARE_STATEMENT(remove_all, "DELETE FROM `entries` WHERE `modname` = ?"); PREPARE_STATEMENT(remove_all, "DELETE FROM `entries` WHERE `modname` = ?");
} }
bool ModMetadataDatabaseSQLite3::getModEntries(const std::string &modname, StringMap *storage) bool ModStorageDatabaseSQLite3::getModEntries(const std::string &modname, StringMap *storage)
{ {
verifyDatabase(); verifyDatabase();
@ -827,7 +827,7 @@ bool ModMetadataDatabaseSQLite3::getModEntries(const std::string &modname, Strin
return true; return true;
} }
bool ModMetadataDatabaseSQLite3::getModKeys(const std::string &modname, bool ModStorageDatabaseSQLite3::getModKeys(const std::string &modname,
std::vector<std::string> *storage) std::vector<std::string> *storage)
{ {
verifyDatabase(); verifyDatabase();
@ -845,7 +845,7 @@ bool ModMetadataDatabaseSQLite3::getModKeys(const std::string &modname,
return true; return true;
} }
bool ModMetadataDatabaseSQLite3::getModEntry(const std::string &modname, bool ModStorageDatabaseSQLite3::getModEntry(const std::string &modname,
const std::string &key, std::string *value) const std::string &key, std::string *value)
{ {
verifyDatabase(); verifyDatabase();
@ -866,7 +866,7 @@ bool ModMetadataDatabaseSQLite3::getModEntry(const std::string &modname,
return found; return found;
} }
bool ModMetadataDatabaseSQLite3::hasModEntry(const std::string &modname, bool ModStorageDatabaseSQLite3::hasModEntry(const std::string &modname,
const std::string &key) const std::string &key)
{ {
verifyDatabase(); verifyDatabase();
@ -883,7 +883,7 @@ bool ModMetadataDatabaseSQLite3::hasModEntry(const std::string &modname,
return found; return found;
} }
bool ModMetadataDatabaseSQLite3::setModEntry(const std::string &modname, bool ModStorageDatabaseSQLite3::setModEntry(const std::string &modname,
const std::string &key, const std::string &value) const std::string &key, const std::string &value)
{ {
verifyDatabase(); verifyDatabase();
@ -900,7 +900,7 @@ bool ModMetadataDatabaseSQLite3::setModEntry(const std::string &modname,
return true; return true;
} }
bool ModMetadataDatabaseSQLite3::removeModEntry(const std::string &modname, bool ModStorageDatabaseSQLite3::removeModEntry(const std::string &modname,
const std::string &key) const std::string &key)
{ {
verifyDatabase(); verifyDatabase();
@ -916,7 +916,7 @@ bool ModMetadataDatabaseSQLite3::removeModEntry(const std::string &modname,
return changes > 0; return changes > 0;
} }
bool ModMetadataDatabaseSQLite3::removeModEntries(const std::string &modname) bool ModStorageDatabaseSQLite3::removeModEntries(const std::string &modname)
{ {
verifyDatabase(); verifyDatabase();
@ -929,7 +929,7 @@ bool ModMetadataDatabaseSQLite3::removeModEntries(const std::string &modname)
return changes > 0; return changes > 0;
} }
void ModMetadataDatabaseSQLite3::listMods(std::vector<std::string> *res) void ModStorageDatabaseSQLite3::listMods(std::vector<std::string> *res)
{ {
verifyDatabase(); verifyDatabase();

@ -233,11 +233,11 @@ private:
sqlite3_stmt *m_stmt_last_insert_rowid = nullptr; sqlite3_stmt *m_stmt_last_insert_rowid = nullptr;
}; };
class ModMetadataDatabaseSQLite3 : private Database_SQLite3, public ModMetadataDatabase class ModStorageDatabaseSQLite3 : private Database_SQLite3, public ModStorageDatabase
{ {
public: public:
ModMetadataDatabaseSQLite3(const std::string &savedir); ModStorageDatabaseSQLite3(const std::string &savedir);
virtual ~ModMetadataDatabaseSQLite3(); virtual ~ModStorageDatabaseSQLite3();
virtual bool getModEntries(const std::string &modname, StringMap *storage); virtual bool getModEntries(const std::string &modname, StringMap *storage);
virtual bool getModKeys(const std::string &modname, std::vector<std::string> *storage); virtual bool getModKeys(const std::string &modname, std::vector<std::string> *storage);

@ -86,10 +86,10 @@ public:
virtual void reload() = 0; virtual void reload() = 0;
}; };
class ModMetadataDatabase : public Database class ModStorageDatabase : public Database
{ {
public: public:
virtual ~ModMetadataDatabase() = default; virtual ~ModStorageDatabase() = default;
virtual bool getModEntries(const std::string &modname, StringMap *storage) = 0; virtual bool getModEntries(const std::string &modname, StringMap *storage) = 0;
virtual bool getModKeys(const std::string &modname, std::vector<std::string> *storage) = 0; virtual bool getModKeys(const std::string &modname, std::vector<std::string> *storage) = 0;

@ -61,7 +61,7 @@ public:
return emptymodspec; return emptymodspec;
} }
const ModSpec* getModSpec(const std::string &modname) const override { return nullptr; } const ModSpec* getModSpec(const std::string &modname) const override { return nullptr; }
ModMetadataDatabase *getModStorageDatabase() override { return m_mod_storage_database; } ModStorageDatabase *getModStorageDatabase() override { return m_mod_storage_database; }
bool joinModChannel(const std::string &channel) override { return false; } bool joinModChannel(const std::string &channel) override { return false; }
bool leaveModChannel(const std::string &channel) override { return false; } bool leaveModChannel(const std::string &channel) override { return false; }
@ -75,5 +75,5 @@ protected:
IItemDefManager *m_itemdef = nullptr; IItemDefManager *m_itemdef = nullptr;
NodeDefManager *m_nodedef = nullptr; NodeDefManager *m_nodedef = nullptr;
ICraftDefManager *m_craftdef = nullptr; ICraftDefManager *m_craftdef = nullptr;
ModMetadataDatabase *m_mod_storage_database = nullptr; ModStorageDatabase *m_mod_storage_database = nullptr;
}; };

@ -32,8 +32,8 @@ class IRollbackManager;
class EmergeManager; class EmergeManager;
class Camera; class Camera;
class ModChannel; class ModChannel;
class ModMetadata; class ModStorage;
class ModMetadataDatabase; class ModStorageDatabase;
namespace irr { namespace scene { namespace irr { namespace scene {
class IAnimatedMesh; class IAnimatedMesh;
@ -73,7 +73,7 @@ public:
virtual const std::vector<ModSpec> &getMods() const = 0; virtual const std::vector<ModSpec> &getMods() const = 0;
virtual const ModSpec* getModSpec(const std::string &modname) const = 0; virtual const ModSpec* getModSpec(const std::string &modname) const = 0;
virtual std::string getWorldPath() const { return ""; } virtual std::string getWorldPath() const { return ""; }
virtual ModMetadataDatabase *getModStorageDatabase() = 0; virtual ModStorageDatabase *getModStorageDatabase() = 0;
virtual bool joinModChannel(const std::string &channel) = 0; virtual bool joinModChannel(const std::string &channel) = 0;
virtual bool leaveModChannel(const std::string &channel) = 0; virtual bool leaveModChannel(const std::string &channel) = 0;

@ -41,7 +41,7 @@ void ModApiStorage::Initialize(lua_State *L, int top)
API_FCT(get_mod_storage); API_FCT(get_mod_storage);
} }
void StorageRef::create(lua_State *L, const std::string &mod_name, ModMetadataDatabase *db) void StorageRef::create(lua_State *L, const std::string &mod_name, ModStorageDatabase *db)
{ {
StorageRef *o = new StorageRef(mod_name, db); StorageRef *o = new StorageRef(mod_name, db);
*(void **)(lua_newuserdata(L, sizeof(void *))) = o; *(void **)(lua_newuserdata(L, sizeof(void *))) = o;

@ -36,7 +36,7 @@ public:
class StorageRef : public MetaDataRef class StorageRef : public MetaDataRef
{ {
private: private:
ModMetadata m_object; ModStorage m_object;
static const luaL_Reg methods[]; static const luaL_Reg methods[];
@ -44,11 +44,11 @@ private:
virtual void clearMeta(); virtual void clearMeta();
public: public:
StorageRef(const std::string &mod_name, ModMetadataDatabase *db): m_object(mod_name, db) {} StorageRef(const std::string &mod_name, ModStorageDatabase *db): m_object(mod_name, db) {}
~StorageRef() = default; ~StorageRef() = default;
static void Register(lua_State *L); static void Register(lua_State *L);
static void create(lua_State *L, const std::string &mod_name, ModMetadataDatabase *db); static void create(lua_State *L, const std::string &mod_name, ModStorageDatabase *db);
static const char className[]; static const char className[];
}; };

@ -4004,7 +4004,7 @@ Translations *Server::getTranslationLanguage(const std::string &lang_code)
return translations; return translations;
} }
ModMetadataDatabase *Server::openModStorageDatabase(const std::string &world_path) ModStorageDatabase *Server::openModStorageDatabase(const std::string &world_path)
{ {
std::string world_mt_path = world_path + DIR_DELIM + "world.mt"; std::string world_mt_path = world_path + DIR_DELIM + "world.mt";
Settings world_mt; Settings world_mt;
@ -4022,22 +4022,22 @@ ModMetadataDatabase *Server::openModStorageDatabase(const std::string &world_pat
return openModStorageDatabase(backend, world_path, world_mt); return openModStorageDatabase(backend, world_path, world_mt);
} }
ModMetadataDatabase *Server::openModStorageDatabase(const std::string &backend, ModStorageDatabase *Server::openModStorageDatabase(const std::string &backend,
const std::string &world_path, const Settings &world_mt) const std::string &world_path, const Settings &world_mt)
{ {
if (backend == "sqlite3") if (backend == "sqlite3")
return new ModMetadataDatabaseSQLite3(world_path); return new ModStorageDatabaseSQLite3(world_path);
#if USE_POSTGRESQL #if USE_POSTGRESQL
if (backend == "postgresql") { if (backend == "postgresql") {
std::string connect_string; std::string connect_string;
world_mt.getNoEx("pgsql_mod_storage_connection", connect_string); world_mt.getNoEx("pgsql_mod_storage_connection", connect_string);
return new ModMetadataDatabasePostgreSQL(connect_string); return new ModStorageDatabasePostgreSQL(connect_string);
} }
#endif // USE_POSTGRESQL #endif // USE_POSTGRESQL
if (backend == "files") if (backend == "files")
return new ModMetadataDatabaseFiles(world_path); return new ModStorageDatabaseFiles(world_path);
if (backend == "dummy") if (backend == "dummy")
return new Database_Dummy(); return new Database_Dummy();
@ -4063,8 +4063,8 @@ bool Server::migrateModStorageDatabase(const GameParams &game_params, const Sett
return false; return false;
} }
ModMetadataDatabase *srcdb = nullptr; ModStorageDatabase *srcdb = nullptr;
ModMetadataDatabase *dstdb = nullptr; ModStorageDatabase *dstdb = nullptr;
bool succeeded = false; bool succeeded = false;

@ -276,7 +276,7 @@ public:
virtual u16 allocateUnknownNodeId(const std::string &name); virtual u16 allocateUnknownNodeId(const std::string &name);
IRollbackManager *getRollbackManager() { return m_rollback; } IRollbackManager *getRollbackManager() { return m_rollback; }
virtual EmergeManager *getEmergeManager() { return m_emerge; } virtual EmergeManager *getEmergeManager() { return m_emerge; }
virtual ModMetadataDatabase *getModStorageDatabase() { return m_mod_storage_database; } virtual ModStorageDatabase *getModStorageDatabase() { return m_mod_storage_database; }
IWritableItemDefManager* getWritableItemDefManager(); IWritableItemDefManager* getWritableItemDefManager();
NodeDefManager* getWritableNodeDefManager(); NodeDefManager* getWritableNodeDefManager();
@ -368,9 +368,9 @@ public:
// Get or load translations for a language // Get or load translations for a language
Translations *getTranslationLanguage(const std::string &lang_code); Translations *getTranslationLanguage(const std::string &lang_code);
static ModMetadataDatabase *openModStorageDatabase(const std::string &world_path); static ModStorageDatabase *openModStorageDatabase(const std::string &world_path);
static ModMetadataDatabase *openModStorageDatabase(const std::string &backend, static ModStorageDatabase *openModStorageDatabase(const std::string &backend,
const std::string &world_path, const Settings &world_mt); const std::string &world_path, const Settings &world_mt);
static bool migrateModStorageDatabase(const GameParams &game_params, static bool migrateModStorageDatabase(const GameParams &game_params,
@ -694,7 +694,7 @@ private:
s32 m_next_sound_id = 0; // positive values only s32 m_next_sound_id = 0; // positive values only
s32 nextSoundId(); s32 nextSoundId();
ModMetadataDatabase *m_mod_storage_database = nullptr; ModStorageDatabase *m_mod_storage_database = nullptr;
float m_mod_storage_save_timer = 10.0f; float m_mod_storage_save_timer = 10.0f;
// CSM restrictions byteflag // CSM restrictions byteflag

@ -16,7 +16,7 @@ set (UNITTEST_SRCS
${CMAKE_CURRENT_SOURCE_DIR}/test_map_settings_manager.cpp ${CMAKE_CURRENT_SOURCE_DIR}/test_map_settings_manager.cpp
${CMAKE_CURRENT_SOURCE_DIR}/test_mapnode.cpp ${CMAKE_CURRENT_SOURCE_DIR}/test_mapnode.cpp
${CMAKE_CURRENT_SOURCE_DIR}/test_modchannels.cpp ${CMAKE_CURRENT_SOURCE_DIR}/test_modchannels.cpp
${CMAKE_CURRENT_SOURCE_DIR}/test_modmetadatadatabase.cpp ${CMAKE_CURRENT_SOURCE_DIR}/test_modstoragedatabase.cpp
${CMAKE_CURRENT_SOURCE_DIR}/test_moveaction.cpp ${CMAKE_CURRENT_SOURCE_DIR}/test_moveaction.cpp
${CMAKE_CURRENT_SOURCE_DIR}/test_nodedef.cpp ${CMAKE_CURRENT_SOURCE_DIR}/test_nodedef.cpp
${CMAKE_CURRENT_SOURCE_DIR}/test_noderesolver.cpp ${CMAKE_CURRENT_SOURCE_DIR}/test_noderesolver.cpp

@ -1,359 +0,0 @@
/*
Minetest
Copyright (C) 2018 bendeutsch, Ben Deutsch <ben@bendeutsch.de>
Copyright (C) 2021 TurkeyMcMac, Jude Melton-Houghton <jwmhjwmh@gmail.com>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
// This file is an edited copy of test_authdatabase.cpp
#include "cmake_config.h"
#include "test.h"
#include <algorithm>
#include <cstdlib>
#include "database/database-dummy.h"
#include "database/database-files.h"
#include "database/database-sqlite3.h"
#if USE_POSTGRESQL
#include "database/database-postgresql.h"
#endif
#include "filesys.h"
namespace
{
// Anonymous namespace to create classes that are only
// visible to this file
//
// These are helpers that return a *ModMetadataDatabase and
// allow us to run the same tests on different databases and
// database acquisition strategies.
class ModMetadataDatabaseProvider
{
public:
virtual ~ModMetadataDatabaseProvider() = default;
virtual ModMetadataDatabase *getModMetadataDatabase() = 0;
};
class FixedProvider : public ModMetadataDatabaseProvider
{
public:
FixedProvider(ModMetadataDatabase *mod_meta_db) : mod_meta_db(mod_meta_db){};
virtual ~FixedProvider(){};
virtual ModMetadataDatabase *getModMetadataDatabase() { return mod_meta_db; };
private:
ModMetadataDatabase *mod_meta_db;
};
class FilesProvider : public ModMetadataDatabaseProvider
{
public:
FilesProvider(const std::string &dir) : dir(dir){};
virtual ~FilesProvider()
{
if (mod_meta_db)
mod_meta_db->endSave();
delete mod_meta_db;
}
virtual ModMetadataDatabase *getModMetadataDatabase()
{
if (mod_meta_db)
mod_meta_db->endSave();
delete mod_meta_db;
mod_meta_db = new ModMetadataDatabaseFiles(dir);
mod_meta_db->beginSave();
return mod_meta_db;
};
private:
std::string dir;
ModMetadataDatabase *mod_meta_db = nullptr;
};
class SQLite3Provider : public ModMetadataDatabaseProvider
{
public:
SQLite3Provider(const std::string &dir) : dir(dir){};
virtual ~SQLite3Provider()
{
if (mod_meta_db)
mod_meta_db->endSave();
delete mod_meta_db;
}
virtual ModMetadataDatabase *getModMetadataDatabase()
{
if (mod_meta_db)
mod_meta_db->endSave();
delete mod_meta_db;
mod_meta_db = new ModMetadataDatabaseSQLite3(dir);
mod_meta_db->beginSave();
return mod_meta_db;
};
private:
std::string dir;
ModMetadataDatabase *mod_meta_db = nullptr;
};
#if USE_POSTGRESQL
void clearPostgreSQLDatabase(const std::string &connect_string)
{
ModMetadataDatabasePostgreSQL db(connect_string);
std::vector<std::string> modnames;
db.beginSave();
db.listMods(&modnames);
for (const std::string &modname : modnames)
db.removeModEntries(modname);
db.endSave();
}
class PostgreSQLProvider : public ModMetadataDatabaseProvider
{
public:
PostgreSQLProvider(const std::string &connect_string): m_connect_string(connect_string) {}
~PostgreSQLProvider()
{
if (m_db)
m_db->endSave();
delete m_db;
}
ModMetadataDatabase *getModMetadataDatabase() override
{
if (m_db)
m_db->endSave();
delete m_db;
m_db = new ModMetadataDatabasePostgreSQL(m_connect_string);
m_db->beginSave();
return m_db;
};
private:
std::string m_connect_string;
ModMetadataDatabase *m_db = nullptr;
};
#endif // USE_POSTGRESQL
}
class TestModMetadataDatabase : public TestBase
{
public:
TestModMetadataDatabase() { TestManager::registerTestModule(this); }
const char *getName() { return "TestModMetadataDatabase"; }
void runTests(IGameDef *gamedef);
void runTestsForCurrentDB();
void testRecallFail();
void testCreate();
void testRecall();
void testChange();
void testRecallChanged();
void testListMods();
void testRemove();
private:
ModMetadataDatabaseProvider *mod_meta_provider;
};
static TestModMetadataDatabase g_test_instance;
void TestModMetadataDatabase::runTests(IGameDef *gamedef)
{
// fixed directory, for persistence
thread_local const std::string test_dir = getTestTempDirectory();
// Each set of tests is run twice for each database type except dummy:
// one where we reuse the same ModMetadataDatabase object (to test local caching),
// and one where we create a new ModMetadataDatabase object for each call
// (to test actual persistence).
// Since the dummy database is only in-memory, it has no persistence to test.
ModMetadataDatabase *mod_meta_db;
rawstream << "-------- Dummy database (same object only)" << std::endl;
mod_meta_db = new Database_Dummy();
mod_meta_provider = new FixedProvider(mod_meta_db);
runTestsForCurrentDB();
delete mod_meta_db;
delete mod_meta_provider;
rawstream << "-------- Files database (same object)" << std::endl;
mod_meta_db = new ModMetadataDatabaseFiles(test_dir);
mod_meta_provider = new FixedProvider(mod_meta_db);
runTestsForCurrentDB();
delete mod_meta_db;
delete mod_meta_provider;
// reset database
fs::RecursiveDelete(test_dir + DIR_DELIM + "mod_storage");
rawstream << "-------- Files database (new objects)" << std::endl;
mod_meta_provider = new FilesProvider(test_dir);
runTestsForCurrentDB();
delete mod_meta_provider;
rawstream << "-------- SQLite3 database (same object)" << std::endl;
mod_meta_db = new ModMetadataDatabaseSQLite3(test_dir);
mod_meta_provider = new FixedProvider(mod_meta_db);
runTestsForCurrentDB();
delete mod_meta_db;
delete mod_meta_provider;
// reset database
fs::DeleteSingleFileOrEmptyDirectory(test_dir + DIR_DELIM + "mod_storage.sqlite");
rawstream << "-------- SQLite3 database (new objects)" << std::endl;
mod_meta_provider = new SQLite3Provider(test_dir);
runTestsForCurrentDB();
delete mod_meta_provider;
#if USE_POSTGRESQL
const char *env_postgresql_connect_string = getenv("MINETEST_POSTGRESQL_CONNECT_STRING");
if (env_postgresql_connect_string) {
std::string connect_string(env_postgresql_connect_string);
rawstream << "-------- PostgreSQL database (same object)" << std::endl;
clearPostgreSQLDatabase(connect_string);
mod_meta_db = new ModMetadataDatabasePostgreSQL(connect_string);
mod_meta_provider = new FixedProvider(mod_meta_db);
runTestsForCurrentDB();
delete mod_meta_db;
delete mod_meta_provider;
rawstream << "-------- PostgreSQL database (new objects)" << std::endl;
clearPostgreSQLDatabase(connect_string);
mod_meta_provider = new PostgreSQLProvider(connect_string);
runTestsForCurrentDB();
delete mod_meta_provider;
}
#endif // USE_POSTGRESQL
}
////////////////////////////////////////////////////////////////////////////////
void TestModMetadataDatabase::runTestsForCurrentDB()
{
TEST(testRecallFail);
TEST(testCreate);
TEST(testRecall);
TEST(testChange);
TEST(testRecallChanged);
TEST(testListMods);
TEST(testRemove);
TEST(testRecallFail);
}
void TestModMetadataDatabase::testRecallFail()
{
ModMetadataDatabase *mod_meta_db = mod_meta_provider->getModMetadataDatabase();
StringMap recalled;
std::vector<std::string> recalled_keys;
mod_meta_db->getModEntries("mod1", &recalled);
mod_meta_db->getModKeys("mod1", &recalled_keys);
UASSERT(recalled.empty());
UASSERT(recalled_keys.empty());
std::string key1_value;
UASSERT(!mod_meta_db->getModEntry("mod1", "key1", &key1_value));
UASSERT(!mod_meta_db->hasModEntry("mod1", "key1"));
}
void TestModMetadataDatabase::testCreate()
{
ModMetadataDatabase *mod_meta_db = mod_meta_provider->getModMetadataDatabase();
UASSERT(mod_meta_db->setModEntry("mod1", "key1", "value1"));
}
void TestModMetadataDatabase::testRecall()
{
ModMetadataDatabase *mod_meta_db = mod_meta_provider->getModMetadataDatabase();
StringMap recalled;
std::vector<std::string> recalled_keys;
mod_meta_db->getModEntries("mod1", &recalled);
mod_meta_db->getModKeys("mod1", &recalled_keys);
UASSERTCMP(std::size_t, ==, recalled.size(), 1);
UASSERTCMP(std::size_t, ==, recalled_keys.size(), 1);
UASSERTCMP(std::string, ==, recalled["key1"], "value1");
UASSERTCMP(std::string, ==, recalled_keys[0], "key1");
std::string key1_value;
UASSERT(mod_meta_db->getModEntry("mod1", "key1", &key1_value));
UASSERTCMP(std::string, ==, key1_value, "value1");
UASSERT(mod_meta_db->hasModEntry("mod1", "key1"));
}
void TestModMetadataDatabase::testChange()
{
ModMetadataDatabase *mod_meta_db = mod_meta_provider->getModMetadataDatabase();
UASSERT(mod_meta_db->setModEntry("mod1", "key1", "value2"));
}
void TestModMetadataDatabase::testRecallChanged()
{
ModMetadataDatabase *mod_meta_db = mod_meta_provider->getModMetadataDatabase();
StringMap recalled;
mod_meta_db->getModEntries("mod1", &recalled);
UASSERTCMP(std::size_t, ==, recalled.size(), 1);
UASSERTCMP(std::string, ==, recalled["key1"], "value2");
std::string key1_value;
UASSERT(mod_meta_db->getModEntry("mod1", "key1", &key1_value));
UASSERTCMP(std::string, ==, key1_value, "value2");
UASSERT(mod_meta_db->hasModEntry("mod1", "key1"));
}
void TestModMetadataDatabase::testListMods()
{
ModMetadataDatabase *mod_meta_db = mod_meta_provider->getModMetadataDatabase();
UASSERT(mod_meta_db->setModEntry("mod2", "key1", "value1"));
UASSERT(mod_meta_db->setModEntry("mod2", "key2", "value1"));
std::vector<std::string> mod_list;
mod_meta_db->listMods(&mod_list);
UASSERTCMP(size_t, ==, mod_list.size(), 2);
UASSERT(std::find(mod_list.cbegin(), mod_list.cend(), "mod1") != mod_list.cend());
UASSERT(std::find(mod_list.cbegin(), mod_list.cend(), "mod2") != mod_list.cend());
}
void TestModMetadataDatabase::testRemove()
{
ModMetadataDatabase *mod_meta_db = mod_meta_provider->getModMetadataDatabase();
UASSERT(mod_meta_db->removeModEntry("mod1", "key1"));
UASSERT(!mod_meta_db->removeModEntries("mod1"));
UASSERT(mod_meta_db->removeModEntries("mod2"));
}

@ -0,0 +1,365 @@
/*
Minetest
Copyright (C) 2018 bendeutsch, Ben Deutsch <ben@bendeutsch.de>
Copyright (C) 2021 TurkeyMcMac, Jude Melton-Houghton <jwmhjwmh@gmail.com>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
// This file is an edited copy of test_authdatabase.cpp
#include "cmake_config.h"
#include "test.h"
#include <algorithm>
#include <cstdlib>
#include "database/database-dummy.h"
#include "database/database-files.h"
#include "database/database-sqlite3.h"
#if USE_POSTGRESQL
#include "database/database-postgresql.h"
#endif
#include "filesys.h"
namespace
{
// Anonymous namespace to create classes that are only
// visible to this file
//
// These are helpers that return a *ModStorageDatabase and
// allow us to run the same tests on different databases and
// database acquisition strategies.
class ModStorageDatabaseProvider
{
public:
virtual ~ModStorageDatabaseProvider() = default;
virtual ModStorageDatabase *getModStorageDatabase() = 0;
};
class FixedProvider : public ModStorageDatabaseProvider
{
public:
FixedProvider(ModStorageDatabase *db): m_db(db) {}
~FixedProvider() = default;
ModStorageDatabase *getModStorageDatabase() override { return m_db; }
private:
ModStorageDatabase *m_db;
};
class FilesProvider : public ModStorageDatabaseProvider
{
public:
FilesProvider(const std::string &dir): m_dir(dir) {}
~FilesProvider()
{
if (m_db)
m_db->endSave();
delete m_db;
}
ModStorageDatabase *getModStorageDatabase() override
{
if (m_db)
m_db->endSave();
delete m_db;
m_db = new ModStorageDatabaseFiles(m_dir);
m_db->beginSave();
return m_db;
}
private:
std::string m_dir;
ModStorageDatabase *m_db = nullptr;
};
class SQLite3Provider : public ModStorageDatabaseProvider
{
public:
SQLite3Provider(const std::string &dir): m_dir(dir) {}
~SQLite3Provider()
{
if (m_db)
m_db->endSave();
delete m_db;
}
ModStorageDatabase *getModStorageDatabase() override
{
if (m_db)
m_db->endSave();
delete m_db;
m_db = new ModStorageDatabaseSQLite3(m_dir);
m_db->beginSave();
return m_db;
}
private:
std::string m_dir;
ModStorageDatabase *m_db = nullptr;
};
#if USE_POSTGRESQL
void clearPostgreSQLDatabase(const std::string &connect_string)
{
ModStorageDatabasePostgreSQL db(connect_string);
std::vector<std::string> modnames;
db.beginSave();
db.listMods(&modnames);
for (const std::string &modname : modnames)
db.removeModEntries(modname);
db.endSave();
}
class PostgreSQLProvider : public ModStorageDatabaseProvider
{
public:
PostgreSQLProvider(const std::string &connect_string): m_connect_string(connect_string) {}
~PostgreSQLProvider()
{
if (m_db)
m_db->endSave();
delete m_db;
}
ModStorageDatabase *getModStorageDatabase() override
{
if (m_db)
m_db->endSave();
delete m_db;
m_db = new ModStorageDatabasePostgreSQL(m_connect_string);
m_db->beginSave();
return m_db;
};
private:
std::string m_connect_string;
ModStorageDatabase *m_db = nullptr;
};
#endif // USE_POSTGRESQL
}
class TestModStorageDatabase : public TestBase
{
public:
TestModStorageDatabase() { TestManager::registerTestModule(this); }
const char *getName() { return "TestModStorageDatabase"; }
void runTests(IGameDef *gamedef);
void runTestsForCurrentDB();
void testRecallFail();
void testCreate();
void testRecall();
void testChange();
void testRecallChanged();
void testListMods();
void testRemove();
private:
ModStorageDatabaseProvider *mod_storage_provider;
};
static TestModStorageDatabase g_test_instance;
void TestModStorageDatabase::runTests(IGameDef *gamedef)
{
// fixed directory, for persistence
thread_local const std::string test_dir = getTestTempDirectory();
// Each set of tests is run twice for each database type except dummy:
// one where we reuse the same ModStorageDatabase object (to test local caching),
// and one where we create a new ModStorageDatabase object for each call
// (to test actual persistence).
// Since the dummy database is only in-memory, it has no persistence to test.
ModStorageDatabase *mod_storage_db;
rawstream << "-------- Dummy database (same object only)" << std::endl;
mod_storage_db = new Database_Dummy();
mod_storage_provider = new FixedProvider(mod_storage_db);
runTestsForCurrentDB();
delete mod_storage_db;
delete mod_storage_provider;
rawstream << "-------- Files database (same object)" << std::endl;
mod_storage_db = new ModStorageDatabaseFiles(test_dir);
mod_storage_provider = new FixedProvider(mod_storage_db);
runTestsForCurrentDB();
delete mod_storage_db;
delete mod_storage_provider;
// reset database
fs::RecursiveDelete(test_dir + DIR_DELIM + "mod_storage");
rawstream << "-------- Files database (new objects)" << std::endl;
mod_storage_provider = new FilesProvider(test_dir);
runTestsForCurrentDB();
delete mod_storage_provider;
rawstream << "-------- SQLite3 database (same object)" << std::endl;
mod_storage_db = new ModStorageDatabaseSQLite3(test_dir);
mod_storage_provider = new FixedProvider(mod_storage_db);
runTestsForCurrentDB();
delete mod_storage_db;
delete mod_storage_provider;
// reset database
fs::DeleteSingleFileOrEmptyDirectory(test_dir + DIR_DELIM + "mod_storage.sqlite");
rawstream << "-------- SQLite3 database (new objects)" << std::endl;
mod_storage_provider = new SQLite3Provider(test_dir);
runTestsForCurrentDB();
delete mod_storage_provider;
#if USE_POSTGRESQL
const char *env_postgresql_connect_string = getenv("MINETEST_POSTGRESQL_CONNECT_STRING");
if (env_postgresql_connect_string) {
std::string connect_string(env_postgresql_connect_string);
rawstream << "-------- PostgreSQL database (same object)" << std::endl;
clearPostgreSQLDatabase(connect_string);
mod_storage_db = new ModStorageDatabasePostgreSQL(connect_string);
mod_storage_provider = new FixedProvider(mod_storage_db);
runTestsForCurrentDB();
delete mod_storage_db;
delete mod_storage_provider;
rawstream << "-------- PostgreSQL database (new objects)" << std::endl;
clearPostgreSQLDatabase(connect_string);
mod_storage_provider = new PostgreSQLProvider(connect_string);
runTestsForCurrentDB();
delete mod_storage_provider;
}
#endif // USE_POSTGRESQL
}
////////////////////////////////////////////////////////////////////////////////
void TestModStorageDatabase::runTestsForCurrentDB()
{
TEST(testRecallFail);
TEST(testCreate);
TEST(testRecall);
TEST(testChange);
TEST(testRecallChanged);
TEST(testListMods);
TEST(testRemove);
TEST(testRecallFail);
}
void TestModStorageDatabase::testRecallFail()
{
ModStorageDatabase *mod_storage_db = mod_storage_provider->getModStorageDatabase();
StringMap recalled;
std::vector<std::string> recalled_keys;
mod_storage_db->getModEntries("mod1", &recalled);
mod_storage_db->getModKeys("mod1", &recalled_keys);
UASSERT(recalled.empty());
UASSERT(recalled_keys.empty());
std::string key1_value;
UASSERT(!mod_storage_db->getModEntry("mod1", "key1", &key1_value));
UASSERT(!mod_storage_db->hasModEntry("mod1", "key1"));
}
void TestModStorageDatabase::testCreate()
{
ModStorageDatabase *mod_storage_db = mod_storage_provider->getModStorageDatabase();
UASSERT(mod_storage_db->setModEntry("mod1", "key1", "value1"));
}
void TestModStorageDatabase::testRecall()
{
ModStorageDatabase *mod_storage_db = mod_storage_provider->getModStorageDatabase();
StringMap recalled;
std::vector<std::string> recalled_keys;
mod_storage_db->getModEntries("mod1", &recalled);
mod_storage_db->getModKeys("mod1", &recalled_keys);
UASSERTCMP(std::size_t, ==, recalled.size(), 1);
UASSERTCMP(std::size_t, ==, recalled_keys.size(), 1);
UASSERTCMP(std::string, ==, recalled["key1"], "value1");
UASSERTCMP(std::string, ==, recalled_keys[0], "key1");
std::string key1_value;
UASSERT(mod_storage_db->getModEntry("mod1", "key1", &key1_value));
UASSERTCMP(std::string, ==, key1_value, "value1");
UASSERT(mod_storage_db->hasModEntry("mod1", "key1"));
}
void TestModStorageDatabase::testChange()
{
ModStorageDatabase *mod_storage_db = mod_storage_provider->getModStorageDatabase();
UASSERT(mod_storage_db->setModEntry("mod1", "key1", "value2"));
}
void TestModStorageDatabase::testRecallChanged()
{
ModStorageDatabase *mod_storage_db = mod_storage_provider->getModStorageDatabase();
StringMap recalled;
mod_storage_db->getModEntries("mod1", &recalled);
UASSERTCMP(std::size_t, ==, recalled.size(), 1);
UASSERTCMP(std::string, ==, recalled["key1"], "value2");
std::string key1_value;
UASSERT(mod_storage_db->getModEntry("mod1", "key1", &key1_value));
UASSERTCMP(std::string, ==, key1_value, "value2");
UASSERT(mod_storage_db->hasModEntry("mod1", "key1"));
}
void TestModStorageDatabase::testListMods()
{
ModStorageDatabase *mod_storage_db = mod_storage_provider->getModStorageDatabase();
UASSERT(mod_storage_db->setModEntry("mod2", "key1", "value1"));
UASSERT(mod_storage_db->setModEntry("mod2", "key2", "value1"));
std::vector<std::string> mod_list;
mod_storage_db->listMods(&mod_list);
UASSERTCMP(size_t, ==, mod_list.size(), 2);
UASSERT(std::find(mod_list.cbegin(), mod_list.cend(), "mod1") != mod_list.cend());
UASSERT(std::find(mod_list.cbegin(), mod_list.cend(), "mod2") != mod_list.cend());
}
void TestModStorageDatabase::testRemove()
{
ModStorageDatabase *mod_storage_db = mod_storage_provider->getModStorageDatabase();
UASSERT(mod_storage_db->removeModEntry("mod1", "key1"));
UASSERT(!mod_storage_db->removeModEntries("mod1"));
UASSERT(mod_storage_db->removeModEntries("mod2"));
}