1 /**
2 * Copyright 2016-2017 Couchbase, Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "couchbase.h"
18
19 #define LOGARGS(instance, lvl) LCB_LOG_##lvl, instance, "pcbc/remove", __FILE__, __LINE__
20
remove_callback(lcb_t instance,int cbtype,const lcb_RESPREMOVE * resp)21 void remove_callback(lcb_t instance, int cbtype, const lcb_RESPREMOVE *resp)
22 {
23 opcookie_store_res *result = ecalloc(1, sizeof(opcookie_store_res));
24 const lcb_MUTATION_TOKEN *mutinfo;
25 TSRMLS_FETCH();
26
27 PCBC_RESP_ERR_COPY(result->header, cbtype, resp);
28 result->key_len = resp->nkey;
29 if (resp->nkey) {
30 result->key = estrndup(resp->key, resp->nkey);
31 }
32 result->cas = resp->cas;
33 mutinfo = lcb_resp_get_mutation_token(cbtype, resp);
34 if (mutinfo) {
35 memcpy(&result->token, mutinfo, sizeof(result->token));
36 }
37
38 opcookie_push((opcookie *)resp->cookie, &result->header);
39 }
40
41 // remove($id {, $cas, $groupid}) : MetaDoc
PHP_METHOD(Bucket,remove)42 PHP_METHOD(Bucket, remove)
43 {
44 pcbc_bucket_t *obj = Z_BUCKET_OBJ_P(getThis());
45 int ii, ncmds, nscheduled;
46 pcbc_pp_state pp_state;
47 pcbc_pp_id id;
48 opcookie *cookie;
49 zval *zcas, *zgroupid;
50 lcb_error_t err;
51 #ifdef LCB_TRACING
52 lcbtrace_TRACER *tracer = NULL;
53 #endif
54
55 // Note that groupid is experimental here and should not be used.
56 if (pcbc_pp_begin(ZEND_NUM_ARGS() TSRMLS_CC, &pp_state, "id||cas,groupid", &id, &zcas, &zgroupid) != SUCCESS) {
57 throw_pcbc_exception("Invalid arguments.", LCB_EINVAL);
58 RETURN_NULL();
59 }
60
61 ncmds = pcbc_pp_keycount(&pp_state);
62 cookie = opcookie_init();
63 #ifdef LCB_TRACING
64 tracer = lcb_get_tracer(obj->conn->lcb);
65 if (tracer) {
66 cookie->span = lcbtrace_span_start(tracer, "php/" LCBTRACE_OP_REMOVE, 0, NULL);
67 lcbtrace_span_add_tag_str(cookie->span, LCBTRACE_TAG_COMPONENT, pcbc_client_string);
68 lcbtrace_span_add_tag_str(cookie->span, LCBTRACE_TAG_SERVICE, LCBTRACE_TAG_SERVICE_KV);
69 }
70 #endif
71
72 nscheduled = 0;
73 for (ii = 0; pcbc_pp_next(&pp_state); ++ii) {
74 lcb_CMDREMOVE cmd = {0};
75
76 PCBC_CHECK_ZVAL_STRING(zcas, "cas must be a string");
77 PCBC_CHECK_ZVAL_STRING(zgroupid, "groupid must be a string");
78
79 LCB_CMD_SET_KEY(&cmd, id.str, id.len);
80 if (zcas) {
81 cmd.cas = pcbc_cas_decode(zcas TSRMLS_CC);
82 }
83 if (zgroupid) {
84 LCB_KREQ_SIMPLE(&cmd._hashkey, Z_STRVAL_P(zgroupid), Z_STRLEN_P(zgroupid));
85 }
86
87 #ifdef LCB_TRACING
88 if (cookie->span) {
89 LCB_CMD_SET_TRACESPAN(&cmd, cookie->span);
90 }
91 #endif
92 err = lcb_remove3(obj->conn->lcb, cookie, &cmd);
93 if (err != LCB_SUCCESS) {
94 break;
95 }
96 nscheduled++;
97 }
98 pcbc_assert_number_of_commands(obj->conn->lcb, "remove", nscheduled, ncmds, err);
99
100 if (nscheduled) {
101 lcb_wait(obj->conn->lcb);
102
103 err = proc_remove_results(obj, return_value, cookie, pcbc_pp_ismapped(&pp_state) TSRMLS_CC);
104 }
105
106 #ifdef LCB_TRACING
107 if (cookie->span) {
108 lcbtrace_span_finish(cookie->span, LCBTRACE_NOW);
109 }
110 #endif
111 opcookie_destroy(cookie);
112
113 if (err != LCB_SUCCESS) {
114 throw_lcb_exception(err);
115 }
116 }
117