// ---------------------------------------------------------------------------------- // // register udf from file // // def register_udf(path_to_file, server_path, language = :lua, options = {}) // // params: // path_to_file - absolute path to udf file // server_path - where to put udf on the server // language - udf language (in aerospike-c-client v3.1.24, only lua language is available) // // ------ // RETURN: // 1. AerospikeC::UdfTask object // // @TODO options policy // static VALUE register_udf(int argc, VALUE * argv, VALUE self) { rb_aero_TIMED(tm); as_error err; aerospike * as = rb_aero_CLIENT; VALUE path_to_file; VALUE server_path; VALUE language; VALUE options; rb_scan_args(argc, argv, "22", &path_to_file, &server_path, &language, &options); // default values for optional arguments if ( NIL_P(options) ) options = rb_hash_new(); if ( NIL_P(language) ) { language = lua_sym; } else { if ( language != lua_sym ) rb_raise(rb_aero_OptionError, "[AerospikeC::Client][register_udf] in aerospike-c-client v3.1.24, only lua language is available"); } FILE* file = fopen(StringValueCStr(path_to_file), "r"); if (! file) rb_raise(rb_aero_OptionError, "[AerospikeC::Client][register_udf] Cannot read udf from given path: %s", StringValueCStr(path_to_file)); uint8_t * content = (uint8_t *) malloc(1024 * 1024); if (! content) rb_raise(rb_aero_MemoryError, "[AerospikeC::Client][register_udf] Error while allocating memory for udf file content"); // read the file content into a local buffer uint8_t * p_write = content; int read = (int)fread(p_write, 1, 512, file); int size = 0; while (read) { size += read; p_write += read; read = (int)fread(p_write, 1, 512, file); } fclose(file); as_bytes udf_content; as_bytes_init_wrap(&udf_content, content, size, true); // register the UDF file in the database cluster if ( aerospike_udf_put(as, &err, NULL, StringValueCStr(server_path), AS_UDF_TYPE_LUA, &udf_content) != AEROSPIKE_OK ) { as_bytes_destroy(&udf_content); raise_as_error(err); } as_bytes_destroy(&udf_content); rb_aero_logger(AS_LOG_LEVEL_DEBUG, &tm, 1, rb_str_new2("[Client][register_udf] done")); return rb_funcall(rb_aero_UdfTask, rb_intern("new"), 2, server_path, self); }
int udf_put(cl_cluster * asc, const char * module, const char * module_path) { int rc = 0; byte * content = NULL; char filename[1024] = {0}; uint32_t content_len = 0; char * error = NULL; sprintf(filename,"%s%s.lua", module_path, module); rc = read_file(filename, &content, &content_len); if ( rc == 0 ) { as_bytes udf_content; as_bytes_init_wrap(&udf_content, content, content_len, true /*is_malloc*/); // want to re-use content rc = citrusleaf_udf_put(asc, basename(filename), &udf_content, AS_UDF_LUA, &error); if ( rc != 0 ) { fprintf(stderr, "error: unable to upload module: %s\n", filename); fprintf(stderr, "error: (%d) %s\n", rc, error); free(error); error = NULL; } else { fprintf(stderr, "info: module uploaded: %s\n",filename); } as_bytes_destroy(&udf_content); } else { fprintf(stderr, "error: unable to read module: %s\n", filename); } return rc; }
//------------------------------------------------ // Register a UDF function in the database. // bool example_register_udf(aerospike* p_as, const char* udf_file_path) { FILE* file = fopen(udf_file_path, "r"); if (! file) { // If we get here it's likely that we're not running the example from // the right directory - the specific example directory. LOG("cannot open script file %s : %s", udf_file_path, strerror(errno)); return false; } // Read the file's content into a local buffer. uint8_t* content = (uint8_t*)malloc(1024 * 1024); if (! content) { LOG("script content allocation failed"); return false; } uint8_t* p_write = content; int read = (int)fread(p_write, 1, 512, file); int size = 0; while (read) { size += read; p_write += read; read = (int)fread(p_write, 1, 512, file); } fclose(file); // Wrap the local buffer as an as_bytes object. as_bytes udf_content; as_bytes_init_wrap(&udf_content, content, size, true); as_error err; as_string base_string; const char * base = as_basename(&base_string, udf_file_path); // Register the UDF file in the database cluster. if (aerospike_udf_put(p_as, &err, NULL, base, AS_UDF_TYPE_LUA, &udf_content) == AEROSPIKE_OK) { // Wait for the system metadata to spread to all nodes. aerospike_udf_put_wait(p_as, &err, NULL, base, 100); } else { LOG("aerospike_udf_put() returned %d - %s", err.code, err.message); } as_string_destroy(&base_string); // This frees the local buffer. as_bytes_destroy(&udf_content); return err.code == AEROSPIKE_OK; }
/* ****************************************************************************************************** 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; }