/*
 ******************************************************************************************************
 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;
}
Exemple #2
0
bool udf_exists(const char * filename) {
    
	as_error err;
	as_error_reset(&err);

    as_udf_file file;
    as_udf_file_init(&file);

	as_string filename_string;
	const char * base = as_basename(&filename_string, filename);

    if ( aerospike_udf_get(as, &err, NULL, base, AS_UDF_TYPE_LUA, &file) != AEROSPIKE_OK ) {
        error("error caused by aerospike_udf_get: (%d) %s @ %s[%s:%d]", err.code, err.message, err.func, err.file, err.line);
    }

	as_string_destroy(&filename_string);

    as_udf_file_destroy(&file);

    return err.code == AEROSPIKE_OK;
}
/*
 ******************************************************************************************************
 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;
}
/**
 *******************************************************************************************************
 * Gets the code for a UDF module registered with the server
 *
 * @param self                  AerospikeClient object
 * @param args                  The args is a tuple object containing an argument
 *                              list passed from Python to a C function
 * @param kwds                  Dictionary of keywords
 *
 * Returns the content of the UDF module.
 *******************************************************************************************************
 */
PyObject * AerospikeClient_UDF_Get_UDF(AerospikeClient * self, PyObject *args, PyObject * kwds)
{
	// Initialize error
	as_error err;
	as_error_init(&err);

	// Python Function Arguments
	PyObject * py_module = NULL;
	PyObject * py_policy = NULL;
	long language = 0;
	bool init_udf_file = false;
	PyObject * udf_content = NULL;
	PyObject * py_ustr = NULL;

	// Python Function Keyword Arguments
	static char * kwlist[] = {"module", "language", "policy", NULL};

	// Python Function Argument Parsing
	if ( PyArg_ParseTupleAndKeywords(args, kwds, "O|lO:udf_get", kwlist,
				&py_module ,&language, &py_policy) == false ) {
		return NULL;
	}

	if (!self || !self->as) {
		as_error_update(&err, AEROSPIKE_ERR_PARAM, "Invalid aerospike object");
		goto CLEANUP;
	}

	if (!self->is_conn_16) {
		as_error_update(&err, AEROSPIKE_ERR_CLUSTER, "No connection to aerospike cluster");
		goto CLEANUP;
	}

	if(language != AS_UDF_TYPE_LUA)
	{
		as_error_update(&err, AEROSPIKE_ERR_CLIENT, "Invalid language");
		goto CLEANUP;
	}
	char* strModule = NULL;
	if ( PyUnicode_Check(py_module) ) {
		py_ustr = PyUnicode_AsUTF8String(py_module);
		strModule = PyString_AsString(py_ustr);
	}
	else if ( PyString_Check(py_module)){
		strModule = PyString_AsString(py_module);
	}
	else
	{
		as_error_update(&err, AEROSPIKE_ERR_CLIENT, "Module name should be a string or unicode string.");
		goto CLEANUP;
	}

	// Convert python object to policy_info
	as_policy_info *info_policy_p = NULL, info_policy;

	pyobject_to_policy_info( &err, py_policy, &info_policy, &info_policy_p,
			&self->as->config.policies.info);
	if ( err.code != AEROSPIKE_OK ) {
		goto CLEANUP;
	}

	as_udf_file file;
	as_udf_file_init(&file);
	init_udf_file=true;

	// Invoke operation
    Py_BEGIN_ALLOW_THREADS
	aerospike_udf_get(self->as, &err, info_policy_p, strModule, (language - AS_UDF_TYPE_LUA) , &file);
    Py_END_ALLOW_THREADS
	if ( err.code != AEROSPIKE_OK ) {
		as_error_update(&err, err.code, NULL);
		goto CLEANUP;
	}
	udf_content = Py_BuildValue("s#", file.content.bytes, file.content.size);

CLEANUP:

	if (py_ustr){
		Py_DECREF(py_ustr);
	}
	if(init_udf_file)
	{
		as_udf_file_destroy(&file);
	}
	if ( err.code != AEROSPIKE_OK ) {
		PyObject * py_err = NULL;
		error_to_pyobject(&err, &py_err);
		PyObject *exception_type = raise_exception(&err);
		if(PyObject_HasAttrString(exception_type, "module")) {
			PyObject_SetAttrString(exception_type, "module", py_module);
		} 
		if(PyObject_HasAttrString(exception_type, "func")) {
			PyObject_SetAttrString(exception_type, "func", Py_None);
		}
		PyErr_SetObject(exception_type, py_err);
		Py_DECREF(py_err);
		return NULL;
	}

	return udf_content;
}