static inline struct item * _add_key(struct response *rsp, struct bstring *key) { struct element *reply = (struct element *)array_get(rsp->token, 0); struct item *it; item_rstatus_e istatus; it = item_get(key); if (it != NULL) { rsp->type = reply->type = ELEM_ERR; reply->bstr = str2bstr(RSP_EXIST); INCR(process_metrics, list_create_exist); return NULL; } else { /* TODO: figure out a TTL story here */ istatus = item_reserve(&it, key, NULL, ZIPLIST_HEADER_SIZE, 0, INT32_MAX); if (istatus != ITEM_OK) { rsp->type = reply->type = ELEM_ERR; reply->bstr = str2bstr(RSP_ERR_STORAGE); INCR(process_metrics, list_create_ex); INCR(process_metrics, process_ex); } else { INCR(process_metrics, list_create_stored); } return it; } }
END_TEST START_TEST(test_error) { #define ERR "something is wrong" #define SERIALIZED "-" ERR "\r\n" struct element el_c, el_p; int ret; int len = sizeof(SERIALIZED) - 1; char *pos; test_reset(); /* compose */ el_c.type = ELEM_ERR; el_c.bstr = str2bstr(ERR); ret = compose_element(&buf, &el_c); ck_assert_msg(ret == len, "bytes expected: %d, returned: %d", len, ret); ck_assert_int_eq(cc_bcmp(buf->rpos, SERIALIZED, ret), 0); /* parse */ pos = buf->rpos + 1; ret = parse_element(&el_p, buf); ck_assert_int_eq(ret, PARSE_OK); ck_assert(buf->rpos == buf->wpos); ck_assert(el_p.type == ELEM_ERR); ck_assert(el_p.bstr.len == sizeof(ERR) - 1); ck_assert(el_p.bstr.data == pos); #undef SERIALIZED #undef ERR }
void cmd_list_create(struct response *rsp, struct request *req, struct command *cmd) { struct item *it; struct bstring *key = _get_key(req); struct element *reply = (struct element *)array_push(rsp->token); INCR(process_metrics, list_create); it = _add_key(rsp, key); if (it == NULL) { log_debug("command '%.*s' '%.*s' failed: cannot store", cmd->bstr.len, cmd->bstr.data, key->len, key->data); return; } /* initialize data structure */ ziplist_reset((ziplist_p)item_data(it)); it->vlen = ZIPLIST_HEADER_SIZE; /* link into index */ item_insert(it, key); rsp->type = reply->type = ELEM_STR; reply->bstr = str2bstr(RSP_OK); log_verb("command '%.*s' '%.*s' succeeded", cmd->bstr.len, cmd->bstr.data, key->len, key->data); }
static void _admin_version(struct response *rsp, struct request *req) { INCR(admin_metrics, version); rsp->type = RSP_GENERIC; cc_snprintf(version_buf, VERSION_PRINT_LEN, VERSION_PRINT_FMT, VERSION_STRING); rsp->data = str2bstr(version_buf); }
void cmd_list_delete(struct response *rsp, struct request *req, struct command *cmd) { struct bstring *key = _get_key(req); struct element *reply = (struct element *)array_push(rsp->token); INCR(process_metrics, list_delete); if (item_delete(key)) { reply->bstr = str2bstr(RSP_OK); INCR(process_metrics, list_delete_deleted); log_verb("command '%.*s' '%.*s' succeeded", cmd->bstr.len, cmd->bstr.data, key->len, key->data); } else { reply->bstr = str2bstr(RSP_NOTFOUND); INCR(process_metrics, list_delete_notfound); log_verb("command '%.*s' '%.*s' completed as no-op, key not found", cmd->bstr.len, cmd->bstr.data, key->len, key->data); } }
void admin_process_request(struct response *rsp, struct request *req) { rsp->type = RSP_GENERIC; switch (req->type) { case REQ_STATS: _admin_stats(rsp, req); break; case REQ_VERSION: rsp->data = str2bstr(VERSION_PRINTED); break; default: rsp->type = RSP_INVALID; break; } }
END_TEST START_TEST(test_bulk_string) { #define BULK "foo bar\r\n" #define SERIALIZED "$9\r\n" BULK "\r\n" #define EMPTY "$0\r\n\r\n" struct element el_c, el_p; int ret; int len = sizeof(SERIALIZED) - 1; test_reset(); /* compose */ el_c.type = ELEM_BULK; el_c.bstr = str2bstr(BULK); ret = compose_element(&buf, &el_c); ck_assert_msg(ret == len, "bytes expected: %d, returned: %d", len, ret); ck_assert_int_eq(cc_bcmp(buf->rpos, SERIALIZED, ret), 0); /* parse */ ck_assert_int_eq(parse_element(&el_p, buf), PARSE_OK); ck_assert(buf->rpos == buf->wpos); ck_assert(el_p.type == ELEM_BULK); ck_assert(el_p.bstr.len == sizeof(BULK) - 1); ck_assert(el_p.bstr.data + el_p.bstr.len == buf->rpos - CRLF_LEN); ck_assert(buf->rpos == buf->wpos); /* empty string */ buf_reset(buf); len = sizeof(EMPTY) - 1; el_c.bstr = null_bstring; ret = compose_element(&buf, &el_c); ck_assert_msg(ret == len, "bytes expected: %d, returned: %d", len, ret); ck_assert_int_eq(cc_bcmp(buf->rpos, EMPTY, ret), 0); ck_assert_int_eq(parse_element(&el_p, buf), PARSE_OK); ck_assert(el_p.bstr.len == 0); #undef EMPTY #undef SERIALIZED #undef BULK }