memcached_return memcached_verbosity(memcached_st *ptr, unsigned int verbosity) { unsigned int x; size_t send_length; memcached_return rc; char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE]; send_length= snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, "verbosity %u\r\n", verbosity); unlikely (send_length >= MEMCACHED_DEFAULT_COMMAND_SIZE) return MEMCACHED_WRITE_FAILURE; rc= MEMCACHED_SUCCESS; for (x= 0; x < ptr->number_of_hosts; x++) { memcached_return rrc; rrc= memcached_do(&ptr->hosts[x], buffer, send_length, 1); if (rrc != MEMCACHED_SUCCESS) { rc= MEMCACHED_SOME_ERRORS; continue; } unlikely (ptr->flags & MEM_USE_UDP) continue; rrc= memcached_response(&ptr->hosts[x], buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, NULL); if (rrc != MEMCACHED_SUCCESS) rc= MEMCACHED_SOME_ERRORS; } return rc; }
static memcached_return binary_incr_decr(memcached_st *ptr, uint8_t cmd, const char *key, size_t key_length, uint32_t offset, uint64_t *value) { unsigned int server_key; #ifndef _MSC_VER protocol_binary_request_incr request= {.bytes= {0}}; #else protocol_binary_request_incr request= {0}; #endif unlikely (ptr->hosts == NULL || ptr->number_of_hosts == 0) return MEMCACHED_NO_SERVERS; server_key= memcached_generate_hash(ptr, key, key_length); request.message.header.request.magic= PROTOCOL_BINARY_REQ; request.message.header.request.opcode= cmd; request.message.header.request.keylen= htons((uint16_t)key_length); request.message.header.request.extlen= 20; request.message.header.request.datatype= PROTOCOL_BINARY_RAW_BYTES; request.message.header.request.bodylen= htonl(key_length + request.message.header.request.extlen); request.message.body.delta= htonll(offset); /* TODO: The binary protocol allows you to specify initial and expiry time */ if ((memcached_do(&ptr->hosts[server_key], request.bytes, sizeof(request.bytes), 0)!=MEMCACHED_SUCCESS) || (memcached_io_write(&ptr->hosts[server_key], key, key_length, 1) == -1)) { memcached_io_reset(&ptr->hosts[server_key]); return MEMCACHED_WRITE_FAILURE; } return memcached_response(&ptr->hosts[server_key], (char*)value, sizeof(*value), NULL); }
static memcached_return_t memcached_flush_textual(memcached_st *ptr, time_t expiration) { unsigned int x; size_t send_length; memcached_return_t rc; char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE]; unlikely (memcached_server_count(ptr) == 0) return MEMCACHED_NO_SERVERS; for (x= 0; x < memcached_server_count(ptr); x++) { bool no_reply= ptr->flags.no_reply; memcached_server_write_instance_st instance= memcached_server_instance_fetch(ptr, x); if (expiration) send_length= (size_t) snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, "flush_all %llu%s\r\n", (unsigned long long)expiration, no_reply ? " noreply" : ""); else send_length= (size_t) snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, "flush_all%s\r\n", no_reply ? " noreply" : ""); rc= memcached_do(instance, buffer, send_length, true); if (rc == MEMCACHED_SUCCESS && !no_reply) (void)memcached_response(instance, buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, NULL); } return MEMCACHED_SUCCESS; }
static memcached_return memcached_auto(memcached_st *ptr, const char *verb, const char *key, size_t key_length, unsigned int offset, uint64_t *value) { size_t send_length; memcached_return rc; char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE]; unsigned int server_key; unlikely (ptr->hosts == NULL || ptr->number_of_hosts == 0) return MEMCACHED_NO_SERVERS; if ((ptr->flags & MEM_VERIFY_KEY) && (memcachd_key_test((char **)&key, &key_length, 1) == MEMCACHED_BAD_KEY_PROVIDED)) return MEMCACHED_BAD_KEY_PROVIDED; server_key= memcached_generate_hash(ptr, key, key_length); send_length= snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, "%s %s%.*s %u\r\n", verb, ptr->prefix_key, (int)key_length, key, offset); unlikely (send_length >= MEMCACHED_DEFAULT_COMMAND_SIZE) return MEMCACHED_WRITE_FAILURE; rc= memcached_do(&ptr->hosts[server_key], buffer, send_length, 1); if (rc != MEMCACHED_SUCCESS) return rc; rc= memcached_response(&ptr->hosts[server_key], buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, NULL); /* So why recheck responce? Because the protocol is brain dead :) The number returned might end up equaling one of the string values. Less chance of a mistake with strncmp() so we will use it. We still called memcached_response() though since it worked its magic for non-blocking IO. */ if (!strncmp(buffer, "ERROR\r\n", 7)) { *value= 0; rc= MEMCACHED_PROTOCOL_ERROR; } else if (!strncmp(buffer, "NOT_FOUND\r\n", 11)) { *value= 0; rc= MEMCACHED_NOTFOUND; } else { *value= strtoull(buffer, (char **)NULL, 10); rc= MEMCACHED_SUCCESS; } return rc; }
char *memcached_fetch(memcached_st *ptr, char *key, size_t *key_length, size_t *value_length, uint32_t *flags, memcached_return *error) { memcached_result_st *result_buffer= &ptr->result; while (ptr->cursor_server < ptr->number_of_hosts) { char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE]; if (memcached_server_response_count(&ptr->hosts[ptr->cursor_server]) == 0) { ptr->cursor_server++; continue; } *error= memcached_response(&ptr->hosts[ptr->cursor_server], buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, result_buffer); if (*error == MEMCACHED_END) /* END means that we move on to the next */ { memcached_server_response_reset(&ptr->hosts[ptr->cursor_server]); ptr->cursor_server++; continue; } else if (*error == MEMCACHED_SUCCESS) { *value_length= memcached_string_length(&result_buffer->value); if (key) { strncpy(key, result_buffer->key, result_buffer->key_length); *key_length= result_buffer->key_length; } if (result_buffer->flags) *flags= result_buffer->flags; else *flags= 0; return memcached_string_c_copy(&result_buffer->value); } else { *value_length= 0; return NULL; } } ptr->cursor_server= 0; *value_length= 0; return NULL; }
memcached_result_st *memcached_fetch_result(memcached_st *ptr, memcached_result_st *result, memcached_return *error) { if (result == NULL) result= memcached_result_create(ptr, NULL); #ifdef UNUSED if (ptr->flags & MEM_NO_BLOCK) memcached_io_preread(ptr); #endif while (ptr->cursor_server < ptr->number_of_hosts) { char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE]; if (memcached_server_response_count(&ptr->hosts[ptr->cursor_server]) == 0) { ptr->cursor_server++; continue; } *error= memcached_response(&ptr->hosts[ptr->cursor_server], buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, result); if (*error == MEMCACHED_END) /* END means that we move on to the next */ { memcached_server_response_reset(&ptr->hosts[ptr->cursor_server]); ptr->cursor_server++; continue; } else if (*error == MEMCACHED_SUCCESS) return result; else return NULL; } /* We have completed reading data */ if (result->is_allocated) memcached_result_free(result); else memcached_string_reset(&result->value); ptr->cursor_server= 0; return NULL; }
static memcached_return_t ascii_dump(memcached_st *ptr, memcached_dump_fn *callback, void *context, uint32_t number_of_callbacks) { memcached_return_t rc= MEMCACHED_SUCCESS; char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE]; size_t send_length; uint32_t server_key; uint32_t x; unlikely (memcached_server_count(ptr) == 0) return MEMCACHED_NO_SERVERS; for (server_key= 0; server_key < memcached_server_count(ptr); server_key++) { memcached_server_instance_st *instance; instance= memcached_server_instance_fetch(ptr, server_key); /* 256 I BELIEVE is the upper limit of slabs */ for (x= 0; x < 256; x++) { send_length= (size_t) snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, "stats cachedump %u 0 0\r\n", x); rc= memcached_do(instance, buffer, send_length, true); unlikely (rc != MEMCACHED_SUCCESS) goto error; while (1) { uint32_t callback_counter; rc= memcached_response(instance, buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, NULL); if (rc == MEMCACHED_ITEM) { char *string_ptr, *end_ptr; char *key; string_ptr= buffer; string_ptr+= 5; /* Move past ITEM */ for (end_ptr= string_ptr; isgraph(*end_ptr); end_ptr++); key= string_ptr; key[(size_t)(end_ptr-string_ptr)]= 0; for (callback_counter= 0; callback_counter < number_of_callbacks; callback_counter++) { rc= (*callback[callback_counter])(ptr, key, (size_t)(end_ptr-string_ptr), context); if (rc != MEMCACHED_SUCCESS) break; } } else if (rc == MEMCACHED_END) break; else if (rc == MEMCACHED_SERVER_ERROR || rc == MEMCACHED_CLIENT_ERROR) { /* If we try to request stats cachedump for a slab class that is too big * the server will return an incorrect error message: * "MEMCACHED_SERVER_ERROR failed to allocate memory" * This isn't really a fatal error, so let's just skip it. I want to * fix the return value from the memcached server to a CLIENT_ERROR, * so let's add support for that as well right now. */ rc= MEMCACHED_END; break; } else goto error; } } } error: if (rc == MEMCACHED_END) return MEMCACHED_SUCCESS; else return rc; }
static inline memcached_return_t memcached_send(memcached_st *ptr, const char *master_key, size_t master_key_length, const char *key, size_t key_length, const char *value, size_t value_length, time_t expiration, uint32_t flags, uint64_t cas, memcached_storage_action_t verb) { bool to_write; size_t write_length; memcached_return_t rc; char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE]; uint32_t server_key; memcached_server_instance_st *instance; WATCHPOINT_ASSERT(!(value == NULL && value_length > 0)); rc= memcached_validate_key_length(key_length, ptr->flags.binary_protocol); unlikely (rc != MEMCACHED_SUCCESS) return rc; unlikely (memcached_server_count(ptr) == 0) return MEMCACHED_NO_SERVERS; if (ptr->flags.verify_key && (memcached_key_test((const char **)&key, &key_length, 1) == MEMCACHED_BAD_KEY_PROVIDED)) return MEMCACHED_BAD_KEY_PROVIDED; if (ptr->flags.binary_protocol) { return memcached_send_binary(ptr, master_key, master_key_length, key, key_length, value, value_length, expiration, flags, cas, verb); } server_key= memcached_generate_hash(ptr, master_key, master_key_length); instance= memcached_server_instance_fetch(ptr, server_key); if (cas) { write_length= (size_t) snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, "%s %.*s%.*s %u %llu %zu %llu%s\r\n", storage_op_string(verb), (int)ptr->prefix_key_length, ptr->prefix_key, (int)key_length, key, flags, (unsigned long long)expiration, value_length, (unsigned long long)cas, (ptr->flags.no_reply) ? " noreply" : ""); } else { char *buffer_ptr= buffer; const char *command= storage_op_string(verb); /* Copy in the command, no space needed, we handle that in the command function*/ memcpy(buffer_ptr, command, strlen(command)); /* Copy in the key prefix, switch to the buffer_ptr */ buffer_ptr= memcpy((buffer_ptr + strlen(command)), ptr->prefix_key, ptr->prefix_key_length); /* Copy in the key, adjust point if a key prefix was used. */ buffer_ptr= memcpy(buffer_ptr + (ptr->prefix_key_length ? ptr->prefix_key_length : 0), key, key_length); buffer_ptr+= key_length; buffer_ptr[0]= ' '; buffer_ptr++; write_length= (size_t)(buffer_ptr - buffer); write_length+= (size_t) snprintf(buffer_ptr, MEMCACHED_DEFAULT_COMMAND_SIZE, "%u %llu %zu%s\r\n", flags, (unsigned long long)expiration, value_length, ptr->flags.no_reply ? " noreply" : ""); } if (ptr->flags.use_udp && ptr->flags.buffer_requests) { size_t cmd_size= write_length + value_length + 2; if (cmd_size > MAX_UDP_DATAGRAM_LENGTH - UDP_DATAGRAM_HEADER_LENGTH) return MEMCACHED_WRITE_FAILURE; if (cmd_size + instance->write_buffer_offset > MAX_UDP_DATAGRAM_LENGTH) memcached_io_write(instance, NULL, 0, true); } if (write_length >= MEMCACHED_DEFAULT_COMMAND_SIZE) { rc= MEMCACHED_WRITE_FAILURE; goto error; } /* Send command header */ rc= memcached_do(instance, buffer, write_length, false); if (rc != MEMCACHED_SUCCESS) goto error; /* Send command body */ if (memcached_io_write(instance, value, value_length, false) == -1) { rc= MEMCACHED_WRITE_FAILURE; goto error; } if (ptr->flags.buffer_requests && verb == SET_OP) { to_write= false; } else { to_write= true; } if (memcached_io_write(instance, "\r\n", 2, to_write) == -1) { rc= MEMCACHED_WRITE_FAILURE; goto error; } if (ptr->flags.no_reply) return (to_write == false) ? MEMCACHED_BUFFERED : MEMCACHED_SUCCESS; if (to_write == false) return MEMCACHED_BUFFERED; rc= memcached_response(instance, buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, NULL); if (rc == MEMCACHED_STORED) return MEMCACHED_SUCCESS; else return rc; error: memcached_io_reset(instance); return rc; }
static inline memcached_return memcached_send(memcached_st *ptr, const char *master_key, size_t master_key_length, const char *key, size_t key_length, const char *value, size_t value_length, time_t expiration, uint32_t flags, uint64_t cas, memcached_storage_action verb) { char to_write; size_t write_length; ssize_t sent_length; memcached_return rc; char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE]; unsigned int server_key; WATCHPOINT_ASSERT(!(value == NULL && value_length > 0)); rc= memcached_validate_key_length(key_length, ptr->flags & MEM_BINARY_PROTOCOL); unlikely (rc != MEMCACHED_SUCCESS) return rc; unlikely (ptr->number_of_hosts == 0) return MEMCACHED_NO_SERVERS; if ((ptr->flags & MEM_VERIFY_KEY) && (memcached_key_test((const char **)&key, &key_length, 1) == MEMCACHED_BAD_KEY_PROVIDED)) return MEMCACHED_BAD_KEY_PROVIDED; if (ptr->flags & MEM_BINARY_PROTOCOL) return memcached_send_binary(ptr, master_key, master_key_length, key, key_length, value, value_length, expiration, flags, cas, verb); server_key= memcached_generate_hash(ptr, master_key, master_key_length); if (cas) write_length= (size_t) snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, "%s%s%.*s %u %llu %zu %llu%s\r\n", storage_op_string(verb), ptr->prefix_key, (int)key_length, key, flags, (unsigned long long)expiration, value_length, (unsigned long long)cas, (ptr->flags & MEM_NOREPLY) ? " noreply" : ""); else { char *buffer_ptr= buffer; const char *command= storage_op_string(verb); /* Copy in the command, no space needed, we handle that in the command function*/ memcpy(buffer_ptr, command, strlen(command)); /* Copy in the key prefix, switch to the buffer_ptr */ buffer_ptr= memcpy(buffer_ptr + strlen(command) , ptr->prefix_key, strlen(ptr->prefix_key)); /* Copy in the key, adjust point if a key prefix was used. */ buffer_ptr= memcpy(buffer_ptr + (ptr->prefix_key ? strlen(ptr->prefix_key) : 0), key, key_length); buffer_ptr+= key_length; buffer_ptr[0]= ' '; buffer_ptr++; write_length= (size_t)(buffer_ptr - buffer); if (verb == DELETE_OP) { if (ptr->flags & MEM_NOREPLY) write_length+= (size_t) snprintf(buffer_ptr, MEMCACHED_DEFAULT_COMMAND_SIZE, "noreply"); } else { write_length+= (size_t) snprintf(buffer_ptr, MEMCACHED_DEFAULT_COMMAND_SIZE, "%u %llu %zu%s\r\n", flags, (unsigned long long)expiration, value_length, (ptr->flags & MEM_NOREPLY) ? " noreply" : ""); } } if (ptr->flags & MEM_USE_UDP && ptr->flags & MEM_BUFFER_REQUESTS) { size_t cmd_size= write_length + value_length + 2; if (cmd_size > MAX_UDP_DATAGRAM_LENGTH - UDP_DATAGRAM_HEADER_LENGTH) return MEMCACHED_WRITE_FAILURE; if (cmd_size + ptr->hosts[server_key].write_buffer_offset > MAX_UDP_DATAGRAM_LENGTH) memcached_io_write(&ptr->hosts[server_key], NULL, 0, 1); } if (write_length >= MEMCACHED_DEFAULT_COMMAND_SIZE) return MEMCACHED_WRITE_FAILURE; /* Send command header */ rc= memcached_do(&ptr->hosts[server_key], buffer, write_length, 0); if (rc != MEMCACHED_SUCCESS) return rc; /* Send command body */ if ((sent_length= memcached_io_write(&ptr->hosts[server_key], value, value_length, 0)) == -1) return MEMCACHED_WRITE_FAILURE; if ((ptr->flags & MEM_BUFFER_REQUESTS) && (verb == SET_OP || verb == PREPEND_OP || verb == APPEND_OP || verb == DELETE_OP)) to_write= 0; else to_write= 1; if ((sent_length= memcached_io_write(&ptr->hosts[server_key], "\r\n", 2, to_write)) == -1) return MEMCACHED_WRITE_FAILURE; if (ptr->flags & MEM_NOREPLY) return (to_write == 0) ? MEMCACHED_BUFFERED : MEMCACHED_SUCCESS; if (to_write == 0) return MEMCACHED_BUFFERED; rc= memcached_response(&ptr->hosts[server_key], buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, NULL); if (rc == MEMCACHED_STORED || rc == MEMCACHED_DELETED) return MEMCACHED_SUCCESS; return rc; }
static inline memcached_return memcached_send(memcached_st *ptr, const char *master_key, size_t master_key_length, const char *key, size_t key_length, const char *value, size_t value_length, time_t expiration, uint32_t flags, uint64_t cas, memcached_storage_action verb) { char to_write; size_t write_length; ssize_t sent_length; memcached_return rc; char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE]; unsigned int server_key; WATCHPOINT_ASSERT(!(value == NULL && value_length > 0)); rc= memcached_validate_key_length(key_length, ptr->flags & MEM_BINARY_PROTOCOL); unlikely (rc != MEMCACHED_SUCCESS) return rc; unlikely (ptr->number_of_hosts == 0) return MEMCACHED_NO_SERVERS; if ((ptr->flags & MEM_VERIFY_KEY) && (memcachd_key_test((char **)&key, &key_length, 1) == MEMCACHED_BAD_KEY_PROVIDED)) return MEMCACHED_BAD_KEY_PROVIDED; server_key= memcached_generate_hash(ptr, master_key, master_key_length); if (ptr->flags & MEM_BINARY_PROTOCOL) return memcached_send_binary(&ptr->hosts[server_key], key, key_length, value, value_length, expiration, flags, cas, verb); #ifndef _WIN32 if (cas) write_length= snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, "%s %s%.*s %u %llu %zu %llu%s\r\n", storage_op_string(verb), ptr->prefix_key, (int)key_length, key, flags, (unsigned long long)expiration, value_length, (unsigned long long)cas, (ptr->flags & MEM_NOREPLY) ? " noreply" : ""); else write_length= snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, "%s %s%.*s %u %llu %zu%s\r\n", storage_op_string(verb), ptr->prefix_key, (int)key_length, key, flags, (unsigned long long)expiration, value_length, (ptr->flags & MEM_NOREPLY) ? " noreply" : ""); #else if (cas) write_length= snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, "%s %s%.*s %u %llu %u %llu%s\r\n", storage_op_string(verb), ptr->prefix_key, (int)key_length, key, flags, (unsigned long long)expiration, value_length, (unsigned long long)cas, (ptr->flags & MEM_NOREPLY) ? " noreply" : ""); else write_length= snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, "%s %s%.*s %u %llu %u%s\r\n", storage_op_string(verb), ptr->prefix_key, (int)key_length, key, flags, (unsigned long long)expiration, value_length, (ptr->flags & MEM_NOREPLY) ? " noreply" : ""); #endif if (write_length >= MEMCACHED_DEFAULT_COMMAND_SIZE) { rc= MEMCACHED_WRITE_FAILURE; goto error; } /* Send command header */ rc= memcached_do(&ptr->hosts[server_key], buffer, write_length, 0); if (rc != MEMCACHED_SUCCESS) goto error; /* Send command body */ if ((sent_length= memcached_io_write(&ptr->hosts[server_key], value, value_length, 0)) == -1) { rc= MEMCACHED_WRITE_FAILURE; goto error; } if ((ptr->flags & MEM_BUFFER_REQUESTS) && verb == SET_OP) to_write= 0; else to_write= 1; if ((sent_length= memcached_io_write(&ptr->hosts[server_key], "\r\n", 2, to_write)) == -1) { rc= MEMCACHED_WRITE_FAILURE; goto error; } if (ptr->flags & MEM_NOREPLY) { memcached_server_response_decrement(&ptr->hosts[server_key]); return (to_write == 0) ? MEMCACHED_BUFFERED : MEMCACHED_SUCCESS; } if (to_write == 0) return MEMCACHED_BUFFERED; rc= memcached_response(&ptr->hosts[server_key], buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, NULL); if (rc == MEMCACHED_STORED) return MEMCACHED_SUCCESS; else return rc; error: memcached_io_reset(&ptr->hosts[server_key]); return rc; }