Add in missing files.
[m6w6/libmemcached] / libmemcached / purge.cc
1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
2 *
3 * LibMemcached
4 *
5 * Copyright (C) 2011 Data Differential, http://datadifferential.com/
6 * Copyright (C) 2006-2009 Brian Aker
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions are
11 * met:
12 *
13 * * Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 *
16 * * Redistributions in binary form must reproduce the above
17 * copyright notice, this list of conditions and the following disclaimer
18 * in the documentation and/or other materials provided with the
19 * distribution.
20 *
21 * * The names of its contributors may not be used to endorse or
22 * promote products derived from this software without specific prior
23 * written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 *
37 */
38
39
40 #include <libmemcached/common.h>
41
42 #define memcached_set_purging(__object, __value) ((__object)->state.is_purging= (__value))
43
44 class Purge
45 {
46 public:
47 Purge(memcached_st* arg) :
48 _memc(arg)
49 {
50 memcached_set_purging(_memc, true);
51 }
52
53 ~Purge()
54 {
55 memcached_set_purging(_memc, false);
56 }
57
58 private:
59 memcached_st* _memc;
60 };
61
62 class PollTimeout
63 {
64 public:
65 PollTimeout(memcached_st* arg) :
66 _timeout(arg->poll_timeout),
67 _origin(arg->poll_timeout)
68 {
69 _origin = 2000;
70 }
71
72 ~PollTimeout()
73 {
74 _origin= _timeout;
75 }
76
77 private:
78 int32_t _timeout;
79 int32_t& _origin;
80 };
81
82 bool memcached_purge(org::libmemcached::Instance* ptr)
83 {
84 memcached_st *root= (memcached_st *)ptr->root;
85
86 if (memcached_is_purging(ptr->root) || /* already purging */
87 (memcached_server_response_count(ptr) < ptr->root->io_msg_watermark &&
88 ptr->io_bytes_sent < ptr->root->io_bytes_watermark) ||
89 (ptr->io_bytes_sent >= ptr->root->io_bytes_watermark &&
90 memcached_server_response_count(ptr) < 2))
91 {
92 return true;
93 }
94
95 /*
96 memcached_io_write and memcached_response may call memcached_purge
97 so we need to be able stop any recursion..
98 */
99 Purge set_purge(root);
100
101 WATCHPOINT_ASSERT(ptr->fd != INVALID_SOCKET);
102 /*
103 Force a flush of the buffer to ensure that we don't have the n-1 pending
104 requests buffered up..
105 */
106 if (memcached_io_write(ptr) == false)
107 {
108 memcached_set_error(*ptr, MEMCACHED_WRITE_FAILURE, MEMCACHED_AT);
109 return false;
110 }
111 WATCHPOINT_ASSERT(ptr->fd != INVALID_SOCKET);
112
113 bool is_successful= true;
114 uint32_t no_msg= memcached_server_response_count(ptr) - 1;
115 if (no_msg > 0)
116 {
117 memcached_result_st result;
118
119 /*
120 * We need to increase the timeout, because we might be waiting for
121 * data to be sent from the server (the commands was in the output buffer
122 * and just flushed
123 */
124 PollTimeout poll_timeout(ptr->root);
125
126 memcached_result_st* result_ptr= memcached_result_create(root, &result);
127 assert(result_ptr);
128
129 for (uint32_t x= 0; x < no_msg; x++)
130 {
131 memcached_result_reset(result_ptr);
132 memcached_return_t rc= memcached_read_one_response(ptr, result_ptr);
133 /*
134 * Purge doesn't care for what kind of command results that is received.
135 * The only kind of errors I care about if is I'm out of sync with the
136 * protocol or have problems reading data from the network..
137 */
138 if (rc== MEMCACHED_PROTOCOL_ERROR or rc == MEMCACHED_UNKNOWN_READ_FAILURE or rc == MEMCACHED_READ_FAILURE)
139 {
140 WATCHPOINT_ERROR(rc);
141 memcached_io_reset(ptr);
142 is_successful= false;
143 }
144
145 if (ptr->root->callbacks != NULL)
146 {
147 memcached_callback_st cb = *ptr->root->callbacks;
148 if (memcached_success(rc))
149 {
150 for (uint32_t y= 0; y < cb.number_of_callback; y++)
151 {
152 if (memcached_fatal((*cb.callback[y])(ptr->root, result_ptr, cb.context)))
153 {
154 break;
155 }
156 }
157 }
158 }
159 }
160
161 memcached_result_free(result_ptr);
162 }
163
164 return is_successful;
165 }