]> git.lizzy.rs Git - minetest.git/blob - src/database-redis.cpp
Add minetest.register_lbm() to run code on block load only
[minetest.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 std::string Database_Redis::loadBlock(const v3s16 &pos)
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         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);
119                 return str;
120         }
121         case REDIS_REPLY_ERROR: {
122                 std::string errstr(reply->str, reply->len);
123                 freeReplyObject(reply);
124                 errorstream << "loadBlock: loading block " << PP(pos)
125                         << " failed: " << errstr << std::endl;
126                 throw FileNotGoodException(std::string(
127                         "Redis command 'HGET %s %s' errored: ") + errstr);
128         }
129         case REDIS_REPLY_NIL: {
130                 // block not found in database
131                 freeReplyObject(reply);
132                 return "";
133         }
134         }
135         errorstream << "loadBlock: loading block " << PP(pos)
136                 << " returned invalid reply type " << reply->type
137                 << ": " << std::string(reply->str, reply->len) << std::endl;
138         freeReplyObject(reply);
139         throw FileNotGoodException(std::string(
140                 "Redis command 'HGET %s %s' gave invalid reply."));
141 }
142
143 bool Database_Redis::deleteBlock(const v3s16 &pos)
144 {
145         std::string tmp = i64tos(getBlockAsInteger(pos));
146
147         redisReply *reply = static_cast<redisReply *>(redisCommand(ctx,
148                 "HDEL %s %s", hash.c_str(), tmp.c_str()));
149         if (!reply) {
150                 throw FileNotGoodException(std::string(
151                         "Redis command 'HDEL %s %s' failed: ") + ctx->errstr);
152         } else if (reply->type == REDIS_REPLY_ERROR) {
153                 warningstream << "deleteBlock: deleting block " << PP(pos)
154                         << " failed: " << std::string(reply->str, reply->len) << std::endl;
155                 freeReplyObject(reply);
156                 return false;
157         }
158
159         freeReplyObject(reply);
160         return true;
161 }
162
163 void Database_Redis::listAllLoadableBlocks(std::vector<v3s16> &dst)
164 {
165         redisReply *reply = static_cast<redisReply *>(redisCommand(ctx, "HKEYS %s", hash.c_str()));
166         if (!reply) {
167                 throw FileNotGoodException(std::string(
168                         "Redis command 'HKEYS %s' failed: ") + ctx->errstr);
169         }
170         switch (reply->type) {
171         case REDIS_REPLY_ARRAY:
172                 dst.reserve(reply->elements);
173                 for (size_t i = 0; i < reply->elements; i++) {
174                         assert(reply->element[i]->type == REDIS_REPLY_STRING);
175                         dst.push_back(getIntegerAsBlock(stoi64(reply->element[i]->str)));
176                 }
177                 break;
178         case REDIS_REPLY_ERROR:
179                 throw FileNotGoodException(std::string(
180                         "Failed to get keys from database: ") +
181                         std::string(reply->str, reply->len));
182         }
183         freeReplyObject(reply);
184 }
185
186 #endif // USE_REDIS
187