X-Git-Url: https://git.m6w6.name/?a=blobdiff_plain;f=lib%2Fmemcached_get.c;h=251cd59201c0ac67236cf289b6ee48006185af2f;hb=7add2cb42be7d072888ab916eb35cdef8a2774a1;hp=49b64f3b23d97e0cdec9e74ead4c2021462b8a11;hpb=85a66fec1e07e874e5410ce56a2976d68fbe14fd;p=m6w6%2Flibmemcached diff --git a/lib/memcached_get.c b/lib/memcached_get.c index 49b64f3b..251cd592 100644 --- a/lib/memcached_get.c +++ b/lib/memcached_get.c @@ -34,10 +34,13 @@ static char *memcached_value_fetch(memcached_st *ptr, char *key, size_t *key_len if (load_key) { memset(key, 0, MEMCACHED_MAX_KEY); + *key_length= 0; + for (; end_ptr == string_ptr || *string_ptr != ' '; string_ptr++) { *key= *string_ptr; key++; + (*key_length)++; } } else /* Skip characters */ @@ -82,17 +85,23 @@ static char *memcached_value_fetch(memcached_st *ptr, char *key, size_t *key_len /* We add two bytes so that we can walk the \r\n */ value= (char *)malloc(((*value_length) +2) * sizeof(char)); - memset(value, 0, ((*value_length) +2) * sizeof(char)); - if (!value) { *value_length= 0; *error= MEMCACHED_MEMORY_ALLOCATION_FAILURE; return NULL; } + memset(value, 0, ((*value_length) +2) * sizeof(char)); value_ptr= value; read_length= 0; + /* + We read the \r\n into the string since not doing so is more + cycles then the waster of memory to do so. + + We are null terminating through, which will most likely make + some people lazy about using the return length. + */ to_read= (*value_length) + 2; read_length= memcached_io_read(ptr, server_key, @@ -104,6 +113,9 @@ static char *memcached_value_fetch(memcached_st *ptr, char *key, size_t *key_len goto read_error; } + value[*value_length]= 0; + value[(*value_length) + 1]= 0; + return value; } } @@ -127,14 +139,14 @@ char *memcached_get(memcached_st *ptr, char *key, size_t key_length, char *value; LIBMEMCACHED_MEMCACHED_GET_START(); + server_key= memcached_generate_hash(ptr, key, key_length); + *value_length= 0; - *error= memcached_connect(ptr); + *error= memcached_connect(ptr, server_key); if (*error != MEMCACHED_SUCCESS) goto error; - server_key= memcached_generate_hash(key, key_length) % ptr->number_of_hosts; - send_length= snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, "get %.*s\r\n", (int)key_length, key); @@ -151,6 +163,8 @@ char *memcached_get(memcached_st *ptr, char *key, size_t key_length, *error= MEMCACHED_NOTFOUND; goto error; } + else if (*error == MEMCACHED_END) + assert(0); /* If this happens we have somehow messed up the fetch */ else if (*error == MEMCACHED_SUCCESS) { memcached_return rc; @@ -192,11 +206,6 @@ memcached_return memcached_mget(memcached_st *ptr, ptr->cursor_server= 0; memset(buffer, 0, HUGE_STRING_LEN); - rc= memcached_connect(ptr); - - if (rc != MEMCACHED_SUCCESS) - return rc; - cursor_key_exec= (memcached_string_st **)malloc(sizeof(memcached_string_st *) * ptr->number_of_hosts); memset(cursor_key_exec, 0, sizeof(memcached_string_st *) * ptr->number_of_hosts); @@ -205,7 +214,7 @@ memcached_return memcached_mget(memcached_st *ptr, { unsigned int server_key; - server_key= memcached_generate_hash(keys[x], key_length[x]) % ptr->number_of_hosts; + server_key= memcached_generate_hash(ptr, keys[x], key_length[x]); if (cursor_key_exec[server_key]) { @@ -237,6 +246,9 @@ memcached_return memcached_mget(memcached_st *ptr, { if (cursor_key_exec[x]) { + /* We need to doo something about non-connnected hosts in the future */ + rc= memcached_connect(ptr, x); + memcached_string_st *string= cursor_key_exec[x]; memcached_string_append(ptr, string, "\r\n", 2); @@ -248,7 +260,10 @@ memcached_return memcached_mget(memcached_st *ptr, } memcached_string_free(ptr, string); cursor_key_exec[x]= NULL; /* Remove warning */ + ptr->hosts[x].cursor_active= 1; } + else + ptr->hosts[x].cursor_active= 0; } free(cursor_key_exec); @@ -266,16 +281,28 @@ char *memcached_fetch(memcached_st *ptr, char *key, size_t *key_length, while (ptr->cursor_server < ptr->number_of_hosts) { + if (!ptr->hosts[ptr->cursor_server].cursor_active) + { + ptr->cursor_server++; + continue; + } + value_check= memcached_value_fetch(ptr, key, key_length, value_length, flags, error, 1, ptr->cursor_server); if (*error == MEMCACHED_NOTFOUND) ptr->cursor_server++; + else if (*error == MEMCACHED_END && *value_length == 0) + return NULL; + else if (*error == MEMCACHED_END) + assert(0); /* If this happens we have somehow messed up the fetch */ else if (*error != MEMCACHED_SUCCESS) return NULL; else return value_check; + } + *value_length= 0; return NULL; }