Node highlighting.
[oweals/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         Redis databases
25 */
26
27
28 #include "database-redis.h"
29 #include <hiredis.h>
30
31 #include "map.h"
32 #include "mapsector.h"
33 #include "mapblock.h"
34 #include "serialization.h"
35 #include "main.h"
36 #include "settings.h"
37 #include "log.h"
38
39 Database_Redis::Database_Redis(ServerMap *map, std::string savedir)
40 {
41         Settings conf;
42         conf.readConfigFile((std::string(savedir) + DIR_DELIM + "world.mt").c_str());
43         std::string tmp;
44         try {
45         tmp = conf.get("redis_address");
46         hash = conf.get("redis_hash");
47         } catch(SettingNotFoundException e) {
48                 throw SettingNotFoundException("Set redis_address and redis_hash in world.mt to use the redis backend");
49         }
50         const char *addr = tmp.c_str();
51         int port = conf.exists("redis_port") ? conf.getU16("redis_port") : 6379;
52         ctx = redisConnect(addr, port);
53         if(!ctx)
54                 throw FileNotGoodException("Cannot allocate redis context");
55         else if(ctx->err) {
56                 std::string err = std::string("Connection error: ") + ctx->errstr;
57                 redisFree(ctx);
58                 throw FileNotGoodException(err);
59         }
60         srvmap = map;
61 }
62
63 int Database_Redis::Initialized(void)
64 {
65         return 1;
66 }
67
68 void Database_Redis::beginSave() {
69         redisReply *reply;
70         reply = (redisReply*) redisCommand(ctx, "MULTI");
71         if(!reply)
72                 throw FileNotGoodException(std::string("redis command 'MULTI' failed: ") + ctx->errstr);
73         freeReplyObject(reply);
74 }
75
76 void Database_Redis::endSave() {
77         redisReply *reply;
78         reply = (redisReply*) redisCommand(ctx, "EXEC");
79         if(!reply)
80                 throw FileNotGoodException(std::string("redis command 'EXEC' failed: ") + ctx->errstr);
81         freeReplyObject(reply);
82 }
83
84 bool Database_Redis::saveBlock(v3s16 blockpos, std::string &data)
85 {
86         std::string tmp = i64tos(getBlockAsInteger(blockpos));
87
88         redisReply *reply = (redisReply *)redisCommand(ctx, "HSET %s %s %b",
89                         hash.c_str(), tmp.c_str(), data.c_str(), data.size());
90         if (!reply) {
91                 errorstream << "WARNING: saveBlock: redis command 'HSET' failed on "
92                         "block " << PP(blockpos) << ": " << ctx->errstr << std::endl;
93                 freeReplyObject(reply);
94                 return false;
95         }
96
97         if (reply->type == REDIS_REPLY_ERROR) {
98                 errorstream << "WARNING: saveBlock: saving block " << PP(blockpos)
99                         << "failed" << std::endl;
100                 freeReplyObject(reply);
101                 return false;
102         }
103
104         freeReplyObject(reply);
105         return true;
106 }
107
108 std::string Database_Redis::loadBlock(v3s16 blockpos)
109 {
110         std::string tmp = i64tos(getBlockAsInteger(blockpos));
111         redisReply *reply;
112         reply = (redisReply*) redisCommand(ctx, "HGET %s %s", hash.c_str(), tmp.c_str());
113
114         if(!reply)
115                 throw FileNotGoodException(std::string("redis command 'HGET %s %s' failed: ") + ctx->errstr);
116         if(reply->type != REDIS_REPLY_STRING)
117                 return "";
118
119         std::string str(reply->str, reply->len);
120         freeReplyObject(reply); // std::string copies the memory so this won't cause any problems
121         return str;
122 }
123
124 void Database_Redis::listAllLoadableBlocks(std::list<v3s16> &dst)
125 {
126         redisReply *reply;
127         reply = (redisReply*) redisCommand(ctx, "HKEYS %s", hash.c_str());
128         if(!reply)
129                 throw FileNotGoodException(std::string("redis command 'HKEYS %s' failed: ") + ctx->errstr);
130         if(reply->type != REDIS_REPLY_ARRAY)
131                 throw FileNotGoodException("Failed to get keys from database");
132         for(size_t i = 0; i < reply->elements; i++)
133         {
134                 assert(reply->element[i]->type == REDIS_REPLY_STRING);
135                 dst.push_back(getIntegerAsBlock(stoi64(reply->element[i]->str)));
136         }
137         freeReplyObject(reply);
138 }
139
140 Database_Redis::~Database_Redis()
141 {
142         redisFree(ctx);
143 }
144 #endif