/* ****************************************************************************************************** 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; }
/* ******************************************************************************************************* * Wrapper function to perform an aerospike_key_exists within the C client. * * @param as_object_p The C client's aerospike object. * @param as_key_p The C client's as_key that identifies the record. * @param metadata_p The return metadata for the exists/getMetadata API to be * populated by this function. * @param options_p The user's optional policy options to be used if set, else defaults. * @param error_p The as_error to be populated by the function * with the encountered error if any. * ******************************************************************************************************* */ extern as_status aerospike_php_exists_metadata(aerospike* as_object_p, zval* key_record_p, zval* metadata_p, zval* options_p, as_error* error_p) { as_status status = AEROSPIKE_OK; as_key as_key_for_put_record; int16_t initializeKey = 0; if ((!as_object_p) || (!key_record_p)) { status = AEROSPIKE_ERR; goto exit; } if (PHP_TYPE_ISNOTARR(key_record_p) || ((options_p) && (PHP_TYPE_ISNOTARR(options_p)))) { status = AEROSPIKE_ERR_PARAM; DEBUG_PHP_EXT_ERROR("input parameters (type) for exist/getMetdata function not proper."); goto exit; } if (PHP_TYPE_ISNOTARR(metadata_p)) { zval* metadata_arr_p = NULL; MAKE_STD_ZVAL(metadata_arr_p); array_init(metadata_arr_p); ZVAL_ZVAL(metadata_p, metadata_arr_p, 1, 1); } if (AEROSPIKE_OK != (status = aerospike_transform_iterate_for_rec_key_params(Z_ARRVAL_P(key_record_p), &as_key_for_put_record, &initializeKey))) { DEBUG_PHP_EXT_ERROR("unable to iterate through exists/getMetadata key params"); goto exit; } if (AEROSPIKE_OK != (status = aerospike_record_operations_exists(as_object_p, &as_key_for_put_record, error_p, metadata_p, options_p))) { DEBUG_PHP_EXT_ERROR("exists/getMetadata: unable to fetch the record"); goto exit; } exit: if (initializeKey) { as_key_destroy(&as_key_for_put_record); } return status; }
/* ****************************************************************************************************** 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; 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; } 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, &udf_file)) { DEBUG_PHP_EXT_ERROR("%s", error_p->message); goto exit; } ZVAL_STRINGL(udf_code_p, (char*) udf_file.content.bytes, udf_file.content.size, 1); exit: if (init_udf_file) { as_udf_file_destroy(&udf_file); } return error_p->code; }
/* ****************************************************************************************************** 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; }
/* ****************************************************************************************************** Remove a UDF module from the aerospike DB. * * @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 remove from the Aerospike DB. * @param module_len The module name length. * @param options_p The optional policy. * * @return AEROSPIKE_OK if success. Otherwise AEROSPIKE_x. ****************************************************************************************************** */ extern as_status aerospike_udf_deregister(Aerospike_object* aerospike_obj_p, as_error* error_p, char* module_p, zval* options_p) { as_policy_info info_policy; 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; } if (AEROSPIKE_OK != aerospike_udf_remove(aerospike_obj_p->as_ref_p->as_p, error_p, NULL, module_p)) { DEBUG_PHP_EXT_ERROR(error_p->message); goto exit; } exit: 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; 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; } 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 - AS_UDF_TYPE, udf_content_p)) { DEBUG_PHP_EXT_DEBUG(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; }