Made the memcached_string stuff private, but still testable.
[m6w6/libmemcached] / libmemcached / fetch.c
1 #include "common.h"
2
3 char *memcached_fetch(memcached_st *ptr, char *key, size_t *key_length,
4 size_t *value_length,
5 uint32_t *flags,
6 memcached_return_t *error)
7 {
8 memcached_result_st *result_buffer= &ptr->result;
9
10 unlikely (ptr->flags.use_udp)
11 {
12 *error= MEMCACHED_NOT_SUPPORTED;
13 return NULL;
14 }
15
16 result_buffer= memcached_fetch_result(ptr, result_buffer, error);
17
18 if (result_buffer == NULL || *error != MEMCACHED_SUCCESS)
19 {
20 WATCHPOINT_ASSERT(result_buffer == NULL);
21 *value_length= 0;
22 return NULL;
23 }
24
25 *value_length= memcached_string_length(&result_buffer->value);
26
27 if (key)
28 {
29 strncpy(key, result_buffer->key, result_buffer->key_length);
30 *key_length= result_buffer->key_length;
31 }
32
33 if (result_buffer->flags)
34 *flags= result_buffer->flags;
35 else
36 *flags= 0;
37
38 return memcached_string_c_copy(&result_buffer->value);
39 }
40
41 memcached_result_st *memcached_fetch_result(memcached_st *ptr,
42 memcached_result_st *result,
43 memcached_return_t *error)
44 {
45 memcached_server_st *server;
46
47 unlikely (ptr->flags.use_udp)
48 {
49 *error= MEMCACHED_NOT_SUPPORTED;
50 return NULL;
51 }
52
53 if (result == NULL)
54 if ((result= memcached_result_create(ptr, NULL)) == NULL)
55 return NULL;
56
57 while ((server = memcached_io_get_readable_server(ptr)) != NULL)
58 {
59 char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE];
60 *error= memcached_response(server, buffer, sizeof(buffer), result);
61
62 if (*error == MEMCACHED_SUCCESS)
63 return result;
64 else if (*error == MEMCACHED_END)
65 memcached_server_response_reset(server);
66 else if (*error != MEMCACHED_NOTFOUND)
67 break;
68 }
69
70 /* We have completed reading data */
71 if (memcached_is_allocated(result))
72 {
73 memcached_result_free(result);
74 }
75 else
76 {
77 memcached_string_reset(&result->value);
78 }
79
80 return NULL;
81 }
82
83 memcached_return_t memcached_fetch_execute(memcached_st *ptr,
84 memcached_execute_fn *callback,
85 void *context,
86 uint32_t number_of_callbacks)
87 {
88 memcached_result_st *result= &ptr->result;
89 memcached_return_t rc= MEMCACHED_FAILURE;
90 unsigned int x;
91
92 while ((result= memcached_fetch_result(ptr, result, &rc)) != NULL)
93 {
94 if (rc == MEMCACHED_SUCCESS)
95 {
96 for (x= 0; x < number_of_callbacks; x++)
97 {
98 rc= (*callback[x])(ptr, result, context);
99 if (rc != MEMCACHED_SUCCESS)
100 break;
101 }
102 }
103 }
104 return rc;
105 }