ade0b59b98e6d632ebe6e994361bd1138327dae4
[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 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((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= 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 uint32_t offset, uint64_t *value)
66 {
67 unsigned int server_key;
68 bool no_reply= (ptr->flags & MEM_NOREPLY);
69
70 unlikely (ptr->hosts == NULL || ptr->number_of_hosts == 0)
71 return MEMCACHED_NO_SERVERS;
72
73 server_key= memcached_generate_hash(ptr, key, key_length);
74
75 if (no_reply)
76 {
77 if(cmd == PROTOCOL_BINARY_CMD_DECREMENT)
78 cmd= PROTOCOL_BINARY_CMD_DECREMENTQ;
79 if(cmd == PROTOCOL_BINARY_CMD_INCREMENT)
80 cmd= PROTOCOL_BINARY_CMD_INCREMENTQ;
81 }
82 protocol_binary_request_incr request= {.bytes= {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 if (no_reply)
102 return MEMCACHED_SUCCESS;
103 return memcached_response(&ptr->hosts[server_key], (char*)value, sizeof(*value), NULL);
104 }
105
106 memcached_return memcached_increment(memcached_st *ptr,
107 const char *key, size_t key_length,
108 uint32_t offset,
109 uint64_t *value)
110 {
111 memcached_return rc= memcached_validate_key_length(key_length, ptr->flags & MEM_BINARY_PROTOCOL);
112 unlikely (rc != MEMCACHED_SUCCESS)
113 return rc;
114
115 LIBMEMCACHED_MEMCACHED_INCREMENT_START();
116 if (ptr->flags & MEM_BINARY_PROTOCOL)
117 rc= binary_incr_decr(ptr, PROTOCOL_BINARY_CMD_INCREMENT, key,
118 key_length, offset, value);
119 else
120 rc= memcached_auto(ptr, "incr", key, key_length, offset, value);
121
122 LIBMEMCACHED_MEMCACHED_INCREMENT_END();
123
124 return rc;
125 }
126
127 memcached_return memcached_decrement(memcached_st *ptr,
128 const char *key, size_t key_length,
129 uint32_t offset,
130 uint64_t *value)
131 {
132 memcached_return rc= memcached_validate_key_length(key_length, ptr->flags & MEM_BINARY_PROTOCOL);
133 unlikely (rc != MEMCACHED_SUCCESS)
134 return rc;
135
136 LIBMEMCACHED_MEMCACHED_DECREMENT_START();
137 if (ptr->flags & MEM_BINARY_PROTOCOL)
138 rc= binary_incr_decr(ptr, PROTOCOL_BINARY_CMD_DECREMENT, key,
139 key_length, offset, value);
140 else
141 rc= memcached_auto(ptr, "decr", key, key_length, offset, value);
142
143 LIBMEMCACHED_MEMCACHED_DECREMENT_END();
144
145 return rc;
146 }