-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinterface.c
416 lines (330 loc) · 9.64 KB
/
interface.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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "php.h"
#include "ext/standard/info.h"
#include "php_atree.h"
#include "zend_exceptions.h"
#include "atree.h"
/* Structure for Adaptive Tree object. */
typedef struct _php_atree_db_object {
zend_object zo;
int initialised;
art_tree *t;
zend_bool exception;
} php_atree_db_object;
/* Handlers */
static zend_object_handlers atree_object_handlers;
/* Class entries */
zend_class_entry *php_atree_sc_entry;
/* {{{ arginfo */
ZEND_BEGIN_ARG_INFO(arginfo_atree_put, 0)
ZEND_ARG_INFO(0, key)
ZEND_ARG_INFO(0, buf)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO(arginfo_atree_opr, 0)
ZEND_ARG_INFO(0, key)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO(arginfo_atree_void, 0)
ZEND_END_ARG_INFO()
/* }}} */
/* {{{ Error Handler
*/
static void php_atree_error(php_atree_db_object *obj, char *format, ...)
{
va_list arg;
char *message;
TSRMLS_FETCH();
va_start(arg, format);
vspprintf(&message, 0, format, arg);
va_end(arg);
if (obj && obj->exception) {
zend_throw_exception(zend_exception_get_default(TSRMLS_C), message, 0 TSRMLS_CC);
} else {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", message);
}
if (message) {
efree(message);
}
}
/* }}} */
/* {{{ proto resource Atree::init()
Initializes a new ART tree. */
PHP_METHOD(atree, init)
{
zval *object = getThis();
if (zend_parse_parameters_none() == FAILURE) {
RETURN_NULL();
}
art_tree *t = (art_tree *)emalloc(sizeof(art_tree));
if (art_tree_init(t) != 0) {
zend_throw_exception(zend_exception_get_default(TSRMLS_C), "Unable to open tree", 0 TSRMLS_CC);
RETURN_NULL();
}
php_atree_db_object *obj = (php_atree_db_object *)zend_object_store_get_object(object TSRMLS_CC);
if (obj->initialised) {
zend_throw_exception(zend_exception_get_default(TSRMLS_C), "Already initialised Atree Object", 0 TSRMLS_CC);
RETURN_NULL();
}
obj->t = t;
obj->initialised = 1;
}
/* }}} */
/* {{{ proto bool Atree::put( string $key, mixed $value )
Put new item into tree. */
PHP_METHOD(atree, put)
{
char *key;
int key_len;
zval *datavar, *databuf;
php_atree_db_object *obj = (php_atree_db_object *)zend_object_store_get_object(getThis() TSRMLS_CC);
if (!obj || !obj->initialised) {
php_atree_error(obj, "The object has not been correctly initialised");
RETURN_FALSE;
}
if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sz", &key, &key_len, &datavar)) {
RETURN_NULL();
}
ALLOC_INIT_ZVAL(databuf);
*databuf = *datavar;
zval_copy_ctor(databuf);
if (art_insert(obj->t, (unsigned char *)key, key_len, (void *)databuf) != NULL) {
php_atree_error(obj, "Unable to insert key");
RETURN_FALSE;
}
RETURN_TRUE;
}
/* }}} */
/* {{{ proto mixed Atree::get( string $key )
Retrieve item from tree, NULL returned on failure. */
PHP_METHOD(atree, get)
{
char *key;
int key_len;
php_atree_db_object *obj = (php_atree_db_object *)zend_object_store_get_object(getThis() TSRMLS_CC);
if (!obj || !obj->initialised) {
php_atree_error(obj, "The object has not been correctly initialised");
RETURN_FALSE;
}
if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &key, &key_len)) {
RETURN_NULL();
}
void *val = art_search(obj->t, (unsigned char *)key, key_len);
if (val == NULL) {
php_atree_error(obj, "No value found for key: %s", key);
RETURN_NULL();
}
*return_value = *((zval *)val);
zval_copy_ctor(return_value);
}
/* }}} */
/* {{{ proto mixed Atree::delete( string $key )
Remove item from tree, NULL returned on failure. */
PHP_METHOD(atree, delete)
{
char *key;
int key_len;
php_atree_db_object *obj = (php_atree_db_object *)zend_object_store_get_object(getThis() TSRMLS_CC);
if (!obj || !obj->initialised) {
php_atree_error(obj, "The object has not been correctly initialised");
RETURN_FALSE;
}
if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &key, &key_len)) {
RETURN_NULL();
}
void *val = art_delete(obj->t, (unsigned char *)key, key_len);
if (val == NULL) {
php_atree_error(obj, "No value found for key: %s", key);
RETURN_NULL();
}
*return_value = *((zval *)val);
zval_copy_ctor(return_value);
}
/* }}} */
/* {{{ Iterator callback
Build array from iterator results. */
int all_cb_array(void *data, const unsigned char* key, uint32_t key_len, void *val) {
zval *arr = data;
zval *tempdata;
ALLOC_INIT_ZVAL(tempdata);
*tempdata = *((zval *)val);
zval_copy_ctor(tempdata);
add_assoc_zval_ex(arr, (char *)key, key_len + 1, tempdata);
return 0;
}
/* }}} */
/* {{{ proto array Atree::all()
Retrieve all tree items. */
PHP_METHOD(atree, all)
{
php_atree_db_object *obj = (php_atree_db_object *)zend_object_store_get_object(getThis() TSRMLS_CC);
if (!obj || !obj->initialised) {
php_atree_error(obj, "The object has not been correctly initialised");
RETURN_FALSE;
}
if (zend_parse_parameters_none() == FAILURE) {
RETURN_NULL();
}
array_init(return_value);
if (art_iter(obj->t, all_cb_array, return_value) != 0) {
php_atree_error(obj, "Cannot iterate tree");
RETURN_FALSE;
}
}
/* }}} */
/* {{{ proto array Atree::prefix( string $prefix )
Retrieve items matching the prefix. */
PHP_METHOD(atree, prefix)
{
char *key;
int key_len;
php_atree_db_object *obj = (php_atree_db_object *)zend_object_store_get_object(getThis() TSRMLS_CC);
if (!obj || !obj->initialised) {
php_atree_error(obj, "The object has not been correctly initialised");
RETURN_FALSE;
}
if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &key, &key_len)) {
RETURN_NULL();
}
array_init(return_value);
if (art_iter_prefix(obj->t, (unsigned char *)key, key_len, all_cb_array, return_value) != 0) {
php_atree_error(obj, "Cannot iterate tree");
RETURN_FALSE;
}
}
/* }}} */
/* {{{ proto long Atree::size()
Return number of active items in the tree. */
PHP_METHOD(atree, size)
{
php_atree_db_object *obj = (php_atree_db_object *)zend_object_store_get_object(getThis() TSRMLS_CC);
if (!obj || !obj->initialised) {
php_atree_error(obj, "The object has not been correctly initialised");
RETURN_FALSE;
}
if (zend_parse_parameters_none() == FAILURE) {
RETURN_NULL();
}
RETURN_LONG(obj->t->size);
}
/* }}} */
/* {{{ proto bool Atree::clear()
Truncate the tree and initialize a new one. */
PHP_METHOD(atree, clear)
{
php_atree_db_object *obj = (php_atree_db_object *)zend_object_store_get_object(getThis() TSRMLS_CC);
if (!obj || !obj->initialised) {
php_atree_error(obj, "The object has not been correctly initialised");
RETURN_FALSE;
}
if (zend_parse_parameters_none() == FAILURE) {
RETURN_NULL();
}
if (art_tree_clear(obj->t) != 0) {
php_atree_error(obj, "Cannot clear tree");
RETURN_FALSE;
}
RETURN_TRUE;
}
/* }}} */
/* {{{ proto string Atree::version()
Returns the version as a string constant. */
PHP_METHOD(atree, version)
{
if (zend_parse_parameters_none() == FAILURE) {
return;
}
RETVAL_STRING(PHP_ATREE_VERSION, 1);
}
/* }}} */
/* {{{ php_atree_class_methods */
static zend_function_entry php_atree_class_methods[] = {
PHP_ME(atree, init, arginfo_atree_void, ZEND_ACC_PUBLIC)
PHP_ME(atree, put, arginfo_atree_put, ZEND_ACC_PUBLIC)
PHP_ME(atree, get, arginfo_atree_opr, ZEND_ACC_PUBLIC)
PHP_ME(atree, delete, arginfo_atree_opr, ZEND_ACC_PUBLIC)
PHP_ME(atree, all, arginfo_atree_void, ZEND_ACC_PUBLIC)
PHP_ME(atree, prefix, arginfo_atree_opr, ZEND_ACC_PUBLIC)
PHP_ME(atree, size, arginfo_atree_void, ZEND_ACC_PUBLIC)
PHP_ME(atree, clear, arginfo_atree_void, ZEND_ACC_PUBLIC)
PHP_ME(atree, version, arginfo_atree_void, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC)
/* Aliases */
PHP_MALIAS(atree, __construct, init, arginfo_atree_void, ZEND_ACC_PUBLIC | ZEND_ACC_CTOR)
PHP_FE_END
};
/* }}} */
/* {{{ Class deconstructor */
void atree_free_storage(void *object TSRMLS_DC)
{
php_atree_db_object *obj = (php_atree_db_object *)object;
if (!obj || !obj->initialised) {
return;
}
art_tree_destroy(obj->t);
efree(obj->t);
zend_object_std_dtor(&obj->zo TSRMLS_CC);
efree(obj);
}
/* }}} */
/* {{{ Class handler */
static zend_object_value atree_create_handler(zend_class_entry *type TSRMLS_DC)
{
zval *tmp;
zend_object_value retval;
php_atree_db_object *obj = (php_atree_db_object *)emalloc(sizeof(php_atree_db_object));
memset(obj, 0, sizeof(php_atree_db_object));
obj->exception = 1;
obj->initialised = 0;
obj->zo.ce = type;
zend_object_std_init(&obj->zo, type TSRMLS_CC);
object_properties_init(&obj->zo, type);
retval.handle = zend_objects_store_put(obj, NULL, atree_free_storage, NULL TSRMLS_CC);
retval.handlers = &atree_object_handlers;
return retval;
}
/* }}} */
/* {{{ PHP_MINIT_FUNCTION
*/
PHP_MINIT_FUNCTION(atree)
{
zend_class_entry ce;
memcpy(&atree_object_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
/* Register Atree 3 Class */
INIT_CLASS_ENTRY(ce, "Atree", php_atree_class_methods);
php_atree_sc_entry = zend_register_internal_class(&ce TSRMLS_CC);
php_atree_sc_entry->create_object = atree_create_handler;
memcpy(&atree_object_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
atree_object_handlers.clone_obj = NULL;
return SUCCESS;
}
/* }}} */
/* {{{ PHP_MINFO_FUNCTION
*/
PHP_MINFO_FUNCTION(atree)
{
php_info_print_table_start();
php_info_print_table_row(2, "Adaptive Tree suppport", "enabled");
php_info_print_table_row(2, "Atree module version", PHP_ATREE_VERSION);
php_info_print_table_row(2, "Tree algorithm", "LibART");
php_info_print_table_end();
DISPLAY_INI_ENTRIES();
}
/* }}} */
/* {{{ atree_module_entry
*/
zend_module_entry atree_module_entry = {
STANDARD_MODULE_HEADER,
PHP_ATREE_EXTNAME,
NULL,
PHP_MINIT(atree),
NULL,
NULL,
NULL,
PHP_MINFO(atree),
PHP_ATREE_VERSION,
STANDARD_MODULE_PROPERTIES
};
/* }}} */
#ifdef COMPILE_DL_ATREE
ZEND_GET_MODULE(atree)
#endif