2 We use this to dump all keys.
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.
10 static memcached_return_t
ascii_dump(memcached_st
*ptr
, memcached_dump_fn
*callback
, void *context
, uint32_t number_of_callbacks
)
12 memcached_return_t rc
= MEMCACHED_SUCCESS
;
13 char buffer
[MEMCACHED_DEFAULT_COMMAND_SIZE
];
17 unlikely (memcached_server_count(ptr
) == 0)
18 return MEMCACHED_NO_SERVERS
;
20 for (server_key
= 0; server_key
< memcached_server_count(ptr
); server_key
++)
22 memcached_server_write_instance_st instance
;
23 instance
= memcached_server_instance_fetch(ptr
, server_key
);
25 /* 256 I BELIEVE is the upper limit of slabs */
26 for (x
= 0; x
< 256; x
++)
29 send_length
= snprintf(buffer
, MEMCACHED_DEFAULT_COMMAND_SIZE
,
30 "stats cachedump %u 0 0\r\n", x
);
32 if (send_length
>= MEMCACHED_DEFAULT_COMMAND_SIZE
|| send_length
< 0)
34 return MEMCACHED_FAILURE
;
37 rc
= memcached_do(instance
, buffer
, (size_t)send_length
, true);
39 unlikely (rc
!= MEMCACHED_SUCCESS
)
44 uint32_t callback_counter
;
45 rc
= memcached_response(instance
, buffer
, MEMCACHED_DEFAULT_COMMAND_SIZE
, NULL
);
47 if (rc
== MEMCACHED_ITEM
)
49 char *string_ptr
, *end_ptr
;
53 string_ptr
+= 5; /* Move past ITEM */
54 for (end_ptr
= string_ptr
; isgraph(*end_ptr
); end_ptr
++);
56 key
[(size_t)(end_ptr
-string_ptr
)]= 0;
57 for (callback_counter
= 0; callback_counter
< number_of_callbacks
; callback_counter
++)
59 rc
= (*callback
[callback_counter
])(ptr
, key
, (size_t)(end_ptr
-string_ptr
), context
);
60 if (rc
!= MEMCACHED_SUCCESS
)
64 else if (rc
== MEMCACHED_END
)
66 else if (rc
== MEMCACHED_SERVER_ERROR
|| rc
== MEMCACHED_CLIENT_ERROR
)
68 /* If we try to request stats cachedump for a slab class that is too big
69 * the server will return an incorrect error message:
70 * "MEMCACHED_SERVER_ERROR failed to allocate memory"
71 * This isn't really a fatal error, so let's just skip it. I want to
72 * fix the return value from the memcached server to a CLIENT_ERROR,
73 * so let's add support for that as well right now.
85 if (rc
== MEMCACHED_END
)
86 return MEMCACHED_SUCCESS
;
91 memcached_return_t
memcached_dump(memcached_st
*ptr
, memcached_dump_fn
*callback
, void *context
, uint32_t number_of_callbacks
)
93 /* No support for Binary protocol yet */
94 if (ptr
->flags
.binary_protocol
)
95 return MEMCACHED_FAILURE
;
97 return ascii_dump(ptr
, callback
, context
, number_of_callbacks
);