static void *createNilObject(const redisReadTask *task) { redisReply *r, *parent; r = createReplyObject(REDIS_REPLY_NIL); if (r == NULL) return NULL; if (task->parent) { parent = task->parent->obj; assert(parent->type == REDIS_REPLY_ARRAY); parent->element[task->idx] = r; } return r; }
void *notsupportCommandProc(proxyContext *p, int argc, char **argv, redisKeyInfo *keyInfo) { PROXY_NOTUSED(p); PROXY_NOTUSED(argc); PROXY_NOTUSED(argv); PROXY_NOTUSED(keyInfo); redisReply *reply; reply = createReplyObject(REDIS_REPLY_ERROR); char err[1024]; sprintf(err, "ERR not support %s command in proxy", argv[0]); reply->str = malloc(strlen(err)+1); strcpy(reply->str, err); return reply; }
/** *创建整数对象 */ static void *createIntegerObject(const redisReadTask *task, long long value) { redisReply *r, *parent; r = createReplyObject(REDIS_REPLY_INTEGER); if (r == NULL) return NULL; r->integer = value; if (task->parent) { parent = task->parent->obj; assert(parent->type == REDIS_REPLY_ARRAY); parent->element[task->idx] = r; } return r; }
static void *createStringObject(const redisReadTask *task, char *str, size_t len) { redisReply *r, *parent; char *buf; r = createReplyObject(task->type); if (r == NULL) return NULL; buf = malloc(len+1); if (buf == NULL) { freeReplyObject(r); return NULL; } /* Copy string value */ memcpy(buf,str,len); buf[len] = '\0'; switch(task->type) { case REDIS_REPLY_ERROR: r->reply_error.str = buf; r->reply_error.len = len; break; case REDIS_REPLY_STATUS: r->reply_status.str = buf; r->reply_status.len = len; break; case REDIS_REPLY_STRING: r->reply_string.str = buf; r->reply_string.len = len; break; default: freeReplyObject(r); return NULL; } if (task->parent) { parent = task->parent->obj; assert(parent->type == REDIS_REPLY_ARRAY); parent->reply_array.element[task->idx] = r; } return r; }
static redisReply *redisReadMultiBulkReply(int fd) { sds replylen = redisReadLine(fd); long elements, j; redisReply *r; if (replylen == NULL) return redisIOError(); elements = strtol(replylen,NULL,10); sdsfree(replylen); if (elements == -1) return createReplyObject(REDIS_REPLY_NIL,sdsempty()); if ((r = malloc(sizeof(*r))) == NULL) redisOOM(); r->type = REDIS_REPLY_ARRAY; r->elements = elements; if ((r->element = malloc(sizeof(*r)*elements)) == NULL) redisOOM(); for (j = 0; j < elements; j++) r->element[j] = redisReadReply(fd); return r; }
static void *createStringObject(const redisReadTask *task, char *str, size_t len) { redisReply *r = createReplyObject(task->type); char *value = malloc(len+1); if (!value) redisOOM(); assert(task->type == REDIS_REPLY_ERROR || task->type == REDIS_REPLY_STATUS || task->type == REDIS_REPLY_STRING); /* Copy string value */ memcpy(value,str,len); value[len] = '\0'; r->str = value; r->len = len; if (task->parent) { redisReply *parent = task->parent->obj; assert(parent->type == REDIS_REPLY_ARRAY); parent->element[task->idx] = r; } return r; }
void *sumIntegerKeyProc(proxyContext *p, int argc, char **argv, redisKeyInfo *keyInfo){ PROXY_NOTUSED(keyInfo); redisContext *c; redisReply *replyAll; replyAll = createReplyObject(REDIS_REPLY_INTEGER); if( replyAll ){ for( int i = 0; i < p->count; i++ ){ redisReply *reply; c = getRedisContextWithIdx( p, i ); reply = proxyCommandArgvList( p, c, argc, (const char **)argv ); int value = 0; if( reply ) { value = reply->integer; freeReplyObject(reply); } replyAll->integer += value; } } return replyAll; }
static void *createArrayObject(const redisReadTask *task, int elements) { redisReply *r, *parent; r = createReplyObject(REDIS_REPLY_ARRAY); if (r == NULL) return NULL; if (elements > 0) { r->element = calloc(elements,sizeof(redisReply*)); if (r->element == NULL) { freeReplyObject(r); return NULL; } } r->elements = elements; if (task->parent) { parent = task->parent->obj; assert(parent->type == REDIS_REPLY_ARRAY); parent->element[task->idx] = r; } return r; }
static redisReply *redisIOError(void) { return createReplyObject(REDIS_REPLY_ERROR,sdsnew("I/O error")); }
static redisReply *redisReadSingleLineReply(int fd, int type) { sds buf = redisReadLine(fd); if (buf == NULL) return redisIOError(); return createReplyObject(type,buf); }