Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

start options support

  • Loading branch information...
commit a65d660db0196f8108db0c3a88e1efd696e01af0 1 parent a7ec29d
Arpad Ray authored

Showing 2 changed files with 265 additions and 31 deletions. Show diff stats Hide diff stats

  1. +259 31 leveldb.cpp
  2. +6 0 php_leveldb.h
290 leveldb.cpp
@@ -147,9 +147,7 @@ zend_class_entry *php_leveldb_options_class_entry;
147 147 #define PHP_LEVELDB_OPTIONS_CLASS_NAME "LevelDbOptions"
148 148
149 149 static zend_function_entry leveldb_options_class_functions[] = {
150   - /*
151 150 PHP_ME(LevelDbOptions, __construct, arginfo_leveldb_void, ZEND_ACC_PUBLIC | ZEND_ACC_CTOR)
152   - */
153 151 {NULL, NULL, NULL}
154 152 };
155 153 /* }}} */
@@ -159,9 +157,7 @@ zend_class_entry *php_leveldb_read_options_class_entry;
159 157 #define PHP_LEVELDB_READ_OPTIONS_CLASS_NAME "LevelDbReadOptions"
160 158
161 159 static zend_function_entry leveldb_read_options_class_functions[] = {
162   - /*
163 160 PHP_ME(LevelDbReadOptions, __construct, arginfo_leveldb_void, ZEND_ACC_PUBLIC | ZEND_ACC_CTOR)
164   - */
165 161 {NULL, NULL, NULL}
166 162 };
167 163 /* }}} */
@@ -171,9 +167,7 @@ zend_class_entry *php_leveldb_write_options_class_entry;
171 167 #define PHP_LEVELDB_WRITE_OPTIONS_CLASS_NAME "LevelDbWriteOptions"
172 168
173 169 static zend_function_entry leveldb_write_options_class_functions[] = {
174   - /*
175 170 PHP_ME(LevelDbWriteOptions, __construct, arginfo_leveldb_void, ZEND_ACC_PUBLIC | ZEND_ACC_CTOR)
176   - */
177 171 {NULL, NULL, NULL}
178 172 };
179 173 /* }}} */
@@ -227,11 +221,174 @@ zend_object_value leveldb_object_new(zend_class_entry *type TSRMLS_DC)
227 221 {
228 222 return leveldb_object_new_ex(type, NULL TSRMLS_CC);
229 223 }
  224 +/* }}} */
  225 +
  226 +/* {{{ LevelDbOptions */
  227 +zend_object_handlers leveldb_options_object_handlers;
  228 +
  229 +struct leveldb_options_object {
  230 + zend_object std;
  231 + Options options;
  232 +};
  233 +
  234 +void leveldb_options_free_storage(void *object TSRMLS_DC)
  235 +{
  236 + leveldb_options_object *obj = (leveldb_options_object *)object;
  237 + zend_objects_free_object_storage((zend_object *)object TSRMLS_CC);
  238 +}
  239 +
  240 +zend_object_value leveldb_options_object_new_ex(zend_class_entry *type, leveldb_options_object **ptr TSRMLS_DC)
  241 +{
  242 + zval *tmp;
  243 + zend_object_value retval;
  244 + leveldb_options_object *obj;
  245 +
  246 + obj = (leveldb_options_object *)emalloc(sizeof(leveldb_options_object));
  247 + memset(obj, 0, sizeof(leveldb_options_object));
  248 +
  249 + if (ptr) {
  250 + *ptr = obj;
  251 + }
  252 +
  253 + zend_object_std_init(&obj->std, type TSRMLS_CC);
  254 +#if ZEND_MODULE_API_NO >= 20100409
  255 + object_properties_init(&obj->std, type);
  256 +#else
  257 + zend_hash_copy(obj->std.properties, &type->default_properties, (copy_ctor_func_t)zval_add_ref, (void *)&tmp, sizeof(zval *));
  258 +#endif
  259 +
  260 + retval.handle = zend_objects_store_put(obj, NULL, leveldb_options_free_storage, NULL TSRMLS_CC);
  261 + retval.handlers = &leveldb_options_object_handlers;
  262 + return retval;
  263 +}
  264 +
  265 +zend_object_value leveldb_options_object_new(zend_class_entry *type TSRMLS_DC)
  266 +{
  267 + return leveldb_options_object_new_ex(type, NULL TSRMLS_CC);
  268 +}
  269 +/* }}} */
  270 +
  271 +/* {{{ LevelDbReadOptions */
  272 +zend_object_handlers leveldb_read_options_object_handlers;
  273 +
  274 +struct leveldb_read_options_object {
  275 + zend_object std;
  276 + ReadOptions options;
  277 +};
  278 +
  279 +void leveldb_read_options_free_storage(void *object TSRMLS_DC)
  280 +{
  281 + leveldb_read_options_object *obj = (leveldb_read_options_object *)object;
  282 + zend_objects_free_object_storage((zend_object *)object TSRMLS_CC);
  283 +}
  284 +
  285 +zend_object_value leveldb_read_options_object_new_ex(zend_class_entry *type, leveldb_read_options_object **ptr TSRMLS_DC)
  286 +{
  287 + zval *tmp;
  288 + zend_object_value retval;
  289 + leveldb_read_options_object *obj;
  290 +
  291 + obj = (leveldb_read_options_object *)emalloc(sizeof(leveldb_read_options_object));
  292 + memset(obj, 0, sizeof(leveldb_read_options_object));
  293 +
  294 + if (ptr) {
  295 + *ptr = obj;
  296 + }
  297 +
  298 + zend_object_std_init(&obj->std, type TSRMLS_CC);
  299 +#if ZEND_MODULE_API_NO >= 20100409
  300 + object_properties_init(&obj->std, type);
  301 +#else
  302 + zend_hash_copy(obj->std.properties, &type->default_properties, (copy_ctor_func_t)zval_add_ref, (void *)&tmp, sizeof(zval *));
  303 +#endif
  304 +
  305 + retval.handle = zend_objects_store_put(obj, NULL, leveldb_read_options_free_storage, NULL TSRMLS_CC);
  306 + retval.handlers = &leveldb_read_options_object_handlers;
  307 + return retval;
  308 +}
  309 +
  310 +zend_object_value leveldb_read_options_object_new(zend_class_entry *type TSRMLS_DC)
  311 +{
  312 + return leveldb_read_options_object_new_ex(type, NULL TSRMLS_CC);
  313 +}
  314 +/* }}} */
  315 +
  316 +/* {{{ LevelDbWriteOptions */
  317 +zend_object_handlers leveldb_write_options_object_handlers;
  318 +
  319 +struct leveldb_write_options_object {
  320 + zend_object std;
  321 + WriteOptions options;
  322 +};
  323 +
  324 +void leveldb_write_options_free_storage(void *object TSRMLS_DC)
  325 +{
  326 + leveldb_write_options_object *obj = (leveldb_write_options_object *)object;
  327 + zend_objects_free_object_storage((zend_object *)object TSRMLS_CC);
  328 +}
  329 +
  330 +zend_object_value leveldb_write_options_object_new_ex(zend_class_entry *type, leveldb_write_options_object **ptr TSRMLS_DC)
  331 +{
  332 + zval *tmp;
  333 + zend_object_value retval;
  334 + leveldb_write_options_object *obj;
230 335
  336 + obj = (leveldb_write_options_object *)emalloc(sizeof(leveldb_write_options_object));
  337 + memset(obj, 0, sizeof(leveldb_write_options_object));
  338 +
  339 + if (ptr) {
  340 + *ptr = obj;
  341 + }
  342 +
  343 + zend_object_std_init(&obj->std, type TSRMLS_CC);
  344 +#if ZEND_MODULE_API_NO >= 20100409
  345 + object_properties_init(&obj->std, type);
  346 +#else
  347 + zend_hash_copy(obj->std.properties, &type->default_properties, (copy_ctor_func_t)zval_add_ref, (void *)&tmp, sizeof(zval *));
  348 +#endif
  349 +
  350 + retval.handle = zend_objects_store_put(obj, NULL, leveldb_write_options_free_storage, NULL TSRMLS_CC);
  351 + retval.handlers = &leveldb_write_options_object_handlers;
  352 + return retval;
  353 +}
  354 +
  355 +zend_object_value leveldb_write_options_object_new(zend_class_entry *type TSRMLS_DC)
  356 +{
  357 + return leveldb_write_options_object_new_ex(type, NULL TSRMLS_CC);
  358 +}
