Exemplo n.º 1
0
mrb_value mrb_redis_basic_zrange(mrb_state *mrb, mrb_value self, const char *cmd)
{
    int i;
    mrb_value list, array;
    mrb_int arg1, arg2;
    redisContext *rc = DATA_PTR(self);

    mrb_get_args(mrb, "oii", &list, &arg1, &arg2);
    mrb_value arg1_str = mrb_fixnum_to_str(mrb, mrb_fixnum_value(arg1), 10);
    mrb_value arg2_str = mrb_fixnum_to_str(mrb, mrb_fixnum_value(arg2), 10);
    const char *argv[] = {"ZADD", RSTRING_PTR(list), RSTRING_PTR(arg1_str), RSTRING_PTR(arg2_str)};
    size_t lens[] = {4, RSTRING_LEN(list), RSTRING_LEN(arg1_str), RSTRING_LEN(arg2_str)};
    redisReply *rr = redisCommandArgv(rc, 4, argv, lens);
    if (rr->type == REDIS_REPLY_ARRAY) {
        array = mrb_ary_new(mrb);
        for (i = 0; i < rr->elements; i++) {
            mrb_ary_push(mrb, array, mrb_str_new(mrb, rr->element[i]->str, rr->element[i]->len));
        }
    } else {
        freeReplyObject(rr);
        return mrb_nil_value();
    }

    freeReplyObject(rr);

    return array;
}
Exemplo n.º 2
0
static int lconn_command(lua_State * L)
{
  redisContext * pContext = check_connection(L, 1);

  const char * argv[LUAHIREDIS_MAXARGS];
  size_t argvlen[LUAHIREDIS_MAXARGS];
  int nargs = load_args(L, pContext, 2, argv, argvlen);

  int nret = 0;

  redisReply * pReply = (redisReply *)redisCommandArgv(
      pContext, nargs, argv, argvlen
    );
  if (pReply == NULL)
  {
    /* TODO: Shouldn't we clear the context error state somehow after this? */
    return push_error(L, pContext);
  }

  nret = push_reply(L, pReply);

  /*
  * TODO: Not entirely safe: if above code throws error, reply object is leaked.
  */
  freeReplyObject(pReply);
  pReply = NULL;

  return nret;
}
Exemplo n.º 3
0
/* issuue one command with one key paratemer and return the result */
SEXP cr_cmd(SEXP sc, SEXP sArgs) {
    rconn_t *c;
    const char **argv = argbuf;
    int n, i;
    redisReply *reply;
    SEXP res;

    if (!Rf_inherits(sc, "redisConnection")) Rf_error("invalid connection");
    c = (rconn_t*) EXTPTR_PTR(sc);
    if (!c) Rf_error("invalid connection (NULL)");
    rc_validate_connection(c, 0);
    if (TYPEOF(sArgs) != STRSXP || LENGTH(sArgs) < 1)
	Rf_error("invalid command - must be a string");
    n = LENGTH(sArgs);
    if (n + 1 > NARGBUF) {
	argv = malloc(sizeof(const char*) * (n + 2));
	if (!argv)
	    Rf_error("out of memory");
    }
    for (i = 0; i < n; i++)
	argv[i] = CHAR(STRING_ELT(sArgs, i));
    /* we use strings only, so no need to supply argvlen */
    reply = redisCommandArgv(c->rc, n, argv, 0);
    if (!reply && (c->flags & RCF_RETRY)) {
	SEXP es = Rf_mkChar(c->rc->errstr);
	rc_close(c);
	rc_validate_connection(c, 1);
	if (c->rc)
	    reply = redisCommandArgv(c->rc, 2, argv, 0);
	else {
	    if (argv != argbuf)
		free(argv);
	    Rf_error("%s error: %s and re-connect failed", argv[0], CHAR(es));
	}
    }
    if (argv != argbuf)
	free(argv);
    if (!reply) {
	SEXP es = Rf_mkChar(c->rc->errstr);
	rc_close(c);
	Rf_error("%s error: %s", argv[0], CHAR(es));
    }
    /* Rprintf("reply, type=%d\n", reply->type); */
    res = rc_reply2R(reply);
    freeReplyObject(reply);
    return res;
}
Exemplo n.º 4
0
SEXP cr_del(SEXP sc, SEXP keys) {
    rconn_t *c;
    int n, i;
    const char **argv = argbuf;
    redisReply *reply;

    if (!Rf_inherits(sc, "redisConnection")) Rf_error("invalid connection");
    c = (rconn_t*) EXTPTR_PTR(sc);
    if (!c) Rf_error("invalid connection (NULL)");
    rc_validate_connection(c, 0);
    if (TYPEOF(keys) != STRSXP)
	Rf_error("invalid keys");
    n = LENGTH(keys);
    if (n + 1 > NARGBUF) {
	argv = malloc(sizeof(const char*) * (n + 2));
	if (!argv)
	    Rf_error("out of memory");
    }
    argv[0] = "DEL";
    for (i = 0; i < n; i++)
	argv[i + 1] = CHAR(STRING_ELT(keys, i));
    /* we use strings only, so no need to supply argvlen */
    reply = redisCommandArgv(c->rc, n + 1, argv, 0);
    if (!reply && (c->flags & RCF_RETRY)) {
	SEXP es = Rf_mkChar(c->rc->errstr);
	rc_close(c);
	rc_validate_connection(c, 1);
	if (c->rc)
	    reply = redisCommandArgv(c->rc, n + 1, argv, 0);
	else {
	    if (argv != argbuf)
		free(argv);
	    Rf_error("DEL error: %s and re-connect failed", CHAR(es));
	}
    }
    if (argv != argbuf)
	free(argv);
    if (!reply) {
	SEXP es = Rf_mkChar(c->rc->errstr);
	rc_close(c);
	Rf_error("DEL error: %s", CHAR(es));
    }
    /* Rprintf("reply, type=%d\n", reply->type); */
    freeReplyObject(reply);
    return R_NilValue;
}
Exemplo n.º 5
0
/**
 * Executes a redis command.
 * Command is coded using a vector of strings, and a vector of lenghts.
 *
 * @param rsrv Pointer to a redis_server_t structure.
 * @param argc number of elements in the command vector.
 * @param argv vector of zero terminated strings forming the command.
 * @param argvlen vector of command string lenghts or NULL.
 * @return redisReply structure or NULL if there was an error.
 */
