Merge Monty
[m6w6/libmemcached] / libmemcached / dump.c
1 /*
2 We use this to dump all keys.
3
4 At this point we only support a callback method. This could be optimized by first
5 calling items and finding active slabs. For the moment though we just loop through
6 all slabs on servers and "grab" the keys.
7 */
8
9 #include "common.h"
10 static memcached_return_t ascii_dump(memcached_st *ptr, memcached_dump_fn *callback, void *context, uint32_t number_of_callbacks)
11 {
12 memcached_return_t rc= MEMCACHED_SUCCESS;
13 char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE];
14 size_t send_length;
15 uint32_t server_key;
16 uint32_t x;
17
18 unlikely (memcached_server_count(ptr) == 0)
19 return MEMCACHED_NO_SERVERS;
20
21 for (server_key= 0; server_key < memcached_server_count(ptr); server_key++)
22 {
23 memcached_server_instance_st *instance;
24 instance= memcached_server_instance_fetch(ptr, server_key);
25
26 /* 256 I BELIEVE is the upper limit of slabs */
27 for (x= 0; x < 256; x++)
28 {
29 send_length= (size_t) snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE,
30 "stats cachedump %u 0 0\r\n", x);
31
32 rc= memcached_do(instance, buffer, send_length, 1);
33
34 unlikely (rc != MEMCACHED_SUCCESS)
35 goto error;
36
37 while (1)
38 {
39 uint32_t callback_counter;
40 rc= memcached_response(instance, buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, NULL);
41
42 if (rc == MEMCACHED_ITEM)
43 {
44 char *string_ptr, *end_ptr;
45 char *key;
46
47 string_ptr= buffer;
48 string_ptr+= 5; /* Move past ITEM */
49 for (end_ptr= string_ptr; isgraph(*end_ptr); end_ptr++);
50 key= string_ptr;
51 key[(size_t)(end_ptr-string_ptr)]= 0;
52 for (callback_counter= 0; callback_counter < number_of_callbacks; callback_counter++)
53 {
54 rc= (*callback[callback_counter])(ptr, key, (size_t)(end_ptr-string_ptr), context);
55 if (rc != MEMCACHED_SUCCESS)
56 break;
57 }
58 }
59 else if (rc == MEMCACHED_END)
60 break;
61 else if (rc == MEMCACHED_SERVER_ERROR || rc == MEMCACHED_CLIENT_ERROR)
62 {
63 /* If we try to request stats cachedump for a slab class that is too big
64 * the server will return an incorrect error message:
65 * "MEMCACHED_SERVER_ERROR failed to allocate memory"
66 * This isn't really a fatal error, so let's just skip it. I want to
67 * fix the return value from the memcached server to a CLIENT_ERROR,
68 * so let's add support for that as well right now.
69 */
70 rc= MEMCACHED_END;
71 break;
72 }
73 else
74 goto error;
75 }
76 }
77 }
78
79 error:
80 if (rc == MEMCACHED_END)
81 return MEMCACHED_SUCCESS;
82 else
83 return rc;
84 }
85
86 memcached_return_t memcached_dump(memcached_st *ptr, memcached_dump_fn *callback, void *context, uint32_t number_of_callbacks)
87 {
88 /* No support for Binary protocol yet */
89 if (ptr->flags.binary_protocol)
90 return MEMCACHED_FAILURE;
91
92 return ascii_dump(ptr, callback, context, number_of_callbacks);
93 }