1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
5 * Copyright (C) 2011 Data Differential, http://datadifferential.com/
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
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
19 * * The names of its contributors may not be used to endorse or
20 * promote products derived from this software without specific prior
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.
37 #include <libmemcached/common.h>
39 static memcached_return_t
ascii_exist(memcached_st
*memc
, memcached_server_write_instance_st instance
, const char *key
, size_t key_length
)
41 libmemcached_io_vector_st vector
[]=
44 { memcached_literal_param("add ") },
45 { memcached_array_string(memc
->_namespace
), memcached_array_size(memc
->_namespace
) },
47 { memcached_literal_param(" 0") },
48 { memcached_literal_param(" 2678400") },
49 { memcached_literal_param(" 0") },
50 { memcached_literal_param("\r\n") },
51 { memcached_literal_param("\r\n") }
54 /* Send command header */
55 memcached_return_t rc
;
56 if (memcached_fatal(rc
= memcached_vdo(instance
, vector
, 9, true)))
61 char buffer
[MEMCACHED_DEFAULT_COMMAND_SIZE
];
62 rc
= memcached_response(instance
, buffer
, MEMCACHED_DEFAULT_COMMAND_SIZE
, NULL
);
64 if (rc
== MEMCACHED_NOTSTORED
)
66 rc
= MEMCACHED_SUCCESS
;
69 if (rc
== MEMCACHED_STORED
)
71 rc
= MEMCACHED_NOTFOUND
;
77 static memcached_return_t
binary_exist(memcached_st
*memc
, memcached_server_write_instance_st instance
, const char *key
, size_t key_length
)
79 protocol_binary_request_set request
= {};
80 size_t send_length
= sizeof(request
.bytes
);
82 request
.message
.header
.request
.magic
= PROTOCOL_BINARY_REQ
;
83 request
.message
.header
.request
.opcode
= PROTOCOL_BINARY_CMD_ADD
;
84 request
.message
.header
.request
.keylen
= htons((uint16_t)(key_length
+ memcached_array_size(memc
->_namespace
)));
85 request
.message
.header
.request
.datatype
= PROTOCOL_BINARY_RAW_BYTES
;
86 request
.message
.header
.request
.extlen
= 8;
87 request
.message
.body
.flags
= 0;
88 request
.message
.body
.expiration
= htonl(2678400);
90 request
.message
.header
.request
.bodylen
= htonl((uint32_t) (key_length
91 +memcached_array_size(memc
->_namespace
)
92 +request
.message
.header
.request
.extlen
));
94 libmemcached_io_vector_st vector
[]=
97 { request
.bytes
, send_length
},
98 { memcached_array_string(memc
->_namespace
), memcached_array_size(memc
->_namespace
) },
102 /* write the header */
103 memcached_return_t rc
;
104 if (memcached_fatal(rc
= memcached_vdo(instance
, vector
, 4, true)))
109 rc
= memcached_response(instance
, NULL
, 0, NULL
);
111 if (rc
== MEMCACHED_SUCCESS
)
113 rc
= MEMCACHED_NOTFOUND
;
116 if (rc
== MEMCACHED_DATA_EXISTS
)
118 rc
= MEMCACHED_SUCCESS
;
124 memcached_return_t
memcached_exist(memcached_st
*memc
, const char *key
, size_t key_length
)
126 return memcached_exist_by_key(memc
, key
, key_length
, key
, key_length
);
129 memcached_return_t
memcached_exist_by_key(memcached_st
*memc
,
130 const char *group_key
, size_t group_key_length
,
131 const char *key
, size_t key_length
)
133 memcached_return_t rc
;
134 if (memcached_failed(rc
= initialize_query(memc
, true)))
139 if (memcached_is_udp(memc
))
141 return memcached_set_error(*memc
, MEMCACHED_NOT_SUPPORTED
, MEMCACHED_AT
);
144 uint32_t server_key
= memcached_generate_hash_with_redistribution(memc
, group_key
, group_key_length
);
145 memcached_server_write_instance_st instance
= memcached_server_instance_fetch(memc
, server_key
);
147 if (memcached_is_binary(memc
))
149 rc
= binary_exist(memc
, instance
, key
, key_length
);
153 rc
= ascii_exist(memc
, instance
, key
, key_length
);
156 if (memcached_fatal(rc
))
158 memcached_io_reset(instance
);