void * redisc_exec_argv(redisc_server_t *rsrv, int argc, const char **argv, const size_t *argvlen)
{
	redisReply *res=NULL;

	if(rsrv==NULL || rsrv->ctxRedis==NULL)
	{
		LM_ERR("no redis context found for server %.*s\n",
			   rsrv->sname->len, rsrv->sname->s);
		return NULL;
	}
	if(argc<=0)
	{
		LM_ERR("invalid parameters\n");
		return NULL;
	}
	if(argv==NULL || *argv==NULL)
	{
		LM_ERR("invalid parameters\n");
		return NULL;
	}
	res = redisCommandArgv(rsrv->ctxRedis, argc, argv, argvlen);
	if(res)
	{
		return res;
	}

	/* null reply, reconnect and try again */
	if(rsrv->ctxRedis->err)
	{
		LM_ERR("Redis error: %s\n", rsrv->ctxRedis->errstr);
	}
	if(redisc_reconnect_server(rsrv)==0)
	{
		res = redisCommandArgv(rsrv->ctxRedis, argc, argv, argvlen);
	}
	else
	{
		LM_ERR("Unable to reconnect to server: %.*s\n",
			   rsrv->sname->len, rsrv->sname->s);
		return NULL;
	}

	return res;
}
Exemplo n.º 6
0
static JSVAL command(JSARGS args) {
    redisContext *c = (redisContext *)JSOPAQUE(args[0]);
    String::Utf8Value _query(args[1]->ToString());
    char *argstr = strdup(*_query);
    char *in = argstr;
    int n = 0;
    while (*in) {
        while (isspace(*in)) in++;
        if (*in) {
            n++;
            if (*in == '"') {
                in++;
                while (*in && *in != '"') in++;
                if (*in == '"') in++;
            }
            else {
                while (*in && !isspace(*in)) in++;
            }
        }
    }
    in = argstr;
    int argc = n;
    char *argv[n];
    n = 0;
    while (*in) {
        while (isspace(*in)) in++;
        if (*in) {
            if (*in == '"') {
                in++;
                argv[n++] = in;
                while (*in && *in != '"') in++;
                if (*in == '"') {
                    *in = '\0';
                    in++;
                }
            }
            else {
                argv[n++] = in;
                while (*in && !isspace(*in)) {
					in++;
				}
				if (*in != '\0') {
                	*in++ = '\0';
				}
            }
        }
    }
    // printf("%s\n", *_query);
    // redisReply *reply = (redisReply *)redisCommand(c, *_query);
    redisReply *reply = (redisReply *)redisCommandArgv(c, argc, (const char **)argv, NULL);
    free(argstr);
    return Opaque::New(reply);    
}
Exemplo n.º 7
0
long long _do_redis_command( const char ** args,const size_t * argvlen, size_t arg_count) {

    pthread_mutex_lock(&sredisContext_mutex);

    redisContext *c = NULL;
    redisReply *reply;


    c = (redisContext*)_redis_context_init();
    if (!c) {
	info_print("_redis_context_init return null, connect failed\n");
	pthread_mutex_unlock(&sredisContext_mutex);
	return -1;
    }

    debug_print("%s %s %s\n",args[0] ,args[1],args[2]);

    reply =  redisCommandArgv(c,arg_count,args,argvlen);
    if(!reply) {

    	c = (redisContext*)_redis_context_reinit();
        if (!c) {
            info_print("_do_redis_command, Cannot reconnect to redis\n ");
            pthread_mutex_unlock(&sredisContext_mutex);
            return -1;
        }
    	reply = redisCommandArgv(c,arg_count,args,argvlen);
        if (!reply) {
            info_print("_do_redis_command, reconnect to redis and re-execute redisCommandArgv failed\n ");
            pthread_mutex_unlock(&sredisContext_mutex);
            return -1;
        }
    }

	if(reply) {
	    freeReplyObject(reply);
	}
	pthread_mutex_unlock(&sredisContext_mutex);
    return 0;
}
Exemplo n.º 8
0
/*
 *	Query the redis database
 */
