diff options
Diffstat (limited to 'src/database/database-redis.cpp')
-rw-r--r-- | src/database/database-redis.cpp | 85 |
1 files changed, 48 insertions, 37 deletions
diff --git a/src/database/database-redis.cpp b/src/database/database-redis.cpp index 096ea504d..e75063f73 100644 --- a/src/database/database-redis.cpp +++ b/src/database/database-redis.cpp @@ -31,7 +31,6 @@ with this program; if not, write to the Free Software Foundation, Inc., #include <hiredis.h> #include <cassert> - Database_Redis::Database_Redis(Settings &conf) { std::string tmp; @@ -39,13 +38,15 @@ Database_Redis::Database_Redis(Settings &conf) tmp = conf.get("redis_address"); hash = conf.get("redis_hash"); } catch (SettingNotFoundException &) { - throw SettingNotFoundException("Set redis_address and " - "redis_hash in world.mt to use the redis backend"); + throw SettingNotFoundException( + "Set redis_address and " + "redis_hash in world.mt to use the redis backend"); } const char *addr = tmp.c_str(); int port = conf.exists("redis_port") ? conf.getU16("redis_port") : 6379; // if redis_address contains '/' assume unix socket, else hostname/ip - ctx = tmp.find('/') != std::string::npos ? redisConnectUnix(addr) : redisConnect(addr, port); + ctx = tmp.find('/') != std::string::npos ? redisConnectUnix(addr) + : redisConnect(addr, port); if (!ctx) { throw DatabaseException("Cannot allocate redis context"); } else if (ctx->err) { @@ -55,11 +56,13 @@ Database_Redis::Database_Redis(Settings &conf) } if (conf.exists("redis_password")) { tmp = conf.get("redis_password"); - redisReply *reply = static_cast<redisReply *>(redisCommand(ctx, "AUTH %s", tmp.c_str())); + redisReply *reply = static_cast<redisReply *>( + redisCommand(ctx, "AUTH %s", tmp.c_str())); if (!reply) throw DatabaseException("Redis authentication failed"); if (reply->type == REDIS_REPLY_ERROR) { - std::string err = "Redis authentication failed: " + std::string(reply->str, reply->len); + std::string err = "Redis authentication failed: " + + std::string(reply->str, reply->len); freeReplyObject(reply); throw DatabaseException(err); } @@ -72,20 +75,22 @@ Database_Redis::~Database_Redis() redisFree(ctx); } -void Database_Redis::beginSave() { +void Database_Redis::beginSave() +{ redisReply *reply = static_cast<redisReply *>(redisCommand(ctx, "MULTI")); if (!reply) { - throw DatabaseException(std::string( - "Redis command 'MULTI' failed: ") + ctx->errstr); + throw DatabaseException(std::string("Redis command 'MULTI' failed: ") + + ctx->errstr); } freeReplyObject(reply); } -void Database_Redis::endSave() { +void Database_Redis::endSave() +{ redisReply *reply = static_cast<redisReply *>(redisCommand(ctx, "EXEC")); if (!reply) { - throw DatabaseException(std::string( - "Redis command 'EXEC' failed: ") + ctx->errstr); + throw DatabaseException(std::string("Redis command 'EXEC' failed: ") + + ctx->errstr); } freeReplyObject(reply); } @@ -98,14 +103,16 @@ bool Database_Redis::saveBlock(const v3s16 &pos, const std::string &data) hash.c_str(), tmp.c_str(), data.c_str(), data.size())); if (!reply) { warningstream << "saveBlock: redis command 'HSET' failed on " - "block " << PP(pos) << ": " << ctx->errstr << std::endl; + "block " + << PP(pos) << ": " << ctx->errstr << std::endl; freeReplyObject(reply); return false; } if (reply->type == REDIS_REPLY_ERROR) { warningstream << "saveBlock: saving block " << PP(pos) - << " failed: " << std::string(reply->str, reply->len) << std::endl; + << " failed: " << std::string(reply->str, reply->len) + << std::endl; freeReplyObject(reply); return false; } @@ -117,12 +124,13 @@ bool Database_Redis::saveBlock(const v3s16 &pos, const std::string &data) void Database_Redis::loadBlock(const v3s16 &pos, std::string *block) { std::string tmp = i64tos(getBlockAsInteger(pos)); - redisReply *reply = static_cast<redisReply *>(redisCommand(ctx, - "HGET %s %s", hash.c_str(), tmp.c_str())); + redisReply *reply = static_cast<redisReply *>( + redisCommand(ctx, "HGET %s %s", hash.c_str(), tmp.c_str())); if (!reply) { - throw DatabaseException(std::string( - "Redis command 'HGET %s %s' failed: ") + ctx->errstr); + throw DatabaseException( + std::string("Redis command 'HGET %s %s' failed: ") + + ctx->errstr); } switch (reply->type) { @@ -136,9 +144,10 @@ void Database_Redis::loadBlock(const v3s16 &pos, std::string *block) std::string errstr(reply->str, reply->len); freeReplyObject(reply); errorstream << "loadBlock: loading block " << PP(pos) - << " failed: " << errstr << std::endl; - throw DatabaseException(std::string( - "Redis command 'HGET %s %s' errored: ") + errstr); + << " failed: " << errstr << std::endl; + throw DatabaseException( + std::string("Redis command 'HGET %s %s' errored: ") + + errstr); } case REDIS_REPLY_NIL: { *block = ""; @@ -149,25 +158,27 @@ void Database_Redis::loadBlock(const v3s16 &pos, std::string *block) } errorstream << "loadBlock: loading block " << PP(pos) - << " returned invalid reply type " << reply->type - << ": " << std::string(reply->str, reply->len) << std::endl; + << " returned invalid reply type " << reply->type << ": " + << std::string(reply->str, reply->len) << std::endl; freeReplyObject(reply); - throw DatabaseException(std::string( - "Redis command 'HGET %s %s' gave invalid reply.")); + throw DatabaseException( + std::string("Redis command 'HGET %s %s' gave invalid reply.")); } bool Database_Redis::deleteBlock(const v3s16 &pos) { std::string tmp = i64tos(getBlockAsInteger(pos)); - redisReply *reply = static_cast<redisReply *>(redisCommand(ctx, - "HDEL %s %s", hash.c_str(), tmp.c_str())); + redisReply *reply = static_cast<redisReply *>( + redisCommand(ctx, "HDEL %s %s", hash.c_str(), tmp.c_str())); if (!reply) { - throw DatabaseException(std::string( - "Redis command 'HDEL %s %s' failed: ") + ctx->errstr); + throw DatabaseException( + std::string("Redis command 'HDEL %s %s' failed: ") + + ctx->errstr); } else if (reply->type == REDIS_REPLY_ERROR) { warningstream << "deleteBlock: deleting block " << PP(pos) - << " failed: " << std::string(reply->str, reply->len) << std::endl; + << " failed: " << std::string(reply->str, reply->len) + << std::endl; freeReplyObject(reply); return false; } @@ -178,10 +189,11 @@ bool Database_Redis::deleteBlock(const v3s16 &pos) void Database_Redis::listAllLoadableBlocks(std::vector<v3s16> &dst) { - redisReply *reply = static_cast<redisReply *>(redisCommand(ctx, "HKEYS %s", hash.c_str())); + redisReply *reply = static_cast<redisReply *>( + redisCommand(ctx, "HKEYS %s", hash.c_str())); if (!reply) { - throw DatabaseException(std::string( - "Redis command 'HKEYS %s' failed: ") + ctx->errstr); + throw DatabaseException(std::string("Redis command 'HKEYS %s' failed: ") + + ctx->errstr); } switch (reply->type) { case REDIS_REPLY_ARRAY: @@ -192,12 +204,11 @@ void Database_Redis::listAllLoadableBlocks(std::vector<v3s16> &dst) } break; case REDIS_REPLY_ERROR: - throw DatabaseException(std::string( - "Failed to get keys from database: ") + - std::string(reply->str, reply->len)); + throw DatabaseException( + std::string("Failed to get keys from database: ") + + std::string(reply->str, reply->len)); } freeReplyObject(reply); } #endif // USE_REDIS - |