3 Copyright (C) 2014 celeron55, Perttu Ahola <celeron55@gmail.com>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
28 #include "database-redis.h"
32 #include "mapsector.h"
34 #include "serialization.h"
39 Database_Redis::Database_Redis(ServerMap *map, std::string savedir)
42 conf.readConfigFile((std::string(savedir) + DIR_DELIM + "world.mt").c_str());
45 tmp = conf.get("redis_address");
46 hash = conf.get("redis_hash");
47 } catch(SettingNotFoundException e) {
48 throw SettingNotFoundException("Set redis_address and redis_hash in world.mt to use the redis backend");
50 const char *addr = tmp.c_str();
51 int port = conf.exists("redis_port") ? conf.getU16("redis_port") : 6379;
52 ctx = redisConnect(addr, port);
54 throw FileNotGoodException("Cannot allocate redis context");
56 std::string err = std::string("Connection error: ") + ctx->errstr;
58 throw FileNotGoodException(err);
63 int Database_Redis::Initialized(void)
68 void Database_Redis::beginSave() {
70 reply = (redisReply*) redisCommand(ctx, "MULTI");
72 throw FileNotGoodException(std::string("redis command 'MULTI' failed: ") + ctx->errstr);
73 freeReplyObject(reply);
76 void Database_Redis::endSave() {
78 reply = (redisReply*) redisCommand(ctx, "EXEC");
80 throw FileNotGoodException(std::string("redis command 'EXEC' failed: ") + ctx->errstr);
81 freeReplyObject(reply);
84 void Database_Redis::saveBlock(MapBlock *block)
86 DSTACK(__FUNCTION_NAME);
88 Dummy blocks are not written
95 // Format used for writing
96 u8 version = SER_FMT_VER_HIGHEST_WRITE;
98 v3s16 p3d = block->getPos();
101 [0] u8 serialization version
105 std::ostringstream o(std::ios_base::binary);
106 o.write((char*)&version, 1);
108 block->serialize(o, version, true);
109 // Write block to database
110 std::string tmp1 = o.str();
111 std::string tmp2 = i64tos(getBlockAsInteger(p3d));
114 reply = (redisReply*) redisCommand(ctx, "HSET %s %s %b", hash.c_str(), tmp2.c_str(), tmp1.c_str(), tmp1.size());
116 throw FileNotGoodException(std::string("redis command 'HSET %s %s %b' failed: ") + ctx->errstr);
117 if(reply->type == REDIS_REPLY_ERROR)
118 throw FileNotGoodException("Failed to store block in Database");
119 freeReplyObject(reply);
121 // We just wrote it to the disk so clear modified flag
122 block->resetModified();
125 MapBlock* Database_Redis::loadBlock(v3s16 blockpos)
127 v2s16 p2d(blockpos.X, blockpos.Z);
129 std::string tmp = i64tos(getBlockAsInteger(blockpos));
131 reply = (redisReply*) redisCommand(ctx, "HGET %s %s", hash.c_str(), tmp.c_str());
133 throw FileNotGoodException(std::string("redis command 'HGET %s %s' failed: ") + ctx->errstr);
135 if (reply->type == REDIS_REPLY_STRING && reply->len == 0) {
136 freeReplyObject(reply);
137 errorstream << "Blank block data in database (reply->len == 0) ("
138 << blockpos.X << "," << blockpos.Y << "," << blockpos.Z << ")" << std::endl;
140 if (g_settings->getBool("ignore_world_load_errors")) {
141 errorstream << "Ignoring block load error. Duck and cover! "
142 << "(ignore_world_load_errors)" << std::endl;
144 throw SerializationError("Blank block data in database");
149 if (reply->type == REDIS_REPLY_STRING) {
151 Make sure sector is loaded
153 MapSector *sector = srvmap->createSector(p2d);
156 std::istringstream is(std::string(reply->str, reply->len), std::ios_base::binary);
157 freeReplyObject(reply); // std::string copies the memory so we can already do this here
158 u8 version = SER_FMT_VER_INVALID;
159 is.read((char *)&version, 1);
162 throw SerializationError("ServerMap::loadBlock(): Failed"
163 " to read MapBlock version");
165 MapBlock *block = NULL;
166 bool created_new = false;
167 block = sector->getBlockNoCreateNoEx(blockpos.Y);
170 block = sector->createBlankBlockNoInsert(blockpos.Y);
175 block->deSerialize(is, version, true);
177 // If it's a new block, insert it to the map
179 sector->insertBlock(block);
181 // We just loaded it from, so it's up-to-date.
182 block->resetModified();
184 catch (SerializationError &e)
186 errorstream << "Invalid block data in database"
187 << " (" << blockpos.X << "," << blockpos.Y << "," << blockpos.Z
188 << ") (SerializationError): " << e.what() << std::endl;
189 // TODO: Block should be marked as invalid in memory so that it is
190 // not touched but the game can run
192 if (g_settings->getBool("ignore_world_load_errors")) {
193 errorstream << "Ignoring block load error. Duck and cover! "
194 << "(ignore_world_load_errors)" << std::endl;
196 throw SerializationError("Invalid block data in database");
200 return srvmap->getBlockNoCreateNoEx(blockpos); // should not be using this here
205 void Database_Redis::listAllLoadableBlocks(std::list<v3s16> &dst)
208 reply = (redisReply*) redisCommand(ctx, "HKEYS %s", hash.c_str());
210 throw FileNotGoodException(std::string("redis command 'HKEYS %s' failed: ") + ctx->errstr);
211 if(reply->type != REDIS_REPLY_ARRAY)
212 throw FileNotGoodException("Failed to get keys from database");
213 for(size_t i = 0; i < reply->elements; i++)
215 assert(reply->element[i]->type == REDIS_REPLY_STRING);
216 dst.push_back(getIntegerAsBlock(stoi64(reply->element[i]->str)));
218 freeReplyObject(reply);
221 Database_Redis::~Database_Redis()