2 #include "memcached_io.h"
4 static char *memcached_value_fetch(memcached_st
*ptr
, char *key
, size_t *key_length
,
7 memcached_return
*error
,
9 unsigned int server_key
)
11 char buffer
[MEMCACHED_DEFAULT_COMMAND_SIZE
];
19 memset(buffer
, 0, MEMCACHED_DEFAULT_COMMAND_SIZE
);
20 end_ptr
= buffer
+ MEMCACHED_DEFAULT_COMMAND_SIZE
;
24 *error
= memcached_response(ptr
, buffer
, MEMCACHED_DEFAULT_COMMAND_SIZE
, server_key
);
26 if (*error
== MEMCACHED_SUCCESS
)
31 string_ptr
+= 6; /* "VALUE " */
36 memset(key
, 0, MEMCACHED_MAX_KEY
);
39 for (; end_ptr
== string_ptr
|| *string_ptr
!= ' '; string_ptr
++)
46 else /* Skip characters */
47 for (; end_ptr
== string_ptr
|| *string_ptr
!= ' '; string_ptr
++);
49 if (end_ptr
== string_ptr
)
52 /* Flags fetch move past space */
54 if (end_ptr
== string_ptr
)
56 for (next_ptr
= string_ptr
; end_ptr
== string_ptr
|| *string_ptr
!= ' '; string_ptr
++);
57 *flags
= (uint16_t)strtol(next_ptr
, &string_ptr
, 10);
59 if (end_ptr
== string_ptr
)
62 /* Length fetch move past space*/
64 if (end_ptr
== string_ptr
)
67 for (next_ptr
= string_ptr
; end_ptr
== string_ptr
|| *string_ptr
!= ' '; string_ptr
++);
68 *value_length
= (size_t)strtoll(next_ptr
, &string_ptr
, 10);
70 if (end_ptr
== string_ptr
)
73 /* Skip past the \r\n */
76 if (end_ptr
< string_ptr
)
86 /* We add two bytes so that we can walk the \r\n */
87 value
= (char *)malloc(((*value_length
) +2) * sizeof(char));
88 memset(value
, 0, ((*value_length
) +2) * sizeof(char));
93 *error
= MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
99 to_read
= (*value_length
) + 2;
101 read_length
= memcached_io_read(ptr
, server_key
,
104 if (read_length
!= (size_t)(*value_length
+ 2))
113 else if (*error
== MEMCACHED_END
)
114 *error
= MEMCACHED_NOTFOUND
;
118 *error
= MEMCACHED_PARTIAL_READ
;
122 char *memcached_get(memcached_st
*ptr
, char *key
, size_t key_length
,
123 size_t *value_length
,
125 memcached_return
*error
)
128 char buffer
[MEMCACHED_DEFAULT_COMMAND_SIZE
];
129 unsigned int server_key
;
131 LIBMEMCACHED_MEMCACHED_GET_START();
133 server_key
= memcached_generate_hash(ptr
, key
, key_length
);
136 *error
= memcached_connect(ptr
, server_key
);
138 if (*error
!= MEMCACHED_SUCCESS
)
141 send_length
= snprintf(buffer
, MEMCACHED_DEFAULT_COMMAND_SIZE
, "get %.*s\r\n",
142 (int)key_length
, key
);
144 if ((memcached_io_write(ptr
, server_key
, buffer
, send_length
, 1)) == -1)
146 *error
= MEMCACHED_WRITE_FAILURE
;
150 value
= memcached_value_fetch(ptr
, key
, &key_length
, value_length
, flags
,
151 error
, 0, server_key
);
152 if (*error
== MEMCACHED_END
&& *value_length
== 0)
154 *error
= MEMCACHED_NOTFOUND
;
157 else if (*error
== MEMCACHED_SUCCESS
)
160 /* We need to read END */
161 rc
= memcached_response(ptr
, buffer
, MEMCACHED_DEFAULT_COMMAND_SIZE
, server_key
);
163 if (rc
!= MEMCACHED_END
)
165 *error
= MEMCACHED_PROTOCOL_ERROR
;
172 LIBMEMCACHED_MEMCACHED_GET_END();
180 LIBMEMCACHED_MEMCACHED_GET_END();
185 memcached_return
memcached_mget(memcached_st
*ptr
,
186 char **keys
, size_t *key_length
,
187 unsigned int number_of_keys
)
189 char buffer
[HUGE_STRING_LEN
];
192 memcached_string_st
**cursor_key_exec
;
193 LIBMEMCACHED_MEMCACHED_MGET_START();
195 ptr
->cursor_server
= 0;
196 memset(buffer
, 0, HUGE_STRING_LEN
);
198 cursor_key_exec
= (memcached_string_st
**)malloc(sizeof(memcached_string_st
*) * ptr
->number_of_hosts
);
199 memset(cursor_key_exec
, 0, sizeof(memcached_string_st
*) * ptr
->number_of_hosts
);
202 for (x
= 0; x
< number_of_keys
; x
++)
204 unsigned int server_key
;
206 server_key
= memcached_generate_hash(ptr
, keys
[x
], key_length
[x
]);
208 if (cursor_key_exec
[server_key
])
210 memcached_string_st
*string
= cursor_key_exec
[server_key
];
212 memcached_string_append_character(ptr
, string
, ' ');
213 memcached_string_append(ptr
, string
, keys
[x
], key_length
[x
]);
217 memcached_string_st
*string
= memcached_string_create(ptr
, SMALL_STRING_LEN
);
219 /* We need to figure out the correct way to error in case of this failure */
223 memcached_string_append(ptr
, string
, "get ", 4);
224 memcached_string_append(ptr
, string
, keys
[x
], key_length
[x
]);
226 cursor_key_exec
[server_key
]= string
;
232 Should we muddle on if some servers are dead?
234 for (x
= 0; x
< ptr
->number_of_hosts
; x
++)
236 if (cursor_key_exec
[x
])
238 /* We need to doo something about non-connnected hosts in the future */
239 rc
= memcached_connect(ptr
, x
);
241 memcached_string_st
*string
= cursor_key_exec
[x
];
242 memcached_string_append(ptr
, string
, "\r\n", 2);
244 if ((memcached_io_write(ptr
, x
, string
->string
,
245 memcached_string_length(ptr
, string
), 1)) == -1)
248 rc
= MEMCACHED_SOME_ERRORS
;
250 memcached_string_free(ptr
, string
);
251 cursor_key_exec
[x
]= NULL
; /* Remove warning */
255 free(cursor_key_exec
);
257 LIBMEMCACHED_MEMCACHED_MGET_END();
261 char *memcached_fetch(memcached_st
*ptr
, char *key
, size_t *key_length
,
262 size_t *value_length
,
264 memcached_return
*error
)
268 while (ptr
->cursor_server
< ptr
->number_of_hosts
)
270 value_check
= memcached_value_fetch(ptr
, key
, key_length
, value_length
, flags
,
271 error
, 1, ptr
->cursor_server
);
273 if (*error
== MEMCACHED_NOTFOUND
)
274 ptr
->cursor_server
++;
275 else if (*error
!= MEMCACHED_SUCCESS
)