231 359 /* }}} */
232 360
233 361 /* }}} */
234 362
  363 +/* {{{ Methods */
  364 +
  365 +/* {{{ static inline bool _create_object(zval *obj, zend_class_entry *ce) */
  366 +static inline bool _create_object(zval **obj, zend_class_entry *ce)
  367 +{
  368 + zval *ctor, unused;
  369 +
  370 + MAKE_STD_ZVAL(*obj);
  371 + Z_TYPE_PP(obj) = IS_OBJECT;
  372 + object_init_ex(*obj, ce);
  373 +
  374 + MAKE_STD_ZVAL(ctor);
  375 + array_init_size(ctor, 2);
  376 + Z_ADDREF_PP(obj);
  377 + add_next_index_zval(ctor, *obj);
  378 + add_next_index_string(ctor, "__construct", 1);
  379 + if (call_user_function(&ce->function_table, obj, ctor, &unused, 0, NULL TSRMLS_CC) == FAILURE) {
  380 + php_error_docref(NULL TSRMLS_CC, E_ERROR, "Unable to construct %s", ce->name);
  381 + zval_add_ref(obj);
  382 + zval_ptr_dtor(obj);
  383 + return false;
  384 + }
  385 +
  386 + zval_ptr_dtor(&ctor);
  387 + Z_DELREF_PP(obj);
  388 + return true;
  389 +}
  390 +/* }}} */
  391 +
