Clean up database API and save the local map on an interval

This commit is contained in:
ShadowNinja 2014-11-16 15:31:57 -05:00
parent c7454d4732
commit 708337dfc2
16 changed files with 391 additions and 519 deletions

@ -47,10 +47,8 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "config.h" #include "config.h"
#include "version.h" #include "version.h"
#include "drawscene.h" #include "drawscene.h"
#include "subgame.h"
#include "server.h"
#include "database.h"
#include "database-sqlite3.h" #include "database-sqlite3.h"
#include "serialization.h"
extern gui::IGUIEnvironment* guienv; extern gui::IGUIEnvironment* guienv;
@ -266,16 +264,13 @@ Client::Client(
m_time_of_day_update_timer(0), m_time_of_day_update_timer(0),
m_recommended_send_interval(0.1), m_recommended_send_interval(0.1),
m_removed_sounds_check_timer(0), m_removed_sounds_check_timer(0),
m_state(LC_Created) m_state(LC_Created),
m_localdb(NULL)
{ {
/* // Add local player
Add local player m_env.addPlayer(new LocalPlayer(this, playername));
*/
{
Player *player = new LocalPlayer(this, playername);
m_env.addPlayer(player); m_cache_save_interval = g_settings->getU16("server_map_save_interval");
}
m_cache_smooth_lighting = g_settings->getBool("smooth_lighting"); m_cache_smooth_lighting = g_settings->getBool("smooth_lighting");
m_cache_enable_shaders = g_settings->getBool("enable_shaders"); m_cache_enable_shaders = g_settings->getBool("enable_shaders");
@ -285,10 +280,10 @@ void Client::Stop()
{ {
//request all client managed threads to stop //request all client managed threads to stop
m_mesh_update_thread.Stop(); m_mesh_update_thread.Stop();
if (localdb != NULL) { // Save local server map
actionstream << "Local map saving ended" << std::endl; if (m_localdb) {
localdb->endSave(); infostream << "Local map saving ended." << std::endl;
delete localserver; m_localdb->endSave();
} }
} }
@ -679,6 +674,13 @@ void Client::step(float dtime)
Send(pkt); Send(pkt);
} }
} }
// Write server map
if (m_localdb && m_localdb_save_interval.step(dtime,
m_cache_save_interval)) {
m_localdb->endSave();
m_localdb->beginSave();
}
} }
bool Client::loadMedia(const std::string &data, const std::string &filename) bool Client::loadMedia(const std::string &data, const std::string &filename)
@ -813,34 +815,19 @@ void Client::initLocalMapSaving(const Address &address,
const std::string &hostname, const std::string &hostname,
bool is_local_server) bool is_local_server)
{ {
localdb = NULL; if (!g_settings->getBool("enable_local_map_saving") || is_local_server) {
if (!g_settings->getBool("enable_local_map_saving") || is_local_server)
return; return;
}
const std::string world_path = porting::path_user const std::string world_path = porting::path_user
+ DIR_DELIM + "worlds" + DIR_DELIM + "worlds"
+ DIR_DELIM + "server_" + DIR_DELIM + "server_"
+ hostname + "_" + to_string(address.getPort()); + hostname + "_" + to_string(address.getPort());
SubgameSpec gamespec; fs::CreateAllDirs(world_path);
if (!getWorldExists(world_path)) { m_localdb = new Database_SQLite3(world_path);
gamespec = findSubgame(g_settings->get("default_game")); m_localdb->beginSave();
if (!gamespec.isValid())
gamespec = findSubgame("minimal");
} else {
gamespec = findWorldSubgame(world_path);
}
if (!gamespec.isValid()) {
errorstream << "Couldn't find subgame for local map saving." << std::endl;
return;
}
localserver = new Server(world_path, gamespec, false, false);
localdb = new Database_SQLite3(&(ServerMap&)localserver->getMap(), world_path);
localdb->beginSave();
actionstream << "Local map saving started, map will be saved at '" << world_path << "'" << std::endl; actionstream << "Local map saving started, map will be saved at '" << world_path << "'" << std::endl;
} }
@ -1663,7 +1650,8 @@ void Client::makeScreenshot(IrrlichtDevice *device)
if (image) { if (image) {
raw_image->copyTo(image); raw_image->copyTo(image);
irr::c8 filename[256]; irr::c8 filename[256];
snprintf(filename, sizeof(filename), "%s" DIR_DELIM "screenshot_%u.png", snprintf(filename, sizeof(filename),
(std::string("%s") + DIR_DELIM + "screenshot_%u.png").c_str(),
g_settings->get("screenshot_path").c_str(), g_settings->get("screenshot_path").c_str(),
device->getTimer()->getRealTime()); device->getTimer()->getRealTime());
std::ostringstream sstr; std::ostringstream sstr;

@ -48,7 +48,6 @@ struct MapDrawControl;
class MtEventManager; class MtEventManager;
struct PointedThing; struct PointedThing;
class Database; class Database;
class Server;
struct QueuedMeshUpdate struct QueuedMeshUpdate
{ {
@ -620,8 +619,9 @@ private:
LocalClientState m_state; LocalClientState m_state;
// Used for saving server map to disk client-side // Used for saving server map to disk client-side
Database *localdb; Database *m_localdb;
Server *localserver; IntervalLimiter m_localdb_save_interval;
u16 m_cache_save_interval;
// TODO: Add callback to update these when g_settings changes // TODO: Add callback to update these when g_settings changes
bool m_cache_smooth_lighting; bool m_cache_smooth_lighting;

@ -18,64 +18,38 @@ with this program; if not, write to the Free Software Foundation, Inc.,
*/ */
/* /*
Dummy "database" class Dummy database class
*/ */
#include "database-dummy.h" #include "database-dummy.h"
#include "map.h"
#include "mapsector.h"
#include "mapblock.h"
#include "serialization.h"
#include "main.h"
#include "settings.h"
#include "log.h"
Database_Dummy::Database_Dummy(ServerMap *map) bool Database_Dummy::saveBlock(const v3s16 &pos, const std::string &data)
{ {
srvmap = map; m_database[getBlockAsInteger(pos)] = data;
}
int Database_Dummy::Initialized(void)
{
return 1;
}
void Database_Dummy::beginSave() {}
void Database_Dummy::endSave() {}
bool Database_Dummy::saveBlock(v3s16 blockpos, std::string &data)
{
m_database[getBlockAsInteger(blockpos)] = data;
return true; return true;
} }
std::string Database_Dummy::loadBlock(v3s16 blockpos) std::string Database_Dummy::loadBlock(const v3s16 &pos)
{ {
if (m_database.count(getBlockAsInteger(blockpos))) s64 i = getBlockAsInteger(pos);
return m_database[getBlockAsInteger(blockpos)]; if (m_database.count(i))
return m_database[i];
else else
return ""; return "";
} }
bool Database_Dummy::deleteBlock(v3s16 blockpos) bool Database_Dummy::deleteBlock(const v3s16 &pos)
{ {
m_database.erase(getBlockAsInteger(blockpos)); m_database.erase(getBlockAsInteger(pos));
return true; return true;
} }
void Database_Dummy::listAllLoadableBlocks(std::vector<v3s16> &dst) void Database_Dummy::listAllLoadableBlocks(std::vector<v3s16> &dst)
{ {
for(std::map<u64, std::string>::iterator x = m_database.begin(); x != m_database.end(); ++x) for (std::map<s64, std::string>::const_iterator x = m_database.begin();
{ x != m_database.end(); ++x) {
v3s16 p = getIntegerAsBlock(x->first); dst.push_back(getIntegerAsBlock(x->first));
//dstream<<"block_i="<<block_i<<" p="<<PP(p)<<std::endl;
dst.push_back(p);
} }
} }
Database_Dummy::~Database_Dummy()
{
m_database.clear();
}

@ -25,22 +25,17 @@ 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 ServerMap;
class Database_Dummy : public Database class Database_Dummy : public Database
{ {
public: public:
Database_Dummy(ServerMap *map); virtual bool saveBlock(const v3s16 &pos, const std::string &data);
virtual void beginSave(); virtual std::string loadBlock(const v3s16 &pos);
virtual void endSave(); virtual bool deleteBlock(const v3s16 &pos);
virtual bool saveBlock(v3s16 blockpos, std::string &data);
virtual std::string loadBlock(v3s16 blockpos);
virtual bool deleteBlock(v3s16 blockpos);
virtual void listAllLoadableBlocks(std::vector<v3s16> &dst); virtual void listAllLoadableBlocks(std::vector<v3s16> &dst);
virtual int Initialized(void);
~Database_Dummy();
private: private:
ServerMap *srvmap; std::map<s64, std::string> m_database;
std::map<u64, std::string> m_database;
}; };
#endif #endif

@ -22,57 +22,54 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#if USE_LEVELDB #if USE_LEVELDB
#include "database-leveldb.h" #include "database-leveldb.h"
#include "leveldb/db.h"
#include "map.h"
#include "mapsector.h"
#include "mapblock.h"
#include "serialization.h"
#include "main.h"
#include "settings.h"
#include "log.h" #include "log.h"
#include "filesys.h" #include "filesys.h"
#include "exceptions.h"
#include "util/string.h"
#include "leveldb/db.h"
#define ENSURE_STATUS_OK(s) \ #define ENSURE_STATUS_OK(s) \
if (!(s).ok()) { \ if (!(s).ok()) { \
throw FileNotGoodException(std::string("LevelDB error: ") + (s).ToString()); \ throw FileNotGoodException(std::string("LevelDB error: ") + \
(s).ToString()); \
} }
Database_LevelDB::Database_LevelDB(ServerMap *map, std::string savedir)
Database_LevelDB::Database_LevelDB(const std::string &savedir)
{ {
leveldb::Options options; leveldb::Options options;
options.create_if_missing = true; options.create_if_missing = true;
leveldb::Status status = leveldb::DB::Open(options, savedir + DIR_DELIM + "map.db", &m_database); leveldb::Status status = leveldb::DB::Open(options,
savedir + DIR_DELIM + "map.db", &m_database);
ENSURE_STATUS_OK(status); ENSURE_STATUS_OK(status);
srvmap = map;
} }
int Database_LevelDB::Initialized(void) Database_LevelDB::~Database_LevelDB()
{ {
return 1; delete m_database;
} }
void Database_LevelDB::beginSave() {} bool Database_LevelDB::saveBlock(const v3s16 &pos, const std::string &data)
void Database_LevelDB::endSave() {}
bool Database_LevelDB::saveBlock(v3s16 blockpos, std::string &data)
{ {
leveldb::Status status = m_database->Put(leveldb::WriteOptions(), leveldb::Status status = m_database->Put(leveldb::WriteOptions(),
i64tos(getBlockAsInteger(blockpos)), data); i64tos(getBlockAsInteger(pos)), data);
if (!status.ok()) { if (!status.ok()) {
errorstream << "WARNING: saveBlock: LevelDB error saving block " errorstream << "WARNING: saveBlock: LevelDB error saving block "
<< PP(blockpos) << ": " << status.ToString() << std::endl; << PP(pos) << ": " << status.ToString() << std::endl;
return false; return false;
} }
return true; return true;
} }
std::string Database_LevelDB::loadBlock(v3s16 blockpos) std::string Database_LevelDB::loadBlock(const v3s16 &pos)
{ {
std::string datastr; std::string datastr;
leveldb::Status status = m_database->Get(leveldb::ReadOptions(), leveldb::Status status = m_database->Get(leveldb::ReadOptions(),
i64tos(getBlockAsInteger(blockpos)), &datastr); i64tos(getBlockAsInteger(pos)), &datastr);
if(status.ok()) if(status.ok())
return datastr; return datastr;
@ -80,13 +77,13 @@ std::string Database_LevelDB::loadBlock(v3s16 blockpos)
return ""; return "";
} }
bool Database_LevelDB::deleteBlock(v3s16 blockpos) bool Database_LevelDB::deleteBlock(const v3s16 &pos)
{ {
leveldb::Status status = m_database->Delete(leveldb::WriteOptions(), leveldb::Status status = m_database->Delete(leveldb::WriteOptions(),
i64tos(getBlockAsInteger(blockpos))); i64tos(getBlockAsInteger(pos)));
if (!status.ok()) { if (!status.ok()) {
errorstream << "WARNING: deleteBlock: LevelDB error deleting block " errorstream << "WARNING: deleteBlock: LevelDB error deleting block "
<< PP(blockpos) << ": " << status.ToString() << std::endl; << PP(pos) << ": " << status.ToString() << std::endl;
return false; return false;
} }
@ -103,8 +100,5 @@ void Database_LevelDB::listAllLoadableBlocks(std::vector<v3s16> &dst)
delete it; delete it;
} }
Database_LevelDB::~Database_LevelDB() #endif // USE_LEVELDB
{
delete m_database;
}
#endif

@ -28,23 +28,22 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "leveldb/db.h" #include "leveldb/db.h"
#include <string> #include <string>
class ServerMap;
class Database_LevelDB : public Database class Database_LevelDB : public Database
{ {
public: public:
Database_LevelDB(ServerMap *map, std::string savedir); Database_LevelDB(const std::string &savedir);
virtual void beginSave();
virtual void endSave();
virtual bool saveBlock(v3s16 blockpos, std::string &data);
virtual std::string loadBlock(v3s16 blockpos);
virtual bool deleteBlock(v3s16 blockpos);
virtual void listAllLoadableBlocks(std::vector<v3s16> &dst);
virtual int Initialized(void);
~Database_LevelDB(); ~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);
private: private:
ServerMap *srvmap; leveldb::DB *m_database;
leveldb::DB* m_database;
}; };
#endif // USE_LEVELDB
#endif #endif
#endif

@ -20,84 +20,78 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "config.h" #include "config.h"
#if USE_REDIS #if USE_REDIS
/*
Redis databases
*/
#include "database-redis.h" #include "database-redis.h"
#include <hiredis.h>
#include "map.h"
#include "mapsector.h"
#include "mapblock.h"
#include "serialization.h"
#include "main.h"
#include "settings.h" #include "settings.h"
#include "log.h" #include "log.h"
#include "filesys.h" #include "exceptions.h"
#include "util/string.h"
#include <hiredis.h>
#include <cassert>
Database_Redis::Database_Redis(ServerMap *map, std::string savedir) Database_Redis::Database_Redis(Settings &conf)
{ {
Settings conf;
conf.readConfigFile((std::string(savedir) + DIR_DELIM + "world.mt").c_str());
std::string tmp; std::string tmp;
try { try {
tmp = conf.get("redis_address"); tmp = conf.get("redis_address");
hash = conf.get("redis_hash"); hash = conf.get("redis_hash");
} catch(SettingNotFoundException e) { } catch (SettingNotFoundException) {
throw SettingNotFoundException("Set redis_address and redis_hash in world.mt to use the redis backend"); throw SettingNotFoundException("Set redis_address and "
"redis_hash in world.mt to use the redis backend");
} }
const char *addr = tmp.c_str(); const char *addr = tmp.c_str();
int port = conf.exists("redis_port") ? conf.getU16("redis_port") : 6379; int port = conf.exists("redis_port") ? conf.getU16("redis_port") : 6379;
ctx = redisConnect(addr, port); ctx = redisConnect(addr, port);
if(!ctx) if (!ctx) {
throw FileNotGoodException("Cannot allocate redis context"); throw FileNotGoodException("Cannot allocate redis context");
else if(ctx->err) { } else if(ctx->err) {
std::string err = std::string("Connection error: ") + ctx->errstr; std::string err = std::string("Connection error: ") + ctx->errstr;
redisFree(ctx); redisFree(ctx);
throw FileNotGoodException(err); throw FileNotGoodException(err);
} }
srvmap = map;
} }
int Database_Redis::Initialized(void) Database_Redis::~Database_Redis()
{ {
return 1; redisFree(ctx);
} }
void Database_Redis::beginSave() { void Database_Redis::beginSave() {
redisReply *reply; redisReply *reply = static_cast<redisReply *>(redisCommand(ctx, "MULTI"));
reply = (redisReply*) redisCommand(ctx, "MULTI"); if (!reply) {
if(!reply) throw FileNotGoodException(std::string(
throw FileNotGoodException(std::string("redis command 'MULTI' failed: ") + ctx->errstr); "Redis command 'MULTI' failed: ") + ctx->errstr);
}
freeReplyObject(reply); freeReplyObject(reply);
} }
void Database_Redis::endSave() { void Database_Redis::endSave() {
redisReply *reply; redisReply *reply = static_cast<redisReply *>(redisCommand(ctx, "EXEC"));
reply = (redisReply*) redisCommand(ctx, "EXEC"); if (!reply) {
if(!reply) throw FileNotGoodException(std::string(
throw FileNotGoodException(std::string("redis command 'EXEC' failed: ") + ctx->errstr); "Redis command 'EXEC' failed: ") + ctx->errstr);
}
freeReplyObject(reply); freeReplyObject(reply);
} }
bool Database_Redis::saveBlock(v3s16 blockpos, std::string &data) bool Database_Redis::saveBlock(const v3s16 &pos, const std::string &data)
{ {
std::string tmp = i64tos(getBlockAsInteger(blockpos)); std::string tmp = i64tos(getBlockAsInteger(pos));
redisReply *reply = (redisReply *)redisCommand(ctx, "HSET %s %s %b", redisReply *reply = static_cast<redisReply *>(redisCommand(ctx, "HSET %s %s %b",
hash.c_str(), tmp.c_str(), data.c_str(), data.size()); hash.c_str(), tmp.c_str(), data.c_str(), data.size()));
if (!reply) { if (!reply) {
errorstream << "WARNING: saveBlock: redis command 'HSET' failed on " errorstream << "WARNING: saveBlock: redis command 'HSET' failed on "
"block " << PP(blockpos) << ": " << ctx->errstr << std::endl; "block " << PP(pos) << ": " << ctx->errstr << std::endl;
freeReplyObject(reply); freeReplyObject(reply);
return false; return false;
} }
if (reply->type == REDIS_REPLY_ERROR) { if (reply->type == REDIS_REPLY_ERROR) {
errorstream << "WARNING: saveBlock: saving block " << PP(blockpos) errorstream << "WARNING: saveBlock: saving block " << PP(pos)
<< "failed" << std::endl; << "failed" << std::endl;
freeReplyObject(reply); freeReplyObject(reply);
return false; return false;
@ -107,38 +101,36 @@ bool Database_Redis::saveBlock(v3s16 blockpos, std::string &data)
return true; return true;
} }
std::string Database_Redis::loadBlock(v3s16 blockpos) std::string Database_Redis::loadBlock(const v3s16 &pos)
{ {
std::string tmp = i64tos(getBlockAsInteger(blockpos)); std::string tmp = i64tos(getBlockAsInteger(pos));
redisReply *reply; redisReply *reply = static_cast<redisReply *>(redisCommand(ctx,
reply = (redisReply*) redisCommand(ctx, "HGET %s %s", hash.c_str(), tmp.c_str()); "HGET %s %s", hash.c_str(), tmp.c_str()));
if(!reply) if (!reply) {
throw FileNotGoodException(std::string("redis command 'HGET %s %s' failed: ") + ctx->errstr); throw FileNotGoodException(std::string(
if(reply->type != REDIS_REPLY_STRING) "Redis command 'HGET %s %s' failed: ") + ctx->errstr);
} else if (reply->type != REDIS_REPLY_STRING) {
return ""; return "";
}
std::string str(reply->str, reply->len); std::string str(reply->str, reply->len);
freeReplyObject(reply); // std::string copies the memory so this won't cause any problems freeReplyObject(reply); // std::string copies the memory so this won't cause any problems
return str; return str;
} }
bool Database_Redis::deleteBlock(v3s16 blockpos) bool Database_Redis::deleteBlock(const v3s16 &pos)
{ {
std::string tmp = i64tos(getBlockAsInteger(blockpos)); std::string tmp = i64tos(getBlockAsInteger(pos));
redisReply *reply = (redisReply *)redisCommand(ctx, "HDEL %s %s", redisReply *reply = static_cast<redisReply *>(redisCommand(ctx,
hash.c_str(), tmp.c_str()); "HDEL %s %s", hash.c_str(), tmp.c_str()));
if (!reply) { if (!reply) {
errorstream << "WARNING: deleteBlock: redis command 'HDEL' failed on " throw FileNotGoodException(std::string(
"block " << PP(blockpos) << ": " << ctx->errstr << std::endl; "Redis command 'HDEL %s %s' failed: ") + ctx->errstr);
freeReplyObject(reply); } else if (reply->type == REDIS_REPLY_ERROR) {
return false; errorstream << "WARNING: deleteBlock: deleting block " << PP(pos)
} << " failed" << std::endl;
if (reply->type == REDIS_REPLY_ERROR) {
errorstream << "WARNING: deleteBlock: deleting block " << PP(blockpos)
<< "failed" << std::endl;
freeReplyObject(reply); freeReplyObject(reply);
return false; return false;
} }
@ -149,21 +141,19 @@ bool Database_Redis::deleteBlock(v3s16 blockpos)
void Database_Redis::listAllLoadableBlocks(std::vector<v3s16> &dst) void Database_Redis::listAllLoadableBlocks(std::vector<v3s16> &dst)
{ {
redisReply *reply; redisReply *reply = static_cast<redisReply *>(redisCommand(ctx, "HKEYS %s", hash.c_str()));
reply = (redisReply*) redisCommand(ctx, "HKEYS %s", hash.c_str()); if (!reply) {
if(!reply) throw FileNotGoodException(std::string(
throw FileNotGoodException(std::string("redis command 'HKEYS %s' failed: ") + ctx->errstr); "Redis command 'HKEYS %s' failed: ") + ctx->errstr);
if(reply->type != REDIS_REPLY_ARRAY) } else if (reply->type != REDIS_REPLY_ARRAY) {
throw FileNotGoodException("Failed to get keys from database"); throw FileNotGoodException("Failed to get keys from database");
for(size_t i = 0; i < reply->elements; i++) { }
for (size_t i = 0; i < reply->elements; i++) {
assert(reply->element[i]->type == REDIS_REPLY_STRING); assert(reply->element[i]->type == REDIS_REPLY_STRING);
dst.push_back(getIntegerAsBlock(stoi64(reply->element[i]->str))); dst.push_back(getIntegerAsBlock(stoi64(reply->element[i]->str)));
} }
freeReplyObject(reply); freeReplyObject(reply);
} }
Database_Redis::~Database_Redis() #endif // USE_REDIS
{
redisFree(ctx);
}
#endif

@ -28,24 +28,28 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include <hiredis.h> #include <hiredis.h>
#include <string> #include <string>
class ServerMap; class Settings;
class Database_Redis : public Database class Database_Redis : public Database
{ {
public: public:
Database_Redis(ServerMap *map, std::string savedir); Database_Redis(Settings &conf);
~Database_Redis();
virtual void beginSave(); virtual void beginSave();
virtual void endSave(); virtual void endSave();
virtual bool saveBlock(v3s16 blockpos, std::string &data);
virtual std::string loadBlock(v3s16 blockpos); virtual bool saveBlock(const v3s16 &pos, const std::string &data);
virtual bool deleteBlock(v3s16 blockpos); virtual std::string loadBlock(const v3s16 &pos);
virtual bool deleteBlock(const v3s16 &pos);
virtual void listAllLoadableBlocks(std::vector<v3s16> &dst); virtual void listAllLoadableBlocks(std::vector<v3s16> &dst);
virtual int Initialized(void);
~Database_Redis();
private: private:
ServerMap *srvmap;
redisContext *ctx; redisContext *ctx;
std::string hash; std::string hash;
}; };
#endif // USE_REDIS
#endif #endif
#endif

@ -18,259 +18,205 @@ with this program; if not, write to the Free Software Foundation, Inc.,
*/ */
/* /*
SQLite format specification: SQLite format specification:
- Initially only replaces sectors/ and sectors2/ blocks:
(PK) INT id
If map.sqlite does not exist in the save dir BLOB data
or the block was not found in the database
the map will try to load from sectors folder.
In either case, map.sqlite will be created
and all future saves will save there.
Structure of map.sqlite:
Tables:
blocks
(PK) INT pos
BLOB data
*/ */
#include "database-sqlite3.h" #include "database-sqlite3.h"
#include "map.h"
#include "mapsector.h"
#include "mapblock.h"
#include "serialization.h"
#include "main.h"
#include "settings.h"
#include "log.h" #include "log.h"
#include "filesys.h" #include "filesys.h"
#include "exceptions.h"
#include "main.h"
#include "settings.h"
#include "util/string.h"
Database_SQLite3::Database_SQLite3(ServerMap *map, std::string savedir) #include <cassert>
{
m_database = NULL;
m_database_read = NULL;
m_database_write = NULL;
m_database_list = NULL;
m_database_delete = NULL;
m_savedir = savedir;
srvmap = map;
}
int Database_SQLite3::Initialized(void)
#define SQLRES(s, r) \
if ((s) != (r)) { \
throw FileNotGoodException(std::string(\
"SQLite3 database error (" \
__FILE__ ":" TOSTRING(__LINE__) \
"): ") +\
sqlite3_errmsg(m_database)); \
}
#define SQLOK(s) SQLRES(s, SQLITE_OK)
#define PREPARE_STATEMENT(name, query) \
SQLOK(sqlite3_prepare_v2(m_database, query, -1, &m_stmt_##name, NULL))
#define FINALIZE_STATEMENT(statement) \
if (sqlite3_finalize(statement) != SQLITE_OK) { \
throw FileNotGoodException(std::string( \
"SQLite3: Failed to finalize " #statement ": ") + \
sqlite3_errmsg(m_database)); \
}
Database_SQLite3::Database_SQLite3(const std::string &savedir) :
m_initialized(false),
m_savedir(savedir),
m_database(NULL),
m_stmt_read(NULL),
m_stmt_write(NULL),
m_stmt_list(NULL),
m_stmt_delete(NULL)
{ {
return m_database ? 1 : 0;
} }
void Database_SQLite3::beginSave() { void Database_SQLite3::beginSave() {
verifyDatabase(); verifyDatabase();
if(sqlite3_exec(m_database, "BEGIN;", NULL, NULL, NULL) != SQLITE_OK) SQLRES(sqlite3_step(m_stmt_begin), SQLITE_DONE);
errorstream<<"WARNING: beginSave() failed, saving might be slow."; sqlite3_reset(m_stmt_begin);
} }
void Database_SQLite3::endSave() { void Database_SQLite3::endSave() {
verifyDatabase(); verifyDatabase();
if(sqlite3_exec(m_database, "COMMIT;", NULL, NULL, NULL) != SQLITE_OK) SQLRES(sqlite3_step(m_stmt_end), SQLITE_DONE);
errorstream<<"WARNING: endSave() failed, map might not have saved."; sqlite3_reset(m_stmt_end);
} }
void Database_SQLite3::createDirs(std::string path) void Database_SQLite3::openDatabase()
{ {
if(fs::CreateAllDirs(path) == false) if (m_database) return;
{
infostream<<DTIME<<"Database_SQLite3: Failed to create directory "
<<"\""<<path<<"\""<<std::endl;
throw BaseException("Database_SQLite3 failed to create directory");
}
}
void Database_SQLite3::verifyDatabase() { std::string dbp = m_savedir + DIR_DELIM + "map.sqlite";
if(m_database)
return;
std::string dbp = m_savedir + DIR_DELIM "map.sqlite";
bool needs_create = false;
int d;
// Open the database connection // Open the database connection
createDirs(m_savedir); // ? if (!fs::CreateAllDirs(m_savedir)) {
infostream << "Database_SQLite3: Failed to create directory \""
<< m_savedir << "\"" << std::endl;
throw FileNotGoodException("Failed to create database "
"save directory");
}
if(!fs::PathExists(dbp)) bool needs_create = !fs::PathExists(dbp);
needs_create = true;
d = sqlite3_open_v2(dbp.c_str(), &m_database, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL); if (sqlite3_open_v2(dbp.c_str(), &m_database,
if(d != SQLITE_OK) { SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE,
errorstream<<"SQLite3 database failed to open: "<<sqlite3_errmsg(m_database)<<std::endl; NULL) != SQLITE_OK) {
errorstream << "SQLite3 database failed to open: "
<< sqlite3_errmsg(m_database) << std::endl;
throw FileNotGoodException("Cannot open database file"); throw FileNotGoodException("Cannot open database file");
} }
if(needs_create) if (needs_create) {
createDatabase(); createDatabase();
}
std::string querystr = std::string("PRAGMA synchronous = ") std::string query_str = std::string("PRAGMA synchronous = ")
+ itos(g_settings->getU16("sqlite_synchronous")); + itos(g_settings->getU16("sqlite_synchronous"));
d = sqlite3_exec(m_database, querystr.c_str(), NULL, NULL, NULL); SQLOK(sqlite3_exec(m_database, query_str.c_str(), NULL, NULL, NULL));
if(d != SQLITE_OK) { }
errorstream<<"Database pragma set failed: "
<<sqlite3_errmsg(m_database)<<std::endl;
throw FileNotGoodException("Cannot set pragma");
}
d = sqlite3_prepare(m_database, "SELECT `data` FROM `blocks` WHERE `pos`=? LIMIT 1", -1, &m_database_read, NULL); void Database_SQLite3::verifyDatabase()
if(d != SQLITE_OK) { {
errorstream<<"SQLite3 read statment failed to prepare: "<<sqlite3_errmsg(m_database)<<std::endl; if (m_initialized) return;
throw FileNotGoodException("Cannot prepare read statement");
} openDatabase();
PREPARE_STATEMENT(begin, "BEGIN");
PREPARE_STATEMENT(end, "COMMIT");
PREPARE_STATEMENT(read, "SELECT `data` FROM `blocks` WHERE `pos` = ? LIMIT 1");
#ifdef __ANDROID__ #ifdef __ANDROID__
d = sqlite3_prepare(m_database, "INSERT INTO `blocks` VALUES(?, ?);", -1, &m_database_write, NULL); PREPARE_STATEMENT(write, "INSERT INTO `blocks` (`pos`, `data`) VALUES (?, ?)");
#else #else
d = sqlite3_prepare(m_database, "REPLACE INTO `blocks` VALUES(?, ?);", -1, &m_database_write, NULL); PREPARE_STATEMENT(write, "REPLACE INTO `blocks` (`pos`, `data`) VALUES (?, ?)");
#endif #endif
if(d != SQLITE_OK) { PREPARE_STATEMENT(delete, "DELETE FROM `blocks` WHERE `pos` = ?");
errorstream<<"SQLite3 write statment failed to prepare: "<<sqlite3_errmsg(m_database)<<std::endl; PREPARE_STATEMENT(list, "SELECT `pos` FROM `blocks`");
throw FileNotGoodException("Cannot prepare write statement");
}
d = sqlite3_prepare(m_database, "DELETE FROM `blocks` WHERE `pos`=?;", -1, &m_database_delete, NULL); m_initialized = true;
if(d != SQLITE_OK) {
infostream<<"WARNING: SQLite3 database delete statment failed to prepare: "<<sqlite3_errmsg(m_database)<<std::endl;
throw FileNotGoodException("Cannot prepare delete statement");
}
d = sqlite3_prepare(m_database, "SELECT `pos` FROM `blocks`", -1, &m_database_list, NULL); verbosestream << "ServerMap: SQLite3 database opened." << std::endl;
if(d != SQLITE_OK) {
infostream<<"SQLite3 list statment failed to prepare: "<<sqlite3_errmsg(m_database)<<std::endl;
throw FileNotGoodException("Cannot prepare read statement");
}
infostream<<"ServerMap: SQLite3 database opened"<<std::endl;
} }
bool Database_SQLite3::deleteBlock(v3s16 blockpos) inline void Database_SQLite3::bindPos(sqlite3_stmt *stmt, const v3s16 &pos, int index)
{
SQLOK(sqlite3_bind_int64(stmt, index, getBlockAsInteger(pos)));
}
bool Database_SQLite3::deleteBlock(const v3s16 &pos)
{ {
verifyDatabase(); verifyDatabase();
if (sqlite3_bind_int64(m_database_delete, 1, bindPos(m_stmt_delete, pos);
getBlockAsInteger(blockpos)) != SQLITE_OK) {
errorstream << "WARNING: Could not bind block position for delete: "
<< sqlite3_errmsg(m_database) << std::endl;
}
if (sqlite3_step(m_database_delete) != SQLITE_DONE) { bool good = sqlite3_step(m_stmt_delete) == SQLITE_DONE;
sqlite3_reset(m_stmt_delete);
if (!good) {
errorstream << "WARNING: deleteBlock: Block failed to delete " errorstream << "WARNING: deleteBlock: Block failed to delete "
<< PP(blockpos) << ": " << sqlite3_errmsg(m_database) << std::endl; << PP(pos) << ": " << sqlite3_errmsg(m_database) << std::endl;
sqlite3_reset(m_database_delete);
return false;
} }
return good;
sqlite3_reset(m_database_delete);
return true;
} }
bool Database_SQLite3::saveBlock(v3s16 blockpos, std::string &data) bool Database_SQLite3::saveBlock(const v3s16 &pos, const std::string &data)
{ {
verifyDatabase(); verifyDatabase();
s64 bkey = getBlockAsInteger(blockpos);
#ifdef __ANDROID__ #ifdef __ANDROID__
/** /**
* Note: For some unknown reason sqlite3 fails to REPLACE blocks on android, * Note: For some unknown reason SQLite3 fails to REPLACE blocks on Android,
* deleting them and inserting first works. * deleting them and then inserting works.
*/ */
if (sqlite3_bind_int64(m_database_read, 1, bkey) != SQLITE_OK) { bindPos(m_stmt_read, pos);
infostream << "WARNING: Could not bind block position for load: "
<< sqlite3_errmsg(m_database)<<std::endl; if (sqlite3_step(m_stmt_read) == SQLITE_ROW) {
} deleteBlock(pos);
int step_result = sqlite3_step(m_database_read);
sqlite3_reset(m_database_read);
if (step_result == SQLITE_ROW) {
if (sqlite3_bind_int64(m_database_delete, 1, bkey) != SQLITE_OK) {
infostream << "WARNING: Could not bind block position for delete: "
<< sqlite3_errmsg(m_database)<<std::endl;
}
if (sqlite3_step(m_database_delete) != SQLITE_DONE) {
errorstream << "WARNING: saveBlock: Block failed to delete "
<< PP(blockpos) << ": " << sqlite3_errmsg(m_database) << std::endl;
return false;
}
sqlite3_reset(m_database_delete);
} }
sqlite3_reset(m_stmt_read);
#endif #endif
if (sqlite3_bind_int64(m_database_write, 1, bkey) != SQLITE_OK) { bindPos(m_stmt_write, pos);
errorstream << "WARNING: saveBlock: Block position failed to bind: " SQLOK(sqlite3_bind_blob(m_stmt_write, 2, data.data(), data.size(), NULL));
<< PP(blockpos) << ": " << sqlite3_errmsg(m_database) << std::endl;
sqlite3_reset(m_database_write);
return false;
}
if (sqlite3_bind_blob(m_database_write, 2, (void *)data.c_str(), SQLRES(sqlite3_step(m_stmt_write), SQLITE_DONE)
data.size(), NULL) != SQLITE_OK) { sqlite3_reset(m_stmt_write);
errorstream << "WARNING: saveBlock: Block data failed to bind: "
<< PP(blockpos) << ": " << sqlite3_errmsg(m_database) << std::endl;
sqlite3_reset(m_database_write);
return false;
}
if (sqlite3_step(m_database_write) != SQLITE_DONE) {
errorstream << "WARNING: saveBlock: Block failed to save "
<< PP(blockpos) << ": " << sqlite3_errmsg(m_database) << std::endl;
sqlite3_reset(m_database_write);
return false;
}
sqlite3_reset(m_database_write);
return true; return true;
} }
std::string Database_SQLite3::loadBlock(v3s16 blockpos) std::string Database_SQLite3::loadBlock(const v3s16 &pos)
{ {
verifyDatabase(); verifyDatabase();
if (sqlite3_bind_int64(m_database_read, 1, getBlockAsInteger(blockpos)) != SQLITE_OK) { bindPos(m_stmt_read, pos);
errorstream << "Could not bind block position for load: "
<< sqlite3_errmsg(m_database)<<std::endl; if (sqlite3_step(m_stmt_read) != SQLITE_ROW) {
sqlite3_reset(m_stmt_read);
return "";
} }
const char *data = (const char *) sqlite3_column_blob(m_stmt_read, 0);
size_t len = sqlite3_column_bytes(m_stmt_read, 0);
if (sqlite3_step(m_database_read) == SQLITE_ROW) { std::string s;
const char *data = (const char *) sqlite3_column_blob(m_database_read, 0); if (data)
size_t len = sqlite3_column_bytes(m_database_read, 0); s = std::string(data, len);
std::string s = ""; sqlite3_step(m_stmt_read);
if(data) // We should never get more than 1 row, so ok to reset
s = std::string(data, len); sqlite3_reset(m_stmt_read);
sqlite3_step(m_database_read); return s;
// We should never get more than 1 row, so ok to reset
sqlite3_reset(m_database_read);
return s;
}
sqlite3_reset(m_database_read);
return "";
} }
void Database_SQLite3::createDatabase() void Database_SQLite3::createDatabase()
{ {
int e;
assert(m_database); assert(m_database);
e = sqlite3_exec(m_database, SQLOK(sqlite3_exec(m_database,
"CREATE TABLE IF NOT EXISTS `blocks` (" "CREATE TABLE IF NOT EXISTS `blocks` (\n"
"`pos` INT NOT NULL PRIMARY KEY," " `pos` INT PRIMARY KEY,\n"
"`data` BLOB" " `data` BLOB\n"
");" ");\n",
, NULL, NULL, NULL); NULL, NULL, NULL));
if(e != SQLITE_OK)
throw FileNotGoodException("Could not create sqlite3 database structure");
else
infostream<<"ServerMap: SQLite3 database structure was created";
} }
@ -278,36 +224,25 @@ void Database_SQLite3::listAllLoadableBlocks(std::vector<v3s16> &dst)
{ {
verifyDatabase(); verifyDatabase();
while(sqlite3_step(m_database_list) == SQLITE_ROW) { while (sqlite3_step(m_stmt_list) == SQLITE_ROW) {
sqlite3_int64 block_i = sqlite3_column_int64(m_database_list, 0); dst.push_back(getIntegerAsBlock(sqlite3_column_int64(m_stmt_list, 0)));
v3s16 p = getIntegerAsBlock(block_i);
//dstream<<"block_i="<<block_i<<" p="<<PP(p)<<std::endl;
dst.push_back(p);
} }
sqlite3_reset(m_stmt_list);
} }
#define FINALIZE_STATEMENT(statement) \
if ( statement ) \
rc = sqlite3_finalize(statement); \
if ( rc != SQLITE_OK ) \
errorstream << "Database_SQLite3::~Database_SQLite3():" \
<< "Failed to finalize: " << #statement << ": rc=" << rc << std::endl;
Database_SQLite3::~Database_SQLite3() Database_SQLite3::~Database_SQLite3()
{ {
int rc = SQLITE_OK; FINALIZE_STATEMENT(m_stmt_read)
FINALIZE_STATEMENT(m_stmt_write)
FINALIZE_STATEMENT(m_stmt_list)
FINALIZE_STATEMENT(m_stmt_begin)
FINALIZE_STATEMENT(m_stmt_end)
FINALIZE_STATEMENT(m_stmt_delete)
FINALIZE_STATEMENT(m_database_read) if (sqlite3_close(m_database) != SQLITE_OK) {
FINALIZE_STATEMENT(m_database_write)
FINALIZE_STATEMENT(m_database_list)
FINALIZE_STATEMENT(m_database_delete)
if(m_database)
rc = sqlite3_close(m_database);
if (rc != SQLITE_OK) {
errorstream << "Database_SQLite3::~Database_SQLite3(): " errorstream << "Database_SQLite3::~Database_SQLite3(): "
<< "Failed to close database: rc=" << rc << std::endl; << "Failed to close database: "
<< sqlite3_errmsg(m_database) << std::endl;
} }
} }

@ -27,35 +27,43 @@ extern "C" {
#include "sqlite3.h" #include "sqlite3.h"
} }
class ServerMap;
class Database_SQLite3 : public Database class Database_SQLite3 : public Database
{ {
public: public:
Database_SQLite3(ServerMap *map, std::string savedir); Database_SQLite3(const std::string &savedir);
virtual void beginSave(); virtual void beginSave();
virtual void endSave(); virtual void endSave();
virtual bool saveBlock(v3s16 blockpos, std::string &data); virtual bool saveBlock(const v3s16 &pos, const std::string &data);
virtual std::string loadBlock(v3s16 blockpos); virtual std::string loadBlock(const v3s16 &pos);
virtual bool deleteBlock(v3s16 blockpos); virtual bool deleteBlock(const v3s16 &pos);
virtual void listAllLoadableBlocks(std::vector<v3s16> &dst); virtual void listAllLoadableBlocks(std::vector<v3s16> &dst);
virtual int Initialized(void); virtual bool initialized() const { return m_initialized; }
~Database_SQLite3(); ~Database_SQLite3();
private:
ServerMap *srvmap;
std::string m_savedir;
sqlite3 *m_database;
sqlite3_stmt *m_database_read;
sqlite3_stmt *m_database_write;
sqlite3_stmt *m_database_delete;
sqlite3_stmt *m_database_list;
private:
// Open the database
void openDatabase();
// Create the database structure // Create the database structure
void createDatabase(); void createDatabase();
// Verify we can read/write to the database // Open and initialize the database if needed
void verifyDatabase(); void verifyDatabase();
void createDirs(std::string path);
void bindPos(sqlite3_stmt *stmt, const v3s16 &pos, int index=1);
bool m_initialized;
std::string m_savedir;
sqlite3 *m_database;
sqlite3_stmt *m_stmt_read;
sqlite3_stmt *m_stmt_write;
sqlite3_stmt *m_stmt_list;
sqlite3_stmt *m_stmt_delete;
sqlite3_stmt *m_stmt_begin;
sqlite3_stmt *m_stmt_end;
}; };
#endif #endif

@ -48,7 +48,7 @@ static inline s64 pythonmodulo(s64 i, s16 mod)
} }
s64 Database::getBlockAsInteger(const v3s16 pos) const s64 Database::getBlockAsInteger(const v3s16 &pos)
{ {
return (u64) pos.Z * 0x1000000 + return (u64) pos.Z * 0x1000000 +
(u64) pos.Y * 0x1000 + (u64) pos.Y * 0x1000 +
@ -56,7 +56,7 @@ s64 Database::getBlockAsInteger(const v3s16 pos) const
} }
v3s16 Database::getIntegerAsBlock(s64 i) const v3s16 Database::getIntegerAsBlock(s64 i)
{ {
v3s16 pos; v3s16 pos;
pos.X = unsigned_to_signed(pythonmodulo(i, 4096), 2048); pos.X = unsigned_to_signed(pythonmodulo(i, 4096), 2048);

@ -32,16 +32,22 @@ with this program; if not, write to the Free Software Foundation, Inc.,
class Database class Database
{ {
public: public:
virtual void beginSave() = 0; virtual ~Database() {}
virtual void endSave() = 0;
virtual void beginSave() {}
virtual void endSave() {}
virtual bool saveBlock(const v3s16 &pos, const std::string &data) = 0;
virtual std::string loadBlock(const v3s16 &pos) = 0;
virtual bool deleteBlock(const v3s16 &pos) = 0;
static s64 getBlockAsInteger(const v3s16 &pos);
static v3s16 getIntegerAsBlock(s64 i);
virtual bool saveBlock(v3s16 blockpos, std::string &data) = 0;
virtual std::string loadBlock(v3s16 blockpos) = 0;
virtual bool deleteBlock(v3s16 blockpos) = 0;
s64 getBlockAsInteger(const v3s16 pos) const;
v3s16 getIntegerAsBlock(s64 i) const;
virtual void listAllLoadableBlocks(std::vector<v3s16> &dst) = 0; virtual void listAllLoadableBlocks(std::vector<v3s16> &dst) = 0;
virtual int Initialized(void)=0;
virtual ~Database() {}; virtual bool initialized() const { return true; }
}; };
#endif #endif

@ -914,52 +914,46 @@ static bool run_dedicated_server(const GameParams &game_params, const Settings &
static bool migrate_database(const GameParams &game_params, const Settings &cmd_args, static bool migrate_database(const GameParams &game_params, const Settings &cmd_args,
Server *server) Server *server)
{ {
std::string migrate_to = cmd_args.get("migrate");
Settings world_mt; Settings world_mt;
bool success = world_mt.readConfigFile((game_params.world_path std::string world_mt_path = game_params.world_path + DIR_DELIM + "world.mt";
+ DIR_DELIM + "world.mt").c_str()); bool success = world_mt.readConfigFile(world_mt_path.c_str());
if (!success) { if (!success) {
errorstream << "Cannot read world.mt" << std::endl; errorstream << "Cannot read world.mt" << std::endl;
return false; return 1;
} }
if (!world_mt.exists("backend")) { if (!world_mt.exists("backend")) {
errorstream << "Please specify your current backend in world.mt file:" errorstream << "Please specify your current backend in world.mt:"
<< std::endl << " backend = {sqlite3|leveldb|redis|dummy}" << std::endl;
<< std::endl; errorstream << " backend = {sqlite3|leveldb|redis|dummy}"
return false; << std::endl;
return 1;
} }
std::string backend = world_mt.get("backend"); std::string backend = world_mt.get("backend");
Database *new_db; Database *new_db;
std::string migrate_to = cmd_args.get("migrate");
if (backend == migrate_to) { if (backend == migrate_to) {
errorstream << "Cannot migrate: new backend is same as the old one" errorstream << "Cannot migrate: new backend is same"
<< std::endl; <<" as the old one" << std::endl;
return false; return 1;
} }
if (migrate_to == "sqlite3") if (migrate_to == "sqlite3")
new_db = new Database_SQLite3(&(ServerMap&)server->getMap(), new_db = new Database_SQLite3(game_params.world_path);
game_params.world_path); #if USE_LEVELDB
#if USE_LEVELDB
else if (migrate_to == "leveldb") else if (migrate_to == "leveldb")
new_db = new Database_LevelDB(&(ServerMap&)server->getMap(), new_db = new Database_LevelDB(game_params.world_path);
game_params.world_path); #endif
#endif #if USE_REDIS
#if USE_REDIS
else if (migrate_to == "redis") else if (migrate_to == "redis")
new_db = new Database_Redis(&(ServerMap&)server->getMap(), new_db = new Database_Redis(world_mt);
game_params.world_path); #endif
#endif
else { else {
errorstream << "Migration to " << migrate_to << " is not supported" errorstream << "Migration to " << migrate_to
<< std::endl; << " is not supported" << std::endl;
return false; return 1;
} }
std::vector<v3s16> blocks; std::vector<v3s16> blocks;
ServerMap &old_map = ((ServerMap&)server->getMap()); ServerMap &old_map = (ServerMap &) server->getMap();
old_map.listAllLoadableBlocks(blocks); old_map.listAllLoadableBlocks(blocks);
int count = 0; int count = 0;
new_db->beginSave(); new_db->beginSave();
@ -975,16 +969,15 @@ static bool migrate_database(const GameParams &game_params, const Settings &cmd_
} }
++count; ++count;
if (count % 500 == 0) if (count % 500 == 0)
actionstream << "Migrated " << count << " blocks " actionstream << "Migrated " << count << " blocks "
<< (100.0 * count / blocks.size()) << "% completed" << std::endl; << (100.0 * count / blocks.size()) << "% completed" << std::endl;
} }
new_db->endSave(); new_db->endSave();
delete new_db; delete new_db;
actionstream << "Successfully migrated " << count << " blocks" << std::endl; actionstream << "Successfully migrated " << count << " blocks" << std::endl;
world_mt.set("backend", migrate_to); world_mt.set("backend", migrate_to);
if (!world_mt.updateConfigFile( if (!world_mt.updateConfigFile(world_mt_path.c_str()))
(game_params.world_path+ DIR_DELIM + "world.mt").c_str()))
errorstream << "Failed to update world.mt!" << std::endl; errorstream << "Failed to update world.mt!" << std::endl;
else else
actionstream << "world.mt updated" << std::endl; actionstream << "world.mt updated" << std::endl;

@ -53,22 +53,6 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#define PP(x) "("<<(x).X<<","<<(x).Y<<","<<(x).Z<<")" #define PP(x) "("<<(x).X<<","<<(x).Y<<","<<(x).Z<<")"
/*
SQLite format specification:
- Initially only replaces sectors/ and sectors2/
If map.sqlite does not exist in the save dir
or the block was not found in the database
the map will try to load from sectors folder.
In either case, map.sqlite will be created
and all future saves will save there.
Structure of map.sqlite:
Tables:
blocks
(PK) INT pos
BLOB data
*/
/* /*
Map Map
@ -2031,25 +2015,26 @@ ServerMap::ServerMap(std::string savedir, IGameDef *gamedef, EmergeManager *emer
bool succeeded = conf.readConfigFile(conf_path.c_str()); bool succeeded = conf.readConfigFile(conf_path.c_str());
if (!succeeded || !conf.exists("backend")) { if (!succeeded || !conf.exists("backend")) {
// fall back to sqlite3 // fall back to sqlite3
dbase = new Database_SQLite3(this, savedir);
conf.set("backend", "sqlite3"); conf.set("backend", "sqlite3");
} else {
std::string backend = conf.get("backend");
if (backend == "dummy")
dbase = new Database_Dummy(this);
else if (backend == "sqlite3")
dbase = new Database_SQLite3(this, savedir);
#if USE_LEVELDB
else if (backend == "leveldb")
dbase = new Database_LevelDB(this, savedir);
#endif
#if USE_REDIS
else if (backend == "redis")
dbase = new Database_Redis(this, savedir);
#endif
else
throw BaseException("Unknown map backend");
} }
std::string backend = conf.get("backend");
if (backend == "dummy")
dbase = new Database_Dummy();
else if (backend == "sqlite3")
dbase = new Database_SQLite3(savedir);
#if USE_LEVELDB
else if (backend == "leveldb")
dbase = new Database_LevelDB(savedir);
#endif
#if USE_REDIS
else if (backend == "redis")
dbase = new Database_Redis(conf);
#endif
else
throw BaseException("Unknown map backend");
if (!conf.updateConfigFile(conf_path.c_str()))
errorstream << "ServerMap::ServerMap(): Failed to update world.mt!" << std::endl;
m_savedir = savedir; m_savedir = savedir;
m_map_saving_enabled = false; m_map_saving_enabled = false;
@ -2828,7 +2813,8 @@ plan_b:
} }
bool ServerMap::loadFromFolders() { bool ServerMap::loadFromFolders() {
if(!dbase->Initialized() && !fs::PathExists(m_savedir + DIR_DELIM + "map.sqlite")) // ? if(!dbase->initialized() &&
!fs::PathExists(m_savedir + DIR_DELIM + "map.sqlite"))
return true; return true;
return false; return false;
} }
@ -2850,14 +2836,14 @@ std::string ServerMap::getSectorDir(v2s16 pos, int layout)
{ {
case 1: case 1:
snprintf(cc, 9, "%.4x%.4x", snprintf(cc, 9, "%.4x%.4x",
(unsigned int)pos.X&0xffff, (unsigned int) pos.X & 0xffff,
(unsigned int)pos.Y&0xffff); (unsigned int) pos.Y & 0xffff);
return m_savedir + DIR_DELIM + "sectors" + DIR_DELIM + cc; return m_savedir + DIR_DELIM + "sectors" + DIR_DELIM + cc;
case 2: case 2:
snprintf(cc, 9, "%.3x" DIR_DELIM "%.3x", snprintf(cc, 9, (std::string("%.3x") + DIR_DELIM + "%.3x").c_str(),
(unsigned int)pos.X&0xfff, (unsigned int) pos.X & 0xfff,
(unsigned int)pos.Y&0xfff); (unsigned int) pos.Y & 0xfff);
return m_savedir + DIR_DELIM + "sectors2" + DIR_DELIM + cc; return m_savedir + DIR_DELIM + "sectors2" + DIR_DELIM + cc;
default: default:
@ -2881,10 +2867,10 @@ v2s16 ServerMap::getSectorPos(std::string dirname)
{ {
// New layout // New layout
fs::RemoveLastPathComponent(dirname, &component, 2); fs::RemoveLastPathComponent(dirname, &component, 2);
r = sscanf(component.c_str(), "%3x" DIR_DELIM "%3x", &x, &y); r = sscanf(component.c_str(), (std::string("%3x") + DIR_DELIM + "%3x").c_str(), &x, &y);
// Sign-extend the 12 bit values up to 16 bits... // Sign-extend the 12 bit values up to 16 bits...
if(x&0x800) x|=0xF000; if(x & 0x800) x |= 0xF000;
if(y&0x800) y|=0xF000; if(y & 0x800) y |= 0xF000;
} }
else else
{ {
@ -3299,7 +3285,7 @@ bool ServerMap::saveBlock(MapBlock *block, Database *db)
std::string data = o.str(); std::string data = o.str();
bool ret = db->saveBlock(p3d, data); bool ret = db->saveBlock(p3d, data);
if(ret) { if (ret) {
// We just wrote it to the disk so clear modified flag // We just wrote it to the disk so clear modified flag
block->resetModified(); block->resetModified();
} }
@ -3311,7 +3297,7 @@ void ServerMap::loadBlock(std::string sectordir, std::string blockfile,
{ {
DSTACK(__FUNCTION_NAME); DSTACK(__FUNCTION_NAME);
std::string fullpath = sectordir+DIR_DELIM+blockfile; std::string fullpath = sectordir + DIR_DELIM + blockfile;
try { try {
std::ifstream is(fullpath.c_str(), std::ios_base::binary); std::ifstream is(fullpath.c_str(), std::ios_base::binary);
@ -3513,7 +3499,7 @@ MapBlock* ServerMap::loadBlock(v3s16 blockpos)
*/ */
std::string blockfilename = getBlockFilename(blockpos); std::string blockfilename = getBlockFilename(blockpos);
if(fs::PathExists(sectordir+DIR_DELIM+blockfilename) == false) if(fs::PathExists(sectordir + DIR_DELIM + blockfilename) == false)
return NULL; return NULL;
/* /*

@ -481,8 +481,8 @@ public:
// Returns true if sector now resides in memory // Returns true if sector now resides in memory
//bool deFlushSector(v2s16 p2d); //bool deFlushSector(v2s16 p2d);
bool saveBlock(MapBlock *block, Database *db);
bool saveBlock(MapBlock *block); bool saveBlock(MapBlock *block);
static bool saveBlock(MapBlock *block, Database *db);
// This will generate a sector with getSector if not found. // This will generate a sector with getSector if not found.
void loadBlock(std::string sectordir, std::string blockfile, MapSector *sector, bool save_after_load=false); void loadBlock(std::string sectordir, std::string blockfile, MapSector *sector, bool save_after_load=false);
MapBlock* loadBlock(v3s16 p); MapBlock* loadBlock(v3s16 p);

@ -168,8 +168,8 @@ void Client::handleCommand_BlockData(NetworkPacket* pkt)
sector->insertBlock(block); sector->insertBlock(block);
} }
if (localdb != NULL) { if (m_localdb) {
((ServerMap&) localserver->getMap()).saveBlock(block, localdb); ServerMap::saveBlock(block, m_localdb);
} }
/* /*