2 #include "memcached_io.h"
4 memcached_return
value_fetch(memcached_server_st
*ptr
,
6 memcached_result_st
*result
)
8 memcached_return rc
= MEMCACHED_SUCCESS
;
17 end_ptr
= buffer
+ MEMCACHED_DEFAULT_COMMAND_SIZE
;
19 result
->key_length
= 0;
21 memcached_string_reset(&result
->value
);
24 string_ptr
+= 6; /* "VALUE " */
32 result
->key_length
= 0;
34 for (; isgraph(*string_ptr
); string_ptr
++)
40 result
->key
[result
->key_length
]= 0;
43 if (end_ptr
== string_ptr
)
46 /* Flags fetch move past space */
48 if (end_ptr
== string_ptr
)
50 for (next_ptr
= string_ptr
; isdigit(*string_ptr
); string_ptr
++);
51 result
->flags
= (uint32_t)strtol(next_ptr
, &string_ptr
, 10);
53 if (end_ptr
== string_ptr
)
56 /* Length fetch move past space*/
58 if (end_ptr
== string_ptr
)
61 for (next_ptr
= string_ptr
; isdigit(*string_ptr
); string_ptr
++);
62 value_length
= (size_t)strtoll(next_ptr
, &string_ptr
, 10);
64 if (end_ptr
== string_ptr
)
68 if (*string_ptr
== '\r')
70 /* Skip past the \r\n */
77 for (next_ptr
= string_ptr
; isdigit(*string_ptr
); string_ptr
++);
78 result
->cas
= (size_t)strtoll(next_ptr
, &string_ptr
, 10);
81 if (end_ptr
< string_ptr
)
84 /* We add two bytes so that we can walk the \r\n */
85 rc
= memcached_string_check(&result
->value
, value_length
+2);
86 if (rc
!= MEMCACHED_SUCCESS
)
89 return MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
92 value_ptr
= memcached_string_value(&result
->value
);
95 We read the \r\n into the string since not doing so is more
96 cycles then the waster of memory to do so.
98 We are null terminating through, which will most likely make
99 some people lazy about using the return length.
101 to_read
= (value_length
) + 2;
102 read_length
= memcached_io_read(ptr
, value_ptr
, to_read
);
103 if (read_length
!= (size_t)(value_length
+ 2))
108 /* This next bit blows the API, but this is internal....*/
111 char_ptr
= memcached_string_value(&result
->value
);;
112 char_ptr
[value_length
]= 0;
113 char_ptr
[value_length
+ 1]= 0;
114 memcached_string_set_length(&result
->value
, value_length
);
117 return MEMCACHED_SUCCESS
;
120 memcached_io_reset(ptr
);
122 return MEMCACHED_PARTIAL_READ
;
125 char *memcached_fetch(memcached_st
*ptr
, char *key
, size_t *key_length
,
126 size_t *value_length
,
128 memcached_return
*error
)
130 memcached_result_st
*result_buffer
= &ptr
->result
;
132 while (ptr
->cursor_server
< ptr
->number_of_hosts
)
134 char buffer
[MEMCACHED_DEFAULT_COMMAND_SIZE
];
136 if (memcached_server_response_count(&ptr
->hosts
[ptr
->cursor_server
]) == 0)
138 ptr
->cursor_server
++;
142 *error
= memcached_response(&ptr
->hosts
[ptr
->cursor_server
], buffer
, MEMCACHED_DEFAULT_COMMAND_SIZE
, result_buffer
);
144 if (*error
== MEMCACHED_END
) /* END means that we move on to the next */
146 memcached_server_response_reset(&ptr
->hosts
[ptr
->cursor_server
]);
147 ptr
->cursor_server
++;
150 else if (*error
== MEMCACHED_SUCCESS
)
152 *value_length
= memcached_string_length(&result_buffer
->value
);
156 strncpy(key
, result_buffer
->key
, result_buffer
->key_length
);
157 *key_length
= result_buffer
->key_length
;
160 if (result_buffer
->flags
)
161 *flags
= result_buffer
->flags
;
163 return memcached_string_c_copy(&result_buffer
->value
);
172 ptr
->cursor_server
= 0;
177 memcached_result_st
*memcached_fetch_result(memcached_st
*ptr
,
178 memcached_result_st
*result
,
179 memcached_return
*error
)
182 result
= memcached_result_create(ptr
, NULL
);
184 WATCHPOINT_ASSERT(result
->value
.is_allocated
!= MEMCACHED_USED
);
187 if (ptr
->flags
& MEM_NO_BLOCK
)
188 memcached_io_preread(ptr
);
191 while (ptr
->cursor_server
< ptr
->number_of_hosts
)
193 char buffer
[MEMCACHED_DEFAULT_COMMAND_SIZE
];
195 if (memcached_server_response_count(&ptr
->hosts
[ptr
->cursor_server
]) == 0)
197 ptr
->cursor_server
++;
201 *error
= memcached_response(&ptr
->hosts
[ptr
->cursor_server
], buffer
, MEMCACHED_DEFAULT_COMMAND_SIZE
, result
);
203 if (*error
== MEMCACHED_END
) /* END means that we move on to the next */
205 memcached_server_response_reset(&ptr
->hosts
[ptr
->cursor_server
]);
206 ptr
->cursor_server
++;
209 else if (*error
== MEMCACHED_SUCCESS
)
215 /* We have completed reading data */
216 if (result
->is_allocated
== MEMCACHED_ALLOCATED
)
217 memcached_result_free(result
);
219 memcached_string_reset(&result
->value
);
221 ptr
->cursor_server
= 0;