forked from couchbase/php-couchbase
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcouchbase.c
316 lines (268 loc) · 9.21 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
/**
* Copyright 2016 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 "couchbase.h"
#include "cas.h"
#include "metadoc.h"
#include "docfrag.h"
#include "token.h"
#include "n1ix_spec.h"
#include "phpstubstr.h"
#include "zap.h"
#include "ext/standard/info.h"
#ifdef HAVE_FASTLZ_H
#include <fastlz.h>
#else
#include "fastlz/fastlz.h"
#endif
#if HAVE_ZLIB
#include <zlib.h>
#endif
typedef unsigned char uint8_t;
typedef unsigned int uint32_t;
ZEND_DECLARE_MODULE_GLOBALS(couchbase)
#define PCBC_LONG_CONSTANT(key, val) \
REGISTER_LONG_CONSTANT("COUCHBASE_"key, val, CONST_CS | CONST_PERSISTENT)
#define PCBC_REGISTER_CONST(c) \
REGISTER_LONG_CONSTANT("COUCHBASE_"#c, c, CONST_CS | CONST_PERSISTENT)
#define PCBC_REGISTER_LCBCONST(c) \
REGISTER_LONG_CONSTANT("COUCHBASE_"#c, LCB_##c, CONST_CS | CONST_PERSISTENT)
static void php_extname_init_globals(zend_couchbase_globals *couchbase_globals)
{
couchbase_globals->first_bconn = NULL;
couchbase_globals->last_bconn = NULL;
}
PHP_MINIT_FUNCTION(couchbase)
{
ZEND_INIT_MODULE_GLOBALS(couchbase, php_extname_init_globals, NULL);
couchbase_init_exceptions(INIT_FUNC_ARGS_PASSTHRU);
couchbase_init_metadoc(INIT_FUNC_ARGS_PASSTHRU);
couchbase_init_docfrag(INIT_FUNC_ARGS_PASSTHRU);
couchbase_init_n1ix_spec(INIT_FUNC_ARGS_PASSTHRU);
couchbase_init_cluster(INIT_FUNC_ARGS_PASSTHRU);
couchbase_init_bucket(INIT_FUNC_ARGS_PASSTHRU);
couchbase_init_token(INIT_FUNC_ARGS_PASSTHRU);
PCBC_REGISTER_CONST(PERSISTTO_MASTER);
PCBC_REGISTER_CONST(PERSISTTO_ONE);
PCBC_REGISTER_CONST(PERSISTTO_TWO);
PCBC_REGISTER_CONST(PERSISTTO_THREE);
PCBC_REGISTER_CONST(REPLICATETO_ONE);
PCBC_REGISTER_CONST(REPLICATETO_TWO);
PCBC_REGISTER_CONST(REPLICATETO_THREE);
PCBC_REGISTER_LCBCONST(SDCMD_GET);
PCBC_REGISTER_LCBCONST(SDCMD_REPLACE);
PCBC_REGISTER_LCBCONST(SDCMD_EXISTS);
PCBC_REGISTER_LCBCONST(SDCMD_DICT_ADD);
PCBC_REGISTER_LCBCONST(SDCMD_DICT_UPSERT);
PCBC_REGISTER_LCBCONST(SDCMD_ARRAY_ADD_FIRST);
PCBC_REGISTER_LCBCONST(SDCMD_ARRAY_ADD_LAST);
PCBC_REGISTER_LCBCONST(SDCMD_ARRAY_INSERT);
PCBC_REGISTER_LCBCONST(SDCMD_ARRAY_ADD_UNIQUE);
PCBC_REGISTER_LCBCONST(SDCMD_COUNTER);
PCBC_REGISTER_LCBCONST(SDCMD_REMOVE);
PCBC_REGISTER_LCBCONST(CNTL_OP_TIMEOUT);
PCBC_REGISTER_LCBCONST(CNTL_VIEW_TIMEOUT);
PCBC_REGISTER_LCBCONST(CNTL_DURABILITY_INTERVAL);
PCBC_REGISTER_LCBCONST(CNTL_DURABILITY_TIMEOUT);
PCBC_REGISTER_LCBCONST(CNTL_HTTP_TIMEOUT);
PCBC_REGISTER_LCBCONST(CNTL_CONFIGURATION_TIMEOUT);
PCBC_REGISTER_LCBCONST(CNTL_CONFDELAY_THRESH);
PCBC_REGISTER_LCBCONST(CNTL_CONFIG_NODE_TIMEOUT);
PCBC_REGISTER_LCBCONST(CNTL_HTCONFIG_IDLE_TIMEOUT);
PCBC_LONG_CONSTANT("VALUE_F_JSON", LCB_VALUE_F_JSON);
PCBC_REGISTER_LCBCONST(N1XSPEC_T_DEFAULT);
PCBC_REGISTER_LCBCONST(N1XSPEC_T_GSI);
PCBC_REGISTER_LCBCONST(N1XSPEC_T_VIEW);
{
char buf[128];
#if PHP_VERSION_ID >= 70000
#define PCBC_CONST_LENGTH(buf) (strlen(buf))
#else
#define PCBC_CONST_LENGTH(buf) (strlen(buf) + 1)
#endif
#define X(name, value, cls, s) \
{ \
ap_php_snprintf(buf, sizeof(buf), "COUCHBASE_%s", #name + 4); \
zend_register_long_constant(buf, PCBC_CONST_LENGTH(buf), value, \
CONST_CS | CONST_PERSISTENT, \
module_number TSRMLS_CC); \
}
LCB_XERR(X)
#undef X
#undef PCBC_CONST_LENGTH
}
// TODO: Maybe not have these?
PCBC_LONG_CONSTANT("TMPFAIL", LCB_ETMPFAIL);
PCBC_LONG_CONSTANT("KEYALREADYEXISTS", LCB_KEY_EEXISTS);
PCBC_LONG_CONSTANT("KEYNOTFOUND", LCB_KEY_ENOENT);
return SUCCESS;
}
PHP_MSHUTDOWN_FUNCTION(couchbase)
{
couchbase_shutdown_bucket(SHUTDOWN_FUNC_ARGS_PASSTHRU);
return SUCCESS;
}
PHP_RSHUTDOWN_FUNCTION(couchbase)
{
if (PG(last_error_message) && PG(last_error_type) == E_ERROR) {
couchbase_shutdown_bucket(SHUTDOWN_FUNC_ARGS_PASSTHRU);
}
return SUCCESS;
}
PHP_RINIT_FUNCTION(couchbase)
{
int stub_idx;
for (stub_idx = 0; stub_idx < sizeof(PCBC_PHP_CODESTR) / sizeof(pcbc_stub_data); ++stub_idx) {
pcbc_stub_data *this_stub = &PCBC_PHP_CODESTR[stub_idx];
int retval = zend_eval_string((char*)this_stub->data, NULL, (char*)this_stub->filename TSRMLS_CC);
if (retval != SUCCESS) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to inject Couchbase stub: %s.", (char *)this_stub->filename);
return FAILURE;
}
}
return SUCCESS;
}
PHP_FUNCTION(couchbase_zlib_compress)
{
#if HAVE_ZLIB
zval *zdata;
void *dataIn, *dataOut;
unsigned long dataSize, dataOutSize;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
"z", &zdata) == FAILURE) {
RETURN_NULL();
}
dataIn = Z_STRVAL_P(zdata);
dataSize = Z_STRLEN_P(zdata);
dataOutSize = compressBound(dataSize);
dataOut = emalloc(dataOutSize);
compress((uint8_t*)dataOut + 4, &dataOutSize, dataIn, dataSize);
*(uint32_t*)dataOut = dataSize;
zap_zval_stringl_p(return_value, dataOut, 4 + dataOutSize);
efree(dataOut);
#else
zend_throw_exception(NULL, "The zlib library was not available when the couchbase extension was built.", 0 TSRMLS_CC);
#endif
}
PHP_FUNCTION(couchbase_zlib_decompress)
{
#if HAVE_ZLIB
zval *zdata;
void *dataIn, *dataOut;
unsigned long dataSize, dataOutSize;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
"z", &zdata) == FAILURE) {
RETURN_NULL();
}
dataIn = Z_STRVAL_P(zdata);
dataSize = Z_STRLEN_P(zdata);
dataOutSize = *(uint32_t*)dataIn;
dataOut = emalloc(dataOutSize);
uncompress(dataOut, &dataOutSize, (uint8_t*)dataIn + 4, dataSize - 4);
zap_zval_stringl_p(return_value, dataOut, dataOutSize);
efree(dataOut);
#else
zend_throw_exception(NULL, "The zlib library was not available when the couchbase extension was built.", 0 TSRMLS_CC);
#endif
}
PHP_FUNCTION(couchbase_fastlz_compress)
{
zval *zdata;
void *dataIn, *dataOut;
unsigned long dataSize, dataOutSize;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
"z", &zdata) == FAILURE) {
RETURN_NULL();
}
dataIn = Z_STRVAL_P(zdata);
dataSize = Z_STRLEN_P(zdata);
dataOutSize = 4 + (dataSize * 1.05);
dataOut = emalloc(dataOutSize);
dataOutSize = fastlz_compress(dataIn, dataSize, (uint8_t*)dataOut + 4);
*(uint32_t*)dataOut = dataSize;
zap_zval_stringl_p(return_value, dataOut, 4 + dataOutSize);
efree(dataOut);
}
PHP_FUNCTION(couchbase_fastlz_decompress)
{
zval *zdata;
void *dataIn, *dataOut;
unsigned long dataSize, dataOutSize;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
"z", &zdata) == FAILURE) {
RETURN_NULL();
}
dataIn = Z_STRVAL_P(zdata);
dataSize = Z_STRLEN_P(zdata);
dataOutSize = *(uint32_t*)dataIn;
dataOut = emalloc(dataOutSize);
dataOutSize = fastlz_decompress(
(uint8_t*)dataIn + 4, dataSize - 4, dataOut, dataOutSize);
zap_zval_stringl_p(return_value, dataOut, dataOutSize);
efree(dataOut);
}
static PHP_MINFO_FUNCTION(couchbase)
{
char buf[128];
const char *changeset;
lcb_error_t err;
err = lcb_cntl(NULL, LCB_CNTL_GET, LCB_CNTL_CHANGESET, (void*)&changeset);
if (err != LCB_SUCCESS) {
changeset = "UNKNOWN";
}
ap_php_snprintf(buf, sizeof(buf), "%s (git: %s)", lcb_get_version(NULL), changeset);
php_info_print_table_start();
php_info_print_table_row(2, "couchbase support", "enabled");
php_info_print_table_row(2, "extension version", PHP_COUCHBASE_VERSION);
php_info_print_table_row(2, "libcouchbase runtime version", buf);
php_info_print_table_row(2, "libcouchbase headers version", LCB_VERSION_STRING " (git: " LCB_VERSION_CHANGESET ")");
php_info_print_table_end();
}
static zend_function_entry couchbase_functions[] = {
PHP_FE(couchbase_fastlz_compress, NULL)
PHP_FE(couchbase_fastlz_decompress, NULL)
PHP_FE(couchbase_zlib_compress, NULL)
PHP_FE(couchbase_zlib_decompress, NULL)
{NULL, NULL, NULL}
};
#if ZEND_MODULE_API_NO >= 20050617
static zend_module_dep php_couchbase_deps[] = {
ZEND_MOD_REQUIRED("json")
{NULL,NULL,NULL}
};
#endif
zend_module_entry couchbase_module_entry = {
#if ZEND_MODULE_API_NO >= 20050617
STANDARD_MODULE_HEADER_EX,
NULL,
php_couchbase_deps,
#elif ZEND_MODULE_API_NO >= 20010901
STANDARD_MODULE_HEADER,
#endif
PHP_COUCHBASE_EXTNAME,
couchbase_functions,
PHP_MINIT(couchbase),
PHP_MSHUTDOWN(couchbase),
PHP_RINIT(couchbase),
PHP_RSHUTDOWN(couchbase),
PHP_MINFO(couchbase),
#if ZEND_MODULE_API_NO >= 20010901
PHP_COUCHBASE_VERSION,
#endif
STANDARD_MODULE_PROPERTIES
};
#ifdef COMPILE_DL_COUCHBASE
ZEND_GET_MODULE(couchbase)
#endif