]> git.lizzy.rs Git - minetest.git/blob - src/database-redis.cpp
Don't unload blocks if save failed
[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(MapBlock *block)
85 {
86         DSTACK(__FUNCTION_NAME);
87
88         v3s16 p3d = block->getPos();
89
90         /*
91                 Dummy blocks are not written
92         */
93         if(block->isDummy())
94         {
95                 errorstream << "WARNING: saveBlock: Not writing dummy block "
96                                 << PP(p3d) << std::endl;
97                 return true;
98         }
99
100         // Format used for writing
101         u8 version = SER_FMT_VER_HIGHEST_WRITE;
102
103         /*
104                 [0] u8 serialization version
105                 [1] data
106         */
107
108         std::ostringstream o(std::ios_base::binary);
109         o.write((char*)&version, 1);
110         // Write basic data
111         block->serialize(o, version, true);
112         // Write block to database
113         std::string tmp1 = o.str();
114         std::string tmp2 = i64tos(getBlockAsInteger(p3d));
115
116         redisReply *reply = (redisReply *)redisCommand(ctx, "HSET %s %s %b",
117                         hash.c_str(), tmp2.c_str(), tmp1.c_str(), tmp1.size());
118         if (!reply) {
119                 errorstream << "WARNING: saveBlock: redis command 'HSET' failed on "
120                         "block " << PP(p3d) << ": " << ctx->errstr << std::endl;
121                 freeReplyObject(reply);
122                 return false;
123         }
124
125         if (reply->type == REDIS_REPLY_ERROR) {
126                 errorstream << "WARNING: saveBlock: save block " << PP(p3d)
127                         << "failed" << std::endl;
128                 freeReplyObject(reply);
129                 return false;
130         }
131
132         // We just wrote it to the disk so clear modified flag
133         block->resetModified();
134         freeReplyObject(reply);
135         return true;
136 }
137
138 MapBlock* Database_Redis::loadBlock(v3s16 blockpos)
139 {
140         v2s16 p2d(blockpos.X, blockpos.Z);
141
142         std::string tmp = i64tos(getBlockAsInteger(blockpos));
143         redisReply *reply;
144         reply = (redisReply*) redisCommand(ctx, "HGET %s %s", hash.c_str(), tmp.c_str());
145         if(!reply)
146                 throw FileNotGoodException(std::string("redis command 'HGET %s %s' failed: ") + ctx->errstr);
147
148         if (reply->type == REDIS_REPLY_STRING && reply->len == 0) {
149                 freeReplyObject(reply);
150                 errorstream << "Blank block data in database (reply->len == 0) ("
151                         << blockpos.X << "," << blockpos.Y << "," << blockpos.Z << ")" << std::endl;
152
153                 if (g_settings->getBool("ignore_world_load_errors")) {
154                         errorstream << "Ignoring block load error. Duck and cover! "
155                                 << "(ignore_world_load_errors)" << std::endl;
156                 } else {
157                         throw SerializationError("Blank block data in database");
158                 }
159                 return NULL;
160         }
161
162         if (reply->type == REDIS_REPLY_STRING) {
163                 /*
164                         Make sure sector is loaded
165                 */
166                 MapSector *sector = srvmap->createSector(p2d);
167
168                 try {
169                         std::istringstream is(std::string(reply->str, reply->len), std::ios_base::binary);
170                         freeReplyObject(reply); // std::string copies the memory so we can already do this here
171                         u8 version = SER_FMT_VER_INVALID;
172                         is.read((char *)&version, 1);
173
174                         if (is.fail())
175                                 throw SerializationError("ServerMap::loadBlock(): Failed"
176                                         " to read MapBlock version");
177
178                         MapBlock *block = NULL;
179                         bool created_new = false;
180                         block = sector->getBlockNoCreateNoEx(blockpos.Y);
181                         if (block == NULL)
182                         {
183                                 block = sector->createBlankBlockNoInsert(blockpos.Y);
184                                 created_new = true;
185                         }
186
187                         // Read basic data
188                         block->deSerialize(is, version, true);
189
190                         // If it's a new block, insert it to the map
191                         if (created_new)
192                                 sector->insertBlock(block);
193
194                         // We just loaded it from, so it's up-to-date.
195                         block->resetModified();
196                 }
197                 catch (SerializationError &e)
198                 {
199                         errorstream << "Invalid block data in database"
200                                 << " (" << blockpos.X << "," << blockpos.Y << "," << blockpos.Z
201                                 << ") (SerializationError): " << e.what() << std::endl;
202                         // TODO: Block should be marked as invalid in memory so that it is
203                         // not touched but the game can run
204
205                         if (g_settings->getBool("ignore_world_load_errors")) {
206                                 errorstream << "Ignoring block load error. Duck and cover! "
207                                         << "(ignore_world_load_errors)" << std::endl;
208                         } else {
209                                 throw SerializationError("Invalid block data in database");
210                         }
211                 }
212
213                 return srvmap->getBlockNoCreateNoEx(blockpos);  // should not be using this here
214         }
215         return NULL;
216 }
217
218 void Database_Redis::listAllLoadableBlocks(std::list<v3s16> &dst)
219 {
220         redisReply *reply;
221         reply = (redisReply*) redisCommand(ctx, "HKEYS %s", hash.c_str());
222         if(!reply)
223                 throw FileNotGoodException(std::string("redis command 'HKEYS %s' failed: ") + ctx->errstr);
224         if(reply->type != REDIS_REPLY_ARRAY)
225                 throw FileNotGoodException("Failed to get keys from database");
226         for(size_t i = 0; i < reply->elements; i++)
227         {
228                 assert(reply->element[i]->type == REDIS_REPLY_STRING);
229                 dst.push_back(getIntegerAsBlock(stoi64(reply->element[i]->str)));
230         }
231         freeReplyObject(reply);
232 }
233
234 Database_Redis::~Database_Redis()
235 {
236         redisFree(ctx);
237 }
238 #endif