return true;
}
-std::string Database_Dummy::loadBlock(const v3s16 &pos)
+void Database_Dummy::loadBlock(const v3s16 &pos, std::string *block)
{
s64 i = getBlockAsInteger(pos);
std::map<s64, std::string>::iterator it = m_database.find(i);
- if (it == m_database.end())
- return "";
- return it->second;
+ if (it == m_database.end()) {
+ *block = "";
+ return;
+ }
+
+ *block = it->second;
}
bool Database_Dummy::deleteBlock(const v3s16 &pos)
class Database_Dummy : public Database
{
public:
- 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);
+ 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);
private:
std::map<s64, std::string> m_database;
return true;
}
-std::string Database_LevelDB::loadBlock(const v3s16 &pos)
+void Database_LevelDB::loadBlock(const v3s16 &pos, std::string *block)
{
std::string datastr;
leveldb::Status status = m_database->Get(leveldb::ReadOptions(),
i64tos(getBlockAsInteger(pos)), &datastr);
- if(status.ok())
- return datastr;
- else
- return "";
+ *block = (status.ok()) ? datastr : "";
}
bool Database_LevelDB::deleteBlock(const v3s16 &pos)
Database_LevelDB(const std::string &savedir);
~Database_LevelDB();
- 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);
+ 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);
private:
leveldb::DB *m_database;
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));
redisReply *reply = static_cast<redisReply *>(redisCommand(ctx,
throw FileNotGoodException(std::string(
"Redis command 'HGET %s %s' failed: ") + ctx->errstr);
}
+
switch (reply->type) {
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
freeReplyObject(reply);
- return str;
+ return;
}
case REDIS_REPLY_ERROR: {
std::string errstr(reply->str, reply->len);
"Redis command 'HGET %s %s' errored: ") + errstr);
}
case REDIS_REPLY_NIL: {
+ *block = "";
// block not found in database
freeReplyObject(reply);
- return "";
+ return;
}
}
+
errorstream << "loadBlock: loading block " << PP(pos)
<< " returned invalid reply type " << reply->type
<< ": " << std::string(reply->str, reply->len) << std::endl;
Database_Redis(Settings &conf);
~Database_Redis();
- virtual void beginSave();
- virtual void endSave();
+ void beginSave();
+ 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);
+ 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);
private:
redisContext *ctx;
return true;
}
-std::string Database_SQLite3::loadBlock(const v3s16 &pos)
+void Database_SQLite3::loadBlock(const v3s16 &pos, std::string *block)
{
verifyDatabase();
if (sqlite3_step(m_stmt_read) != SQLITE_ROW) {
sqlite3_reset(m_stmt_read);
- return "";
+ return;
}
+
const char *data = (const char *) sqlite3_column_blob(m_stmt_read, 0);
size_t len = sqlite3_column_bytes(m_stmt_read, 0);
- std::string s;
- if (data)
- s = std::string(data, len);
+ *block = (data) ? std::string(data, len) : "";
sqlite3_step(m_stmt_read);
// We should never get more than 1 row, so ok to reset
sqlite3_reset(m_stmt_read);
-
- return s;
}
void Database_SQLite3::createDatabase()
{
public:
Database_SQLite3(const std::string &savedir);
+ ~Database_SQLite3();
- virtual void beginSave();
- virtual void endSave();
+ void beginSave();
+ 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();
+ 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:
// Open the database
virtual void endSave() {}
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;
static s64 getBlockAsInteger(const v3s16 &pos);
for (std::vector<v3s16>::const_iterator it = blocks.begin(); it != blocks.end(); ++it) {
if (kill) return false;
- const std::string &data = old_db->loadBlock(*it);
+ std::string data;
+ old_db->loadBlock(*it, &data);
if (!data.empty()) {
new_db->saveBlock(*it, data);
} else {
v2s16 p2d(blockpos.X, blockpos.Z);
std::string ret;
-
- ret = dbase->loadBlock(blockpos);
+ dbase->loadBlock(blockpos, &ret);
if (ret != "") {
loadBlock(&ret, blockpos, createSector(p2d), false);
return getBlockNoCreateNoEx(blockpos);