Updating the exceptions header file with some slight modifications.
[m6w6/libmemcached] / libmemcached / memcached_auto.c
1 #include "common.h"
2
3 static memcached_return memcached_auto(memcached_st *ptr,
4 const char *verb,
5 const char *key, size_t key_length,
6 unsigned int offset,
7 uint64_t *value)
8 {
9 size_t send_length;
10 memcached_return rc;
11 char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE];
12 unsigned int server_key;
13 bool no_reply= (ptr->flags & MEM_NOREPLY);
14
15 unlikely (ptr->hosts == NULL || ptr->number_of_hosts == 0)
16 return MEMCACHED_NO_SERVERS;
17
18 if ((ptr->flags & MEM_VERIFY_KEY) && (memcached_key_test((const char **)&key, &key_length, 1) == MEMCACHED_BAD_KEY_PROVIDED))
19 return MEMCACHED_BAD_KEY_PROVIDED;
20
21 server_key= memcached_generate_hash(ptr, key, key_length);
22
23 send_length= (size_t)snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE,
24 "%s %s%.*s %u%s\r\n", verb,
25 ptr->prefix_key,
26 (int)key_length, key,
27 offset, no_reply ? " noreply" : "");
28 unlikely (send_length >= MEMCACHED_DEFAULT_COMMAND_SIZE)
29 return MEMCACHED_WRITE_FAILURE;
30
31 rc= memcached_do(&ptr->hosts[server_key], buffer, send_length, 1);
32 if (no_reply || rc != MEMCACHED_SUCCESS)
33 return rc;
34
35 rc= memcached_response(&ptr->hosts[server_key], buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, NULL);
36
37 /*
38 So why recheck responce? Because the protocol is brain dead :)
39 The number returned might end up equaling one of the string
40 values. Less chance of a mistake with strncmp() so we will
41 use it. We still called memcached_response() though since it
42 worked its magic for non-blocking IO.
43 */
44 if (!strncmp(buffer, "ERROR\r\n", 7))
45 {
46 *value= 0;
47 rc= MEMCACHED_PROTOCOL_ERROR;
48 }
49 else if (!strncmp(buffer, "NOT_FOUND\r\n", 11))
50 {
51 *value= 0;
52 rc= MEMCACHED_NOTFOUND;
53 }
54 else
55 {
56 *value= strtoull(buffer, (char **)NULL, 10);
57 rc= MEMCACHED_SUCCESS;
58 }
59
60 return rc;
61 }
62
63 static memcached_return binary_incr_decr(memcached_st *ptr, uint8_t cmd,
64 const char *key, size_t key_length,
65 uint64_t offset, uint64_t initial,
66 time_t expiration,
67 uint64_t *value)
68 {
69 unsigned int server_key;
70 bool no_reply= (ptr->flags & MEM_NOREPLY);
71
72 unlikely (ptr->hosts == NULL || ptr->number_of_hosts == 0)
73 return MEMCACHED_NO_SERVERS;
74
75 server_key= memcached_generate_hash(ptr, key, key_length);
76
77 if (no_reply)
78 {
79 if(cmd == PROTOCOL_BINARY_CMD_DECREMENT)
80 cmd= PROTOCOL_BINARY_CMD_DECREMENTQ;
81 if(cmd == PROTOCOL_BINARY_CMD_INCREMENT)
82 cmd= PROTOCOL_BINARY_CMD_INCREMENTQ;
83 }
84 protocol_binary_request_incr request= {.bytes= {0}};
85
86 request.message.header.request.magic= PROTOCOL_BINARY_REQ;
87 request.message.header.request.opcode= cmd;
88 request.message.header.request.keylen= htons((uint16_t) key_length);
89 request.message.header.request.extlen= 20;
90 request.message.header.request.datatype= PROTOCOL_BINARY_RAW_BYTES;
91 request.message.header.request.bodylen= htonl((uint32_t) (key_length + request.message.header.request.extlen));
92 request.message.body.delta= htonll(offset);
93 request.message.body.initial= htonll(initial);
94 request.message.body.expiration= htonl((uint32_t) expiration);
95
96 if ((memcached_do(&ptr->hosts[server_key], request.bytes,
97 sizeof(request.bytes), 0)!=MEMCACHED_SUCCESS) ||
98 (memcached_io_write(&ptr->hosts[server_key], key, key_length, 1) == -1))
99 {
100 memcached_io_reset(&ptr->hosts[server_key]);
101 return MEMCACHED_WRITE_FAILURE;
102 }
103
104 if (no_reply)
105 return MEMCACHED_SUCCESS;
106 return memcached_response(&ptr->hosts[server_key], (char*)value, sizeof(*value), NULL);
107 }
108
109 memcached_return memcached_increment(memcached_st *ptr,
110 const char *key, size_t key_length,
111 uint32_t offset,
112 uint64_t *value)
113 {
114 memcached_return rc= memcached_validate_key_length(key_length, ptr->flags & MEM_BINARY_PROTOCOL);
115 unlikely (rc != MEMCACHED_SUCCESS)
116 return rc;
117
118 LIBMEMCACHED_MEMCACHED_INCREMENT_START();
119 if (ptr->flags & MEM_BINARY_PROTOCOL)
120 rc= binary_incr_decr(ptr, PROTOCOL_BINARY_CMD_INCREMENT, key, key_length,
121 (uint64_t)offset, 0, MEMCACHED_EXPIRATION_NOT_ADD,
122 value);
123 else
124 rc= memcached_auto(ptr, "incr", key, key_length, offset, value);
125
126 LIBMEMCACHED_MEMCACHED_INCREMENT_END();
127
128 return rc;
129 }
130
131 memcached_return memcached_decrement(memcached_st *ptr,
132 const char *key, size_t key_length,
133 uint32_t offset,
134 uint64_t *value)
135 {
136 memcached_return rc= memcached_validate_key_length(key_length, ptr->flags & MEM_BINARY_PROTOCOL);
137 unlikely (rc != MEMCACHED_SUCCESS)
138 return rc;
139
140 LIBMEMCACHED_MEMCACHED_DECREMENT_START();
141 if (ptr->flags & MEM_BINARY_PROTOCOL)
142 rc= binary_incr_decr(ptr, PROTOCOL_BINARY_CMD_DECREMENT, key, key_length,
143 (uint64_t)offset, 0, MEMCACHED_EXPIRATION_NOT_ADD,
144 value);
145 else
146 rc= memcached_auto(ptr, "decr", key, key_length, offset, value);
147
148 LIBMEMCACHED_MEMCACHED_DECREMENT_END();
149
150 return rc;
151 }
152
153 memcached_return memcached_increment_with_initial(memcached_st *ptr,
154 const char *key,
155 size_t key_length,
156 uint64_t offset,
157 uint64_t initial,
158 time_t expiration,
159 uint64_t *value)
160 {
161 memcached_return rc= memcached_validate_key_length(key_length, ptr->flags & MEM_BINARY_PROTOCOL);
162 unlikely (rc != MEMCACHED_SUCCESS)
163 return rc;
164
165 LIBMEMCACHED_MEMCACHED_INCREMENT_WITH_INITIAL_START();
166 if (ptr->flags & MEM_BINARY_PROTOCOL)
167 rc= binary_incr_decr(ptr, PROTOCOL_BINARY_CMD_INCREMENT, key,
168 key_length, offset, initial, expiration, value);
169 else
170 rc= MEMCACHED_PROTOCOL_ERROR;
171
172 LIBMEMCACHED_MEMCACHED_INCREMENT_WITH_INITIAL_END();
173
174 return rc;
175 }
176
177 memcached_return memcached_decrement_with_initial(memcached_st *ptr,
178 const char *key,
179 size_t key_length,
180 uint64_t offset,
181 uint64_t initial,
182 time_t expiration,
183 uint64_t *value)
184 {
185 memcached_return rc= memcached_validate_key_length(key_length, ptr->flags & MEM_BINARY_PROTOCOL);
186 unlikely (rc != MEMCACHED_SUCCESS)
187 return rc;
188
189 LIBMEMCACHED_MEMCACHED_DECREMENT_WITH_INITIAL_START();
190 if (ptr->flags & MEM_BINARY_PROTOCOL)
191 rc= binary_incr_decr(ptr, PROTOCOL_BINARY_CMD_DECREMENT, key,
192 key_length, offset, initial, expiration, value);
193 else
194 rc= MEMCACHED_PROTOCOL_ERROR;
195
196 LIBMEMCACHED_MEMCACHED_DECREMENT_WITH_INITIAL_END();
197
198 return rc;
199 }