/** {{{ proto public Yaf_Config_Simple::key(void) */ PHP_METHOD(yaf_config_simple, key) { zval *prop; zend_string *string; ulong index; prop = zend_read_property(yaf_config_simple_ce, getThis(), ZEND_STRL(YAF_CONFIG_PROPERT_NAME), 1, NULL); zend_hash_get_current_key(Z_ARRVAL_P(prop), &string, &index); switch(zend_hash_get_current_key_type(Z_ARRVAL_P(prop))) { case HASH_KEY_IS_LONG: RETURN_LONG(index); break; case HASH_KEY_IS_STRING: RETURN_STR(zend_string_copy(string)); break; default: RETURN_FALSE; } }
PHP_METHOD(jz_data, key) { zval *prop; zend_string *string; zend_ulong index; prop = zend_read_property(jz_data_class_entry, getThis(), ZEND_STRL(JZ_DATA_PROPERT_NAME), 1, NULL); zend_hash_get_current_key(Z_ARRVAL_P(prop), &string, &index); switch(zend_hash_get_current_key_type(Z_ARRVAL_P(prop))) { case HASH_KEY_IS_LONG: RETURN_LONG(index); break; case HASH_KEY_IS_STRING: RETURN_STR(zend_string_copy(string)); break; default: RETURN_FALSE; } }
static PHP_FUNCTION(params_array) { HashTable *options = NULL; zval *v; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a", &v) == FAILURE) { RETURN_FALSE; } options = Z_ARRVAL_P(v);//读取array类型的值 //zend_hash_internal_pointer_reset //zend_hash_has_more_elements 判断HashTable是否有元素 //zend_hash_move_forward 移动HashTable到顶端 //zend_hash_get_current_key_ex 读取HashTable当前的key,HashTable的key有两种,一种是指定字符串,另一种是顺序的0-n下标 //zend_hash_get_current_data 读取HashTable当前的值到zval //zend_hash_get_current_key_type 获取当前key的类型 for(zend_hash_internal_pointer_reset(options); SUCCESS == zend_hash_has_more_elements(options); zend_hash_move_forward(options)) { char *k1; ulong nkey = -1, keylen; zval **z; zend_hash_get_current_key_ex(options, &k1, &keylen, &nkey, 0, NULL); zend_hash_get_current_data(options, (void **)&z); php_printf(" key: "); if (HASH_KEY_IS_STRING == zend_hash_get_current_key_type(options)) { PHPWRITE(k1, keylen); }else{ php_printf("%ld", nkey); } php_printf(" value: "); if (Z_TYPE_P(*z) == IS_STRING) { //php_printf("string(%d)\n", Z_STRLEN_P(*z)); PHPWRITE(Z_STRVAL_P(*z), Z_STRLEN_P(*z)); }else{ zend_print_zval_r((*z), 0 TSRMLS_CC); } php_printf("\n"); } RETURN_TRUE; }
PHP_COUCHBASE_LOCAL void php_couchbase_store_multi_impl_oo(INTERNAL_FUNCTION_PARAMETERS) { lcb_error_t retval; php_couchbase_res *couchbase_res; php_couchbase_ctx *ctx; time_t exp = {0}; long expire = 0; zval *akeys; long persist_to = 0; long replicate_to = 0; struct observe_entry *entries; int numkeys; lcb_store_cmd_t *cmds; lcb_store_cmd_t **commands; int ii; PHP_COUCHBASE_GET_PARAMS(couchbase_res, PHP_COUCHBASE_ARG_F_OO, "a|lll", &akeys, &expire, &persist_to, &replicate_to); if (pcbc_check_expiry(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1, expire, &exp) == -1) { /* Incorrect expiry time */ return; } if (validate_simple_observe_clause(couchbase_res->handle, persist_to, replicate_to TSRMLS_CC) == -1) { /* Exception already thrown */ return; } numkeys = zend_hash_num_elements(Z_ARRVAL_P(akeys)); if (numkeys == 0) { zend_throw_exception(cb_illegal_key_exception, "No items specified", 0 TSRMLS_CC); return ; } entries = ecalloc(numkeys, sizeof(struct observe_entry)); commands = ecalloc(numkeys, sizeof(lcb_store_cmd_t *)); cmds = ecalloc(numkeys, sizeof(lcb_store_cmd_t)); /* link the command pointers */ for (ii = 0; ii < numkeys; ++ii) { commands[ii] = cmds + ii; } ctx = ecalloc(1, sizeof(php_couchbase_ctx)); ctx->res = couchbase_res; ctx->rv = return_value; array_init(ctx->rv); for (ii = 0, zend_hash_internal_pointer_reset(Z_ARRVAL_P(akeys)); zend_hash_has_more_elements(Z_ARRVAL_P(akeys)) == SUCCESS; zend_hash_move_forward(Z_ARRVAL_P(akeys)), ++ii) { char *key = NULL; uint klen; size_t payload_len = 0; char *payload; unsigned int flags = 0; zval **ppzval; if (zend_hash_get_current_key_type(Z_ARRVAL_P(akeys)) != HASH_KEY_IS_STRING) { int xx; for (xx = 0; xx < ii; ++xx) { efree((void *)cmds[xx].v.v0.bytes); } efree(commands); efree(cmds); efree(ctx); release_entry_array(entries, xx); zend_throw_exception(cb_illegal_key_exception, "Invalid key specified (not a string)", 0 TSRMLS_CC); return ; } zend_hash_get_current_key(Z_ARRVAL_P(akeys), &key, NULL, 0); if ((klen = strlen(key)) == 0) { int xx; for (xx = 0; xx < ii; ++xx) { efree((void *)cmds[xx].v.v0.bytes); } efree(commands); efree(cmds); efree(ctx); release_entry_array(entries, xx); zend_throw_exception(cb_illegal_key_exception, "Invalid key specified (empty string)", 0 TSRMLS_CC); return ; } if (zend_hash_get_current_data(Z_ARRVAL_P(akeys), (void **)&ppzval) == FAILURE) { int xx; for (xx = 0; xx < ii; ++xx) { efree((void *)cmds[xx].v.v0.bytes); } efree(commands); efree(cmds); efree(ctx); release_entry_array(entries, xx); zend_throw_exception(cb_exception, "Failed to get data for key", 0 TSRMLS_CC); return ; } payload = php_couchbase_zval_to_payload(*ppzval, &payload_len, &flags, couchbase_res->serializer, couchbase_res->compressor TSRMLS_CC); if (payload == NULL) { /* Shouldn't we call an exception? */ RETURN_FALSE; } if (couchbase_res->prefix_key_len) { char *new_key; klen = spprintf(&new_key, 0, "%s_%s", couchbase_res->prefix_key, key); key = new_key; } entries[ii].nkey = klen; entries[ii].key = emalloc(klen); memcpy(entries[ii].key, key, klen); cmds[ii].v.v0.operation = LCB_SET; cmds[ii].v.v0.key = entries[ii].key; cmds[ii].v.v0.nkey = klen; cmds[ii].v.v0.bytes = payload; cmds[ii].v.v0.nbytes = payload_len; cmds[ii].v.v0.flags = flags; cmds[ii].v.v0.exptime = exp; if (couchbase_res->prefix_key_len) { efree(key); } } retval = lcb_store(couchbase_res->handle, ctx, numkeys, (const lcb_store_cmd_t * const *)commands); couchbase_res->seqno += numkeys; pcbc_start_loop(couchbase_res); /* * Time to release the payloads... */ for (ii = 0; ii < numkeys; ++ii) { efree((void *)cmds[ii].v.v0.bytes); } efree(cmds); efree(commands); if (LCB_SUCCESS != retval) { efree(ctx); couchbase_report_error(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1, cb_lcb_exception, "Failed to schedule set request: %s", lcb_strerror(couchbase_res->handle, retval)); release_entry_array(entries, numkeys); RETURN_FALSE; } /* * The item was stored successfully. Did the user want to wait until * it was persisted/replicated? */ if (persist_to != 0 || replicate_to != 0) { int ii = 0; for (zend_hash_internal_pointer_reset(Z_ARRVAL_P(return_value)); zend_hash_has_more_elements(Z_ARRVAL_P(return_value)) == SUCCESS; zend_hash_move_forward(Z_ARRVAL_P(return_value)), ++ii) { zval **curr_cas; zend_hash_get_current_data(Z_ARRVAL_P(return_value), (void **)&curr_cas); if (Z_STRLEN_PP(curr_cas)) { entries[ii].cas = strtoull(Z_STRVAL_PP(curr_cas), 0, 10); } else { /* @todo what to do here? */ fprintf(stderr, "wtf!\n"); } } retval = simple_observe(couchbase_res->handle, entries, numkeys, persist_to, replicate_to); couchbase_res->rc = retval; if (retval != LCB_SUCCESS) { if (retval == LCB_ETIMEDOUT) { zend_throw_exception(cb_timeout_exception, "Timed out waiting for the objects to persist", 0 TSRMLS_CC); } else { char errmsg[256]; snprintf(errmsg, sizeof(errmsg), "An error occured while waiting for the objects to persist: %s", lcb_strerror(couchbase_res->handle, retval)); zend_throw_exception(cb_lcb_exception, errmsg, 0 TSRMLS_CC); } } else { int currsize = 4096; char *errmsg = malloc(currsize); int offset = sprintf(errmsg, "The following documents was mutated:"); int errors = 0; for (ii = 0; ii < numkeys; ++ii) { if (entries[ii].mutated) { if ((offset + entries[ii].nkey + 3) > currsize) { char *p = realloc(errmsg, currsize * 2); if (p) { currsize *= 2; errmsg = p; } } if ((offset + entries[ii].nkey + 3) < currsize) { offset += sprintf(errmsg + offset, " \""); memcpy(errmsg + offset, entries[ii].key, entries[ii].nkey); offset += entries[ii].nkey; offset += sprintf(errmsg + offset, "\""); } errors = 1; } } if (errors) { zend_throw_exception(cb_key_mutated_exception, errmsg, 0 TSRMLS_CC); } free(errmsg); } } release_entry_array(entries, numkeys); efree(ctx); }