database-redis.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. // if redis_address contains '/' assume unix socket, else hostname/ip
  38. ctx = tmp.find('/') != std::string::npos ? redisConnectUnix(addr) : redisConnect(addr, port);
  39. if (!ctx) {
  40. throw DatabaseException("Cannot allocate redis context");
  41. } else if (ctx->err) {
  42. std::string err = std::string("Connection error: ") + ctx->errstr;
  43. redisFree(ctx);
  44. throw DatabaseException(err);
  45. }
  46. if (conf.exists("redis_password")) {
  47. tmp = conf.get("redis_password");
  48. redisReply *reply = static_cast<redisReply *>(redisCommand(ctx, "AUTH %s", tmp.c_str()));
  49. if (!reply)
  50. throw DatabaseException("Redis authentication failed");
  51. if (reply->type == REDIS_REPLY_ERROR) {
  52. std::string err = "Redis authentication failed: " + std::string(reply->str, reply->len);
  53. freeReplyObject(reply);
  54. throw DatabaseException(err);
  55. }
  56. freeReplyObject(reply);
  57. }
  58. }
  59. Database_Redis::~Database_Redis()
  60. {
  61. redisFree(ctx);
  62. }
  63. void Database_Redis::beginSave() {
  64. redisReply *reply = static_cast<redisReply *>(redisCommand(ctx, "MULTI"));
  65. if (!reply) {
  66. throw DatabaseException(std::string(
  67. "Redis command 'MULTI' failed: ") + ctx->errstr);
  68. }
  69. freeReplyObject(reply);
  70. }
  71. void Database_Redis::endSave() {
  72. redisReply *reply = static_cast<redisReply *>(redisCommand(ctx, "EXEC"));
  73. if (!reply) {
  74. throw DatabaseException(std::string(
  75. "Redis command 'EXEC' failed: ") + ctx->errstr);
  76. }
  77. freeReplyObject(reply);
  78. }
  79. bool Database_Redis::saveBlock(const v3s16 &pos, const std::string &data)
  80. {
  81. std::string tmp = i64tos(getBlockAsInteger(pos));
  82. redisReply *reply = static_cast<redisReply *>(redisCommand(ctx, "HSET %s %s %b",
  83. hash.c_str(), tmp.c_str(), data.c_str(), data.size()));
  84. if (!reply) {
  85. warningstream << "saveBlock: redis command 'HSET' failed on "
  86. "block " << PP(pos) << ": " << ctx->errstr << std::endl;
  87. freeReplyObject(reply);
  88. return false;
  89. }
  90. if (reply->type == REDIS_REPLY_ERROR) {
  91. warningstream << "saveBlock: saving block " << PP(pos)
  92. << " failed: " << std::string(reply->str, reply->len) << std::endl;
  93. freeReplyObject(reply);
  94. return false;
  95. }
  96. freeReplyObject(reply);
  97. return true;
  98. }
  99. void Database_Redis::loadBlock(const v3s16 &pos, std::string *block)
  100. {
  101. std::string tmp = i64tos(getBlockAsInteger(pos));
  102. redisReply *reply = static_cast<redisReply *>(redisCommand(ctx,
  103. "HGET %s %s", hash.c_str(), tmp.c_str()));
  104. if (!reply) {
  105. throw DatabaseException(std::string(
  106. "Redis command 'HGET %s %s' failed: ") + ctx->errstr);
  107. }
  108. switch (reply->type) {
  109. case REDIS_REPLY_STRING: {
  110. *block = std::string(reply->str, reply->len);
  111. // std::string copies the memory so this won't cause any problems
  112. freeReplyObject(reply);
  113. return;
  114. }
  115. case REDIS_REPLY_ERROR: {
  116. std::string errstr(reply->str, reply->len);
  117. freeReplyObject(reply);
  118. errorstream << "loadBlock: loading block " << PP(pos)
  119. << " failed: " << errstr << std::endl;
  120. throw DatabaseException(std::string(
  121. "Redis command 'HGET %s %s' errored: ") + errstr);
  122. }
  123. case REDIS_REPLY_NIL: {
  124. *block = "";
  125. // block not found in database
  126. freeReplyObject(reply);
  127. return;
  128. }
  129. }
  130. errorstream << "loadBlock: loading block " << PP(pos)
  131. << " returned invalid reply type " << reply->type
  132. << ": " << std::string(reply->str, reply->len) << std::endl;
  133. freeReplyObject(reply);
  134. throw DatabaseException(std::string(
  135. "Redis command 'HGET %s %s' gave invalid reply."));
  136. }
  137. bool Database_Redis::deleteBlock(const v3s16 &pos)
  138. {
  139. std::string tmp = i64tos(getBlockAsInteger(pos));
  140. redisReply *reply = static_cast<redisReply *>(redisCommand(ctx,
  141. "HDEL %s %s", hash.c_str(), tmp.c_str()));
  142. if (!reply) {
  143. throw DatabaseException(std::string(
  144. "Redis command 'HDEL %s %s' failed: ") + ctx->errstr);
  145. } else if (reply->type == REDIS_REPLY_ERROR) {
  146. warningstream << "deleteBlock: deleting block " << PP(pos)
  147. << " failed: " << std::string(reply->str, reply->len) << std::endl;
  148. freeReplyObject(reply);
  149. return false;
  150. }
  151. freeReplyObject(reply);
  152. return true;
  153. }
  154. void Database_Redis::listAllLoadableBlocks(std::vector<v3s16> &dst)
  155. {
  156. redisReply *reply = static_cast<redisReply *>(redisCommand(ctx, "HKEYS %s", hash.c_str()));
  157. if (!reply) {
  158. throw DatabaseException(std::string(
  159. "Redis command 'HKEYS %s' failed: ") + ctx->errstr);
  160. }
  161. switch (reply->type) {
  162. case REDIS_REPLY_ARRAY:
  163. dst.reserve(reply->elements);
  164. for (size_t i = 0; i < reply->elements; i++) {
  165. assert(reply->element[i]->type == REDIS_REPLY_STRING);
  166. dst.push_back(getIntegerAsBlock(stoi64(reply->element[i]->str)));
  167. }
  168. break;
  169. case REDIS_REPLY_ERROR:
  170. throw DatabaseException(std::string(
  171. "Failed to get keys from database: ") +
  172. std::string(reply->str, reply->len));
  173. }
  174. freeReplyObject(reply);
  175. }
  176. #endif // USE_REDIS