1 /* -*- Mode: C; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /*
3  *     Copyright 2010-2012 Couchbase, Inc.
4  *
5  *   Licensed under the Apache License, Version 2.0 (the "License");
6  *   you may not use this file except in compliance with the License.
7  *   You may obtain a copy of the License at
8  *
9  *       http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *   Unless required by applicable law or agreed to in writing, software
12  *   distributed under the License is distributed on an "AS IS" BASIS,
13  *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *   See the License for the specific language governing permissions and
15  *   limitations under the License.
16  */
17 
18 #include "internal.h"
19 #include "trace.h"
20 
21 LIBCOUCHBASE_API
22 lcb_error_t
lcb_touch3(lcb_t instance,const void * cookie,const lcb_CMDTOUCH * cmd)23 lcb_touch3(lcb_t instance, const void *cookie, const lcb_CMDTOUCH *cmd)
24 {
25     protocol_binary_request_touch tcmd;
26     protocol_binary_request_header *hdr = &tcmd.message.header;
27     mc_PIPELINE *pl;
28     mc_PACKET *pkt;
29     lcb_error_t err;
30 
31     if (LCB_KEYBUF_IS_EMPTY(&cmd->key)) {
32         return LCB_EMPTY_KEY;
33     }
34 
35     err = mcreq_basic_packet(&instance->cmdq, cmd, hdr, 4, &pkt, &pl,
36         MCREQ_BASICPACKET_F_FALLBACKOK);
37     if (err != LCB_SUCCESS) {
38         return err;
39     }
40 
41     hdr->request.magic = PROTOCOL_BINARY_REQ;
42     hdr->request.opcode = PROTOCOL_BINARY_CMD_TOUCH;
43     hdr->request.cas = 0;
44     hdr->request.datatype = PROTOCOL_BINARY_RAW_BYTES;
45     hdr->request.opaque = pkt->opaque;
46     hdr->request.bodylen = htonl(4 + ntohs(hdr->request.keylen));
47     tcmd.message.body.expiration = htonl(cmd->exptime);
48     memcpy(SPAN_BUFFER(&pkt->kh_span), tcmd.bytes, sizeof(tcmd.bytes));
49     pkt->u_rdata.reqdata.cookie = cookie;
50     pkt->u_rdata.reqdata.start = gethrtime();
51     LCB_SCHED_ADD(instance, pl, pkt);
52     LCBTRACE_KV_START(instance->settings, cmd, LCBTRACE_OP_TOUCH, pkt->opaque, pkt->u_rdata.reqdata.span);
53     TRACE_TOUCH_BEGIN(instance, hdr, cmd);
54     return LCB_SUCCESS;
55 }
56 
57 LIBCOUCHBASE_API
58 lcb_error_t
lcb_touch(lcb_t instance,const void * cookie,lcb_size_t num,const lcb_touch_cmd_t * const * items)59 lcb_touch(lcb_t instance, const void *cookie, lcb_size_t num,
60           const lcb_touch_cmd_t * const * items)
61 {
62     unsigned ii;
63 
64     lcb_sched_enter(instance);
65     for (ii = 0; ii < num; ii++) {
66         const lcb_touch_cmd_t *src = items[ii];
67         lcb_CMDTOUCH dst;
68         lcb_error_t err;
69 
70         memset(&dst, 0, sizeof(dst));
71         dst.key.contig.bytes = src->v.v0.key;
72         dst.key.contig.nbytes = src->v.v0.nkey;
73         dst._hashkey.contig.bytes = src->v.v0.hashkey;
74         dst._hashkey.contig.nbytes = src->v.v0.nhashkey;
75         dst.exptime = src->v.v0.exptime;
76         err = lcb_touch3(instance, cookie, &dst);
77         if (err != LCB_SUCCESS) {
78             lcb_sched_fail(instance);
79             return err;
80         }
81     }
82     lcb_sched_leave(instance);
83     SYNCMODE_INTERCEPT(instance)
84 }
85