2 #include "memcached_io.h"
4 memcached_return
value_fetch(memcached_st
*ptr
,
6 memcached_result_st
*result
,
7 unsigned int server_key
)
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
++)
40 if (end_ptr
== string_ptr
)
43 /* Flags fetch move past space */
45 if (end_ptr
== string_ptr
)
47 for (next_ptr
= string_ptr
; isdigit(*string_ptr
); string_ptr
++);
48 result
->flags
= (uint32_t)strtol(next_ptr
, &string_ptr
, 10);
50 if (end_ptr
== string_ptr
)
53 /* Length fetch move past space*/
55 if (end_ptr
== string_ptr
)
58 for (next_ptr
= string_ptr
; isdigit(*string_ptr
); string_ptr
++);
59 value_length
= (size_t)strtoll(next_ptr
, &string_ptr
, 10);
61 if (end_ptr
== string_ptr
)
65 if (*string_ptr
== '\r')
67 /* Skip past the \r\n */
74 for (next_ptr
= string_ptr
; isdigit(*string_ptr
); string_ptr
++);
75 result
->cas
= (size_t)strtoll(next_ptr
, &string_ptr
, 10);
78 if (end_ptr
< string_ptr
)
87 /* We add two bytes so that we can walk the \r\n */
88 rc
= memcached_string_check(&result
->value
, value_length
+2);
89 if (rc
!= MEMCACHED_SUCCESS
)
92 return MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
95 value_ptr
= memcached_string_value(&result
->value
);
98 We read the \r\n into the string since not doing so is more
99 cycles then the waster of memory to do so.
101 We are null terminating through, which will most likely make
102 some people lazy about using the return length.
104 to_read
= (value_length
) + 2;
106 read_length
= memcached_io_read(ptr
, server_key
,
109 if (read_length
!= (size_t)(value_length
+ 2))
114 /* This next bit blows the API, but this is internal....*/
117 char_ptr
= memcached_string_value(&result
->value
);;
118 char_ptr
[value_length
]= 0;
119 char_ptr
[value_length
+ 1]= 0;
120 memcached_string_set_length(&result
->value
, value_length
);
123 return MEMCACHED_SUCCESS
;
129 return MEMCACHED_PARTIAL_READ
;
132 char *memcached_fetch(memcached_st
*ptr
, char *key
, size_t *key_length
,
133 size_t *value_length
,
135 memcached_return
*error
)
137 memcached_result_st
*result_buffer
= &ptr
->result
;
139 while (ptr
->cursor_server
< ptr
->number_of_hosts
)
141 char buffer
[MEMCACHED_DEFAULT_COMMAND_SIZE
];
143 if (memcached_server_response_count(ptr
, ptr
->cursor_server
) == 0)
145 ptr
->cursor_server
++;
149 *error
= memcached_response(ptr
, buffer
, MEMCACHED_DEFAULT_COMMAND_SIZE
, result_buffer
, ptr
->cursor_server
);
151 if (*error
== MEMCACHED_END
) /* END means that we move on to the next */
153 memcached_server_response_reset(ptr
, ptr
->cursor_server
);
154 ptr
->cursor_server
++;
157 else if (*error
== MEMCACHED_SUCCESS
)
159 *value_length
= memcached_string_length(&result_buffer
->value
);
163 strncpy(key
, result_buffer
->key
, result_buffer
->key_length
);
164 *key_length
= result_buffer
->key_length
;
166 *flags
= result_buffer
->flags
;
168 return memcached_string_c_copy(&result_buffer
->value
);
177 ptr
->cursor_server
= 0;
182 memcached_result_st
*memcached_fetch_result(memcached_st
*ptr
,
183 memcached_result_st
*result
,
184 memcached_return
*error
)
187 result
= memcached_result_create(ptr
, NULL
);
189 WATCHPOINT_ASSERT(result
->value
.is_allocated
!= MEMCACHED_USED
);
191 if (ptr
->flags
& MEM_NO_BLOCK
)
192 memcached_io_preread(ptr
);
194 while (ptr
->cursor_server
< ptr
->number_of_hosts
)
196 char buffer
[MEMCACHED_DEFAULT_COMMAND_SIZE
];
198 if (memcached_server_response_count(ptr
, ptr
->cursor_server
) == 0)
200 ptr
->cursor_server
++;
204 *error
= memcached_response(ptr
, buffer
, MEMCACHED_DEFAULT_COMMAND_SIZE
, result
, ptr
->cursor_server
);
206 if (*error
== MEMCACHED_END
) /* END means that we move on to the next */
208 memcached_server_response_reset(ptr
, ptr
->cursor_server
);
209 ptr
->cursor_server
++;
212 else if (*error
== MEMCACHED_SUCCESS
)
218 /* We have completed reading data */
219 if (result
->is_allocated
== MEMCACHED_ALLOCATED
)
220 memcached_result_free(result
);
222 memcached_string_reset(&result
->value
);
224 ptr
->cursor_server
= 0;