int rlm_redis_query(REDISSOCK **dissocket_p, REDIS_INST *inst,
		    const char *query, REQUEST *request)
{
	REDISSOCK *dissocket;
	int argc;
	const char *argv[MAX_REDIS_ARGS];
	char argv_buf[MAX_QUERY_LEN];

	if (!query || !*query || !inst || !dissocket_p) {
		return -1;
	}

	argc = rad_expand_xlat(request, query, MAX_REDIS_ARGS, argv, 0,
				sizeof(argv_buf), argv_buf);
	if (argc <= 0)
		return -1;

	dissocket = *dissocket_p;

	DEBUG2("executing %s ...", argv[0]);
	dissocket->reply = redisCommandArgv(dissocket->conn, argc, argv, NULL);

	if (!dissocket->reply) {
		radlog(L_ERR, "rlm_redis: (%s) REDIS error: %s",
		       inst->xlat_name, dissocket->conn->errstr);

		dissocket = fr_connection_reconnect(inst->pool, dissocket);
		if (!dissocket) {
		error:
			*dissocket_p = NULL;
			return -1;
		}

		dissocket->reply = redisCommand(dissocket->conn, query);
		if (!dissocket->reply) {
			radlog(L_ERR, "rlm_redis (%s): failed after re-connect",
			       inst->xlat_name);
			fr_connection_del(inst->pool, dissocket);
			goto error;
		}

		*dissocket_p = dissocket;
	}

	if (dissocket->reply->type == REDIS_REPLY_ERROR) {
		radlog(L_ERR, "rlm_redis (%s): query failed, %s",
		       inst->xlat_name, query);
		return -1;
	}

	return 0;
}
Exemplo n.º 9
0
mrb_value mrb_redis_pub(mrb_state *mrb, mrb_value self)
{
    mrb_value channel, msg;
    redisContext *rc = DATA_PTR(self);

    mrb_get_args(mrb, "oo", &channel, &msg);
    const char *argv[] = {"PUBLISH", RSTRING_PTR(channel), RSTRING_PTR(msg)};
    size_t lens[] = {7, RSTRING_LEN(channel), RSTRING_LEN(msg)};
    redisReply *rr = redisCommandArgv(rc, 3, argv, lens);
    freeReplyObject(rr);

    return  self;
}
Exemplo n.º 10
0
mrb_value mrb_redis_del(mrb_state *mrb, mrb_value self)
{
    mrb_value key;
    redisContext *rc = DATA_PTR(self);

    mrb_get_args(mrb, "o", &key);
    const char *argv[] = {"DEL", RSTRING_PTR(key)};
    size_t lens[] = {3, RSTRING_LEN(key)};
    redisReply *rs = redisCommandArgv(rc, 2, argv, lens);
    freeReplyObject(rs);

    return  self;
}
Exemplo n.º 11
0
mrb_value mrb_redis_hset(mrb_state *mrb, mrb_value self) {
    mrb_value key, field, val;
    redisContext *rc = DATA_PTR(self);
    mrb_int integer;

    mrb_get_args(mrb, "ooo", &key, &field, &val);
    const char *argv[] = {"HSET", RSTRING_PTR(key), RSTRING_PTR(field), RSTRING_PTR(val)};
    size_t lens[] = {4, RSTRING_LEN(key), RSTRING_LEN(field), RSTRING_LEN(val)};
    redisReply *rs = redisCommandArgv(rc, 4, argv, lens);
    integer = rs->integer;
    freeReplyObject(rs);

    return integer ? mrb_true_value() : mrb_false_value();
}
Exemplo n.º 12
0
static int lmset(lua_State *L){
    int i;
    struct redisContext *c = lua_touserdata(L, 1);
    if(c == NULL){
        LOG_ERROR("userdata is null");
        return 0;
    }
    int argc = lua_gettop(L);
    if(redis_argc < argc){
        if(redis_argv != NULL){
            redis_argc = 0;
            free(redis_argv);
        }
        if(redis_argvlen != NULL){
            redis_argc = 0;
            free(redis_argvlen);
        }
        redis_argv = (char **)malloc(argc * sizeof(char *));
        redis_argvlen = (size_t *)malloc(argc * sizeof(size_t));
        if(!redis_argv || !redis_argvlen){
            redis_argv = 0;
            if(redis_argv)free(redis_argv);
            if(redis_argvlen)free(redis_argvlen);
            LOG_ERROR("malloc fail");
            return 0;
        }
        redis_argc = argc;
    }
    redis_argv[0] = s_mset_cmd;
    redis_argvlen[0] = 4;
    for(i = 0; i < (argc - 1)/2; i++){
        size_t k_len = 0;
        size_t v_len = 0;
        const char *k = (const char *)lua_tolstring(L, (i*2) + 2, &k_len);
        const char *v = (const char *)lua_tolstring(L, (i*2) + 3, &v_len);
        redis_argv[(2*i)+1] = (char *)k;
        redis_argvlen[(2*i)+1] = k_len;
        redis_argv[(2*i)+2] = (char *)v;
        redis_argvlen[(2*i)+2] = v_len;
    }
    redisReply * reply = (redisReply *)redisCommandArgv(c, argc, (const char **)redis_argv, redis_argvlen);
    if(reply == NULL){
        LOG_ERROR("MSET FAIL");
        return 0;
    }

    getCallback_push_reply(L, reply);
    freeReplyObject(reply);
    return 1;
}
Exemplo n.º 13
0
mrb_value mrb_redis_zscore(mrb_state *mrb, mrb_value self)
{
    mrb_value key, member, score;
    redisContext *rc = DATA_PTR(self);

    mrb_get_args(mrb, "oo", &key, &member);
    const char *argv[] = {"ZSCORE", RSTRING_PTR(key), RSTRING_PTR(member)};
    size_t lens[] = {6, RSTRING_LEN(key), RSTRING_LEN(member)};
    redisReply *rr = redisCommandArgv(rc, 3, argv, lens);
    score = mrb_str_new(mrb, rr->str, rr->len);
    freeReplyObject(rr);

    return score;
}
Exemplo n.º 14
0
mrb_value mrb_redis_hdel(mrb_state *mrb, mrb_value self) {
    mrb_value key, val;
    mrb_int integer;
    redisContext *rc = DATA_PTR(self);

    mrb_get_args(mrb, "oo", &key, &val);
    const char *argv[] = {"HDEL", RSTRING_PTR(key), RSTRING_PTR(val)};
    size_t lens[] = {4, RSTRING_LEN(key), RSTRING_LEN(val)};
    redisReply *rr = redisCommandArgv(rc, 3, argv, lens);
    integer = rr->integer;
    freeReplyObject(rr);

    return mrb_fixnum_value(integer);
}
Exemplo n.º 15
0
mrb_value mrb_redis_set(mrb_state *mrb, mrb_value self)
{
    mrb_value key, val;
    redisContext *rc = DATA_PTR(self);

    mrb_get_args(mrb, "oo", &key, &val);

    const char *argv[] = {"SET", RSTRING_PTR(key), RSTRING_PTR(val)};
    size_t lens[] = {3, RSTRING_LEN(key), RSTRING_LEN(val)};
    redisReply *rs = redisCommandArgv(rc, 3, argv, lens);
    freeReplyObject(rs);

    return  self;
}
Exemplo n.º 16
0
mrb_value mrb_redis_zadd(mrb_state *mrb, mrb_value self)
{
    mrb_value key, member;
    mrb_float score;
    redisContext *rc = DATA_PTR(self);

    mrb_get_args(mrb, "ofo", &key, &score, &member);
    mrb_value score_str = mrb_float_to_str(mrb, mrb_float_value(mrb, score), "%f");
    const char *argv[] = {"ZADD", RSTRING_PTR(key), RSTRING_PTR(score_str), RSTRING_PTR(member)};
    size_t lens[] = {4, RSTRING_LEN(key), RSTRING_LEN(score_str), RSTRING_LEN(member)};
    redisReply *rs = redisCommandArgv(rc, 4, argv, lens);
    freeReplyObject(rs);

    return  self;
}
Exemplo n.º 17
0
mrb_value mrb_redis_ttl(mrb_state *mrb, mrb_value self)
{
    mrb_value key;
    redisContext *rc = DATA_PTR(self);
    mrb_int integer;

    mrb_get_args(mrb, "o", &key);
    const char *argv[] = {"TTL", RSTRING_PTR(key)};
    size_t lens[] = {3, RSTRING_LEN(key)};
    redisReply *rr = redisCommandArgv(rc, 2, argv, lens);
    integer = rr->integer;
    freeReplyObject(rr);

    return mrb_fixnum_value(integer);
}
Exemplo n.º 18
0
mrb_value mrb_redis_exists(mrb_state *mrb, mrb_value self)
{
    mrb_value key;
    mrb_int counter;
    redisContext *rc = DATA_PTR(self);

    mrb_get_args(mrb, "o", &key);
    const char *argv[] = {"EXISTS", RSTRING_PTR(key)};
    size_t lens[] = {6, RSTRING_LEN(key)};
    redisReply *rr = redisCommandArgv(rc, 2, argv, lens);
    counter = rr->integer;
    freeReplyObject(rr);

    return counter ? mrb_true_value() : mrb_false_value();
}
Exemplo n.º 19
0
mrb_value mrb_redis_basic_zrank(mrb_state *mrb, mrb_value self, const char* cmd)
{
    mrb_value key, member;
    mrb_int rank;
    redisContext *rc = DATA_PTR(self);

    mrb_get_args(mrb, "oo", &key, &member);
    const char *argv[] = {cmd, RSTRING_PTR(key), RSTRING_PTR(member)};
    size_t lens[] = {strlen(cmd), RSTRING_LEN(key), RSTRING_LEN(member)};
    redisReply *rr = redisCommandArgv(rc, 3, argv, lens);
    rank = rr->integer;
    freeReplyObject(rr);

    return mrb_fixnum_value(rank);
}
Exemplo n.º 20
0
mrb_value mrb_redis_expire(mrb_state *mrb, mrb_value self)
{
    mrb_value key;
    mrb_int expire, counter;
    redisContext *rc = DATA_PTR(self);

    mrb_get_args(mrb, "oi", &key, &expire);
    mrb_value val = mrb_fixnum_to_str(mrb, mrb_fixnum_value(expire), 10);
    const char *argv[] = {"EXPIRE", RSTRING_PTR(key), RSTRING_PTR(val)};
    size_t lens[] = {6, RSTRING_LEN(key), RSTRING_LEN(val)};
    redisReply *rr = redisCommandArgv(rc, 3, argv, lens);
    counter = rr->integer;
    freeReplyObject(rr);

    return mrb_bool_value(counter == 1);
}
Exemplo n.º 21
0
mrb_value mrb_redis_decrby(mrb_state *mrb, mrb_value self)
{
    mrb_value key;
    mrb_int val, counter;
    redisContext *rc = DATA_PTR(self);

    mrb_get_args(mrb, "oi", &key, &val);
    mrb_value val_str = mrb_fixnum_to_str(mrb, mrb_fixnum_value(val), 10);
    const char *argv[] = {"DECRBY", RSTRING_PTR(key), RSTRING_PTR(val_str)};
    size_t lens[] = {6, RSTRING_LEN(key), RSTRING_LEN(val_str)};
    redisReply *rr = redisCommandArgv(rc, 3, argv, lens);
    counter = rr->integer;
    freeReplyObject(rr);

    return mrb_fixnum_value(counter);
}
Exemplo n.º 22
0
SEXP redux_redis_unsubscribe(SEXP extPtr, SEXP channel, SEXP pattern) {
  redisContext *context = redis_get_context(extPtr, CLOSED_ERROR);
  // Issue the unsubscribe command:
  const int p = INTEGER(pattern)[0];
  SEXP cmd = PROTECT(allocVector(VECSXP, 2));
  SET_VECTOR_ELT(cmd, 0, mkString(p ? "PUNSUBSCRIBE" : "UNSUBSCRIBE"));
  SET_VECTOR_ELT(cmd, 1, channel);
  cmd = PROTECT(redis_check_command(cmd));
  // Arrange the command:
  const char **argv = NULL;
  size_t *argvlen = NULL;
  const size_t argc = sexp_to_redis(cmd, &argv, &argvlen);
  // Issue the unsubscribe request:
  redisReply *reply = redisCommandArgv(context, argc, argv, argvlen);
  // Then loop until the reply looks correct.
  int n_discarded = 0;
  while (1) {
    if (reply == NULL) {
      // Here, we probably should consider destroying the client as
      // there is no API way of ensuring that the responses are dealt
      // with.
      //
      // This needs testing with toxiproxy.
      error("Redis connection error: client likely in awkward spot");
    }
    // This is possibly over-cautious, but it doesn't really hurt.
    if (reply->type == REDIS_REPLY_ARRAY && reply->elements == 3) {
      redisReply *reply0 = reply->element[0];
      if (reply0->type == REDIS_REPLY_STRING &&
          strcmp(reply0->str, p ? "punsubscribe" : "unsubscribe") == 0) {
        break;
      }
    }
    freeReplyObject(reply);
    reply = NULL;
    n_discarded++;
    redisGetReply(context, (void*)&reply);
  }
  SEXP ret = PROTECT(redis_reply_to_sexp(reply, REPLY_ERROR_THROW));
  freeReplyObject(reply);
  if (n_discarded > 0) {
    setAttrib(ret, mkString("n_discarded"), ScalarInteger(n_discarded));
  }
  UNPROTECT(3);
  return ret;
}
Exemplo n.º 23
0
mrb_value mrb_redis_ltrim(mrb_state *mrb, mrb_value self)
{
    mrb_value list;
    mrb_int arg1, arg2, integer;
    redisContext *rc = DATA_PTR(self);

    mrb_get_args(mrb, "oii", &list, &arg1, &arg2);
    mrb_value val_str1 = mrb_fixnum_to_str(mrb, mrb_fixnum_value(arg1), 10);
    mrb_value val_str2 = mrb_fixnum_to_str(mrb, mrb_fixnum_value(arg2), 10);
    const char *argv[] = {"LTRIM", RSTRING_PTR(list), RSTRING_PTR(val_str1), RSTRING_PTR(val_str2)};
    size_t lens[] = {6, RSTRING_LEN(list), RSTRING_LEN(val_str1), RSTRING_LEN(val_str2)};
    redisReply *rr = redisCommandArgv(rc, 4, argv, lens);
    integer = rr->integer;
    freeReplyObject(rr);

    return  mrb_fixnum_value(integer);
}
Exemplo n.º 24
0
mrb_value mrb_redis_hget(mrb_state *mrb, mrb_value self) {
    mrb_value key, field;
    char *val;
    int len;
    redisContext *rc = DATA_PTR(self);

    mrb_get_args(mrb, "oo", &key, &field);
    const char *argv[] = {"HGET", RSTRING_PTR(key), RSTRING_PTR(field)};
    size_t lens[] = {4, RSTRING_LEN(key), RSTRING_LEN(field)};
    redisReply *rs = redisCommandArgv(rc, 3, argv, lens);
    if (rs->type == REDIS_REPLY_STRING) {
        val = malloc((rs->len + 1) * sizeof(char));
        memcpy(val, rs->str, (rs->len + 1) * sizeof(char));
        len = rs->len;
        freeReplyObject(rs);
        return mrb_str_new(mrb, val, len);
    } else {
        freeReplyObject(rs);
        return mrb_nil_value();
    }
}
Exemplo n.º 25
0
int RedisCli::_doExeCmdNoReconnect(Replyer &rpler, int argc, const char **argv,
                                   const size_t *argvLen)
{
    redisReply *tmpReply = (redisReply *)redisCommandArgv(m_c, argc, argv, argvLen);

    if (tmpReply == NULL) {
        disconnect();
        return -1;
    }
    if (m_c->err) {
        disconnect();
        return -3;
    }
    //logic error, do not disconnect it!
    if (tmpReply->type == REDIS_REPLY_ERROR) {
        freeReplyObject(tmpReply);
        return -4;
    }
    rpler.setReply(tmpReply);
    return 0;
}
Exemplo n.º 26
0
mrb_value mrb_redis_hkeys(mrb_state *mrb, mrb_value self)
{
    mrb_value key, array = mrb_nil_value();
    redisContext *rc = DATA_PTR(self);

    mrb_get_args(mrb, "o", &key);
    const char *argv[] = {"HKEYS", RSTRING_PTR(key)};
    size_t lens[] = {5, RSTRING_LEN(key)};
    redisReply *rr = redisCommandArgv(rc, 2, argv, lens);
    if (rr->type == REDIS_REPLY_ARRAY) {
        if (rr->elements > 0) {
            int i;

            array = mrb_ary_new(mrb);
            for (i = 0; i < rr->elements; i++) {
                mrb_ary_push(mrb, array, mrb_str_new(mrb, rr->element[i]->str, rr->element[i]->len));
            }
        }
    }
    freeReplyObject(rr);
    return array;
}
Exemplo n.º 27
0
mrb_value mrb_redis_lpop(mrb_state *mrb, mrb_value self)
{
    char *val;
    int len;
    mrb_value key;
    redisContext *rc = DATA_PTR(self);

    mrb_get_args(mrb, "o", &key);
    const char *argv[] = {"LPOP", RSTRING_PTR(key)};
    size_t lens[] = {4, RSTRING_LEN(key)};
    redisReply *rr = redisCommandArgv(rc, 2, argv, lens);
    if (rr->type == REDIS_REPLY_STRING) {
        val = malloc((rr->len + 1) * sizeof(char));
        memcpy(val, rr->str, (rr->len + 1) * sizeof(char));
        len = rr->len;
        freeReplyObject(rr);
        return mrb_str_new(mrb, val, len);
    } else {
        freeReplyObject(rr);
        return mrb_nil_value();
    }
}
Exemplo n.º 28
0
  bool cmd(int _argc,const char**_argv,listStr & _list)
  {
  	_list.clear();
	size_t *_argv_len=new size_t[_argc+1];
	for(int i=0;i<_argc;i++)
		_argv_len[i]=strlen(_argv[i]);
  	this->_reply = (redisReply*)redisCommandArgv(this->_connect,_argc+1,_argv,_argv_len);
	delete []_argv_len;
	do
	{
		if(this->_reply)
	    {
	    	if(REDIS_REPLY_ERROR == _reply->type)
				break;
			parse_reply(this->_reply,_list);
	    	freeReplyObject(this->_reply);
			this->_reply=NULL;
			return true;
	    }
	}while(0);
	std::cout<<"ERR::"<<_connect->err<<std::endl;
    false;;
  }
