bool CodisClient::exists(string key, int tt)
{
    Reply rep = RedisCommand(Command("EXISTS")(key), tt);

    return rep.integer()==1;

}
/*
 * Class:     com_redis_sentinel_client_RedisClient
 * Method:    cRedisCommand
 * Signature: (Ljava/lang/String;)Ljava/lang/String;
 */
JNIEXPORT jstring JNICALL Java_com_redis_sentinel_client_RedisClient_cRedisCommand__Ljava_lang_String_2
  (JNIEnv *env, jclass, jstring jCommand)
{
	const char* ccommand = env->GetStringUTFChars(jCommand, NULL);
	std::string comm(ccommand);
	env->ReleaseStringUTFChars(jCommand, ccommand);

	std::vector<std::string> command;
	split(comm, std::string(" "), command);

	Reply ret = client->RedisCommand(command);

	stringstream stream;
	stream << ret.type() << ";"
			<< ret.str() << ";"
			<< ret.error() << ";"
			<< ret.integer() << ";";

	std::vector<Reply> elements = ret.elements();

	for (size_t i=0; i<elements.size(); i++)
	{
		stream << elements[i].type() << ";"
				<< elements[i].str() << ";"
				<< elements[i].error() << ";"
				<< elements[i].integer() << ";";
	}

	jstring reply = env->NewStringUTF(stream.str().c_str());

	return reply;
}
int CodisClient::incrby(string key, int incr, int tt)
{

    Reply rep = RedisCommand(Command("INCRBY")(key)(int2string(incr)), tt);

    return (rep.error()) ? 0 : rep.integer();
}
bool CodisClient::hset(string key, string field, string value, int tt)
{
    if (value.length()>1048576) {
        throw myex;
    }
    Reply rep = RedisCommand(Command("HSET")(key)(field)(value), tt);

    return rep.integer();
}
int CodisClient::rpush(string key, string value, int tt)
{
    if (value.length()>1048576) {
        throw myex;
    }
    Reply rep = RedisCommand(Command("RPUSH")(key)(value), tt);

    return rep.integer();
}
long CodisClient::append(string key, string value, int tt)
{
    if (value.length()>1048576) {
        throw myex;
    }
    Reply rep = RedisCommand(Command("APPEND")(key)(value), tt);

    return rep.integer();
}
bool CodisClient::setnx(string key, string value, int tt)
{
    if (value.length()>1048576) {
        throw myex;
    }
    Reply rep = RedisCommand(Command("SETNX")(key)(value), tt);

    return rep.integer() == 1;
}
int CodisClient::sadd(string key, vector<string> members, int tt)
{
    Command comm("SADD");
    comm(key);
    for (size_t i=0; i<members.size(); i++)
    {
        comm(members[i]);
    }

    Reply rep = RedisCommand(comm, tt);

    return rep.integer();
}
int CodisClient::del(vector<string>& keys, int tt)
{
    Command command("DEL");
    Reply rep;
    for (size_t i=0; i<keys.size(); i++)
    {
//		rep = RedisCommand(command(keys[i]));
        command(keys[i]);
    }

    rep = RedisCommand(command, tt);

    return rep.integer();
}
int CodisClient::rpush(string key, vector<string> values, int tt)
{
    Command comm("RPUSH");
    comm(key);
    for (size_t i=0; i<values.size(); i++)
    {
        if (values[i].length()>1048576) {
            throw myex;
        }
        comm(values[i]);
    }

    Reply rep = RedisCommand(comm, tt);

    return rep.integer();
}
void ReplyToString(Reply& reply, std::string &rep)
{
	stringstream stream;
	stream << reply.type() << ";"
			<< reply.str() << ";"
			<< reply.error() << ";"
			<< reply.integer() << ";";

	std::vector<Reply> elements = reply.elements();

	for (size_t i=0; i<elements.size(); i++)
	{
		stream << elements[i].type() << ";"
				<< elements[i].str() << ";"
				<< elements[i].error() << ";"
				<< elements[i].integer() << ";";
	}

	rep = stream.str();
}
bool CodisClient::hdel(string key, string field, int tt)
{
    Reply rep = RedisCommand(Command("HDEL")(key)(field), tt);

    return rep.integer()==1;
}
int CodisClient::hlen(string key, int tt)
{
    Reply rep = RedisCommand(Command("HLEN")(key), tt);

    return rep.integer();
}
int CodisClient::hincrby(string key, string field, int incr, int tt)
{
    Reply rep = RedisCommand(Command("HINCRBY")(key)(field)(int2string(incr)), tt);

    return rep.integer();
}
bool CodisClient::hexists(string key, string field, int tt)
{
    Reply rep = RedisCommand(Command("HEXISTS")(key)(field), tt);

    return rep.integer()==1;
}
bool CodisClient::expire(string key, int seconds, int tt)
{
    Reply rep = RedisCommand(Command("EXPIRE")(key)(int2string(seconds)), tt);

    return rep.integer()==1;
}
bool CodisClient::lrem(string key, int count, string value, int tt)
{
    Reply rep = RedisCommand(Command("LREM")(key)(int2string(count))(value), tt);

    return rep.integer();
}
int CodisClient::zcard(string key, int tt)
{
    Reply rep = RedisCommand(Command("ZCARD")(key), tt);

    return rep.integer();
}
int CodisClient::zremrangebyscore(string key, int min, int max, int tt)
{
    Reply rep = RedisCommand(Command("ZREMRANGEBYSCORE")(key)(int2string(min))(int2string(max)), tt);

    return rep.integer();
}
int CodisClient::del(string key, int tt)
{
    Reply rep = RedisCommand(Command("DEL")(key), tt);

    return rep.integer();
}
int CodisClient::zcount(string key, int min, int max, int tt)
{
    Reply rep = RedisCommand(Command("ZCOUNT")(key)(int2string(min))(int2string(max)), tt);

    return rep.integer();
}
int CodisClient::bitcount(string key, int tt)
{
    Reply rep = RedisCommand(Command("BITCOUNT")(key), tt);

    return rep.integer();
}
int CodisClient::decr(string key, int tt)
{
    Reply rep = RedisCommand(Command("DECR")(key), tt);

    return (rep.error()) ? 0 : rep.integer();
}
int CodisClient::setbit(string key, int index, bool value, int tt)
{
    Reply rep = RedisCommand(Command("SETBIT")(key)(int2string(index))(int2string(value)), tt);

    return rep.integer();
}
bool CodisClient::zadd(string key, int score, string member, int tt)
{
    Reply rep = RedisCommand(Command("ZADD")(key)(int2string(score))(member), tt);

    return rep.integer()==1;
}
bool CodisClient::sismember(string key, string member, int tt)
{
    Reply rep = RedisCommand(Command("SISMEMBER")(key)(member), tt);

    return rep.integer()==1;
}
std::string ReplyToString(const Reply& rep)
{
	stringstream retstream;

	vector<Reply> reps;
	if (rep.elements().size()>0)
	{
		reps = rep.elements();
	}

	switch (rep.type()) {
		case Reply::ERROR:
			retstream << rep.str();
			break;
		case Reply::STRING:
			retstream << rep.str();
			break;
		case Reply::STATUS:
			retstream << rep.str();
			break;
		case Reply::INTEGER:
		  	retstream << rep.integer();
		  	break;
		case Reply::NIL:
			retstream << "nil";
			break;
		case Reply::ARRAY:
			retstream << "*";
			retstream << rep.elements().size();
			retstream << endl;


			for (size_t idx=0; idx<reps.size(); ++idx) {
		    	switch (reps[idx].type()) {
					case Reply::ERROR:
						retstream << reps[idx].str();
						retstream << endl;
						break;
					case Reply::STRING:
						retstream << reps[idx].str();
						retstream << endl;
						break;
					case Reply::STATUS:
						retstream << reps[idx].str();
						retstream << endl;
						break;
					case Reply::INTEGER:
					  	retstream << reps[idx].integer();
					  	retstream << endl;
					  	break;
					case Reply::NIL:
						retstream << "nil";
						break;
					default:
						retstream << "+OK";
					    break;
		    	}
			}
		    break;
		default:
			retstream << "OK";
			break;
	}

	return retstream.str();
}
bool CodisClient::zrem(string key, string member, int tt)
{
    Reply rep = RedisCommand(Command("ZREM")(key)(member), tt);

    return rep.integer()==1;
}
int CodisClient::zrevrank(string key, string member, int tt)
{
    Reply rep = RedisCommand(Command("ZREVRANK")(key)(member), tt);

    return rep.integer();
}
int CodisClient::getbit(string key, int index, int tt)
{
    Reply rep = RedisCommand(Command("GETBIT")(key)(int2string(index)), tt);

    return rep.integer();
}