1
0
mirror of https://github.com/minetest/minetest.git synced 2024-11-10 12:20:39 +01:00

DB::loadBlock copy removal & DB backend cleanup

* Remove the copy from db::loadBlock by using a pointer to the destination
* cleanup db backend, the child backend doesn't have to set their functions as virtual
This commit is contained in:
Loic Blot 2016-05-14 12:23:15 +02:00
parent decbd396df
commit 143401451c
11 changed files with 47 additions and 47 deletions

View File

@ -30,13 +30,16 @@ bool Database_Dummy::saveBlock(const v3s16 &pos, const std::string &data)
return true; return true;
} }
std::string Database_Dummy::loadBlock(const v3s16 &pos) void Database_Dummy::loadBlock(const v3s16 &pos, std::string *block)
{ {
s64 i = getBlockAsInteger(pos); s64 i = getBlockAsInteger(pos);
std::map<s64, std::string>::iterator it = m_database.find(i); std::map<s64, std::string>::iterator it = m_database.find(i);
if (it == m_database.end()) if (it == m_database.end()) {
return ""; *block = "";
return it->second; return;
}
*block = it->second;
} }
bool Database_Dummy::deleteBlock(const v3s16 &pos) bool Database_Dummy::deleteBlock(const v3s16 &pos)

View File

@ -28,10 +28,10 @@ with this program; if not, write to the Free Software Foundation, Inc.,
class Database_Dummy : public Database class Database_Dummy : public Database
{ {
public: public:
virtual bool saveBlock(const v3s16 &pos, const std::string &data); bool saveBlock(const v3s16 &pos, const std::string &data);
virtual std::string loadBlock(const v3s16 &pos); void loadBlock(const v3s16 &pos, std::string *block);
virtual bool deleteBlock(const v3s16 &pos); bool deleteBlock(const v3s16 &pos);
virtual void listAllLoadableBlocks(std::vector<v3s16> &dst); void listAllLoadableBlocks(std::vector<v3s16> &dst);
private: private:
std::map<s64, std::string> m_database; std::map<s64, std::string> m_database;

View File

@ -65,16 +65,13 @@ bool Database_LevelDB::saveBlock(const v3s16 &pos, const std::string &data)
return true; return true;
} }
std::string Database_LevelDB::loadBlock(const v3s16 &pos) void Database_LevelDB::loadBlock(const v3s16 &pos, std::string *block)
{ {
std::string datastr; std::string datastr;
leveldb::Status status = m_database->Get(leveldb::ReadOptions(), leveldb::Status status = m_database->Get(leveldb::ReadOptions(),
i64tos(getBlockAsInteger(pos)), &datastr); i64tos(getBlockAsInteger(pos)), &datastr);
if(status.ok()) *block = (status.ok()) ? datastr : "";
return datastr;
else
return "";
} }
bool Database_LevelDB::deleteBlock(const v3s16 &pos) bool Database_LevelDB::deleteBlock(const v3s16 &pos)

View File

@ -34,10 +34,10 @@ public:
Database_LevelDB(const std::string &savedir); Database_LevelDB(const std::string &savedir);
~Database_LevelDB(); ~Database_LevelDB();
virtual bool saveBlock(const v3s16 &pos, const std::string &data); bool saveBlock(const v3s16 &pos, const std::string &data);
virtual std::string loadBlock(const v3s16 &pos); void loadBlock(const v3s16 &pos, std::string *block);
virtual bool deleteBlock(const v3s16 &pos); bool deleteBlock(const v3s16 &pos);
virtual void listAllLoadableBlocks(std::vector<v3s16> &dst); void listAllLoadableBlocks(std::vector<v3s16> &dst);
private: private:
leveldb::DB *m_database; leveldb::DB *m_database;

View File

@ -101,7 +101,7 @@ bool Database_Redis::saveBlock(const v3s16 &pos, const std::string &data)
return true; return true;
} }
std::string Database_Redis::loadBlock(const v3s16 &pos) void Database_Redis::loadBlock(const v3s16 &pos, std::string *block)
{ {
std::string tmp = i64tos(getBlockAsInteger(pos)); std::string tmp = i64tos(getBlockAsInteger(pos));
redisReply *reply = static_cast<redisReply *>(redisCommand(ctx, redisReply *reply = static_cast<redisReply *>(redisCommand(ctx,
@ -111,12 +111,13 @@ std::string Database_Redis::loadBlock(const v3s16 &pos)
throw FileNotGoodException(std::string( throw FileNotGoodException(std::string(
"Redis command 'HGET %s %s' failed: ") + ctx->errstr); "Redis command 'HGET %s %s' failed: ") + ctx->errstr);
} }
switch (reply->type) { switch (reply->type) {
case REDIS_REPLY_STRING: { case REDIS_REPLY_STRING: {
std::string str(reply->str, reply->len); *block = std::string(reply->str, reply->len);
// std::string copies the memory so this won't cause any problems // std::string copies the memory so this won't cause any problems
freeReplyObject(reply); freeReplyObject(reply);
return str; return;
} }
case REDIS_REPLY_ERROR: { case REDIS_REPLY_ERROR: {
std::string errstr(reply->str, reply->len); std::string errstr(reply->str, reply->len);
@ -127,11 +128,13 @@ std::string Database_Redis::loadBlock(const v3s16 &pos)
"Redis command 'HGET %s %s' errored: ") + errstr); "Redis command 'HGET %s %s' errored: ") + errstr);
} }
case REDIS_REPLY_NIL: { case REDIS_REPLY_NIL: {
*block = "";
// block not found in database // block not found in database
freeReplyObject(reply); freeReplyObject(reply);
return ""; return;
} }
} }
errorstream << "loadBlock: loading block " << PP(pos) errorstream << "loadBlock: loading block " << PP(pos)
<< " returned invalid reply type " << reply->type << " returned invalid reply type " << reply->type
<< ": " << std::string(reply->str, reply->len) << std::endl; << ": " << std::string(reply->str, reply->len) << std::endl;

