-
-
Notifications
You must be signed in to change notification settings - Fork 265
/
topic_partition.c
381 lines (300 loc) · 9.83 KB
/
topic_partition.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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
/*
+----------------------------------------------------------------------+
| php-rdkafka |
+----------------------------------------------------------------------+
| Copyright (c) 2016 Arnaud Le Blanc |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| [email protected] so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Author: Arnaud Le Blanc <[email protected]> |
+----------------------------------------------------------------------+
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "php.h"
#include "php_ini.h"
#include "ext/standard/info.h"
#include "php_rdkafka.h"
#include "php_rdkafka_priv.h"
#include "librdkafka/rdkafka.h"
#include "Zend/zend_exceptions.h"
#include "ext/spl/spl_exceptions.h"
#include "topic_partition.h"
#if PHP_VERSION_ID < 80000
#include "topic_partition_legacy_arginfo.h"
#else
#include "topic_partition_arginfo.h"
#endif
typedef kafka_topic_partition_intern object_intern;
static HashTable *get_debug_info(Z_RDKAFKA_OBJ *object, int *is_temp);
zend_class_entry * ce_kafka_topic_partition;
static zend_object_handlers handlers;
static void free_object(zend_object *object) /* {{{ */
{
object_intern *intern = php_kafka_from_obj(object_intern, object);
if (intern->topic) {
efree(intern->topic);
}
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(*intern));
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 *z) /* {{{ */
{
object_intern *intern = Z_RDKAFKA_P(object_intern, z);
if (!intern->topic) {
zend_throw_exception_ex(NULL, 0, "RdKafka\\TopicPartition::__construct() has not been called");
return NULL;
}
return intern;
} /* }}} */
kafka_topic_partition_intern * get_topic_partition_object(zval *z) /* {{{ */
{
return get_object(z);
} /* }}} */
static HashTable *get_debug_info(Z_RDKAFKA_OBJ *object, int *is_temp) /* {{{ */
{
zval ary;
object_intern *intern;
*is_temp = 1;
array_init(&ary);
intern = rdkafka_get_debug_object(object_intern, object);
if (!intern || !intern->topic) {
return Z_ARRVAL(ary);
}
if (intern->topic) {
add_assoc_string(&ary, "topic", intern->topic);
} else {
add_assoc_null(&ary, "topic");
}
add_assoc_long(&ary, "partition", intern->partition);
add_assoc_long(&ary, "offset", intern->offset);
add_assoc_long(&ary, "err", (zend_long) intern->err);
return Z_ARRVAL(ary);
}
/* }}} */
void kafka_topic_partition_init(zval *zobj, char * topic, int32_t partition, int64_t offset, rd_kafka_resp_err_t err) /* {{{ */
{
object_intern *intern;
intern = Z_RDKAFKA_P(object_intern, zobj);
if (!intern) {
return;
}
if (intern->topic) {
efree(intern->topic);
}
intern->topic = estrdup(topic);
intern->partition = partition;
intern->offset = offset;
intern->err = err;
} /* }}} */
void kafka_topic_partition_list_to_array(zval *return_value, rd_kafka_topic_partition_list_t *list) /* {{{ */
{
rd_kafka_topic_partition_t *topar;
zval ztopar;
int i;
array_init_size(return_value, list->cnt);
for (i = 0; i < list->cnt; i++) {
topar = &list->elems[i];
ZVAL_NULL(&ztopar);
object_init_ex(&ztopar, ce_kafka_topic_partition);
kafka_topic_partition_init(&ztopar, topar->topic, topar->partition, topar->offset, topar->err);
add_next_index_zval(return_value, &ztopar);
}
} /* }}} */
rd_kafka_topic_partition_list_t * array_arg_to_kafka_topic_partition_list(int argnum, HashTable *ary) { /* {{{ */
HashPosition pos;
rd_kafka_topic_partition_list_t *list;
zval *zv;
list = rd_kafka_topic_partition_list_new(zend_hash_num_elements(ary));
for (zend_hash_internal_pointer_reset_ex(ary, &pos);
(zv = zend_hash_get_current_data_ex(ary, &pos)) != NULL;
zend_hash_move_forward_ex(ary, &pos)) {
kafka_topic_partition_intern *topar_intern;
rd_kafka_topic_partition_t *topar;
if (Z_TYPE_P(zv) != IS_OBJECT || !instanceof_function(Z_OBJCE_P(zv), ce_kafka_topic_partition)) {
const char *space;
const char *class_name = get_active_class_name(&space);
rd_kafka_topic_partition_list_destroy(list);
php_error(E_ERROR,
"Argument %d passed to %s%s%s() must be an array of RdKafka\\TopicPartition, at least one element is a(n) %s",
argnum,
class_name, space,
get_active_function_name(),
zend_zval_type_name(zv));
return NULL;
}
topar_intern = get_topic_partition_object(zv);
if (!topar_intern) {
rd_kafka_topic_partition_list_destroy(list);
return NULL;
}
topar = rd_kafka_topic_partition_list_add(list, topar_intern->topic, topar_intern->partition);
topar->offset = topar_intern->offset;
}
return list;
} /* }}} */
/* {{{ proto void RdKafka\TopicPartition::__construct(string $topic, int $partition[, int $offset])
Constructor */
PHP_METHOD(RdKafka_TopicPartition, __construct)
{
char *topic;
size_t topic_len;
zend_long partition;
zend_long offset = 0;
zend_error_handling error_handling;
zend_replace_error_handling(EH_THROW, spl_ce_InvalidArgumentException, &error_handling);
if (zend_parse_parameters(ZEND_NUM_ARGS(), "sl|l", &topic, &topic_len, &partition, &offset) == FAILURE) {
zend_restore_error_handling(&error_handling);
return;
}
kafka_topic_partition_init(getThis(), topic, partition, offset, RD_KAFKA_RESP_ERR_NO_ERROR);
zend_restore_error_handling(&error_handling);
}
/* }}} */
/* {{{ proto string RdKafka\TopicPartition::getTopic()
Returns topic name */
PHP_METHOD(RdKafka_TopicPartition, getTopic)
{
object_intern *intern;
if (zend_parse_parameters_none() == FAILURE) {
return;
}
intern = get_object(getThis());
if (!intern) {
return;
}
if (intern->topic) {
RETURN_STRING(intern->topic);
} else {
RETURN_NULL();
}
}
/* }}} */
/* {{{ proto TopicPartition RdKafka\TopicPartition::setTopic($topicName)
Sets topic name */
PHP_METHOD(RdKafka_TopicPartition, setTopic)
{
char * topic;
size_t topic_len;
object_intern *intern;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &topic, &topic_len) == FAILURE) {
return;
}
intern = get_object(getThis());
if (!intern) {
return;
}
if (intern->topic) {
efree(intern->topic);
}
intern->topic = estrdup(topic);
RETURN_ZVAL(getThis(), 1, 0);
}
/* }}} */
/* {{{ proto int RdKafka\TopicPartition::getPartition()
Returns partition */
PHP_METHOD(RdKafka_TopicPartition, getPartition)
{
object_intern *intern;
if (zend_parse_parameters_none() == FAILURE) {
return;
}
intern = get_object(getThis());
if (!intern) {
return;
}
RETURN_LONG(intern->partition);
}
/* }}} */
/* {{{ proto TopicPartition RdKafka\TopicPartition::setPartition($partition)
Sets partition */
PHP_METHOD(RdKafka_TopicPartition, setPartition)
{
zend_long partition;
object_intern *intern;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &partition) == FAILURE) {
return;
}
intern = get_object(getThis());
if (!intern) {
return;
}
intern->partition = partition;
RETURN_ZVAL(getThis(), 1, 0);
}
/* }}} */
/* {{{ proto int RdKafka\TopicPartition::getOffset()
Returns offset */
PHP_METHOD(RdKafka_TopicPartition, getOffset)
{
object_intern *intern;
if (zend_parse_parameters_none() == FAILURE) {
return;
}
intern = get_object(getThis());
if (!intern) {
return;
}
RETURN_LONG(intern->offset);
}
/* }}} */
/* {{{ proto TopicPartition RdKafka\TopicPartition::setOffset($offset)
Sets offset */
PHP_METHOD(RdKafka_TopicPartition, setOffset)
{
zend_long offset;
object_intern *intern;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &offset) == FAILURE) {
return;
}
intern = get_object(getThis());
if (!intern) {
return;
}
intern->offset = offset;
RETURN_ZVAL(getThis(), 1, 0);
}
/* }}} */
/* {{{ proto int RdKafka\TopicPartition::getErr()
Returns err */
PHP_METHOD(RdKafka_TopicPartition, getErr)
{
object_intern *intern;
if (zend_parse_parameters_none() == FAILURE) {
return;
}
intern = get_object(getThis());
if (!intern) {
return;
}
RETURN_LONG((zend_long) intern->err);
}
/* }}} */
void kafka_metadata_topic_partition_minit(INIT_FUNC_ARGS) /* {{{ */
{
ce_kafka_topic_partition = register_class_RdKafka_TopicPartition();
ce_kafka_topic_partition->create_object = create_object;
handlers = kafka_default_object_handlers;
handlers.get_debug_info = get_debug_info;
handlers.free_obj = free_object;
handlers.offset = XtOffsetOf(object_intern, std);
} /* }}} */