1- #include <libcouchbase/couchbase.h>
21#include "couchbase.h"
3- #include "phphelpers .h"
2+ #include "ext/standard/php_var .h"
43#include "exception.h"
4+ #include "zap.h"
55#include "cluster.h"
6- #include "metadoc .h"
6+ #include "opcookie .h"
77
8- zend_object_handlers cluster_handlers ;
8+ zap_class_entry cluster_class ;
9+ zend_class_entry * cluster_ce ;
10+
11+ #define PHP_THISOBJ () zap_fetch_this(cluster_object)
912
10- void cluster_free_storage ( void * object TSRMLS_DC )
13+ zap_FREEOBJ_FUNC ( cluster_free_storage )
1114{
12- cluster_object * obj = (cluster_object * ) object ;
15+ cluster_object * obj = zap_get_object (cluster_object , object ) ;
1316
14- zend_hash_destroy (obj -> std .properties );
15- FREE_HASHTABLE (obj -> std .properties );
17+ if (obj -> lcb != NULL ) {
18+ lcb_destroy (obj -> lcb );
19+ obj -> lcb = NULL ;
20+ }
1621
17- efree (obj );
22+ zend_object_std_dtor (& obj -> std TSRMLS_CC );
23+ zap_free_object_storage (obj );
1824}
1925
20- zend_object_value cluster_create_handler ( zend_class_entry * type TSRMLS_DC )
26+ zap_CREATEOBJ_FUNC ( cluster_create_handler )
2127{
22- zend_object_value retval ;
23-
24- cluster_object * obj = (cluster_object * )emalloc (sizeof (cluster_object ));
25- memset (obj , 0 , sizeof (cluster_object ));
26- obj -> std .ce = type ;
28+ cluster_object * obj = zap_alloc_object_storage (cluster_object , type );
2729
28- ALLOC_HASHTABLE (obj -> std .properties );
29- zend_hash_init (obj -> std .properties , 0 , NULL , ZVAL_PTR_DTOR , 0 );
30- phlp_object_properties_init (& obj -> std , type );
30+ zend_object_std_init (& obj -> std , type TSRMLS_CC );
31+ zap_object_properties_init (& obj -> std , type );
3132
32- retval .handle = zend_objects_store_put (obj , NULL ,
33- cluster_free_storage , NULL TSRMLS_CC );
34- retval .handlers = & cluster_handlers ;
33+ obj -> lcb = NULL ;
3534
36- return retval ;
35+ return zap_finalize_object ( obj , & cluster_class ) ;
3736}
3837
3938typedef struct {
40- zval * retval ;
41- cluster_object * owner ;
42- } copcookie ;
43-
44- copcookie * copcookie_init (cluster_object * clusterobj , zval * return_value ) {
45- copcookie * cookie = emalloc (sizeof (copcookie ));
46- cookie -> owner = clusterobj ;
47- cookie -> retval = return_value ;
48- ZVAL_NULL (cookie -> retval );
49- return cookie ;
50- }
51-
52- void ccookie_error (const copcookie * cookie , cluster_object * data , zval * doc ,
53- lcb_error_t error TSRMLS_DC ) {
54- zval * zerror = create_lcb_exception (error TSRMLS_CC );
55- if (Z_TYPE_P (cookie -> retval ) == IS_ARRAY ) {
56- metadoc_from_error (doc , zerror TSRMLS_CC );
57- } else {
58- data -> error = zerror ;
59- }
60- }
39+ opcookie_res header ;
40+ zapval bytes ;
41+ } opcookie_http_res ;
6142
6243static void http_complete_callback (lcb_http_request_t request , lcb_t instance ,
6344 const void * cookie , lcb_error_t error ,
6445 const lcb_http_resp_t * resp ) {
65- cluster_object * data = (cluster_object * )lcb_get_cookie (instance );
66- zval * doc = ((copcookie * )cookie )-> retval ;
67- TSRMLS_FETCH ();
46+ opcookie_http_res * result = ecalloc (1 , sizeof (opcookie_http_res ));
47+ TSRMLS_FETCH ();
6848
69- if ( error == LCB_SUCCESS ) {
70- ZVAL_STRINGL ( doc , resp -> v . v0 . bytes , resp -> v . v0 . nbytes , 1 );
71- } else {
72- ccookie_error ( cookie , data , NULL , error TSRMLS_CC );
73- }
49+ result -> header . err = error ;
50+ zapval_alloc_stringl (
51+ result -> bytes , resp -> v . v0 . bytes , resp -> v . v0 . nbytes );
52+
53+ opcookie_push (( opcookie * ) cookie , & result -> header );
7454}
7555
76- static int pcbc_wait (cluster_object * obj TSRMLS_DC )
56+ static lcb_error_t proc_http_results (cluster_object * cluster , zval * return_value ,
57+ opcookie * cookie TSRMLS_DC )
7758{
78- lcb_t instance = obj -> lcb ;
79- obj -> error = NULL ;
80-
81- lcb_wait (instance );
82-
83- if (obj -> error ) {
84- zend_throw_exception_object (obj -> error TSRMLS_CC );
85- obj -> error = NULL ;
86- return 0 ;
87- }
88-
89- return 1 ;
59+ opcookie_http_res * res ;
60+ lcb_error_t err = LCB_SUCCESS ;
61+
62+ // Any error should cause everything to fail... for now?
63+ err = opcookie_get_first_error (cookie );
64+
65+ if (err == LCB_SUCCESS ) {
66+ // TODO: This could leak with multiple results... It also copies
67+ // which might not be needed...
68+ FOREACH_OPCOOKIE_RES (opcookie_http_res , res , cookie ) {
69+ zap_zval_stringl_p (return_value ,
70+ zapval_strval_p (& res -> bytes ), zapval_strlen_p (& res -> bytes ))
71+ }
72+ }
73+
74+ FOREACH_OPCOOKIE_RES (opcookie_http_res , res , cookie ) {
75+ zapval_destroy (res -> bytes );
76+ }
77+
78+ return err ;
9079}
9180
92- zend_class_entry * cluster_ce ;
93-
9481PHP_METHOD (Cluster , __construct )
9582{
9683 cluster_object * data = PHP_THISOBJ ();
@@ -187,11 +174,12 @@ PHP_METHOD(Cluster, http_request)
187174{
188175 cluster_object * data = PHP_THISOBJ ();
189176 lcb_http_cmd_t cmd = { 0 };
190- copcookie * cookie ;
177+ opcookie * cookie ;
191178 lcb_http_type_t type ;
192179 lcb_http_method_t method ;
193180 const char * contenttype ;
194181 zval * ztype , * zmethod , * zpath , * zbody , * zcontenttype ;
182+ lcb_error_t err ;
195183
196184 if (zend_parse_parameters (ZEND_NUM_ARGS () TSRMLS_CC , "zzzzz" ,
197185 & ztype , & zmethod , & zpath , & zbody , & zcontenttype ) == FAILURE ) {
@@ -240,12 +228,21 @@ PHP_METHOD(Cluster, http_request)
240228 cmd .v .v0 .chunked = 0 ;
241229 cmd .v .v0 .content_type = contenttype ;
242230
243- cookie = copcookie_init (data , return_value );
231+ cookie = opcookie_init ();
232+
233+ err = lcb_make_http_request (data -> lcb , cookie , type , & cmd , NULL );
234+
235+ if (err == LCB_SUCCESS ) {
236+ lcb_wait (data -> lcb );
237+
238+ err = proc_http_results (data , return_value , cookie TSRMLS_CC );
239+ }
244240
245- lcb_make_http_request (data -> lcb , cookie , type , & cmd , NULL );
246- pcbc_wait (data TSRMLS_CC );
241+ opcookie_destroy (cookie );
247242
248- efree (cookie );
243+ if (err != LCB_SUCCESS ) {
244+ throw_lcb_exception (err );
245+ }
249246}
250247
251248zend_function_entry cluster_methods [] = {
@@ -256,12 +253,9 @@ zend_function_entry cluster_methods[] = {
256253};
257254
258255void couchbase_init_cluster (INIT_FUNC_ARGS ) {
259- zend_class_entry ce ;
260- INIT_CLASS_ENTRY (ce , "_CouchbaseCluster" , cluster_methods );
261- ce .create_object = cluster_create_handler ;
262- cluster_ce = zend_register_internal_class (& ce TSRMLS_CC );
263-
264- memcpy (& cluster_handlers ,
265- zend_get_std_object_handlers (), sizeof (zend_object_handlers ));
266- cluster_handlers .clone_obj = NULL ;
256+ zap_init_class_entry (& cluster_class , "_CouchbaseCluster" ,
257+ cluster_methods );
258+ cluster_class .create_obj = cluster_create_handler ;
259+ cluster_class .free_obj = cluster_free_storage ;
260+ cluster_ce = zap_register_internal_class (& cluster_class , cluster_object );
267261}
0 commit comments