7f8a6b52ba622e0f0a5e3f39edc453d7656635eb
[awesomized/libmemcached] / libmemcached / memcached_fetch.c
1 #include "common.h"
2 #include "memcached_io.h"
3
4 char *memcached_fetch(memcached_st *ptr, char *key, size_t *key_length,
5 size_t *value_length,
6 uint32_t *flags,
7 memcached_return *error)
8 {
9 memcached_result_st *result_buffer= &ptr->result;
10
11 if (ptr->flags & MEM_USE_UDP)
12 {
13 *error= MEMCACHED_NOT_SUPPORTED;
14 return NULL;
15 }
16
17 while (ptr->cursor_server < ptr->number_of_hosts)
18 {
19 char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE];
20
21 if (memcached_server_response_count(&ptr->hosts[ptr->cursor_server]) == 0)
22 {
23 ptr->cursor_server++;
24 continue;
25 }
26
27 *error= memcached_response(&ptr->hosts[ptr->cursor_server], buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, result_buffer);
28
29 if (*error == MEMCACHED_END) /* END means that we move on to the next */
30 {
31 memcached_server_response_reset(&ptr->hosts[ptr->cursor_server]);
32 ptr->cursor_server++;
33 continue;
34 }
35 else if (*error == MEMCACHED_SUCCESS)
36 {
37 *value_length= memcached_string_length(&result_buffer->value);
38
39 if (key)
40 {
41 strncpy(key, result_buffer->key, result_buffer->key_length);
42 *key_length= result_buffer->key_length;
43 }
44
45 if (result_buffer->flags)
46 *flags= result_buffer->flags;
47 else
48 *flags= 0;
49
50 return memcached_string_c_copy(&result_buffer->value);
51 }
52 else
53 {
54 *value_length= 0;
55 return NULL;
56 }
57 }
58
59 ptr->cursor_server= 0;
60 *value_length= 0;
61 return NULL;
62 }
63
64 memcached_result_st *memcached_fetch_result(memcached_st *ptr,
65 memcached_result_st *result,
66 memcached_return *error)
67 {
68 memcached_server_st *server;
69
70 if (ptr->flags & MEM_USE_UDP)
71 {
72 *error= MEMCACHED_NOT_SUPPORTED;
73 return NULL;
74 }
75
76 if (result == NULL)
77 if ((result= memcached_result_create(ptr, NULL)) == NULL)
78 return NULL;
79
80 while ((server = memcached_io_get_readable_server(ptr)) != NULL) {
81 char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE];
82 *error= memcached_response(server, buffer, sizeof(buffer), result);
83
84 if (*error == MEMCACHED_SUCCESS)
85 return result;
86 else if (*error == MEMCACHED_END)
87 memcached_server_response_reset(server);
88 else
89 break;
90 }
91
92 /* We have completed reading data */
93 if (result->is_allocated)
94 memcached_result_free(result);
95 else
96 memcached_string_reset(&result->value);
97
98 return NULL;
99 }
100