X-Git-Url: https://git.m6w6.name/?a=blobdiff_plain;f=libmemcached%2Fexist.cc;h=56c1efcec6194c601aa3bae608f19f9d5aa2f673;hb=af788b4b866ef0220dcdc4c357efbcbdb942cd27;hp=6903f83bcd612e1adb87335377e69008d197130b;hpb=3c74b93b3db239f4240c907a91678ed401fa41ad;p=awesomized%2Flibmemcached diff --git a/libmemcached/exist.cc b/libmemcached/exist.cc index 6903f83b..56c1efce 100644 --- a/libmemcached/exist.cc +++ b/libmemcached/exist.cc @@ -36,10 +36,11 @@ #include -static memcached_return_t ascii_exist(memcached_st *memc, memcached_server_write_instance_st instance, const char *key, size_t key_length) +static memcached_return_t ascii_exist(memcached_st *memc, org::libmemcached::Instance* instance, const char *key, size_t key_length) { libmemcached_io_vector_st vector[]= { + { NULL, 0 }, { memcached_literal_param("add ") }, { memcached_array_string(memc->_namespace), memcached_array_size(memc->_namespace) }, { key, key_length }, @@ -51,37 +52,35 @@ static memcached_return_t ascii_exist(memcached_st *memc, memcached_server_write }; /* Send command header */ - memcached_return_t rc= memcached_vdo(instance, vector, 8, true); - if (rc == MEMCACHED_SUCCESS) + memcached_return_t rc; + if (memcached_fatal(rc= memcached_vdo(instance, vector, 9, true))) { - char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE]; - rc= memcached_response(instance, buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, NULL); - - if (rc == MEMCACHED_NOTSTORED) - { - rc= MEMCACHED_SUCCESS; - } - - if (rc == MEMCACHED_STORED) - { - rc= MEMCACHED_NOTFOUND; - } + return rc; } - if (rc == MEMCACHED_WRITE_FAILURE) + char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE]; + rc= memcached_response(instance, buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, NULL); + + if (rc == MEMCACHED_NOTSTORED) { - memcached_io_reset(instance); + rc= MEMCACHED_SUCCESS; + } + + if (rc == MEMCACHED_STORED) + { + rc= MEMCACHED_NOTFOUND; } return rc; } -static memcached_return_t binary_exist(memcached_st *memc, memcached_server_write_instance_st instance, const char *key, size_t key_length) +static memcached_return_t binary_exist(memcached_st *memc, org::libmemcached::Instance* instance, const char *key, size_t key_length) { protocol_binary_request_set request= {}; size_t send_length= sizeof(request.bytes); - request.message.header.request.magic= PROTOCOL_BINARY_REQ; + initialize_binary_request(instance, request.message.header); + request.message.header.request.opcode= PROTOCOL_BINARY_CMD_ADD; request.message.header.request.keylen= htons((uint16_t)(key_length + memcached_array_size(memc->_namespace))); request.message.header.request.datatype= PROTOCOL_BINARY_RAW_BYTES; @@ -95,6 +94,7 @@ static memcached_return_t binary_exist(memcached_st *memc, memcached_server_writ libmemcached_io_vector_st vector[]= { + { NULL, 0 }, { request.bytes, send_length }, { memcached_array_string(memc->_namespace), memcached_array_size(memc->_namespace) }, { key, key_length } @@ -102,19 +102,22 @@ static memcached_return_t binary_exist(memcached_st *memc, memcached_server_writ /* write the header */ memcached_return_t rc; - if ((rc= memcached_vdo(instance, vector, 3, true)) != MEMCACHED_SUCCESS) + if (memcached_fatal(rc= memcached_vdo(instance, vector, 4, true))) { - memcached_io_reset(instance); - return (rc == MEMCACHED_SUCCESS) ? MEMCACHED_WRITE_FAILURE : rc; + return rc; } rc= memcached_response(instance, NULL, 0, NULL); if (rc == MEMCACHED_SUCCESS) + { rc= MEMCACHED_NOTFOUND; + } if (rc == MEMCACHED_DATA_EXISTS) + { rc= MEMCACHED_SUCCESS; + } return rc; } @@ -136,19 +139,25 @@ memcached_return_t memcached_exist_by_key(memcached_st *memc, if (memcached_is_udp(memc)) { - return MEMCACHED_NOT_SUPPORTED; + return memcached_set_error(*memc, MEMCACHED_NOT_SUPPORTED, MEMCACHED_AT); } uint32_t server_key= memcached_generate_hash_with_redistribution(memc, group_key, group_key_length); - memcached_server_write_instance_st instance; - instance= memcached_server_instance_fetch(memc, server_key); + org::libmemcached::Instance* instance= memcached_instance_fetch(memc, server_key); - if (memc->flags.binary_protocol) + if (memcached_is_binary(memc)) { - return binary_exist(memc, instance, key, key_length); + rc= binary_exist(memc, instance, key, key_length); } else { - return ascii_exist(memc, instance, key, key_length); + rc= ascii_exist(memc, instance, key, key_length); } + + if (memcached_fatal(rc)) + { + memcached_io_reset(instance); + } + + return rc; }