database-redis.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. Minetest
  3. Copyright (C) 2014 celeron55, Perttu Ahola <celeron55@gmail.com>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser General Public License as published by
  6. the Free Software Foundation; either version 2.1 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License along
  13. with this program; if not, write to the Free Software Foundation, Inc.,
  14. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  15. */
  16. #include "config.h"
  17. #if USE_REDIS
  18. /*
  19. Redis databases
  20. */
  21. #include "database-redis.h"
  22. #include <hiredis.h>
  23. #include "map.h"
  24. #include "mapsector.h"
  25. #include "mapblock.h"
  26. #include "serialization.h"
  27. #include "main.h"
  28. #include "settings.h"
  29. #include "log.h"
  30. Database_Redis::Database_Redis(ServerMap *map, std::string savedir)
  31. {
  32. Settings conf;
  33. conf.readConfigFile((std::string(savedir) + DIR_DELIM + "world.mt").c_str());
  34. std::string tmp;
  35. try {
  36. tmp = conf.get("redis_address");
  37. hash = conf.get("redis_hash");
  38. } catch(SettingNotFoundException e) {
  39. throw SettingNotFoundException("Set redis_address and redis_hash in world.mt to use the redis backend");
  40. }
  41. const char *addr = tmp.c_str();
  42. int port = conf.exists("redis_port") ? conf.getU16("redis_port") : 6379;
  43. ctx = redisConnect(addr, port);
  44. if(!ctx)
  45. throw FileNotGoodException("Cannot allocate redis context");
  46. else if(ctx->err) {
  47. std::string err = std::string("Connection error: ") + ctx->errstr;
  48. redisFree(ctx);
  49. throw FileNotGoodException(err);
  50. }
  51. srvmap = map;
  52. }
  53. int Database_Redis::Initialized(void)
  54. {
  55. return 1;
  56. }
  57. void Database_Redis::beginSave() {
  58. redisReply *reply;
  59. reply = (redisReply*) redisCommand(ctx, "MULTI");
  60. if(!reply)
  61. throw FileNotGoodException(std::string("redis command 'MULTI' failed: ") + ctx->errstr);
  62. freeReplyObject(reply);
  63. }
  64. void Database_Redis::endSave() {
  65. redisReply *reply;
  66. reply = (redisReply*) redisCommand(ctx, "EXEC");
  67. if(!reply)
  68. throw FileNotGoodException(std::string("redis command 'EXEC' failed: ") + ctx->errstr);
  69. freeReplyObject(reply);
  70. }
  71. bool Database_Redis::saveBlock(v3s16 blockpos, std::string &data)
  72. {
  73. std::string tmp = i64tos(getBlockAsInteger(blockpos));
  74. redisReply *reply = (redisReply *)redisCommand(ctx, "HSET %s %s %b",
  75. hash.c_str(), tmp.c_str(), data.c_str(), data.size());
  76. if (!reply) {
  77. errorstream << "WARNING: saveBlock: redis command 'HSET' failed on "
  78. "block " << PP(blockpos) << ": " << ctx->errstr << std::endl;
  79. freeReplyObject(reply);
  80. return false;
  81. }
  82. if (reply->type == REDIS_REPLY_ERROR) {
  83. errorstream << "WARNING: saveBlock: saving block " << PP(blockpos)
  84. << "failed" << std::endl;
  85. freeReplyObject(reply);
  86. return false;
  87. }
  88. freeReplyObject(reply);
  89. return true;
  90. }
  91. std::string Database_Redis::loadBlock(v3s16 blockpos)
  92. {
  93. std::string tmp = i64tos(getBlockAsInteger(blockpos));
  94. redisReply *reply;
  95. reply = (redisReply*) redisCommand(ctx, "HGET %s %s", hash.c_str(), tmp.c_str());
  96. if(!reply)
  97. throw FileNotGoodException(std::string("redis command 'HGET %s %s' failed: ") + ctx->errstr);
  98. if(reply->type != REDIS_REPLY_STRING)
  99. return "";
  100. std::string str(reply->str, reply->len);
  101. freeReplyObject(reply); // std::string copies the memory so this won't cause any problems
  102. return str;
  103. }
  104. void Database_Redis::listAllLoadableBlocks(std::list<v3s16> &dst)
  105. {
  106. redisReply *reply;
  107. reply = (redisReply*) redisCommand(ctx, "HKEYS %s", hash.c_str());
  108. if(!reply)
  109. throw FileNotGoodException(std::string("redis command 'HKEYS %s' failed: ") + ctx->errstr);
  110. if(reply->type != REDIS_REPLY_ARRAY)
  111. throw FileNotGoodException("Failed to get keys from database");
  112. for(size_t i = 0; i < reply->elements; i++)
  113. {
  114. assert(reply->element[i]->type == REDIS_REPLY_STRING);
  115. dst.push_back(getIntegerAsBlock(stoi64(reply->element[i]->str)));
  116. }
  117. freeReplyObject(reply);
  118. }
  119. Database_Redis::~Database_Redis()
  120. {
  121. redisFree(ctx);
  122. }
  123. #endif