Updating for 1.0.2 release
[awesomized/libmemcached] / libmemcached / exist.cc
1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
2 *
3 * Libmemcached library
4 *
5 * Copyright (C) 2011 Data Differential, http://datadifferential.com/
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are
9 * met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 *
14 * * Redistributions in binary form must reproduce the above
15 * copyright notice, this list of conditions and the following disclaimer
16 * in the documentation and/or other materials provided with the
17 * distribution.
18 *
19 * * The names of its contributors may not be used to endorse or
20 * promote products derived from this software without specific prior
21 * written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
29 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
33 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 *
35 */
36
37 #include <libmemcached/common.h>
38
39 static memcached_return_t ascii_exist(memcached_st *memc, memcached_server_write_instance_st instance, const char *key, size_t key_length)
40 {
41 struct libmemcached_io_vector_st vector[]=
42 {
43 { memcached_literal_param("add ") },
44 { memcached_array_string(memc->_namespace), memcached_array_size(memc->_namespace) },
45 { key, key_length },
46 { memcached_literal_param(" 0") },
47 { memcached_literal_param(" 2678400") },
48 { memcached_literal_param(" 0") },
49 { memcached_literal_param("\r\n") },
50 { memcached_literal_param("\r\n") }
51 };
52
53 /* Send command header */
54 memcached_return_t rc= memcached_vdo(instance, vector, 8, true);
55 if (rc == MEMCACHED_SUCCESS)
56 {
57 char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE];
58 rc= memcached_response(instance, buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, NULL);
59
60 if (rc == MEMCACHED_NOTSTORED)
61 rc= MEMCACHED_SUCCESS;
62
63 if (rc == MEMCACHED_STORED)
64 rc= MEMCACHED_NOTFOUND;
65 }
66
67 if (rc == MEMCACHED_WRITE_FAILURE)
68 memcached_io_reset(instance);
69
70 return rc;
71 }
72
73 static memcached_return_t binary_exist(memcached_st *memc, memcached_server_write_instance_st instance, const char *key, size_t key_length)
74 {
75 protocol_binary_request_set request= {};
76 size_t send_length= sizeof(request.bytes);
77
78 request.message.header.request.magic= PROTOCOL_BINARY_REQ;
79 request.message.header.request.opcode= PROTOCOL_BINARY_CMD_ADD;
80 request.message.header.request.keylen= htons((uint16_t)(key_length + memcached_array_size(memc->_namespace)));
81 request.message.header.request.datatype= PROTOCOL_BINARY_RAW_BYTES;
82 request.message.header.request.extlen= 8;
83 request.message.body.flags= 0;
84 request.message.body.expiration= htonl(2678400);
85
86 request.message.header.request.bodylen= htonl((uint32_t) (key_length
87 +memcached_array_size(memc->_namespace)
88 +request.message.header.request.extlen));
89
90 struct libmemcached_io_vector_st vector[]=
91 {
92 { request.bytes, send_length },
93 { memcached_array_string(memc->_namespace), memcached_array_size(memc->_namespace) },
94 { key, key_length }
95 };
96
97 /* write the header */
98 memcached_return_t rc;
99 if ((rc= memcached_vdo(instance, vector, 3, true)) != MEMCACHED_SUCCESS)
100 {
101 memcached_io_reset(instance);
102 return (rc == MEMCACHED_SUCCESS) ? MEMCACHED_WRITE_FAILURE : rc;
103 }
104
105 rc= memcached_response(instance, NULL, 0, NULL);
106
107 if (rc == MEMCACHED_SUCCESS)
108 rc= MEMCACHED_NOTFOUND;
109
110 if (rc == MEMCACHED_DATA_EXISTS)
111 rc= MEMCACHED_SUCCESS;
112
113 return rc;
114 }
115
116 memcached_return_t memcached_exist(memcached_st *memc, const char *key, size_t key_length)
117 {
118 return memcached_exist_by_key(memc, key, key_length, key, key_length);
119 }
120
121 memcached_return_t memcached_exist_by_key(memcached_st *memc,
122 const char *group_key, size_t group_key_length,
123 const char *key, size_t key_length)
124 {
125 memcached_return_t rc;
126 if (memcached_failed(rc= initialize_query(memc)))
127 {
128 return rc;
129 }
130
131 if (memc->flags.use_udp)
132 {
133 return MEMCACHED_NOT_SUPPORTED;
134 }
135
136
137 uint32_t server_key= memcached_generate_hash_with_redistribution(memc, group_key, group_key_length);
138 memcached_server_write_instance_st instance;
139 instance= memcached_server_instance_fetch(memc, server_key);
140
141 if (memc->flags.binary_protocol)
142 {
143 return binary_exist(memc, instance, key, key_length);
144 }
145 else
146 {
147 return ascii_exist(memc, instance, key, key_length);
148 }
149 }