-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
metadata_collection.c
286 lines (230 loc) · 7.94 KB
/
metadata_collection.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
/**
* BSD 3-Clause License
*
* Copyright (c) 2016, Arnaud Le Blanc (Author)
* Copyright (c) 2020, Nick Chiu
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "php.h"
#include "php_simple_kafka_client_int.h"
#include "ext/spl/spl_iterators.h"
#include "Zend/zend_interfaces.h"
#include "Zend/zend_exceptions.h"
#if PHP_VERSION_ID < 80100
#define ZEND_BEGIN_ARG_WITH_TENTATIVE_RETURN_TYPE_INFO_EX(name, return_reference, required_num_args, type, allow_null) \
ZEND_BEGIN_ARG_INFO_EX(name, return_reference, required_num_args, allow_null)
#endif
#include "metadata_collection_arginfo.h"
typedef struct _object_intern {
zval zmetadata;
const void *items;
size_t item_cnt;
size_t item_size;
size_t position;
kafka_metadata_collection_ctor_t ctor;
zend_object std;
} object_intern;
static HashTable *get_debug_info(Z_KAFKA_OBJ *object, int *is_temp);
static zend_class_entry *ce;
static zend_object_handlers handlers;
static void free_object(zend_object *object) /* {{{ */
{
object_intern *intern = php_kafka_from_obj(object_intern, object);
if (intern->items) {
zval_dtor(&intern->zmetadata);
}
zend_object_std_dtor(&intern->std);
}
/* }}} */
static zend_object *create_object(zend_class_entry *class_type) /* {{{ */
{
zend_object* retval;
object_intern *intern;
intern = ecalloc(1, sizeof(object_intern)+ zend_object_properties_size(class_type));
zend_object_std_init(&intern->std, class_type);
object_properties_init(&intern->std, class_type);
retval = &intern->std;
retval->handlers = &handlers;
return retval;
}
/* }}} */
static object_intern * get_object(zval *zmti)
{
object_intern *omti = Z_KAFKA_P(object_intern, zmti);
if (!omti->items) {
zend_throw_exception_ex(NULL, 0, "SimpleKafkaClient\\Metadata\\Collection::__construct() has not been called");
return NULL;
}
return omti;
}
static HashTable *get_debug_info(Z_KAFKA_OBJ *object, int *is_temp) /* {{{ */
{
zval ary;
object_intern *intern;
size_t i;
zval item;
*is_temp = 1;
array_init(&ary);
intern = kafka_get_debug_object(object_intern, object);
if (!intern) {
return Z_ARRVAL(ary);
}
for (i = 0; i < intern->item_cnt; i++) {
ZVAL_NULL(&item);
intern->ctor(&item, &intern->zmetadata, (char *)intern->items + i * intern->item_size);
add_next_index_zval(&ary, &item);
}
return Z_ARRVAL(ary);
}
/* }}} */
/* {{{ proto int SimpleKafkaClient\Metadata\Collection::count()
*/
ZEND_METHOD(SimpleKafkaClient_Metadata_Collection, count)
{
object_intern *intern;
ZEND_PARSE_PARAMETERS_START_EX(ZEND_PARSE_PARAMS_THROW, 0, 0)
ZEND_PARSE_PARAMETERS_END();
intern = get_object(getThis());
if (!intern) {
return;
}
RETURN_LONG(intern->item_cnt);
}
/* }}} */
/* {{{ proto void SimpleKafkaClient\Metadata\Collection::rewind()
*/
ZEND_METHOD(SimpleKafkaClient_Metadata_Collection, rewind)
{
object_intern *intern;
ZEND_PARSE_PARAMETERS_START_EX(ZEND_PARSE_PARAMS_THROW, 0, 0)
ZEND_PARSE_PARAMETERS_END();
intern = get_object(getThis());
if (!intern) {
return;
}
intern->position = 0;
}
/* }}} */
/* {{{ proto mixed SimpleKafkaClient\Metadata\Collection::current()
*/
ZEND_METHOD(SimpleKafkaClient_Metadata_Collection, current)
{
object_intern *intern;
ZEND_PARSE_PARAMETERS_START_EX(ZEND_PARSE_PARAMS_THROW, 0, 0)
ZEND_PARSE_PARAMETERS_END();
intern = get_object(getThis());
if (!intern) {
return;
}
if (intern->position >= intern->item_cnt) {
zend_throw_exception(ce_kafka_exception, "Called current() on invalid iterator", 0);
return;
}
intern->ctor(return_value, &intern->zmetadata, (char *)intern->items + intern->position * intern->item_size);
}
/* }}} */
/* {{{ proto mixed SimpleKafkaClient\Metadata\Collection::key()
*/
ZEND_METHOD(SimpleKafkaClient_Metadata_Collection, key)
{
object_intern *intern;
ZEND_PARSE_PARAMETERS_START_EX(ZEND_PARSE_PARAMS_THROW, 0, 0)
ZEND_PARSE_PARAMETERS_END();
intern = get_object(getThis());
if (!intern) {
return;
}
if (intern->position >= intern->item_cnt) {
zend_throw_exception(ce_kafka_exception, "Called key() on invalid iterator", 0);
return;
}
RETURN_LONG(intern->position);
}
/* }}} */
/* {{{ proto void SimpleKafkaClient\Metadata\Collection::next()
*/
ZEND_METHOD(SimpleKafkaClient_Metadata_Collection, next)
{
object_intern *intern;
ZEND_PARSE_PARAMETERS_START_EX(ZEND_PARSE_PARAMS_THROW, 0, 0)
ZEND_PARSE_PARAMETERS_END();
intern = get_object(getThis());
if (!intern) {
return;
}
intern->position++;
}
/* }}} */
/* {{{ proto bool SimpleKafkaClient\Metadata\Collection::valid()
*/
ZEND_METHOD(SimpleKafkaClient_Metadata_Collection, valid)
{
object_intern *intern;
ZEND_PARSE_PARAMETERS_START_EX(ZEND_PARSE_PARAMS_THROW, 0, 0)
ZEND_PARSE_PARAMETERS_END();
intern = get_object(getThis());
if (!intern) {
return;
}
RETURN_BOOL(intern->position < intern->item_cnt);
}
/* }}} */
void kafka_metadata_collection_init(INIT_FUNC_ARGS)
{
zend_class_entry tmpce;
INIT_NS_CLASS_ENTRY(tmpce, "SimpleKafkaClient\\Metadata", "Collection", class_SimpleKafkaClient_Metadata_Collection_methods);
ce = zend_register_internal_class(&tmpce);
ce->create_object = create_object;
zend_class_implements(ce, 2, zend_ce_countable, zend_ce_iterator);
handlers = kafka_default_object_handlers;
handlers.get_debug_info = get_debug_info;
handlers.free_obj = free_object;
handlers.offset = XtOffsetOf(object_intern, std);
}
void kafka_metadata_collection_obj_init(zval *return_value, Z_KAFKA_OBJ *zmetadata, const void * items, size_t item_cnt, size_t item_size, kafka_metadata_collection_ctor_t ctor)
{
object_intern *intern;
if (object_init_ex(return_value, ce) != SUCCESS) {
return;
}
intern = Z_KAFKA_P(object_intern, return_value);
if (!intern) {
return;
}
#if PHP_MAJOR_VERSION < 8
ZVAL_ZVAL(&intern->zmetadata, zmetadata, 1, 0);
#endif
intern->items = items;
intern->item_cnt = item_cnt;
intern->item_size = item_size;
intern->ctor = ctor;
intern->position = 0;
}