Exemplo n.º 29
0
Arquivo: rdd.c Projeto: Redsmin/rdd
void rddRedisDelete(redisContext*rd,char*rdd)
{   u32 nb=0;
    char **keys = rddGetAllKeys(rdd,&nb);
    int argnb = nb+1;
    const char ** cargv = (const char**) malloc( argnb*(sizeof(char*)) );
    size_t *cargl = (size_t*)malloc( argnb*(sizeof(size_t*)) );

    cargv[0] = "DEL";
    cargl[0] = 3;

    for(u32 c=0; c<nb; c++)
    {   char*n = (keys[c])+12;
        cargv[c+1] = n;
        cargl[c+1] = strlen(n);
    }
    redisReply * reply = redisCommandArgv(rd, argnb, cargv, cargl);

    //printf("%i keys where removed from redis.\n",reply->integer);

    free(cargv);
    free(cargl);
    free(keys);
}
Exemplo n.º 30
-1
void *db_redis_command_argv(km_redis_con_t *con, redis_key_t *query) {
    char **argv = NULL;
    int argc;

    print_query(query);

    argc = db_redis_key_list2arr(query, &argv);
    if (argc < 0) {
        LM_ERR("Failed to allocate memory for query array\n");
        return NULL;
    }
    LM_DBG("query has %d args\n", argc);

    redisReply *reply = redisCommandArgv(con->con, argc, (const char**)argv, NULL);
    if (con->con->err == REDIS_ERR_EOF) {
        if (db_redis_connect(con) != 0) {
            LM_ERR("Failed to reconnect to redis db\n");
            pkg_free(argv);
            if (con->con) {
                redisFree(con->con);
                con->con = NULL;
            }
            return NULL;
        }
        reply = redisCommandArgv(con->con, argc, (const char**)argv, NULL);
    }
    pkg_free(argv);
    return reply;
}