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 DatabaseException("Cannot allocate redis context");
50 } else if (ctx->err) {
51 std::string err = std::string("Connection error: ") + ctx->errstr;
53 throw DatabaseException(err);
57 Database_Redis::~Database_Redis()
62 void Database_Redis::beginSave() {
63 redisReply *reply = static_cast<redisReply *>(redisCommand(ctx, "MULTI"));
65 throw DatabaseException(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 DatabaseException(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 warningstream << "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 warningstream << "saveBlock: saving block " << PP(pos)
95 << " failed: " << std::string(reply->str, reply->len) << std::endl;
96 freeReplyObject(reply);
100 freeReplyObject(reply);
104 void Database_Redis::loadBlock(const v3s16 &pos, std::string *block)
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 DatabaseException(std::string(
112 "Redis command 'HGET %s %s' failed: ") + ctx->errstr);
115 switch (reply->type) {
116 case REDIS_REPLY_STRING: {
117 *block = std::string(reply->str, reply->len);
118 // std::string copies the memory so this won't cause any problems
119 freeReplyObject(reply);
122 case REDIS_REPLY_ERROR: {
123 std::string errstr(reply->str, reply->len);
124 freeReplyObject(reply);
125 errorstream << "loadBlock: loading block " << PP(pos)
126 << " failed: " << errstr << std::endl;
127 throw DatabaseException(std::string(
128 "Redis command 'HGET %s %s' errored: ") + errstr);
130 case REDIS_REPLY_NIL: {
132 // block not found in database
133 freeReplyObject(reply);
138 errorstream << "loadBlock: loading block " << PP(pos)
139 << " returned invalid reply type " << reply->type
140 << ": " << std::string(reply->str, reply->len) << std::endl;
141 freeReplyObject(reply);
142 throw DatabaseException(std::string(
143 "Redis command 'HGET %s %s' gave invalid reply."));
146 bool Database_Redis::deleteBlock(const v3s16 &pos)
148 std::string tmp = i64tos(getBlockAsInteger(pos));
150 redisReply *reply = static_cast<redisReply *>(redisCommand(ctx,
151 "HDEL %s %s", hash.c_str(), tmp.c_str()));
153 throw DatabaseException(std::string(
154 "Redis command 'HDEL %s %s' failed: ") + ctx->errstr);
155 } else if (reply->type == REDIS_REPLY_ERROR) {
156 warningstream << "deleteBlock: deleting block " << PP(pos)
157 << " failed: " << std::string(reply->str, reply->len) << std::endl;
158 freeReplyObject(reply);
162 freeReplyObject(reply);
166 void Database_Redis::listAllLoadableBlocks(std::vector<v3s16> &dst)
168 redisReply *reply = static_cast<redisReply *>(redisCommand(ctx, "HKEYS %s", hash.c_str()));
170 throw DatabaseException(std::string(
171 "Redis command 'HKEYS %s' failed: ") + ctx->errstr);
173 switch (reply->type) {
174 case REDIS_REPLY_ARRAY:
175 dst.reserve(reply->elements);
176 for (size_t i = 0; i < reply->elements; i++) {
177 assert(reply->element[i]->type == REDIS_REPLY_STRING);
178 dst.push_back(getIntegerAsBlock(stoi64(reply->element[i]->str)));
181 case REDIS_REPLY_ERROR:
182 throw DatabaseException(std::string(
183 "Failed to get keys from database: ") +
184 std::string(reply->str, reply->len));
186 freeReplyObject(reply);