235 392 /* {{{ proto LevelDb LevelDb::__construct(string $name [, LevelDbOptions $options [, LevelDbReadOptions $read_options [, LevelDbWriteOptions $write_options]]])
236 393 Construct a new LevelDb object. */
237 394 PHP_METHOD(LevelDb, __construct)
@@ -239,38 +396,47 @@ PHP_METHOD(LevelDb, __construct)
239 396 char *name;
240 397 int name_len;
241 398 std::string name_str;
242   - zval *options = NULL, *read_options = NULL, *write_options = NULL;
  399 + zval *options_zv = NULL, *read_options_zv = NULL, *write_options_zv = NULL, *this_zv = getThis();
243 400
244 401 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|OOO",
245 402 &name, &name_len,
246   - &options, php_leveldb_options_class_entry,
247   - &read_options, php_leveldb_read_options_class_entry,
248   - &write_options, php_leveldb_write_options_class_entry) == FAILURE) {
  403 + &options_zv, php_leveldb_options_class_entry,
  404 + &read_options_zv, php_leveldb_read_options_class_entry,
  405 + &write_options_zv, php_leveldb_write_options_class_entry) == FAILURE) {
249 406 RETURN_NULL();
250 407 }
251 408
252   - /* todo: handle options obj from php */
253   -
254   - /* todo: set default read and write options prop */
255   -
256   - /* create DB object */
257   - {
258   - DB *obj_db;
259   - Options options;
260   - Status status;
261   -
262   - options.create_if_missing = true;
263   - options.error_if_exists = false;
264   - status = DB::Open(options, std::string(name, name_len), &obj_db);
  409 + /* db options */
  410 + if (!options_zv && !_create_object(&options_zv, php_leveldb_options_class_entry)) {
  411 + RETURN_NULL();
  412 + }
  413 + zend_update_property(php_leveldb_class_entry, this_zv, "options", strlen("options"), options_zv TSRMLS_CC);
  414 +
  415 + /* open db */
  416 + DB *obj_db;
  417 + Status status;
  418 + leveldb_options_object *options_obj = (leveldb_options_object *)zend_object_store_get_object(options_zv TSRMLS_CC);
  419 + status = DB::Open(options_obj->options, std::string(name, name_len), &obj_db);
  420 +
  421 + if (status.ok()) {
  422 + leveldb_object *obj = (leveldb_object *)zend_object_store_get_object(this_zv TSRMLS_CC);
  423 + obj->db = obj_db;
  424 + } else {
  425 + const char *error = status.ToString().c_str();
  426 + zend_throw_exception(spl_ce_InvalidArgumentException, (char *)error, 0 TSRMLS_CC);
  427 + }
265 428
266   - if (status.ok()) {
267   - leveldb_object *obj = (leveldb_object *)zend_object_store_get_object(getThis() TSRMLS_CC);
268   - obj->db = obj_db;
269   - } else {
270   - const char *error = status.ToString().c_str();
271   - zend_throw_exception(spl_ce_InvalidArgumentException, (char *)error, 0 TSRMLS_CC);
272   - }
  429 + /* read options */
  430 + if (!read_options_zv && !_create_object(&read_options_zv, php_leveldb_read_options_class_entry)) {
  431 + RETURN_NULL();
  432 + }
  433 + zend_update_property(php_leveldb_class_entry, this_zv, "readoptions", strlen("readoptions"), read_options_zv TSRMLS_CC);
  434 +
  435 + /* write options */
  436 + if (!write_options_zv && !_create_object(&write_options_zv, php_leveldb_write_options_class_entry)) {
  437 + RETURN_NULL();
273 438 }
  439 + zend_update_property(php_leveldb_class_entry, this_zv, "writeoptions", strlen("writeoptions"), write_options_zv TSRMLS_CC);
274 440 }
275 441 /* }}} */
276 442
@@ -350,6 +516,49 @@ PHP_METHOD(LevelDb, delete)
350 516 }
351 517 /* }}} */
352 518
  519 +/* {{{ proto LevelDbOptions LevelDbOptions::__construct()
  520 + Construct a new LevelDbOptions object. */
  521 +PHP_METHOD(LevelDbOptions, __construct)
  522 +{
  523 + if (zend_parse_parameters_none() == FAILURE) {
  524 + RETURN_NULL();
  525 + }
  526 +
  527 + leveldb_options_object *options_obj = (leveldb_options_object *)zend_object_store_get_object(getThis() TSRMLS_CC);
  528 + options_obj->options = Options();
  529 + options_obj->options.create_if_missing = true;
  530 + options_obj->options.error_if_exists = false;
  531 +}
  532 +/* }}} */
  533 +
  534 +/* {{{ proto LevelDbReadOptions LevelDbReadOptions::__construct()
  535 + Construct a new LevelDbReadOptions object. */
  536 +PHP_METHOD(LevelDbReadOptions, __construct)
  537 +{
  538 + if (zend_parse_parameters_none() == FAILURE) {
  539 + RETURN_NULL();
  540 + }
  541 +
  542 + leveldb_read_options_object *options_obj = (leveldb_read_options_object *)zend_object_store_get_object(getThis() TSRMLS_CC);
  543 + options_obj->options = ReadOptions();
  544 +}
  545 +/* }}} */
  546 +
  547 +/* {{{ proto LevelDbWriteOptions LevelDbWriteOptions::__construct()
  548 + Construct a new LevelDbWriteOptions object. */
  549 +PHP_METHOD(LevelDbWriteOptions, __construct)
  550 +{
  551 + if (zend_parse_parameters_none() == FAILURE) {
  552 + RETURN_NULL();
  553 + }
  554 +
  555 + leveldb_write_options_object *options_obj = (leveldb_write_options_object *)zend_object_store_get_object(getThis() TSRMLS_CC);
  556 + options_obj->options = WriteOptions();
  557 +}
  558 +/* }}} */
  559 +
  560 +/* }}} */
  561 +
