aboutsummaryrefslogtreecommitdiff
path: root/po/ko
diff options
context:
space:
mode:
authorsfan5 <sfan5@live.de>2016-12-29 13:17:24 +0100
committerNer'zhul <nerzhul@users.noreply.github.com>2016-12-29 13:17:24 +0100
commite509ead680cc1c0bde35dfd467d18b8288cc1b86 (patch)
treee3df3b98a7424a32638ae63ee8d90a65502eb463 /po/ko
parent094a5a73d3b9817d07c52754749f0828c4005b8c (diff)
downloadminetest-e509ead680cc1c0bde35dfd467d18b8288cc1b86.tar.gz
minetest-e509ead680cc1c0bde35dfd467d18b8288cc1b86.tar.bz2
minetest-e509ead680cc1c0bde35dfd467d18b8288cc1b86.zip
Buildbot: Update Gettext version (#4971)
Diffstat (limited to 'po/ko')
0 files changed, 0 insertions, 0 deletions
'>113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
/*
Minetest
Copyright (C) 2014 celeron55, Perttu Ahola <celeron55@gmail.com>

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

#include "config.h"

#if USE_REDIS

#include "database-redis.h"

#include "settings.h"
#include "log.h"
#include "exceptions.h"
#include "util/string.h"

#include <hiredis.h>
#include <cassert>


Database_Redis::Database_Redis(Settings &conf)
{
	std::string tmp;
	try {
		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");
	}
	const char *addr = tmp.c_str();
	int port = conf.exists("redis_port") ? conf.getU16("redis_port") : 6379;
	ctx = redisConnect(addr, port);
	if (!ctx) {
		throw FileNotGoodException("Cannot allocate redis context");
	} else if (ctx->err) {
		std::string err = std::string("Connection error: ") + ctx->errstr;
		redisFree(ctx);
		throw FileNotGoodException(err);
	}
}

Database_Redis::~Database_Redis()
{
	redisFree(ctx);
}

void Database_Redis::beginSave() {
	redisReply *reply = static_cast<redisReply *>(redisCommand(ctx, "MULTI"));
	if (!reply) {
		throw FileNotGoodException(std::string(
			"Redis command 'MULTI' failed: ") + ctx->errstr);
	}
	freeReplyObject(reply);
}

void Database_Redis::endSave() {
	redisReply *reply = static_cast<redisReply *>(redisCommand(ctx, "EXEC"));
	if (!reply) {
		throw FileNotGoodException(std::string(
			"Redis command 'EXEC' failed: ") + ctx->errstr);
	}
	freeReplyObject(reply);
}

bool Database_Redis::saveBlock(const v3s16 &pos, const std::string &data)
{
	std::string tmp = i64tos(getBlockAsInteger(pos));

	redisReply *reply = static_cast<redisReply *>(redisCommand(ctx, "HSET %s %s %b",
			hash.c_str(), tmp.c_str(), data.c_str(), data.size()));
	if (!reply) {
		errorstream << "WARNING: saveBlock: redis command 'HSET' failed on "
			"block " << PP(pos) << ": " << ctx->errstr << std::endl;
		freeReplyObject(reply);
		return false;
	}

	if (reply->type == REDIS_REPLY_ERROR) {
		errorstream << "WARNING: saveBlock: saving block " << PP(pos)
			<< " failed: " << reply->str << std::endl;
		freeReplyObject(reply);
		return false;
	}

	freeReplyObject(reply);
	return true;
}

std::string Database_Redis::loadBlock(const v3s16 &pos)
{
	std::string tmp = i64tos(getBlockAsInteger(pos));
	redisReply *reply = static_cast<redisReply *>(redisCommand(ctx,
			"HGET %s %s", hash.c_str(), tmp.c_str()));

	if (!reply) {
		throw FileNotGoodException(std::string(
			"Redis command 'HGET %s %s' failed: ") + ctx->errstr);
	}
	switch (reply->type) {
	case REDIS_REPLY_STRING: {
		std::string str(reply->str, reply->len);
		// std::string copies the memory so this won't cause any problems
		freeReplyObject(reply);
		return str;
	}
	case REDIS_REPLY_ERROR:
		errorstream << "WARNING: loadBlock: loading block " << PP(pos)
			<< " failed: " << reply->str << std::endl;
	}
	freeReplyObject(reply);
	return "";
}