database-redis.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. #include "database-redis.h"
  19. #include "settings.h"
  20. #include "log.h"
  21. #include "exceptions.h"
  22. #include "util/string.h"
  23. #include <hiredis.h>
  24. #include <cassert>
  25. Database_Redis::Database_Redis(Settings &conf)
  26. {
  27. std::string tmp;
  28. try {
  29. tmp = conf.get("redis_address");
  30. hash = conf.get("redis_hash");
  31. } catch (SettingNotFoundException) {
  32. throw SettingNotFoundException("Set redis_address and "
  33. "redis_hash in world.mt to use the redis backend");
  34. }
  35. const char *addr = tmp.c_str();
  36. int port = conf.exists("redis_port") ? conf.getU16("redis_port") : 6379;
  37. ctx = redisConnect(addr, port);
  38. if (!ctx) {
  39. throw FileNotGoodException("Cannot allocate redis context");
  40. } else if (ctx->err) {
  41. std::string err = std::string("Connection error: ") + ctx->errstr;
  42. redisFree(ctx);
  43. throw FileNotGoodException(err);
  44. }
  45. }
  46. Database_Redis::~Database_Redis()
  47. {
  48. redisFree(ctx);
  49. }
  50. void Database_Redis::beginSave() {
  51. redisReply *reply = static_cast<redisReply *>(redisCommand(ctx, "MULTI"));
  52. if (!reply) {
  53. throw FileNotGoodException(std::string(
  54. "Redis command 'MULTI' failed: ") + ctx->errstr);
  55. }
  56. freeReplyObject(reply);
  57. }
  58. void Database_Redis::endSave() {
  59. redisReply *reply = static_cast<redisReply *>(redisCommand(ctx, "EXEC"));
  60. if (!reply) {
  61. throw FileNotGoodException(std::string(
  62. "Redis command 'EXEC' failed: ") + ctx->errstr);
  63. }
  64. freeReplyObject(reply);
  65. }
  66. bool Database_Redis::saveBlock(const v3s16 &pos, const std::string &data)
  67. {
  68. std::string tmp = i64tos(getBlockAsInteger(pos));
  69. redisReply *reply = static_cast<redisReply *>(redisCommand(ctx, "HSET %s %s %b",
  70. hash.c_str(), tmp.c_str(), data.c_str(), data.size()));
  71. if (!reply) {
  72. warningstream << "saveBlock: redis command 'HSET' failed on "
  73. "block " << PP(pos) << ": " << ctx->errstr << std::endl;
  74. freeReplyObject(reply);
  75. return false;
  76. }
  77. if (reply->type == REDIS_REPLY_ERROR) {
  78. warningstream << "saveBlock: saving block " << PP(pos)
  79. << " failed: " << std::string(reply->str, reply->len) << std::endl;
  80. freeReplyObject(reply);
  81. return false;
  82. }
  83. freeReplyObject(reply);
  84. return true;
  85. }
  86. std::string Database_Redis::loadBlock(const v3s16 &pos)
  87. {
  88. std::string tmp = i64tos(getBlockAsInteger(pos));
  89. redisReply *reply = static_cast<redisReply *>(redisCommand(ctx,
  90. "HGET %s %s", hash.c_str(), tmp.c_str()));
  91. if (!reply) {
  92. throw FileNotGoodException(std::string(
  93. "Redis command 'HGET %s %s' failed: ") + ctx->errstr);
  94. }
  95. switch (reply->type) {
  96. case REDIS_REPLY_STRING: {
  97. std::string str(reply->str, reply->len);
  98. // std::string copies the memory so this won't cause any problems
  99. freeReplyObject(reply);
  100. return str;
  101. }
  102. case REDIS_REPLY_ERROR: {
  103. std::string errstr(reply->str, reply->len);
  104. freeReplyObject(reply);
  105. errorstream << "loadBlock: loading block " << PP(pos)
  106. << " failed: " << errstr << std::endl;
  107. throw FileNotGoodException(std::string(
  108. "Redis command 'HGET %s %s' errored: ") + errstr);
  109. }
  110. case REDIS_REPLY_NIL: {
  111. // block not found in database
  112. freeReplyObject(reply);
  113. return "";
  114. }
  115. }
  116. errorstream << "loadBlock: loading block " << PP(pos)
  117. << " returned invalid reply type " << reply->type
  118. << ": " << std::string(reply->str, reply->len) << std::endl;
  119. freeReplyObject(reply);
  120. throw FileNotGoodException(std::string(
  121. "Redis command 'HGET %s %s' gave invalid reply."));
  122. }
  123. bool Database_Redis::deleteBlock(const v3s16 &pos)
  124. {
  125. std::string tmp = i64tos(getBlockAsInteger(pos));
  126. redisReply *reply = static_cast<redisReply *>(redisCommand(ctx,
  127. "HDEL %s %s", hash.c_str(), tmp.c_str()));
  128. if (!reply) {
  129. throw FileNotGoodException(std::string(
  130. "Redis command 'HDEL %s %s' failed: ") + ctx->errstr);
  131. } else if (reply->type == REDIS_REPLY_ERROR) {
  132. warningstream << "deleteBlock: deleting block " << PP(pos)
  133. << " failed: " << std::string(reply->str, reply->len) << std::endl;
  134. freeReplyObject(reply);
  135. return false;
  136. }
  137. freeReplyObject(reply);
  138. return true;
  139. }
  140. void Database_Redis::listAllLoadableBlocks(std::vector<v3s16> &dst)
  141. {
  142. redisReply *reply = static_cast<redisReply *>(redisCommand(ctx, "HKEYS %s", hash.c_str()));
  143. if (!reply) {
  144. throw FileNotGoodException(std::string(
  145. "Redis command 'HKEYS %s' failed: ") + ctx->errstr);
  146. }
  147. switch (reply->type) {
  148. case REDIS_REPLY_ARRAY:
  149. dst.reserve(reply->elements);
  150. for (size_t i = 0; i < reply->elements; i++) {
  151. assert(reply->element[i]->type == REDIS_REPLY_STRING);
  152. dst.push_back(getIntegerAsBlock(stoi64(reply->element[i]->str)));
  153. }
  154. break;
  155. case REDIS_REPLY_ERROR:
  156. throw FileNotGoodException(std::string(
  157. "Failed to get keys from database: ") +
  158. std::string(reply->str, reply->len));
  159. }
  160. freeReplyObject(reply);
  161. }
  162. #endif // USE_REDIS