]> git.lizzy.rs Git - dragonfireclient.git/blob - src/database-redis.cpp
DB::loadBlock copy removal & DB backend cleanup
[dragonfireclient.git] / src / database-redis.cpp
1 /*
2 Minetest
3 Copyright (C) 2014 celeron55, Perttu Ahola <celeron55@gmail.com>
4
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.
9
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.
14
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.
18 */
19
20 #include "config.h"
21
22 #if USE_REDIS
23
24 #include "database-redis.h"
25
26 #include "settings.h"
27 #include "log.h"
28 #include "exceptions.h"
29 #include "util/string.h"
30
31 #include <hiredis.h>
32 #include <cassert>
33
34
35 Database_Redis::Database_Redis(Settings &conf)
36 {
37         std::string tmp;
38         try {
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");
44         }
45         const char *addr = tmp.c_str();
46         int port = conf.exists("redis_port") ? conf.getU16("redis_port") : 6379;
47         ctx = redisConnect(addr, port);
48         if (!ctx) {
49                 throw FileNotGoodException("Cannot allocate redis context");
50         } else if (ctx->err) {
51                 std::string err = std::string("Connection error: ") + ctx->errstr;
52                 redisFree(ctx);
53                 throw FileNotGoodException(err);
54         }
55 }
56
57 Database_Redis::~Database_Redis()
58 {
59         redisFree(ctx);
60 }
61
62 void Database_Redis::beginSave() {
63         redisReply *reply = static_cast<redisReply *>(redisCommand(ctx, "MULTI"));
64         if (!reply) {
65                 throw FileNotGoodException(std::string(
66                         "Redis command 'MULTI' failed: ") + ctx->errstr);
67         }
68         freeReplyObject(reply);
69 }
70
71 void Database_Redis::endSave() {
72         redisReply *reply = static_cast<redisReply *>(redisCommand(ctx, "EXEC"));
73         if (!reply) {
74                 throw FileNotGoodException(std::string(
75                         "Redis command 'EXEC' failed: ") + ctx->errstr);
76         }
77         freeReplyObject(reply);
78 }
79
80 bool Database_Redis::saveBlock(const v3s16 &pos, const std::string &data)
81 {
82         std::string tmp = i64tos(getBlockAsInteger(pos));
83
84         redisReply *reply = static_cast<redisReply *>(redisCommand(ctx, "HSET %s %s %b",
85                         hash.c_str(), tmp.c_str(), data.c_str(), data.size()));
86         if (!reply) {
87                 warningstream << "saveBlock: redis command 'HSET' failed on "
88                         "block " << PP(pos) << ": " << ctx->errstr << std::endl;
89                 freeReplyObject(reply);
90                 return false;
91         }
92
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);
97                 return false;
98         }
99
100         freeReplyObject(reply);
101         return true;
102 }
103
104 void Database_Redis::loadBlock(const v3s16 &pos, std::string *block)
105 {
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()));
109
110         if (!reply) {
111                 throw FileNotGoodException(std::string(
112                         "Redis command 'HGET %s %s' failed: ") + ctx->errstr);
113         }
114
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);
120                 return;
121         }
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 FileNotGoodException(std::string(
128                         "Redis command 'HGET %s %s' errored: ") + errstr);
129         }
130         case REDIS_REPLY_NIL: {
131                 *block = "";
132                 // block not found in database
133                 freeReplyObject(reply);
134                 return;
135         }
136         }
137
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 FileNotGoodException(std::string(
143                 "Redis command 'HGET %s %s' gave invalid reply."));
144 }
145
146 bool Database_Redis::deleteBlock(const v3s16 &pos)
147 {
148         std::string tmp = i64tos(getBlockAsInteger(pos));
149
150         redisReply *reply = static_cast<redisReply *>(redisCommand(ctx,
151                 "HDEL %s %s", hash.c_str(), tmp.c_str()));
152         if (!reply) {
153                 throw FileNotGoodException(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);
159                 return false;
160         }
161
162         freeReplyObject(reply);
163         return true;
164 }
165
166 void Database_Redis::listAllLoadableBlocks(std::vector<v3s16> &dst)
167 {
168         redisReply *reply = static_cast<redisReply *>(redisCommand(ctx, "HKEYS %s", hash.c_str()));
169         if (!reply) {
170                 throw FileNotGoodException(std::string(
171                         "Redis command 'HKEYS %s' failed: ") + ctx->errstr);
172         }
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)));
179                 }
180                 break;
181         case REDIS_REPLY_ERROR:
182                 throw FileNotGoodException(std::string(
183                         "Failed to get keys from database: ") +
184                         std::string(reply->str, reply->len));
185         }
186         freeReplyObject(reply);
187 }
188
189 #endif // USE_REDIS
190