View File

@ -36,13 +36,13 @@ public:
Database_Redis(Settings &conf); Database_Redis(Settings &conf);
~Database_Redis(); ~Database_Redis();
virtual void beginSave(); void beginSave();
virtual void endSave(); void endSave();
virtual bool saveBlock(const v3s16 &pos, const std::string &data); bool saveBlock(const v3s16 &pos, const std::string &data);
virtual std::string loadBlock(const v3s16 &pos); void loadBlock(const v3s16 &pos, std::string *block);
virtual bool deleteBlock(const v3s16 &pos); bool deleteBlock(const v3s16 &pos);
virtual void listAllLoadableBlocks(std::vector<v3s16> &dst); void listAllLoadableBlocks(std::vector<v3s16> &dst);
private: private:
redisContext *ctx; redisContext *ctx;

View File

@ -237,7 +237,7 @@ bool Database_SQLite3::saveBlock(const v3s16 &pos, const std::string &data)
return true; return true;
} }
std::string Database_SQLite3::loadBlock(const v3s16 &pos) void Database_SQLite3::loadBlock(const v3s16 &pos, std::string *block)
{ {
verifyDatabase(); verifyDatabase();
@ -245,20 +245,17 @@ std::string Database_SQLite3::loadBlock(const v3s16 &pos)
if (sqlite3_step(m_stmt_read) != SQLITE_ROW) { if (sqlite3_step(m_stmt_read) != SQLITE_ROW) {
sqlite3_reset(m_stmt_read); sqlite3_reset(m_stmt_read);
return ""; return;
} }
const char *data = (const char *) sqlite3_column_blob(m_stmt_read, 0); const char *data = (const char *) sqlite3_column_blob(m_stmt_read, 0);
size_t len = sqlite3_column_bytes(m_stmt_read, 0); size_t len = sqlite3_column_bytes(m_stmt_read, 0);
std::string s; *block = (data) ? std::string(data, len) : "";
if (data)
s = std::string(data, len);
sqlite3_step(m_stmt_read); sqlite3_step(m_stmt_read);
// We should never get more than 1 row, so ok to reset // We should never get more than 1 row, so ok to reset
sqlite3_reset(m_stmt_read); sqlite3_reset(m_stmt_read);
return s;
} }
void Database_SQLite3::createDatabase() void Database_SQLite3::createDatabase()

View File

@ -31,17 +31,17 @@ class Database_SQLite3 : public Database
{ {
public: public:
Database_SQLite3(const std::string &savedir); Database_SQLite3(const std::string &savedir);
virtual void beginSave();
virtual void endSave();
virtual bool saveBlock(const v3s16 &pos, const std::string &data);
virtual std::string loadBlock(const v3s16 &pos);
virtual bool deleteBlock(const v3s16 &pos);
virtual void listAllLoadableBlocks(std::vector<v3s16> &dst);
virtual bool initialized() const { return m_initialized; }
~Database_SQLite3(); ~Database_SQLite3();
void beginSave();
void endSave();
bool saveBlock(const v3s16 &pos, const std::string &data);
void loadBlock(const v3s16 &pos, std::string *block);
bool deleteBlock(const v3s16 &pos);
void listAllLoadableBlocks(std::vector<v3s16> &dst);
bool initialized() const { return m_initialized; }
private: private:
// Open the database // Open the database
void openDatabase(); void openDatabase();

View File

@ -38,7 +38,7 @@ public:
virtual void endSave() {} virtual void endSave() {}
virtual bool saveBlock(const v3s16 &pos, const std::string &data) = 0; virtual bool saveBlock(const v3s16 &pos, const std::string &data) = 0;
virtual std::string loadBlock(const v3s16 &pos) = 0; virtual void loadBlock(const v3s16 &pos, std::string *block) = 0;
virtual bool deleteBlock(const v3s16 &pos) = 0; virtual bool deleteBlock(const v3s16 &pos) = 0;
static s64 getBlockAsInteger(const v3s16 &pos); static s64 getBlockAsInteger(const v3s16 &pos);

View File

@ -948,7 +948,8 @@ static bool migrate_database(const GameParams &game_params, const Settings &cmd_
for (std::vector<v3s16>::const_iterator it = blocks.begin(); it != blocks.end(); ++it) { for (std::vector<v3s16>::const_iterator it = blocks.begin(); it != blocks.end(); ++it) {
if (kill) return false; if (kill) return false;
const std::string &data = old_db->loadBlock(*it); std::string data;
old_db->loadBlock(*it, &data);
if (!data.empty()) { if (!data.empty()) {
new_db->saveBlock(*it, data); new_db->saveBlock(*it, data);
} else { } else {

View File

@ -3442,8 +3442,7 @@ MapBlock* ServerMap::loadBlock(v3s16 blockpos)
v2s16 p2d(blockpos.X, blockpos.Z); v2s16 p2d(blockpos.X, blockpos.Z);
std::string ret; std::string ret;
dbase->loadBlock(blockpos, &ret);
ret = dbase->loadBlock(blockpos);
if (ret != "") { if (ret != "") {
loadBlock(&ret, blockpos, createSector(p2d), false); loadBlock(&ret, blockpos, createSector(p2d), false);
return getBlockNoCreateNoEx(blockpos); return getBlockNoCreateNoEx(blockpos);