X-Git-Url: https://git.m6w6.name/?a=blobdiff_plain;f=libmemcached%2Fflush.cc;h=45cb458648de5233081745a1cfa0c16c90c825d9;hb=444fb45cfefbc668e1ece02c24ab6e7bc3be0301;hp=5895f030fa5d7e4133c7bc2f33bee49e4426d28b;hpb=32767cce940f7bcde4633cc3cd23efc28ad954bd;p=m6w6%2Flibmemcached diff --git a/libmemcached/flush.cc b/libmemcached/flush.cc index 5895f030..45cb4586 100644 --- a/libmemcached/flush.cc +++ b/libmemcached/flush.cc @@ -51,54 +51,69 @@ memcached_return_t memcached_flush(memcached_st *ptr, time_t expiration) LIBMEMCACHED_MEMCACHED_FLUSH_START(); if (ptr->flags.binary_protocol) + { rc= memcached_flush_binary(ptr, expiration); + } else + { rc= memcached_flush_textual(ptr, expiration); + } LIBMEMCACHED_MEMCACHED_FLUSH_END(); + return rc; } static memcached_return_t memcached_flush_textual(memcached_st *ptr, time_t expiration) { - unlikely (memcached_server_count(ptr) == 0) - return MEMCACHED_NO_SERVERS; + // Invert the logic to make it simpler to read the code + bool reply= (ptr->flags.no_reply) ? false : true; - for (unsigned int x= 0; x < memcached_server_count(ptr); x++) + char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE]; + int send_length= 0; + if (expiration) { - memcached_return_t rc; - char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE]; + send_length= snprintf(buffer, sizeof(buffer), "%llu", (unsigned long long)expiration); + } - bool no_reply= ptr->flags.no_reply; - memcached_server_write_instance_st instance= - memcached_server_instance_fetch(ptr, x); + if (send_length >= MEMCACHED_DEFAULT_COMMAND_SIZE or send_length < 0) + { + return memcached_set_error(*ptr, MEMCACHED_MEMORY_ALLOCATION_FAILURE, MEMCACHED_AT, + memcached_literal_param("snprintf(MEMCACHED_DEFAULT_COMMAND_SIZE)")); + } - int send_length; - if (expiration) - { - send_length= snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, - "flush_all %llu%s\r\n", - (unsigned long long)expiration, no_reply ? " noreply" : ""); - } - else + struct libmemcached_io_vector_st vector[]= + { + { memcached_literal_param("flush_all ") }, + { buffer, send_length }, + { " noreply", reply ? 0 : memcached_literal_param_size(" noreply") }, + { memcached_literal_param("\r\n") } + }; + + memcached_return_t rc= MEMCACHED_SUCCESS; + for (uint32_t x= 0; x < memcached_server_count(ptr); x++) + { + memcached_server_write_instance_st instance= memcached_server_instance_fetch(ptr, x); + + memcached_return_t rrc= memcached_vdo(instance, vector, 4, true); + if (rrc == MEMCACHED_SUCCESS and reply == true) { - send_length= snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, - "flush_all%s\r\n", no_reply ? " noreply" : ""); + char response_buffer[MEMCACHED_DEFAULT_COMMAND_SIZE]; + rrc= memcached_response(instance, response_buffer, sizeof(response_buffer), NULL); } - if (send_length >= MEMCACHED_DEFAULT_COMMAND_SIZE || send_length < 0) + if (memcached_failed(rrc)) { - return memcached_set_error(*instance, MEMCACHED_MEMORY_ALLOCATION_FAILURE, MEMCACHED_AT, - memcached_literal_param("snprintf(MEMCACHED_DEFAULT_COMMAND_SIZE)")); + // If an error has already been reported, then don't add to it + if (instance->error_messages == NULL) + { + memcached_set_error(*instance, rrc, MEMCACHED_AT); + } + rc= MEMCACHED_SOME_ERRORS; } - - rc= memcached_do(instance, buffer, (size_t)send_length, true); - - if (rc == MEMCACHED_SUCCESS && !no_reply) - (void)memcached_response(instance, buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, NULL); } - return MEMCACHED_SUCCESS; + return rc; } static memcached_return_t memcached_flush_binary(memcached_st *ptr, @@ -106,9 +121,6 @@ static memcached_return_t memcached_flush_binary(memcached_st *ptr, { protocol_binary_request_flush request= {}; - unlikely (memcached_server_count(ptr) == 0) - return MEMCACHED_NO_SERVERS; - request.message.header.request.magic= (uint8_t)PROTOCOL_BINARY_REQ; request.message.header.request.opcode= PROTOCOL_BINARY_CMD_FLUSH; request.message.header.request.extlen= 4; @@ -116,10 +128,11 @@ static memcached_return_t memcached_flush_binary(memcached_st *ptr, request.message.header.request.bodylen= htonl(request.message.header.request.extlen); request.message.body.expiration= htonl((uint32_t) expiration); + memcached_return_t rc= MEMCACHED_SUCCESS; + for (uint32_t x= 0; x < memcached_server_count(ptr); x++) { - memcached_server_write_instance_st instance= - memcached_server_instance_fetch(ptr, x); + memcached_server_write_instance_st instance= memcached_server_instance_fetch(ptr, x); if (ptr->flags.no_reply) { @@ -130,21 +143,29 @@ static memcached_return_t memcached_flush_binary(memcached_st *ptr, request.message.header.request.opcode= PROTOCOL_BINARY_CMD_FLUSH; } - if (memcached_do(instance, request.bytes, sizeof(request.bytes), true) != MEMCACHED_SUCCESS) + struct libmemcached_io_vector_st vector[]= + { + { request.bytes, sizeof(request.bytes) } + }; + + memcached_return_t rrc; + if ((rrc= memcached_vdo(instance, vector, 1, true))) { + memcached_set_error(*instance, rrc, MEMCACHED_AT); memcached_io_reset(instance); - return MEMCACHED_WRITE_FAILURE; + rc= MEMCACHED_SOME_ERRORS; } } for (uint32_t x= 0; x < memcached_server_count(ptr); x++) { - memcached_server_write_instance_st instance= - memcached_server_instance_fetch(ptr, x); + memcached_server_write_instance_st instance= memcached_server_instance_fetch(ptr, x); if (memcached_server_response_count(instance) > 0) + { (void)memcached_response(instance, NULL, 0, NULL); + } } - return MEMCACHED_SUCCESS; + return rc; }