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 WATCHPOINT_ASSERT(ptr
->root
);
18 end_ptr
= buffer
+ MEMCACHED_DEFAULT_COMMAND_SIZE
;
20 memcached_result_reset(result
);
23 string_ptr
+= 6; /* "VALUE " */
32 result
->key_length
= 0;
34 for (prefix_length
= ptr
->root
->prefix_key_length
; isgraph(*string_ptr
); string_ptr
++)
36 if (prefix_length
== 0)
45 result
->key
[result
->key_length
]= 0;
48 if (end_ptr
== string_ptr
)
51 /* Flags fetch move past space */
53 if (end_ptr
== string_ptr
)
55 for (next_ptr
= string_ptr
; isdigit(*string_ptr
); string_ptr
++);
56 result
->flags
= (uint32_t)strtol(next_ptr
, &string_ptr
, 10);
58 if (end_ptr
== string_ptr
)
61 /* Length fetch move past space*/
63 if (end_ptr
== string_ptr
)
66 for (next_ptr
= string_ptr
; isdigit(*string_ptr
); string_ptr
++);
67 value_length
= (size_t)strtoll(next_ptr
, &string_ptr
, 10);
69 if (end_ptr
== string_ptr
)
73 if (*string_ptr
== '\r')
75 /* Skip past the \r\n */
81 for (next_ptr
= string_ptr
; isdigit(*string_ptr
); string_ptr
++);
82 result
->cas
= (size_t)strtoll(next_ptr
, &string_ptr
, 10);
85 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;
106 read_length
= memcached_io_read(ptr
, value_ptr
, to_read
);
107 if (read_length
!= (size_t)(value_length
+ 2))
112 /* This next bit blows the API, but this is internal....*/
115 char_ptr
= memcached_string_value(&result
->value
);;
116 char_ptr
[value_length
]= 0;
117 char_ptr
[value_length
+ 1]= 0;
118 memcached_string_set_length(&result
->value
, value_length
);
121 return MEMCACHED_SUCCESS
;
124 memcached_io_reset(ptr
);
126 return MEMCACHED_PARTIAL_READ
;
129 char *memcached_fetch(memcached_st
*ptr
, char *key
, size_t *key_length
,
130 size_t *value_length
,
132 memcached_return
*error
)
134 memcached_result_st
*result_buffer
= &ptr
->result
;
136 while (ptr
->cursor_server
< ptr
->number_of_hosts
)
138 char buffer
[MEMCACHED_DEFAULT_COMMAND_SIZE
];
140 if (memcached_server_response_count(&ptr
->hosts
[ptr
->cursor_server
]) == 0)
142 ptr
->cursor_server
++;
146 *error
= memcached_response(&ptr
->hosts
[ptr
->cursor_server
], buffer
, MEMCACHED_DEFAULT_COMMAND_SIZE
, result_buffer
);
148 if (*error
== MEMCACHED_END
) /* END means that we move on to the next */
150 memcached_server_response_reset(&ptr
->hosts
[ptr
->cursor_server
]);
151 ptr
->cursor_server
++;
154 else if (*error
== MEMCACHED_SUCCESS
)
156 *value_length
= memcached_string_length(&result_buffer
->value
);
160 strncpy(key
, result_buffer
->key
, result_buffer
->key_length
);
161 *key_length
= result_buffer
->key_length
;
164 if (result_buffer
->flags
)
165 *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
);
193 if (ptr
->flags
& MEM_NO_BLOCK
)
194 memcached_io_preread(ptr
);
197 while (ptr
->cursor_server
< ptr
->number_of_hosts
)
199 char buffer
[MEMCACHED_DEFAULT_COMMAND_SIZE
];
201 if (memcached_server_response_count(&ptr
->hosts
[ptr
->cursor_server
]) == 0)
203 ptr
->cursor_server
++;
207 *error
= memcached_response(&ptr
->hosts
[ptr
->cursor_server
], buffer
, MEMCACHED_DEFAULT_COMMAND_SIZE
, result
);
209 if (*error
== MEMCACHED_END
) /* END means that we move on to the next */
211 memcached_server_response_reset(&ptr
->hosts
[ptr
->cursor_server
]);
212 ptr
->cursor_server
++;
215 else if (*error
== MEMCACHED_SUCCESS
)
221 /* We have completed reading data */
222 if (result
->is_allocated
== MEMCACHED_ALLOCATED
)
223 memcached_result_free(result
);
225 memcached_string_reset(&result
->value
);
227 ptr
->cursor_server
= 0;