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"
41 Database_Redis::Database_Redis(ServerMap *map, std::string savedir)
44 conf.readConfigFile((std::string(savedir) + DIR_DELIM + "world.mt").c_str());
47 tmp = conf.get("redis_address");
48 hash = conf.get("redis_hash");
49 } catch(SettingNotFoundException e) {
50 throw SettingNotFoundException("Set redis_address and redis_hash in world.mt to use the redis backend");
52 const char *addr = tmp.c_str();
53 int port = conf.exists("redis_port") ? conf.getU16("redis_port") : 6379;
54 ctx = redisConnect(addr, port);
56 throw FileNotGoodException("Cannot allocate redis context");
58 std::string err = std::string("Connection error: ") + ctx->errstr;
60 throw FileNotGoodException(err);
65 int Database_Redis::Initialized(void)
70 void Database_Redis::beginSave() {
72 reply = (redisReply*) redisCommand(ctx, "MULTI");
74 throw FileNotGoodException(std::string("redis command 'MULTI' failed: ") + ctx->errstr);
75 freeReplyObject(reply);
78 void Database_Redis::endSave() {
80 reply = (redisReply*) redisCommand(ctx, "EXEC");
82 throw FileNotGoodException(std::string("redis command 'EXEC' failed: ") + ctx->errstr);
83 freeReplyObject(reply);
86 bool Database_Redis::saveBlock(v3s16 blockpos, std::string &data)
88 std::string tmp = i64tos(getBlockAsInteger(blockpos));
90 redisReply *reply = (redisReply *)redisCommand(ctx, "HSET %s %s %b",
91 hash.c_str(), tmp.c_str(), data.c_str(), data.size());
93 errorstream << "WARNING: saveBlock: redis command 'HSET' failed on "
94 "block " << PP(blockpos) << ": " << ctx->errstr << std::endl;
95 freeReplyObject(reply);
99 if (reply->type == REDIS_REPLY_ERROR) {
100 errorstream << "WARNING: saveBlock: saving block " << PP(blockpos)
101 << "failed" << std::endl;
102 freeReplyObject(reply);
106 freeReplyObject(reply);
110 std::string Database_Redis::loadBlock(v3s16 blockpos)
112 std::string tmp = i64tos(getBlockAsInteger(blockpos));
114 reply = (redisReply*) redisCommand(ctx, "HGET %s %s", hash.c_str(), tmp.c_str());
117 throw FileNotGoodException(std::string("redis command 'HGET %s %s' failed: ") + ctx->errstr);
118 if(reply->type != REDIS_REPLY_STRING)
121 std::string str(reply->str, reply->len);
122 freeReplyObject(reply); // std::string copies the memory so this won't cause any problems
126 void Database_Redis::listAllLoadableBlocks(std::list<v3s16> &dst)
129 reply = (redisReply*) redisCommand(ctx, "HKEYS %s", hash.c_str());
131 throw FileNotGoodException(std::string("redis command 'HKEYS %s' failed: ") + ctx->errstr);
132 if(reply->type != REDIS_REPLY_ARRAY)
133 throw FileNotGoodException("Failed to get keys from database");
134 for(size_t i = 0; i < reply->elements; i++)
136 assert(reply->element[i]->type == REDIS_REPLY_STRING);
137 dst.push_back(getIntegerAsBlock(stoi64(reply->element[i]->str)));
139 freeReplyObject(reply);
142 Database_Redis::~Database_Redis()