Style change
[awesomized/libmemcached] / libmemcached / dump.cc
1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
2 *
3 * Libmemcached library
4 *
5 * Copyright (C) 2011 Data Differential, http://datadifferential.com/
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are
9 * met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 *
14 * * Redistributions in binary form must reproduce the above
15 * copyright notice, this list of conditions and the following disclaimer
16 * in the documentation and/or other materials provided with the
17 * distribution.
18 *
19 * * The names of its contributors may not be used to endorse or
20 * promote products derived from this software without specific prior
21 * written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
29 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
33 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 *
35 */
36
37 /*
38 We use this to dump all keys.
39
40 At this point we only support a callback method. This could be optimized by first
41 calling items and finding active slabs. For the moment though we just loop through
42 all slabs on servers and "grab" the keys.
43 */
44
45 #include <libmemcached/common.h>
46
47 static memcached_return_t ascii_dump(memcached_st *ptr, memcached_dump_fn *callback, void *context, uint32_t number_of_callbacks)
48 {
49 memcached_return_t rc= MEMCACHED_SUCCESS;
50
51 for (uint32_t server_key= 0; server_key < memcached_server_count(ptr); server_key++)
52 {
53 memcached_server_write_instance_st instance;
54 instance= memcached_server_instance_fetch(ptr, server_key);
55
56 /* 256 I BELIEVE is the upper limit of slabs */
57 for (uint32_t x= 0; x < 256; x++)
58 {
59 char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE];
60 int buffer_length= snprintf(buffer, sizeof(buffer), "%u", x);
61 if (buffer_length >= MEMCACHED_DEFAULT_COMMAND_SIZE or buffer_length < 0)
62 {
63 return memcached_set_error(*ptr, MEMCACHED_MEMORY_ALLOCATION_FAILURE, MEMCACHED_AT,
64 memcached_literal_param("snprintf(MEMCACHED_DEFAULT_COMMAND_SIZE)"));
65 }
66
67 libmemcached_io_vector_st vector[]=
68 {
69 { memcached_literal_param("stats cachedump ") },
70 { buffer, buffer_length },
71 { memcached_literal_param(" 0 0\r\n") }
72 };
73
74 rc= memcached_vdo(instance, vector, 3, true);
75
76 if (rc != MEMCACHED_SUCCESS)
77 {
78 goto error;
79 }
80
81 while (1)
82 {
83 uint32_t callback_counter;
84 rc= memcached_response(instance, buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, NULL);
85
86 if (rc == MEMCACHED_ITEM)
87 {
88 char *string_ptr, *end_ptr;
89
90 string_ptr= buffer;
91 string_ptr+= 5; /* Move past ITEM */
92
93 for (end_ptr= string_ptr; isgraph(*end_ptr); end_ptr++) {} ;
94
95 char *key= string_ptr;
96 key[(size_t)(end_ptr-string_ptr)]= 0;
97
98 for (callback_counter= 0; callback_counter < number_of_callbacks; callback_counter++)
99 {
100 rc= (*callback[callback_counter])(ptr, key, (size_t)(end_ptr-string_ptr), context);
101 if (rc != MEMCACHED_SUCCESS)
102 {
103 break;
104 }
105 }
106 }
107 else if (rc == MEMCACHED_END)
108 {
109 break;
110 }
111 else if (rc == MEMCACHED_SERVER_ERROR or rc == MEMCACHED_CLIENT_ERROR)
112 {
113 /* If we try to request stats cachedump for a slab class that is too big
114 * the server will return an incorrect error message:
115 * "MEMCACHED_SERVER_ERROR failed to allocate memory"
116 * This isn't really a fatal error, so let's just skip it. I want to
117 * fix the return value from the memcached server to a CLIENT_ERROR,
118 * so let's add support for that as well right now.
119 */
120 rc= MEMCACHED_END;
121 break;
122 }
123 else
124 {
125 goto error;
126 }
127 }
128 }
129 }
130
131 error:
132 if (rc == MEMCACHED_END)
133 {
134 return MEMCACHED_SUCCESS;
135 }
136 else
137 {
138 return rc;
139 }
140 }
141
142 memcached_return_t memcached_dump(memcached_st *ptr, memcached_dump_fn *callback, void *context, uint32_t number_of_callbacks)
143 {
144 memcached_return_t rc;
145 if (memcached_failed(rc= initialize_query(ptr, true)))
146 {
147 return rc;
148 }
149
150 /*
151 No support for Binary protocol yet
152 @todo Fix this so that we just flush, switch to ascii, and then go back to binary.
153 */
154 if (ptr->flags.binary_protocol)
155 {
156 return MEMCACHED_FAILURE;
157 }
158
159 return ascii_dump(ptr, callback, context, number_of_callbacks);
160 }