forked from brett19/php-couchbase
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cluster.c
262 lines (216 loc) · 6.26 KB
/
cluster.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
#include <libcouchbase/couchbase.h>
#include "couchbase.h"
#include "phphelpers.h"
#include "exception.h"
#include "cluster.h"
#include "metadoc.h"
zend_object_handlers cluster_handlers;
void cluster_free_storage(void *object TSRMLS_DC)
{
cluster_object *obj = (cluster_object *)object;
zend_hash_destroy(obj->std.properties);
FREE_HASHTABLE(obj->std.properties);
efree(obj);
}
zend_object_value cluster_create_handler(zend_class_entry *type TSRMLS_DC)
{
zend_object_value retval;
cluster_object *obj = (cluster_object *)emalloc(sizeof(cluster_object));
memset(obj, 0, sizeof(cluster_object));
obj->std.ce = type;
ALLOC_HASHTABLE(obj->std.properties);
zend_hash_init(obj->std.properties, 0, NULL, ZVAL_PTR_DTOR, 0);
phlp_object_properties_init(&obj->std, type);
retval.handle = zend_objects_store_put(obj, NULL,
cluster_free_storage, NULL TSRMLS_CC);
retval.handlers = &cluster_handlers;
return retval;
}
typedef struct {
zval *retval;
cluster_object *owner;
} copcookie;
copcookie * copcookie_init(cluster_object *clusterobj, zval *return_value) {
copcookie *cookie = emalloc(sizeof(copcookie));
cookie->owner = clusterobj;
cookie->retval = return_value;
ZVAL_NULL(cookie->retval);
return cookie;
}
void ccookie_error(const copcookie *cookie, cluster_object *data, zval *doc,
lcb_error_t error TSRMLS_DC) {
zval *zerror = create_lcb_exception(error TSRMLS_CC);
if (Z_TYPE_P(cookie->retval) == IS_ARRAY) {
metadoc_from_error(doc, zerror TSRMLS_CC);
} else {
data->error = zerror;
}
}
static void http_complete_callback(lcb_http_request_t request, lcb_t instance,
const void *cookie, lcb_error_t error,
const lcb_http_resp_t *resp) {
cluster_object *data = (cluster_object*)lcb_get_cookie(instance);
zval *doc = ((copcookie*)cookie)->retval;
TSRMLS_FETCH();
if (error == LCB_SUCCESS) {
ZVAL_STRINGL(doc, resp->v.v0.bytes, resp->v.v0.nbytes, 1);
} else {
ccookie_error(cookie, data, NULL, error TSRMLS_CC);
}
}
static int pcbc_wait(cluster_object *obj TSRMLS_DC)
{
lcb_t instance = obj->lcb;
obj->error = NULL;
lcb_wait(instance);
if (obj->error) {
zend_throw_exception_object(obj->error TSRMLS_CC);
obj->error = NULL;
return 0;
}
return 1;
}
zend_class_entry *cluster_ce;
PHP_METHOD(Cluster, __construct)
{
cluster_object *data = PHP_THISOBJ();
zval *zdsn = NULL;
zval *zname = NULL;
zval *zpassword = NULL;
char *dsn = NULL;
char *name = NULL;
char *password = NULL;
lcb_error_t err;
lcb_t instance;
struct lcb_create_st create_options;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|zzz",
&zdsn, &zname, &zpassword) == FAILURE) {
RETURN_NULL();
}
if (zdsn) {
if (Z_TYPE_P(zdsn) == IS_STRING) {
dsn = estrndup(Z_STRVAL_P(zdsn), Z_STRLEN_P(zdsn));
} else {
throw_pcbc_exception("Expected dsn as string", LCB_EINVAL);
RETURN_NULL();
}
}
if (zname) {
if (Z_TYPE_P(zname) == IS_STRING) {
name = estrndup(Z_STRVAL_P(zname), Z_STRLEN_P(zname));
} else {
throw_pcbc_exception("Expected bucket name as string", LCB_EINVAL);
if (dsn) efree(dsn);
RETURN_NULL();
}
}
if (zpassword) {
if (Z_TYPE_P(zpassword) == IS_STRING) {
password = estrndup(Z_STRVAL_P(zpassword), Z_STRLEN_P(zpassword));
} else {
throw_pcbc_exception("Expected bucket password as string", LCB_EINVAL);
if (dsn) efree(dsn);
if (name) efree(name);
RETURN_NULL();
}
}
memset(&create_options, 0, sizeof(create_options));
create_options.version = 3;
create_options.v.v3.connstr = dsn;
create_options.v.v3.username = name;
create_options.v.v3.passwd = password;
create_options.v.v3.type = LCB_TYPE_CLUSTER;
err = lcb_create(&instance, &create_options);
if (dsn) efree(dsn);
if (name) efree(name);
if (password) efree(password);
if (err != LCB_SUCCESS) {
throw_lcb_exception(err);
RETURN_NULL();
}
lcb_set_http_complete_callback(instance, http_complete_callback);
data->lcb = instance;
}
PHP_METHOD(Cluster, connect)
{
cluster_object *data = PHP_THISOBJ();
lcb_error_t err;
err = lcb_connect(data->lcb);
if (err != LCB_SUCCESS) {
throw_lcb_exception(err);
RETURN_NULL();
}
lcb_wait(data->lcb);
err = lcb_get_bootstrap_status(data->lcb);
if (err != LCB_SUCCESS) {
throw_lcb_exception(err);
}
RETURN_NULL();
}
PHP_METHOD(Cluster, http_request)
{
cluster_object *data = PHP_THISOBJ();
lcb_http_cmd_t cmd = { 0 };
copcookie *cookie;
lcb_http_type_t type;
lcb_http_method_t method;
const char *contenttype;
zval *ztype, *zmethod, *zpath, *zbody, *zcontenttype;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zzzzz",
&ztype, &zmethod, &zpath, &zbody, &zcontenttype) == FAILURE) {
RETURN_NULL();
}
if (Z_LVAL_P(ztype) == 1) {
type = LCB_HTTP_TYPE_VIEW;
} else if (Z_LVAL_P(ztype) == 2) {
type = LCB_HTTP_TYPE_MANAGEMENT;
} else {
RETURN_NULL();
}
if (Z_LVAL_P(zmethod) == 1) {
method = LCB_HTTP_METHOD_GET;
} else if (Z_LVAL_P(zmethod) == 2) {
method = LCB_HTTP_METHOD_POST;
} else if (Z_LVAL_P(zmethod) == 3) {
method = LCB_HTTP_METHOD_PUT;
} else if (Z_LVAL_P(zmethod) == 4) {
method = LCB_HTTP_METHOD_DELETE;
} else {
RETURN_NULL();
}
if (Z_LVAL_P(zcontenttype) == 1) {
contenttype = "application/json";
} else if (Z_LVAL_P(zcontenttype) == 2) {
contenttype = "application/x-www-form-urlencoded";
} else {
RETURN_NULL();
}
cmd.v.v0.path = Z_STRVAL_P(zpath);
cmd.v.v0.npath = Z_STRLEN_P(zpath);
if (Z_TYPE_P(zbody) == IS_STRING) {
cmd.v.v0.body = Z_STRVAL_P(zbody);
cmd.v.v0.nbody = Z_STRLEN_P(zbody);
}
cmd.v.v0.method = method;
cmd.v.v0.chunked = 0;
cmd.v.v0.content_type = contenttype;
cookie = copcookie_init(data, return_value);
lcb_make_http_request(data->lcb, cookie, type, &cmd, NULL);
pcbc_wait(data TSRMLS_CC);
efree(cookie);
}
zend_function_entry cluster_methods[] = {
PHP_ME(Cluster, __construct, NULL, ZEND_ACC_PUBLIC | ZEND_ACC_CTOR)
PHP_ME(Cluster, connect, NULL, ZEND_ACC_PUBLIC)
PHP_ME(Cluster, http_request, NULL, ZEND_ACC_PUBLIC)
{NULL, NULL, NULL}
};
void couchbase_init_cluster(INIT_FUNC_ARGS) {
zend_class_entry ce;
INIT_CLASS_ENTRY(ce, "_CouchbaseCluster", cluster_methods);
ce.create_object = cluster_create_handler;
cluster_ce = zend_register_internal_class(&ce TSRMLS_CC);
memcpy(&cluster_handlers,
zend_get_std_object_handlers(), sizeof(zend_object_handlers));
cluster_handlers.clone_obj = NULL;
}