/* ****************************************************************************************************** Lists the UDF modules registered with the server. * * @param aerospike_obj_p The C client's aerospike object. * @param error_p The C client's as_error to be set to the encountered error. * @param array_of_modules_p An array of registered UDF modules * against the Aerospike DB to be populated by * this function. * @param language Optionally filters a subset of modules * matching the given type. * @options_p The optional parameters to the * Aerospike::listRegistered(). * * @return AEROSPIKE_OK if success. Otherwise AEROSPIKE_x. ****************************************************************************************************** */ extern as_status aerospike_list_registered_udf_modules(Aerospike_object* aerospike_obj_p, as_error* error_p, zval* array_of_modules_p, long language, zval* options_p) { as_udf_files udf_files; uint32_t init_udf_files = 0; uint32_t i=0; as_policy_info info_policy; if ((language != -1) && ((language & AS_UDF_TYPE) != AS_UDF_TYPE)) { PHP_EXT_SET_AS_ERR(error_p, AEROSPIKE_ERR_PARAM, "Invalid Value for language"); DEBUG_PHP_EXT_ERROR("Invalid value for language"); goto exit; } set_policy(NULL, NULL, NULL, NULL, &info_policy, NULL, options_p, error_p); if (AEROSPIKE_OK != (error_p->code)) { DEBUG_PHP_EXT_DEBUG("Unable to set policy"); goto exit; } as_udf_files_init(&udf_files, 0); init_udf_files = 1; if (AEROSPIKE_OK != aerospike_udf_list(aerospike_obj_p->as_ref_p->as_p, error_p, &info_policy, &udf_files)) { DEBUG_PHP_EXT_ERROR(error_p->message); goto exit; } for (i = 0; i < udf_files.size; i++) { if ((language != -1) && ((udf_files.entries[i].type) != (language - AS_UDF_TYPE))) { continue; } zval* module_p = NULL; MAKE_STD_ZVAL(module_p); array_init(module_p); add_assoc_stringl(module_p, UDF_MODULE_NAME, udf_files.entries[i].name, strlen(udf_files.entries[i].name), 1); add_assoc_long(module_p, UDF_MODULE_TYPE, (udf_files.entries[i].type + AS_UDF_TYPE)); add_next_index_zval(array_of_modules_p, module_p); } exit: if (init_udf_files) { as_udf_files_destroy(&udf_files); } return error_p->code; }
static void populate_result_for_get_exists_many(as_key *key_p, zval *outer_container_p, zval *inner_container_p, as_error *error_p, bool null_flag TSRMLS_DC) { if (!(as_val*)(key_p->valuep)) { if (!null_flag) { if (0 != add_assoc_zval(outer_container_p, (char *) key_p->digest.value, inner_container_p)) { PHP_EXT_SET_AS_ERR(error_p, AEROSPIKE_ERR, "Unable to get key of a record"); DEBUG_PHP_EXT_DEBUG("Unable to get key of a record"); } } else { if (0 != add_assoc_null(outer_container_p, (char *) key_p->digest.value)) { PHP_EXT_SET_AS_ERR(error_p, AEROSPIKE_ERR, "Unable to get key of a record"); DEBUG_PHP_EXT_DEBUG("Unable to get key of a record"); } } } else { switch (((as_val*)(key_p->valuep))->type) { case AS_STRING: if (!null_flag) { if (0 != add_assoc_zval(outer_container_p, key_p->value.string.value, inner_container_p)) { PHP_EXT_SET_AS_ERR(error_p, AEROSPIKE_ERR, "Unable to get key of a record"); DEBUG_PHP_EXT_DEBUG("Unable to get key of a record"); } } else { if (0 != add_assoc_null(outer_container_p, key_p->value.string.value)) { PHP_EXT_SET_AS_ERR(error_p, AEROSPIKE_ERR, "Unable to get key of a record"); DEBUG_PHP_EXT_DEBUG("Unable to get key of a record"); } } break; case AS_INTEGER: if (!null_flag) { if (FAILURE == add_index_zval(outer_container_p, key_p->value.integer.value, inner_container_p)) { PHP_EXT_SET_AS_ERR(error_p, AEROSPIKE_ERR, "Unable to get key of a record"); DEBUG_PHP_EXT_DEBUG("Unable to get key of a record"); } } else { if (0 != add_index_null(outer_container_p, key_p->value.integer.value)) { PHP_EXT_SET_AS_ERR(error_p, AEROSPIKE_ERR, "Unable to get key of a record"); DEBUG_PHP_EXT_DEBUG("Unable to get key of a record"); } } break; default: break; } } }
/* ****************************************************************************************************** Get the code of registered UDF module. * * @param aerospike_obj_p The C client's aerospike object. * @param error_p The C client's as_error to be set to the encountered error. * @param module_p The name of the UDF module to get from the * server. * @param module_len Length of UDF module name. * @param udf_code_p Code of the UDF to be populated by this * function. * @param language The Aerospike::UDF_TYPE_* constant. * @param options_p The optional policy. * * @return AEROSPIKE_OK if success. Otherwise AEROSPIKE_x. ****************************************************************************************************** */ extern as_status aerospike_get_registered_udf_module_code(Aerospike_object* aerospike_obj_p, as_error* error_p, char* module_p, zval* udf_code_p, long language, zval* options_p) { uint32_t init_udf_file = 0; as_udf_file udf_file; as_policy_info info_policy; if ((language & AS_UDF_TYPE) != AS_UDF_TYPE) { PHP_EXT_SET_AS_ERR(error_p, AEROSPIKE_ERR_PARAM, "Invalid Value for language"); DEBUG_PHP_EXT_ERROR("Invalid value for language"); goto exit; } set_policy(NULL, NULL, NULL, NULL, &info_policy, NULL, options_p, error_p); if (AEROSPIKE_OK != (error_p->code)) { DEBUG_PHP_EXT_DEBUG("Unable to set policy"); goto exit; } as_udf_file_init(&udf_file); init_udf_file = 1; if (AEROSPIKE_OK != aerospike_udf_get(aerospike_obj_p->as_ref_p->as_p, error_p, &info_policy, module_p, (language - AS_UDF_TYPE), &udf_file)) { DEBUG_PHP_EXT_ERROR(error_p->message); goto exit; } ZVAL_STRINGL(udf_code_p, udf_file.content.bytes, udf_file.content.size, 1); exit: if (init_udf_file) { as_udf_file_destroy(&udf_file); } return error_p->code; }
/* ****************************************************************************************************** Registers a UDF module. * * @param aerospike_obj_p The C client's aerospike object. * @param error_p The C client's as_error to be set to the encountered error. * @param path_p The path to the module on the client side * @param language The Aerospike::UDF_TYPE_* constant. * @param options_p The optional policy. * * @return AEROSPIKE_OK if success. Otherwise AEROSPIKE_x. ****************************************************************************************************** */ extern as_status aerospike_udf_register(Aerospike_object* aerospike_obj_p, as_error* error_p, char* path_p, char* module_p, long language, zval* options_p) { FILE* file_p = NULL; uint32_t size = 0; uint32_t content_size; uint8_t* bytes_p = NULL; uint8_t* buff_p = NULL; uint32_t read; as_bytes udf_content; as_bytes* udf_content_p = NULL; as_policy_info info_policy; TSRMLS_FETCH_FROM_CTX(aerospike_obj_p->ts); set_policy(&aerospike_obj_p->as_ref_p->as_p->config, NULL, NULL, NULL, NULL, &info_policy, NULL, NULL, NULL, options_p, error_p TSRMLS_CC); if (AEROSPIKE_OK != (error_p->code)) { DEBUG_PHP_EXT_DEBUG("Unable to set policy"); goto exit; } file_p = fopen(path_p, "r"); if (!file_p) { PHP_EXT_SET_AS_ERR(error_p, AEROSPIKE_ERR_UDF_NOT_FOUND, "Cannot open script file"); DEBUG_PHP_EXT_DEBUG("Cannot open script file"); goto exit; } fseek(file_p, 0L, SEEK_END); content_size = ftell(file_p); fseek(file_p, 0L, SEEK_SET); /* * Using emalloc here to maintain consistency of PHP extension. * malloc can also be used for C-SDK. * if emalloc is used, pass parameter 4 of function as_bytes_init_wrap as * "false" and handle the freeing up of the same here. */ if (NULL == (bytes_p = (uint8_t *) emalloc(content_size + 1))) { PHP_EXT_SET_AS_ERR(error_p, AEROSPIKE_ERR, "Memory allocation failed for contents of UDF"); DEBUG_PHP_EXT_DEBUG("Memory allocation failed for contents of UDF"); goto exit; } buff_p = bytes_p; read = (int) fread(buff_p, 1, LUA_FILE_BUFFER_FRAME, file_p); while (read) { size += read; buff_p += read; read = (int) fread(buff_p, 1, LUA_FILE_BUFFER_FRAME, file_p); } as_bytes_init_wrap(&udf_content, bytes_p, size, false); udf_content_p = &udf_content; /* * Register the UDF file in the database cluster. */ if (AEROSPIKE_OK != aerospike_udf_put(aerospike_obj_p->as_ref_p->as_p, error_p, &info_policy, module_p, language, udf_content_p)) { DEBUG_PHP_EXT_DEBUG("%s", error_p->message); goto exit; } else if (AEROSPIKE_OK != aerospike_udf_put_wait(aerospike_obj_p->as_ref_p->as_p, error_p, &info_policy, module_p, 0)) { DEBUG_PHP_EXT_DEBUG("%s", error_p->message); goto exit; } exit: if (file_p) { fclose(file_p); } if (bytes_p) { efree(bytes_p); } if (udf_content_p) { as_bytes_destroy(udf_content_p); } return error_p->code; }