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/n1ix", __FILE__, __LINE__
20 
21 typedef struct {
22     opcookie_res header;
23 } opcookie_n1ix_drop_res;
24 
n1ix_drop_callback(lcb_t instance,int cbtype,const lcb_RESPN1XMGMT * resp)25 static void n1ix_drop_callback(lcb_t instance, int cbtype, const lcb_RESPN1XMGMT *resp)
26 {
27     opcookie_n1ix_drop_res *result = ecalloc(1, sizeof(opcookie_n1ix_drop_res));
28     TSRMLS_FETCH();
29 
30     result->header.err = resp->rc;
31     if (result->header.err != LCB_SUCCESS) {
32         pcbc_log(LOGARGS(instance, ERROR), "Failed to drop index. %d: %.*s", (int)resp->inner->htresp->htstatus,
33                  (int)resp->inner->nrow, (char *)resp->inner->row);
34     }
35 
36     opcookie_push((opcookie *)resp->cookie, &result->header);
37 }
38 
pcbc_n1ix_drop(pcbc_bucket_manager_t * manager,lcb_CMDN1XMGMT * cmd,zend_bool ignore_if_not_exist,zval * return_value TSRMLS_DC)39 void pcbc_n1ix_drop(pcbc_bucket_manager_t *manager, lcb_CMDN1XMGMT *cmd, zend_bool ignore_if_not_exist,
40                     zval *return_value TSRMLS_DC)
41 {
42     opcookie *cookie;
43     lcb_error_t err;
44 
45     cmd->callback = n1ix_drop_callback;
46     cookie = opcookie_init();
47     err = lcb_n1x_drop(manager->conn->lcb, cookie, cmd);
48     if (err == LCB_SUCCESS) {
49         lcb_wait(manager->conn->lcb);
50         err = opcookie_get_first_error(cookie);
51         if (err == LCB_KEY_ENOENT && ignore_if_not_exist) {
52             err = LCB_SUCCESS;
53         }
54     }
55     opcookie_destroy(cookie);
56     if (err != LCB_SUCCESS) {
57         throw_lcb_exception(err);
58     }
59 }
60