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.
24 #include "database-redis.h"
28 #include "exceptions.h"
29 #include "util/string.h"
35 Database_Redis::Database_Redis(Settings &conf)
39 tmp = conf.get("redis_address");
40 hash = conf.get("redis_hash");
41 } catch (SettingNotFoundException) {
42 throw SettingNotFoundException("Set redis_address and "
43 "redis_hash in world.mt to use the redis backend");
45 const char *addr = tmp.c_str();
46 int port = conf.exists("redis_port") ? conf.getU16("redis_port") : 6379;
47 ctx = redisConnect(addr, port);
49 throw FileNotGoodException("Cannot allocate redis context");
50 } else if (ctx->err) {
51 std::string err = std::string("Connection error: ") + ctx->errstr;
53 throw FileNotGoodException(err);
57 Database_Redis::~Database_Redis()
62 void Database_Redis::beginSave() {
63 redisReply *reply = static_cast<redisReply *>(redisCommand(ctx, "MULTI"));
65 throw FileNotGoodException(std::string(
66 "Redis command 'MULTI' failed: ") + ctx->errstr);
68 freeReplyObject(reply);
71 void Database_Redis::endSave() {
72 redisReply *reply = static_cast<redisReply *>(redisCommand(ctx, "EXEC"));
74 throw FileNotGoodException(std::string(
75 "Redis command 'EXEC' failed: ") + ctx->errstr);
77 freeReplyObject(reply);
80 bool Database_Redis::saveBlock(const v3s16 &pos, const std::string &data)
82 std::string tmp = i64tos(getBlockAsInteger(pos));
84 redisReply *reply = static_cast<redisReply *>(redisCommand(ctx, "HSET %s %s %b",
85 hash.c_str(), tmp.c_str(), data.c_str(), data.size()));
87 errorstream << "WARNING: saveBlock: redis command 'HSET' failed on "
88 "block " << PP(pos) << ": " << ctx->errstr << std::endl;
89 freeReplyObject(reply);
93 if (reply->type == REDIS_REPLY_ERROR) {
94 errorstream << "WARNING: saveBlock: saving block " << PP(pos)
95 << " failed: " << reply->str << std::endl;
96 freeReplyObject(reply);
100 freeReplyObject(reply);
104 std::string Database_Redis::loadBlock(const v3s16 &pos)
106 std::string tmp = i64tos(getBlockAsInteger(pos));
107 redisReply *reply = static_cast<redisReply *>(redisCommand(ctx,
108 "HGET %s %s", hash.c_str(), tmp.c_str()));
111 throw FileNotGoodException(std::string(
112 "Redis command 'HGET %s %s' failed: ") + ctx->errstr);
114 switch (reply->type) {
115 case REDIS_REPLY_STRING: {
116 std::string str(reply->str, reply->len);
117 // std::string copies the memory so this won't cause any problems
118 freeReplyObject(reply);
121 case REDIS_REPLY_ERROR:
122 errorstream << "WARNING: loadBlock: loading block " << PP(pos)
123 << " failed: " << reply->str << std::endl;
125 freeReplyObject(reply);
129 bool Database_Redis::deleteBlock(const v3s16 &pos)
131 std::string tmp = i64tos(getBlockAsInteger(pos));
133 redisReply *reply = static_cast<redisReply *>(redisCommand(ctx,
134 "HDEL %s %s", hash.c_str(), tmp.c_str()));
136 throw FileNotGoodException(std::string(
137 "Redis command 'HDEL %s %s' failed: ") + ctx->errstr);
138 } else if (reply->type == REDIS_REPLY_ERROR) {
139 errorstream << "WARNING: deleteBlock: deleting block " << PP(pos)
140 << " failed: " << reply->str << std::endl;
141 freeReplyObject(reply);
145 freeReplyObject(reply);
149 void Database_Redis::listAllLoadableBlocks(std::vector<v3s16> &dst)
151 redisReply *reply = static_cast<redisReply *>(redisCommand(ctx, "HKEYS %s", hash.c_str()));
153 throw FileNotGoodException(std::string(
154 "Redis command 'HKEYS %s' failed: ") + ctx->errstr);
156 switch (reply->type) {
157 case REDIS_REPLY_ARRAY:
158 for (size_t i = 0; i < reply->elements; i++) {
159 assert(reply->element[i]->type == REDIS_REPLY_STRING);
160 dst.push_back(getIntegerAsBlock(stoi64(reply->element[i]->str)));
162 case REDIS_REPLY_ERROR:
163 throw FileNotGoodException(std::string(
164 "Failed to get keys from database: ") + reply->str);
166 freeReplyObject(reply);