Пример #1
0
// ----------------------------------------------------------------------------------
//
// 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);
}
Пример #2
0
//------------------------------------------------
// 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;
}
Пример #3
0
bool udf_put(const char * filename) {
    
    FILE * file = fopen(filename,"r"); 

    if ( !file ) { 
        error("cannot open script file %s : %s", filename, strerror(errno));  
        return -1; 
    } 

    uint8_t * content = (uint8_t *) malloc(SCRIPT_LEN_MAX); 
    if ( content == NULL ) { 
        error("malloc failed"); 
        return -1;
    }     

    int size = 0; 

    uint8_t * buff = content; 
    int read = (int)fread(buff, 1, 512, file);
    while ( read ) { 
        size += read; 
        buff += read; 
        read = (int)fread(buff, 1, 512, file);
    }                        
    fclose(file); 

    as_bytes udf_content;
    as_bytes_init_wrap(&udf_content, content, size, true);

	as_error err;
	as_error_reset(&err);

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

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

	as_string_destroy(&filename_string);
    as_val_destroy(&udf_content);

    return err.code == AEROSPIKE_OK;
}
Пример #4
0
/*
 ******************************************************************************************************
 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;
}
Пример #5
0
/**
 *******************************************************************************************************
 * Registers a UDF module with the Aerospike DB.
 * @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 an integer status. 0(Zero) is success value.
 * In case of error,appropriate exceptions will be raised.
 *******************************************************************************************************
 */
PyObject * AerospikeClient_UDF_Put(AerospikeClient * self, PyObject *args, PyObject * kwds)
{
	// Initialize error
	as_error err;
	as_error_init(&err);

	// Python Function Arguments
	PyObject * py_filename = NULL;
	long language = 0;
	PyObject * py_udf_type = NULL;
	PyObject * py_policy = NULL;
	PyObject * py_ustr = NULL;

	uint8_t * bytes = NULL;
	as_policy_info info_policy;
	as_policy_info *info_policy_p = NULL;

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

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

	if(language != AS_UDF_TYPE_LUA)
	{
		as_error_update(&err, AEROSPIKE_ERR_CLIENT, "Invalid UDF language");
		goto CLEANUP;
	}
	py_udf_type = PyLong_FromLong(language);

	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;
	}

	// Convert PyObject into a filename string
	char *filename = NULL;
	if (PyUnicode_Check(py_filename)) {
		py_ustr = PyUnicode_AsUTF8String(py_filename);
		filename = PyString_AsString(py_ustr);
	} else if (PyString_Check(py_filename)) {
		filename = PyString_AsString(py_filename);
	} else {
		as_error_update(&err, AEROSPIKE_ERR_PARAM, "Filename should be a string");
		goto CLEANUP;
	}

	// Convert python object to policy_info
	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_type udf_type = (as_udf_type)PyInt_AsLong(py_udf_type);

	// Convert lua file to content
	as_bytes content;
	FILE * file_p = fopen(filename,"r");
	FILE * copy_file_p = NULL;

	char copy_filepath[AS_CONFIG_PATH_MAX_LEN] = {0};
	uint32_t user_path_len = strlen(self->as->config.lua.user_path);
    memcpy( copy_filepath,
        self->as->config.lua.user_path,
        user_path_len);
    if ( self->as->config.lua.user_path[user_path_len-1] != '/' ) {
        memcpy( copy_filepath + user_path_len, "/", 1);
        user_path_len = user_path_len + 1;
    }
    char* extracted_filename = strrchr(filename, '/');
    if (extracted_filename) {
        memcpy( copy_filepath + user_path_len, extracted_filename + 1, strlen(extracted_filename) - 1);
        copy_filepath[user_path_len + strlen(extracted_filename) - 1] = '\0';
    } else {
        memcpy( copy_filepath + user_path_len, filename, strlen(filename));
        copy_filepath[user_path_len + strlen(filename)] = '\0';
    }


	if ( !file_p ) {
		as_error_update(&err, AEROSPIKE_ERR_LUA_FILE_NOT_FOUND, "cannot open script file");
		goto CLEANUP;
	}

	bytes = (uint8_t *) malloc(SCRIPT_LEN_MAX);
	if ( bytes == NULL ) {
		as_error_update(&err, errno, "malloc failed");
		goto CLEANUP;
	}

    int size = 0;

    uint8_t * buff = bytes;

	if (access(self->as->config.lua.user_path, W_OK) == 0) {
		copy_file_p = fopen(copy_filepath, "r");
		if (!copy_file_p) {
			copy_file_p = fopen(copy_filepath, "w+");
			int read  = (int)fread(buff, 1, LUA_FILE_BUFFER_FRAME, file_p);
			if (read && fwrite(buff, 1, read, copy_file_p)) {
				while (read) {
					size += read;
					buff += read;
					read = (int)fread(buff, 1, LUA_FILE_BUFFER_FRAME, file_p);
					if (!fwrite(buff, 1, read, copy_file_p)) {
						break;
					}
				}
			} else {
				as_error_update(&err, AEROSPIKE_ERR_CLIENT, "Write of lua file to user path failed");
				goto CLEANUP;
			}
		} else {
			int read  = (int)fread(buff, 1, LUA_FILE_BUFFER_FRAME, file_p);
			while (read) {
				size += read;
				buff += read;
				read = (int)fread(buff, 1, LUA_FILE_BUFFER_FRAME, file_p);
			}
		}
	} else {
		as_error_update(&err, AEROSPIKE_ERR_CLIENT, "No permissions to write lua file to user path");
		goto CLEANUP;
	}

	if (file_p) {
		fclose(file_p);
	}
	if (copy_file_p) {
		fclose(copy_file_p);
	}

	as_bytes_init_wrap(&content, bytes, size, true);

	// Invoke operation
    Py_BEGIN_ALLOW_THREADS
	aerospike_udf_put(self->as, &err, info_policy_p, filename, udf_type, &content);
    Py_END_ALLOW_THREADS
	if( err.code != AEROSPIKE_OK ) {
		as_error_update(&err, err.code, NULL);
		goto CLEANUP;
	} else {
		aerospike_udf_put_wait(self->as, &err, info_policy_p, as_basename(NULL, filename), 2000);
	}

CLEANUP:
	if(bytes) {
		free(bytes);
	}

	if (py_ustr) {
		Py_DECREF(py_ustr);
	}

	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_None);
		} 
		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 PyLong_FromLong(0);
}