Updating for 1.0.2 release
[awesomized/libmemcached] / libmemcached / dump.cc
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 <libmemcached/common.h>
10
11 static memcached_return_t ascii_dump(memcached_st *ptr, memcached_dump_fn *callback, void *context, uint32_t number_of_callbacks)
12 {
13 memcached_return_t rc= MEMCACHED_SUCCESS;
14
15 for (uint32_t server_key= 0; server_key < memcached_server_count(ptr); server_key++)
16 {
17 memcached_server_write_instance_st instance;
18 instance= memcached_server_instance_fetch(ptr, server_key);
19
20 /* 256 I BELIEVE is the upper limit of slabs */
21 for (uint32_t x= 0; x < 256; x++)
22 {
23 char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE];
24 int send_length;
25 send_length= snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE,
26 "stats cachedump %u 0 0\r\n", x);
27
28 if (send_length >= MEMCACHED_DEFAULT_COMMAND_SIZE || send_length < 0)
29 {
30 return memcached_set_error(*ptr, MEMCACHED_MEMORY_ALLOCATION_FAILURE, MEMCACHED_AT,
31 memcached_literal_param("snprintf(MEMCACHED_DEFAULT_COMMAND_SIZE)"));
32 }
33
34 rc= memcached_do(instance, buffer, (size_t)send_length, true);
35
36 if (rc != MEMCACHED_SUCCESS)
37 {
38 goto error;
39 }
40
41 while (1)
42 {
43 uint32_t callback_counter;
44 rc= memcached_response(instance, buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, NULL);
45
46 if (rc == MEMCACHED_ITEM)
47 {
48 char *string_ptr, *end_ptr;
49
50 string_ptr= buffer;
51 string_ptr+= 5; /* Move past ITEM */
52
53 for (end_ptr= string_ptr; isgraph(*end_ptr); end_ptr++) {} ;
54
55 char *key= string_ptr;
56 key[(size_t)(end_ptr-string_ptr)]= 0;
57
58 for (callback_counter= 0; callback_counter < number_of_callbacks; callback_counter++)
59 {
60 rc= (*callback[callback_counter])(ptr, key, (size_t)(end_ptr-string_ptr), context);
61 if (rc != MEMCACHED_SUCCESS)
62 {
63 break;
64 }
65 }
66 }
67 else if (rc == MEMCACHED_END)
68 {
69 break;
70 }
71 else if (rc == MEMCACHED_SERVER_ERROR or rc == MEMCACHED_CLIENT_ERROR)
72 {
73 /* If we try to request stats cachedump for a slab class that is too big
74 * the server will return an incorrect error message:
75 * "MEMCACHED_SERVER_ERROR failed to allocate memory"
76 * This isn't really a fatal error, so let's just skip it. I want to
77 * fix the return value from the memcached server to a CLIENT_ERROR,
78 * so let's add support for that as well right now.
79 */
80 rc= MEMCACHED_END;
81 break;
82 }
83 else
84 {
85 goto error;
86 }
87 }
88 }
89 }
90
91 error:
92 if (rc == MEMCACHED_END)
93 {
94 return MEMCACHED_SUCCESS;
95 }
96 else
97 {
98 return rc;
99 }
100 }
101
102 memcached_return_t memcached_dump(memcached_st *ptr, memcached_dump_fn *callback, void *context, uint32_t number_of_callbacks)
103 {
104 memcached_return_t rc;
105 if ((rc= initialize_query(ptr)) != MEMCACHED_SUCCESS)
106 {
107 return rc;
108 }
109
110 /*
111 No support for Binary protocol yet
112 @todo Fix this so that we just flush, switch to ascii, and then go back to binary.
113 */
114 if (ptr->flags.binary_protocol)
115 {
116 return MEMCACHED_FAILURE;
117 }
118
119 return ascii_dump(ptr, callback, context, number_of_callbacks);
120 }