2 #include "memcached_io.h"
4 memcached_return
value_fetch(memcached_st
*ptr
,
6 memcached_result_st
*result
,
7 unsigned int server_key
)
9 memcached_return rc
= MEMCACHED_SUCCESS
;
15 end_ptr
= buffer
+ MEMCACHED_DEFAULT_COMMAND_SIZE
;
17 result
->key_length
= 0;
19 memcached_string_reset(&result
->value
);
22 string_ptr
+= 6; /* "VALUE " */
30 result
->key_length
= 0;
32 for (; isgraph(*string_ptr
); string_ptr
++)
38 result
->key
[result
->key_length
]= 0;
41 if (end_ptr
== string_ptr
)
44 /* Flags fetch move past space */
46 if (end_ptr
== string_ptr
)
48 for (next_ptr
= string_ptr
; isdigit(*string_ptr
); string_ptr
++);
49 result
->flags
= (uint32_t)strtol(next_ptr
, &string_ptr
, 10);
51 if (end_ptr
== string_ptr
)
54 /* Length fetch move past space*/
56 if (end_ptr
== string_ptr
)
59 for (next_ptr
= string_ptr
; isdigit(*string_ptr
); string_ptr
++);
60 value_length
= (size_t)strtoll(next_ptr
, &string_ptr
, 10);
62 if (end_ptr
== string_ptr
)
66 if (*string_ptr
== '\r')
68 /* Skip past the \r\n */
75 for (next_ptr
= string_ptr
; isdigit(*string_ptr
); string_ptr
++);
76 result
->cas
= (size_t)strtoll(next_ptr
, &string_ptr
, 10);
79 if (end_ptr
< string_ptr
)
88 /* We add two bytes so that we can walk the \r\n */
89 rc
= memcached_string_check(&result
->value
, value_length
+2);
90 if (rc
!= MEMCACHED_SUCCESS
)
93 return MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
96 value_ptr
= memcached_string_value(&result
->value
);
99 We read the \r\n into the string since not doing so is more
100 cycles then the waster of memory to do so.
102 We are null terminating through, which will most likely make
103 some people lazy about using the return length.
105 to_read
= (value_length
) + 2;
107 read_length
= memcached_io_read(ptr
, server_key
,
110 if (read_length
!= (size_t)(value_length
+ 2))
115 /* This next bit blows the API, but this is internal....*/
118 char_ptr
= memcached_string_value(&result
->value
);;
119 char_ptr
[value_length
]= 0;
120 char_ptr
[value_length
+ 1]= 0;
121 memcached_string_set_length(&result
->value
, value_length
);
124 return MEMCACHED_SUCCESS
;
130 return MEMCACHED_PARTIAL_READ
;
133 char *memcached_fetch(memcached_st
*ptr
, char *key
, size_t *key_length
,
134 size_t *value_length
,
136 memcached_return
*error
)
138 memcached_result_st
*result_buffer
= &ptr
->result
;
140 while (ptr
->cursor_server
< ptr
->number_of_hosts
)
142 char buffer
[MEMCACHED_DEFAULT_COMMAND_SIZE
];
144 if (memcached_server_response_count(ptr
, ptr
->cursor_server
) == 0)
146 ptr
->cursor_server
++;
150 *error
= memcached_response(ptr
, buffer
, MEMCACHED_DEFAULT_COMMAND_SIZE
, result_buffer
, ptr
->cursor_server
);
152 if (*error
== MEMCACHED_END
) /* END means that we move on to the next */
154 memcached_server_response_reset(ptr
, ptr
->cursor_server
);
155 ptr
->cursor_server
++;
158 else if (*error
== MEMCACHED_SUCCESS
)
160 *value_length
= memcached_string_length(&result_buffer
->value
);
164 strncpy(key
, result_buffer
->key
, result_buffer
->key_length
);
165 *key_length
= result_buffer
->key_length
;
167 *flags
= result_buffer
->flags
;
169 return memcached_string_c_copy(&result_buffer
->value
);
178 ptr
->cursor_server
= 0;
183 memcached_result_st
*memcached_fetch_result(memcached_st
*ptr
,
184 memcached_result_st
*result
,
185 memcached_return
*error
)
188 result
= memcached_result_create(ptr
, NULL
);
190 WATCHPOINT_ASSERT(result
->value
.is_allocated
!= MEMCACHED_USED
);
192 if (ptr
->flags
& MEM_NO_BLOCK
)
193 memcached_io_preread(ptr
);
195 while (ptr
->cursor_server
< ptr
->number_of_hosts
)
197 char buffer
[MEMCACHED_DEFAULT_COMMAND_SIZE
];
199 if (memcached_server_response_count(ptr
, ptr
->cursor_server
) == 0)
201 ptr
->cursor_server
++;
205 *error
= memcached_response(ptr
, buffer
, MEMCACHED_DEFAULT_COMMAND_SIZE
, result
, ptr
->cursor_server
);
207 if (*error
== MEMCACHED_END
) /* END means that we move on to the next */
209 memcached_server_response_reset(ptr
, ptr
->cursor_server
);
210 ptr
->cursor_server
++;
213 else if (*error
== MEMCACHED_SUCCESS
)
219 /* We have completed reading data */
220 if (result
->is_allocated
== MEMCACHED_ALLOCATED
)
221 memcached_result_free(result
);
223 memcached_string_reset(&result
->value
);
225 ptr
->cursor_server
= 0;