1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
5 * Copyright (C) 2011 Data Differential, http://datadifferential.com/
6 * Copyright (C) 2006-2009 Brian Aker All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * * Redistributions in binary form must reproduce the above
16 * copyright notice, this list of conditions and the following disclaimer
17 * in the documentation and/or other materials provided with the
20 * * The names of its contributors may not be used to endorse or
21 * promote products derived from this software without specific prior
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
27 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
28 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
30 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
34 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 #include <libmemcached/common.h>
39 #include <libmemcached/memcached/protocol_binary.h>
41 static memcached_return_t
ascii_touch(memcached_server_write_instance_st instance
,
42 const char *key
, size_t key_length
,
47 int buffer_length
= snprintf(buffer
, sizeof(buffer
), " %u", uint32_t(expiration
));
48 struct libmemcached_io_vector_st vector
[]=
50 { memcached_literal_param("touch ") },
51 { memcached_array_string(instance
->root
->_namespace
), memcached_array_size(instance
->root
->_namespace
) },
53 { buffer
, buffer_length
},
54 { memcached_literal_param("\r\n") }
57 memcached_return_t rc
;
58 if (memcached_failed(rc
= memcached_vdo(instance
, vector
, 5, true)))
60 memcached_io_reset(instance
);
61 return memcached_set_error(*instance
, MEMCACHED_WRITE_FAILURE
, MEMCACHED_AT
);
67 static memcached_return_t
binary_touch(memcached_server_write_instance_st instance
,
68 const char *key
, size_t key_length
,
71 protocol_binary_request_touch request
= {}; //{.bytes= {0}};
72 request
.message
.header
.request
.magic
= PROTOCOL_BINARY_REQ
;
73 request
.message
.header
.request
.opcode
= PROTOCOL_BINARY_CMD_TOUCH
;
74 request
.message
.header
.request
.extlen
= 4;
75 request
.message
.header
.request
.keylen
= htons((uint16_t)(key_length
+memcached_array_size(instance
->root
->_namespace
)));
76 request
.message
.header
.request
.datatype
= PROTOCOL_BINARY_RAW_BYTES
;
77 request
.message
.header
.request
.bodylen
= htonl((uint32_t)(key_length
+memcached_array_size(instance
->root
->_namespace
) +request
.message
.header
.request
.extlen
));
78 request
.message
.body
.expiration
= htonl((uint32_t) expiration
);
80 struct libmemcached_io_vector_st vector
[]=
82 { request
.bytes
, sizeof(request
.bytes
) },
83 { memcached_array_string(instance
->root
->_namespace
), memcached_array_size(instance
->root
->_namespace
) },
87 memcached_return_t rc
;
88 if (memcached_failed(rc
= memcached_vdo(instance
, vector
, 3, true)))
90 memcached_io_reset(instance
);
91 return memcached_set_error(*instance
, MEMCACHED_WRITE_FAILURE
, MEMCACHED_AT
);
97 memcached_return_t
memcached_touch(memcached_st
*ptr
,
98 const char *key
, size_t key_length
,
101 return memcached_touch_by_key(ptr
, key
, key_length
, key
, key_length
, expiration
);
104 memcached_return_t
memcached_touch_by_key(memcached_st
*ptr
,
105 const char *group_key
, size_t group_key_length
,
106 const char *key
, size_t key_length
,
109 LIBMEMCACHED_MEMCACHED_TOUCH_START();
111 memcached_return_t rc
;
112 if (memcached_failed(rc
= initialize_query(ptr
)))
117 if (memcached_failed(rc
= memcached_validate_key_length(key_length
, ptr
->flags
.binary_protocol
)))
119 return memcached_set_error(*ptr
, rc
, MEMCACHED_AT
);
122 uint32_t server_key
= memcached_generate_hash_with_redistribution(ptr
, group_key
, group_key_length
);
123 memcached_server_write_instance_st instance
= memcached_server_instance_fetch(ptr
, server_key
);
125 if (ptr
->flags
.binary_protocol
)
127 rc
= binary_touch(instance
, key
, key_length
, expiration
);
131 rc
= ascii_touch(instance
, key
, key_length
, expiration
);
134 if (memcached_failed(rc
))
136 return memcached_set_error(*instance
, rc
, MEMCACHED_AT
, memcached_literal_param("Error occcured while writing touch command to server"));
139 char buffer
[MEMCACHED_DEFAULT_COMMAND_SIZE
];
140 rc
= memcached_read_one_response(instance
, buffer
, sizeof(buffer
), NULL
);
142 if (rc
== MEMCACHED_SUCCESS
or rc
== MEMCACHED_NOTFOUND
)
147 return memcached_set_error(*instance
, rc
, MEMCACHED_AT
, memcached_literal_param("Error occcured while reading response"));