forked from couchbase/php-ext-couchbase
/
couchbase.c
151 lines (131 loc) · 4.31 KB
/
couchbase.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*
+----------------------------------------------------------------------+
| PHP Version 5 |
+----------------------------------------------------------------------+
| Copyright 2012 Couchbase, Inc. |
+----------------------------------------------------------------------+
| Licensed under the Apache License, Version 2.0 (the "License"); |
| you may not use this file except in compliance with the License. |
| You may obtain a copy of the License at |
| http://www.apache.org/licenses/LICENSE-2.0 |
| Unless required by applicable law or agreed to in writing, software |
| distributed under the License is distributed on an "AS IS" BASIS, |
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or |
| implied. See the License for the specific language governing |
| permissions and limitations under the License. |
+----------------------------------------------------------------------+
*/
#include "internal.h"
PHP_COUCHBASE_LOCAL
void pcbc_start_loop(struct _php_couchbase_res *res)
{
lcb_wait(res->handle);
}
PHP_COUCHBASE_LOCAL
void pcbc_stop_loop(struct _php_couchbase_res *res)
{
lcb_breakout(res->handle);
}
PHP_COUCHBASE_LOCAL
void php_couchbase_res_dtor(zend_rsrc_list_entry *rsrc TSRMLS_DC)
{
php_couchbase_res *couchbase_res = (php_couchbase_res *)rsrc->ptr;
if (couchbase_res) {
if (couchbase_res->handle) {
lcb_destroy(couchbase_res->handle);
}
free(couchbase_res->errinfo);
free(couchbase_res->bucket);
if (couchbase_res->prefix_key) {
free((void *)couchbase_res->prefix_key);
}
free(couchbase_res);
}
}
PHP_COUCHBASE_LOCAL
void php_couchbase_pres_dtor(zend_rsrc_list_entry *rsrc TSRMLS_DC)
{
php_couchbase_res *couchbase_res = (php_couchbase_res *)rsrc->ptr;
if (couchbase_res) {
if (couchbase_res->handle) {
lcb_destroy(couchbase_res->handle);
}
free(couchbase_res->errinfo);
free(couchbase_res->bucket);
if (couchbase_res->prefix_key) {
free((void *)couchbase_res->prefix_key);
}
free(couchbase_res);
}
}
/* callbacks */
static void php_couchbase_error_callback(lcb_t handle, lcb_error_t error, const char *errinfo)
{
php_couchbase_res *res = (php_couchbase_res *)lcb_get_cookie(handle);
if (res) {
free(res->errinfo);
if (errinfo) {
res->errinfo = strdup(errinfo);
}
}
/**
* @FIXME: when connect to a non-couchbase-server port (but the socket is valid)
* like a apache server, process will be hanged by event_loop
*/
if (res && res->seqno < 0) {
pcbc_stop_loop(res);
}
}
PHP_COUCHBASE_LOCAL
void php_couchbase_setup_callbacks(lcb_t handle)
{
php_couchbase_callbacks_arithmetic_init(handle);
php_couchbase_callbacks_get_init(handle);
php_couchbase_callbacks_store_init(handle);
php_couchbase_callbacks_remove_init(handle);
php_couchbase_callbacks_observe_init(handle);
php_couchbase_callbacks_view_init(handle);
php_couchbase_callbacks_stat_init(handle);
php_couchbase_callbacks_version_init(handle);
php_couchbase_callbacks_unlock_init(handle);
lcb_set_error_callback(handle, php_couchbase_error_callback);
}
PHP_COUCHBASE_LOCAL
int pcbc_check_expiry(INTERNAL_FUNCTION_PARAMETERS, int oo, long expiry, long *out)
{
if (expiry < 0) {
couchbase_report_error(INTERNAL_FUNCTION_PARAM_PASSTHRU, oo,
cb_illegal_arguments_exception,
"Expiry must not be negative (%ld given).",
expiry);
/* php_error(E_RECOVERABLE_ERROR, "Expiry must not be negative (%ld given).", expiry); */
return -1;
}
*out = (time_t)expiry;
return 0;
}
PHP_COUCHBASE_LOCAL
void php_couchbase_get_result_message_impl(INTERNAL_FUNCTION_PARAMETERS, int oo)
{
php_couchbase_res *res;
char *str;
int str_len;
int argflags = (oo ? PHP_COUCHBASE_ARG_F_OO : PHP_COUCHBASE_ARG_F_FUNCTIONAL)
| PHP_COUCHBASE_ARG_F_ONLYVALID;
PHP_COUCHBASE_GET_PARAMS(res, argflags, "");
if (res->errinfo) {
str_len = spprintf(&str, 0, "%s (%s)", lcb_strerror(res->handle, res->rc),
res->errinfo);
} else {
str_len = spprintf(&str, 0, "%s", lcb_strerror(res->handle, res->rc));
}
RETURN_STRINGL(str, str_len, 0);
}
/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* End:
* vim600: noet sw=4 ts=4 fdm=marker
* vim<600: noet sw=4 ts=4
*/