353 562 /* {{{ leveldb_module_entry
354 563 */
355 564 zend_module_entry leveldb_module_entry = {
@@ -376,8 +585,9 @@ ZEND_GET_MODULE(leveldb)
376 585 */
377 586 PHP_MINIT_FUNCTION(leveldb)
378 587 {
379   - /* register LevelDb class */
380 588 zend_class_entry ce;
  589 +
  590 + /* register LevelDb class */
381 591 INIT_CLASS_ENTRY(ce, PHP_LEVELDB_CLASS_NAME, leveldb_class_functions);
382 592 memcpy(&leveldb_object_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
383 593 php_leveldb_class_entry = zend_register_internal_class(&ce TSRMLS_CC);
@@ -386,6 +596,24 @@ PHP_MINIT_FUNCTION(leveldb)
386 596 zend_declare_property_null(php_leveldb_class_entry, "readoptions", strlen("readoptions"), ZEND_ACC_PROTECTED TSRMLS_CC);
387 597 zend_declare_property_null(php_leveldb_class_entry, "writeoptions", strlen("writeoptions"), ZEND_ACC_PROTECTED TSRMLS_CC);
388 598
  599 + /* register LevelDbOptions class */
  600 + INIT_CLASS_ENTRY(ce, PHP_LEVELDB_OPTIONS_CLASS_NAME, leveldb_options_class_functions);
  601 + memcpy(&leveldb_options_object_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
  602 + php_leveldb_options_class_entry = zend_register_internal_class(&ce TSRMLS_CC);
  603 + php_leveldb_options_class_entry->create_object = leveldb_options_object_new;
  604 +
  605 + /* register LevelDbReadOptions class */
  606 + INIT_CLASS_ENTRY(ce, PHP_LEVELDB_READ_OPTIONS_CLASS_NAME, leveldb_read_options_class_functions);
  607 + memcpy(&leveldb_read_options_object_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
  608 + php_leveldb_read_options_class_entry = zend_register_internal_class(&ce TSRMLS_CC);
  609 + php_leveldb_read_options_class_entry->create_object = leveldb_read_options_object_new;
  610 +
  611 + /* register LevelDbWriteOptions class */
  612 + INIT_CLASS_ENTRY(ce, PHP_LEVELDB_WRITE_OPTIONS_CLASS_NAME, leveldb_write_options_class_functions);
  613 + memcpy(&leveldb_write_options_object_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
  614 + php_leveldb_write_options_class_entry = zend_register_internal_class(&ce TSRMLS_CC);
  615 + php_leveldb_write_options_class_entry->create_object = leveldb_write_options_object_new;
  616 +
389 617 return SUCCESS;
390 618 }
391 619 /* }}} */
6 php_leveldb.h
@@ -45,6 +45,12 @@ PHP_METHOD(LevelDb, set);
45 45 PHP_METHOD(LevelDb, get);
46 46 PHP_METHOD(LevelDb, delete);
47 47
  48 +PHP_METHOD(LevelDbOptions, __construct);
  49 +
  50 +PHP_METHOD(LevelDbReadOptions, __construct);
  51 +
  52 +PHP_METHOD(LevelDbWriteOptions, __construct);
  53 +
48 54 PHP_MINIT_FUNCTION(leveldb);
49 55 PHP_MSHUTDOWN_FUNCTION(leveldb);
50 56 PHP_RINIT_FUNCTION(leveldb);

1 comment on commit a65d660

Reeze Xia

Hi Arpad,
will you take a look at this thread about php-leveldb extension ? http://news.php.net/php.pecl.dev/9706
I notice you didn't active on php-leveldb anymore , so I wrote a extension for leveldb, and I'm trying to propose
it to PECL, I'v sent you a mail about it. but it seems you didn't noticed my mail. I need your opinion about
that on the list :)

Thanks

Please sign in to comment.
Something went wrong with that request. Please try again.