Do not try to run the binary protocol testsuite for pre-1.3 servers
[awesomized/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
14 unlikely (key_length == 0)
15 return MEMCACHED_NO_KEY_PROVIDED;
16
17 unlikely (ptr->hosts == NULL || ptr->number_of_hosts == 0)
18 return MEMCACHED_NO_SERVERS;
19
20 if ((ptr->flags & MEM_VERIFY_KEY) && (memcachd_key_test((char **)&key, &key_length, 1) == MEMCACHED_BAD_KEY_PROVIDED))
21 return MEMCACHED_BAD_KEY_PROVIDED;
22
23 server_key= memcached_generate_hash(ptr, key, key_length);
24
25 send_length= snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE,
26 "%s %s%.*s %u\r\n", verb,
27 ptr->prefix_key,
28 (int)key_length, key,
29 offset);
30 unlikely (send_length >= MEMCACHED_DEFAULT_COMMAND_SIZE)
31 return MEMCACHED_WRITE_FAILURE;
32
33 rc= memcached_do(&ptr->hosts[server_key], buffer, send_length, 1);
34 if (rc != MEMCACHED_SUCCESS)
35 return rc;
36
37 rc= memcached_response(&ptr->hosts[server_key], buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, NULL);
38
39 /*
40 So why recheck responce? Because the protocol is brain dead :)
41 The number returned might end up equaling one of the string
42 values. Less chance of a mistake with strncmp() so we will
43 use it. We still called memcached_response() though since it
44 worked its magic for non-blocking IO.
45 */
46 if (!strncmp(buffer, "ERROR\r\n", 7))
47 {
48 *value= 0;
49 rc= MEMCACHED_PROTOCOL_ERROR;
50 }
51 else if (!strncmp(buffer, "NOT_FOUND\r\n", 11))
52 {
53 *value= 0;
54 rc= MEMCACHED_NOTFOUND;
55 }
56 else
57 {
58 *value= strtoull(buffer, (char **)NULL, 10);
59 rc= MEMCACHED_SUCCESS;
60 }
61
62 return rc;
63 }
64
65 static memcached_return binary_incr_decr(memcached_st *ptr, uint8_t cmd,
66 const char *key, size_t key_length,
67 uint32_t offset, uint64_t *value)
68 {
69 unsigned int server_key;
70
71 unlikely (key_length == 0)
72 return MEMCACHED_NO_KEY_PROVIDED;
73
74 unlikely (ptr->hosts == NULL || ptr->number_of_hosts == 0)
75 return MEMCACHED_NO_SERVERS;
76
77 if ((ptr->flags & MEM_VERIFY_KEY) && (memcachd_key_test(&key, &key_length, 1) == MEMCACHED_BAD_KEY_PROVIDED))
78 return MEMCACHED_BAD_KEY_PROVIDED;
79
80 server_key= memcached_generate_hash(ptr, key, key_length);
81
82 protocol_binary_request_incr request= {0};
83
84 request.message.header.request.magic= PROTOCOL_BINARY_REQ;
85 request.message.header.request.opcode= cmd;
86 request.message.header.request.keylen= htons((uint16_t)key_length);
87 request.message.header.request.extlen= 20;
88 request.message.header.request.datatype= PROTOCOL_BINARY_RAW_BYTES;
89 request.message.header.request.bodylen= htonl(key_length + request.message.header.request.extlen);
90 request.message.body.delta= htonll(offset);
91
92 /* TODO: The binary protocol allows you to specify initial and expiry time */
93 if ((memcached_do(&ptr->hosts[server_key], request.bytes,
94 sizeof(request.bytes), 0)!=MEMCACHED_SUCCESS) ||
95 (memcached_io_write(&ptr->hosts[server_key], key, key_length, 1) == -1))
96 {
97 memcached_io_reset(&ptr->hosts[server_key]);
98 return MEMCACHED_WRITE_FAILURE;
99 }
100
101 return memcached_response(&ptr->hosts[server_key], value, sizeof(*value), NULL);
102 }
103
104 memcached_return memcached_increment(memcached_st *ptr,
105 const char *key, size_t key_length,
106 uint32_t offset,
107 uint64_t *value)
108 {
109 memcached_return rc;
110
111 LIBMEMCACHED_MEMCACHED_INCREMENT_START();
112 if (ptr->flags & MEM_BINARY_PROTOCOL)
113 rc= binary_incr_decr(ptr, PROTOCOL_BINARY_CMD_INCREMENT, key,
114 key_length, offset, value);
115 else
116 rc= memcached_auto(ptr, "incr", key, key_length, offset, value);
117
118 LIBMEMCACHED_MEMCACHED_INCREMENT_END();
119
120 return rc;
121 }
122
123 memcached_return memcached_decrement(memcached_st *ptr,
124 const char *key, size_t key_length,
125 uint32_t offset,
126 uint64_t *value)
127 {
128 memcached_return rc;
129
130 LIBMEMCACHED_MEMCACHED_DECREMENT_START();
131 if (ptr->flags & MEM_BINARY_PROTOCOL)
132 rc= binary_incr_decr(ptr, PROTOCOL_BINARY_CMD_DECREMENT, key,
133 key_length, offset, value);
134 else
135 rc= memcached_auto(ptr, "decr", key, key_length, offset, value);
136
137 LIBMEMCACHED_MEMCACHED_DECREMENT_END();
138
139 return rc;
140 }