bool udf_readfile(const char * filename, as_bytes * content) { FILE * file = fopen(filename,"r"); if ( !file ) { error("cannot open script file %s : %s", filename, strerror(errno)); return -1; } uint8_t * bytes = (uint8_t *) malloc(SCRIPT_LEN_MAX); if ( bytes == NULL ) { error("malloc failed"); return -1; } int size = 0; uint8_t * buff = bytes; 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_init_wrap(content, bytes, size, true); return true; }
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; }
/** * Initialize a stack allocated `as_key` to a raw bytes value. */ as_key * as_key_init_rawp(as_key * key, const as_namespace ns, const as_set set, const uint8_t * value, uint32_t size, bool free) { if ( !key ) return key; as_bytes_init_wrap((as_bytes *) &key->value, (uint8_t *) value, size, free); return as_key_cons(key, false, ns, set, &key->value, NULL); }
/** * Initialize a stack allocated `as_key` to a raw bytes value. */ as_key * as_key_new_rawp(const as_namespace ns, const as_set set, const uint8_t * value, uint32_t size, bool free) { as_key * key = (as_key *) malloc(sizeof(as_key)); if ( !key ) return key; as_bytes_init_wrap((as_bytes *) &key->value, (uint8_t *) value, size, free); return as_key_cons(key, true, ns, set, &key->value, NULL); }
// ---------------------------------------------------------------------------------- // // 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); }
//------------------------------------------------ // 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; }
static bool asc_raw_write(aerospike* p_as, as_key* p_key, uint8_t *buf, uint32_t size) { as_error err; as_status status; uint32_t lstack_size = (size+(CHUNK_SIZE-1))/CHUNK_SIZE; uint32_t offset, chksize; // Create a large stack object to use as_ldt lstack; as_ldt_init(&lstack, "data", AS_LDT_LSTACK, NULL); // Make arraylist as_arraylist vals; as_arraylist_inita(&vals, lstack_size); as_bytes *p_bval; p_bval = (as_bytes *)alloca(lstack_size * sizeof(as_bytes)); for (offset = 0; offset < size; offset += chksize, p_bval++) { chksize = MIN(size - offset, CHUNK_SIZE); as_bytes_init_wrap(p_bval, buf + offset, chksize, false); as_arraylist_insert_bytes(&vals, 0, p_bval); } // Push bytes #if 1 // FIXME it's a workaround uint32_t i; for (i = 0; i < vals.size; i++) { status = aerospike_lstack_push(p_as, &err, NULL, p_key, &lstack, vals.elements[i]); if (status != AEROSPIKE_OK) { ERROR("aerospike_lstack_push() - returned %d - %s", err.code, err.message); return false; } } #else status = aerospike_lstack_push_all(p_as, &err, NULL, p_key, &lstack, (as_list *)&vals); if (status != AEROSPIKE_OK) { ERROR("aerospike_lstack_push_all() - returned %d - %s", err.code, err.message); return false; } #endif // Write metadata as_record rec; as_record_inita(&rec, 1); as_record_set_int64(&rec, "size", size); aerospike_key_put(p_as, &err, NULL, p_key, &rec); return true; }
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; }
/* ****************************************************************************************************** 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; }
/** ******************************************************************************************************* * 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); }
uint8_t* as_command_parse_bins(as_record* rec, uint8_t* p, uint32_t n_bins, bool deserialize) { as_bin* bin = rec->bins.entries; // Parse bins for (uint32_t i = 0; i < n_bins; i++, bin++) { uint32_t op_size = cf_swap_from_be32(*(uint32_t*)p); p += 5; uint8_t type = *p; p += 2; uint8_t name_size = *p++; uint8_t name_len = (name_size <= AS_BIN_NAME_MAX_LEN)? name_size : AS_BIN_NAME_MAX_LEN; memcpy(bin->name, p, name_len); bin->name[name_len] = 0; p += name_size; uint32_t value_size = (op_size - (name_size + 4)); switch (type) { case AS_BYTES_UNDEF: { bin->valuep = (as_bin_value*)&as_nil; break; } case AS_BYTES_INTEGER: { int64_t value; if (as_command_bytes_to_int(p, value_size, &value) == 0) { as_integer_init((as_integer*)&bin->value, value); bin->valuep = &bin->value; } break; } case AS_BYTES_DOUBLE: { double value = cf_swap_from_big_float64(*(double*)p); as_double_init((as_double*)&bin->value, value); bin->valuep = &bin->value; break; } case AS_BYTES_STRING: { char* value = malloc(value_size + 1); memcpy(value, p, value_size); value[value_size] = 0; as_string_init_wlen((as_string*)&bin->value, (char*)value, value_size, true); bin->valuep = &bin->value; break; } case AS_BYTES_LIST: case AS_BYTES_MAP: { if (deserialize) { as_val* value = 0; as_buffer buffer; buffer.data = p; buffer.size = value_size; as_serializer ser; as_msgpack_init(&ser); as_serializer_deserialize(&ser, &buffer, &value); as_serializer_destroy(&ser); bin->valuep = (as_bin_value*)value; } else { void* value = malloc(value_size); memcpy(value, p, value_size); as_bytes_init_wrap((as_bytes*)&bin->value, value, value_size, true); bin->value.bytes.type = (as_bytes_type)type; bin->valuep = &bin->value; } break; } default: { void* value = malloc(value_size); memcpy(value, p, value_size); as_bytes_init_wrap((as_bytes*)&bin->value, value, value_size, true); bin->value.bytes.type = (as_bytes_type)type; bin->valuep = &bin->value; break; } } rec->bins.size++; p += value_size; } return p; }
uint8_t* as_command_parse_key(uint8_t* p, uint32_t n_fields, as_key* key) { uint32_t len; uint32_t size; for (uint32_t i = 0; i < n_fields; i++) { len = cf_swap_from_be32(*(uint32_t*)p) - 1; p += 4; switch (*p++) { case AS_FIELD_DIGEST: size = (len < AS_DIGEST_VALUE_SIZE) ? len : AS_DIGEST_VALUE_SIZE; key->digest.init = true; memcpy(key->digest.value, p, size); break; case AS_FIELD_NAMESPACE: size = (len < (AS_NAMESPACE_MAX_SIZE-1)) ? len : (AS_NAMESPACE_MAX_SIZE-1); memcpy(key->ns, p, size); key->ns[size] = 0; break; case AS_FIELD_SETNAME: size = (len < (AS_SET_MAX_SIZE-1)) ? len : (AS_SET_MAX_SIZE-1); memcpy(key->set, p, size); key->set[size] = 0; break; case AS_FIELD_KEY: len--; uint8_t type = *p++; switch (type) { case AS_BYTES_INTEGER: { int64_t value; if (as_command_bytes_to_int(p, len, &value) == 0) { as_integer_init((as_integer*)&key->value, value); key->valuep = &key->value; } break; } case AS_BYTES_DOUBLE: { double value = cf_swap_from_big_float64(*(double*)p); as_double_init((as_double*)&key->value, value); key->valuep = &key->value; break; } case AS_BYTES_STRING: { char* value = malloc(len+1); memcpy(value, p, len); value[len] = 0; as_string_init_wlen((as_string*)&key->value, value, len, true); key->valuep = &key->value; break; } case AS_BYTES_BLOB: { void* value = malloc(len); memcpy(value, p, len); as_bytes_init_wrap((as_bytes*)&key->value, (uint8_t*)value, len, true); key->valuep = &key->value; break; } default: { as_log_error("Invalid key type: %d", type); break; } } break; } p += len; } return p; }
bool as_operations_add_list_set_rawp(as_operations *ops, const as_bin_name name, int64_t index, const uint8_t *value, uint32_t size, bool free) { as_bytes v; as_bytes_init_wrap(&v, (uint8_t *)value, size, free); return AS_OPERATIONS_CDT_OP(ops, name, AS_CDT_OP_LIST_SET, index, &v); }
bool as_operations_add_list_append_rawp(as_operations *ops, const as_bin_name name, const uint8_t *value, uint32_t size, bool free) { as_bytes v; as_bytes_init_wrap(&v, (uint8_t *)value, size, free); return AS_OPERATIONS_CDT_OP(ops, name, AS_CDT_OP_LIST_APPEND, &v); }
static void initialize_bin_for_strictypes(AerospikeClient *self, as_error *err, PyObject *py_value, as_binop *binop, char *bin, as_static_pool *static_pool) { as_bin *binop_bin = &binop->bin; if (PyInt_Check(py_value)) { int val = PyInt_AsLong(py_value); as_integer_init((as_integer *) &binop_bin->value, val); binop_bin->valuep = &binop_bin->value; } else if (PyLong_Check(py_value)) { long val = PyLong_AsLong(py_value); as_integer_init((as_integer *) &binop_bin->value, val); binop_bin->valuep = &binop_bin->value; } else if (PyString_Check(py_value)) { char * val = PyString_AsString(py_value); as_string_init((as_string *) &binop_bin->value, val, false); binop_bin->valuep = &binop_bin->value; } else if (PyUnicode_Check(py_value)) { PyObject *py_ustr1 = PyUnicode_AsUTF8String(py_value); char * val = PyString_AsString(py_ustr1); as_string_init((as_string *) &binop_bin->value, val, false); binop_bin->valuep = &binop_bin->value; } else if (PyFloat_Check(py_value)) { int64_t val = PyFloat_AsDouble(py_value); if (aerospike_has_double(self->as)) { as_double_init((as_double *) &binop_bin->value, val); binop_bin->valuep = &binop_bin->value; } else { as_bytes *bytes; GET_BYTES_POOL(bytes, static_pool, err); serialize_based_on_serializer_policy(self, SERIALIZER_PYTHON, &bytes, py_value, err); ((as_val *) &binop_bin->value)->type = AS_UNKNOWN; binop_bin->valuep = (as_bin_value *) bytes; } } else if (PyList_Check(py_value)) { as_list * list = NULL; pyobject_to_list(self, err, py_value, &list, static_pool, SERIALIZER_PYTHON); ((as_val *) &binop_bin->value)->type = AS_UNKNOWN; binop_bin->valuep = (as_bin_value *) list; } else if (PyDict_Check(py_value)) { as_map * map = NULL; pyobject_to_map(self, err, py_value, &map, static_pool, SERIALIZER_PYTHON); ((as_val *) &binop_bin->value)->type = AS_UNKNOWN; binop_bin->valuep = (as_bin_value *) map; } else if (!strcmp(py_value->ob_type->tp_name, "aerospike.Geospatial")) { PyObject* py_data = PyObject_GenericGetAttr(py_value, PyString_FromString("geo_data")); char *geo_value = PyString_AsString(AerospikeGeospatial_DoDumps(py_data, err)); if (aerospike_has_geo(self->as)) { as_geojson_init((as_geojson *) &binop_bin->value, geo_value, false); binop_bin->valuep = &binop_bin->value; } else { as_bytes *bytes; GET_BYTES_POOL(bytes, static_pool, err); serialize_based_on_serializer_policy(self, SERIALIZER_PYTHON, &bytes, py_data, err); ((as_val *) &binop_bin->value)->type = AS_UNKNOWN; binop_bin->valuep = (as_bin_value *) bytes; } } else if (!strcmp(py_value->ob_type->tp_name, "aerospike.null")) { ((as_val *) &binop_bin->value)->type = AS_UNKNOWN; binop_bin->valuep = (as_bin_value *) &as_nil; } else if (PyByteArray_Check(py_value)) { as_bytes *bytes; GET_BYTES_POOL(bytes, static_pool, err); serialize_based_on_serializer_policy(self, SERIALIZER_PYTHON, &bytes, py_value, err); as_bytes_init_wrap((as_bytes *) &binop_bin->value, bytes->value, bytes->size, false); binop_bin->valuep = &binop_bin->value; } else { as_bytes *bytes; GET_BYTES_POOL(bytes, static_pool, err); serialize_based_on_serializer_policy(self, SERIALIZER_PYTHON, &bytes, py_value, err); ((as_val *) &binop_bin->value)->type = AS_UNKNOWN; binop_bin->valuep = (as_bin_value *) bytes; } strcpy(binop_bin->name, bin); }
int main(int argc, char* argv[]) { aerospike as; as_error err; as_boolean ldt_exists; as_ldt lstack, lstack2; as_integer ival; as_string sval; as_bytes bval; uint32_t n_elements, cap_size; as_arraylist_iterator it; as_list* p_list = NULL; // Parse command line arguments. if (! example_get_opts(argc, argv, EXAMPLE_BASIC_OPTS)) { exit(-1); } // Connect to the aerospike database cluster. example_connect_to_aerospike(&as); // Start clean. example_remove_test_record(&as); // Create a large stack object to use. No need to destroy lstack if using // as_ldt_init() on stack object. as_ldt_init(&lstack, "mystack", AS_LDT_LSTACK, NULL); // Verify that the LDT is not already there. as_boolean_init(&ldt_exists, false); assert (aerospike_lstack_ldt_exists(&as, &err, NULL, &g_key, &lstack, &ldt_exists) == AEROSPIKE_OK); assert (as_boolean_get(&ldt_exists) == false); LOG("verified that lstack ldt is not present"); // Push a few values onto the stack. // No need to destroy sval if using as_string_init() on stack object. // Push an integer as_integer_init(&ival, 123); assert (aerospike_lstack_push(&as, &err, NULL, &g_key, &lstack, (const as_val*)&ival) == AEROSPIKE_OK); // Push a string as_string_init(&sval, "string stack value", false); assert (aerospike_lstack_push(&as, &err, NULL, &g_key, &lstack, (const as_val*)&sval) == AEROSPIKE_OK); // Push bytes uint8_t buf[16] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}; as_bytes_init_wrap(&bval, buf, 16, false); assert (aerospike_lstack_push(&as, &err, NULL, &g_key, &lstack, (const as_val*)&bval) == AEROSPIKE_OK); // Look at the stack size right now. assert (aerospike_lstack_size(&as, &err, NULL, &g_key, &lstack, &n_elements) == AEROSPIKE_OK); LOG("%d values pushed", n_elements); // Peek a few values back. as_ldt_init(&lstack2, "mystack", AS_LDT_LSTACK, NULL); assert (aerospike_lstack_peek(&as, &err, NULL, &g_key, &lstack2, 3, &p_list) == AEROSPIKE_OK); // See if the elements match what we expect. as_arraylist_iterator_init(&it, (const as_arraylist*)p_list); while (as_arraylist_iterator_has_next(&it)) { const as_val* p_val = as_arraylist_iterator_next(&it); char* p_str = as_val_tostring(p_val); LOG(" peek - type = %d, value = %s", as_val_type(p_val), p_str); free(p_str); } as_list_destroy(p_list); p_list = NULL; // Push 3 more items onto the stack. By using as_arraylist_inita() we avoid // some but not all internal heap usage, so we must call // as_arraylist_destroy(). as_arraylist vals; as_arraylist_inita(&vals, 3); as_arraylist_append_int64(&vals, 1000); as_arraylist_append_int64(&vals, 2000); as_arraylist_append_int64(&vals, 3000); assert (aerospike_lstack_push_all(&as, &err, NULL, &g_key, &lstack, (const as_list*)&vals) == AEROSPIKE_OK); as_arraylist_destroy(&vals); LOG("3 more values pushed"); // Peek all the values back again. as_ldt_init(&lstack2, "mystack", AS_LDT_LSTACK, NULL); assert (aerospike_lstack_peek(&as, &err, NULL, &g_key, &lstack2, 10, &p_list) == AEROSPIKE_OK); // See if the elements match what we expect. #if 0 as_arraylist_iterator_init(&it, (const as_arraylist*)p_list); while (as_arraylist_iterator_has_next(&it)) { const as_val* p_val = as_arraylist_iterator_next(&it); char* p_str = as_val_tostring(p_val); LOG(" peek - type = %d, value = %s", as_val_type(p_val), p_str); free(p_str); } #else const as_arraylist* p_array = (const as_arraylist*)p_list; int i; for (i = 0; i < p_array->size; i++) { const as_val* p_val = p_array->elements[i]; char* p_str = as_val_tostring(p_val); LOG(" peek - type = %d, value = %s", as_val_type(p_val), p_str); free(p_str); } #endif as_list_destroy(p_list); p_list = NULL; // Set capacity for the lstack. assert (aerospike_lstack_set_capacity(&as, &err, NULL, &g_key, &lstack, 10000) == AEROSPIKE_OK); // Get capacity from the lstack. assert (aerospike_lstack_get_capacity(&as, &err, NULL, &g_key, &lstack, &cap_size) == AEROSPIKE_OK); assert (cap_size == 10000); // Verify that the LDT is now present. as_boolean_init(&ldt_exists, false); assert (aerospike_lstack_ldt_exists(&as, &err, NULL, &g_key, &lstack, &ldt_exists) == AEROSPIKE_OK); assert (as_boolean_get(&ldt_exists) == true); LOG("verified that lstack ldt is present"); // Destroy the lstack. assert (aerospike_lstack_destroy(&as, &err, NULL, &g_key, &lstack) == AEROSPIKE_OK); // See if we can still do any lstack operations. assert (aerospike_lstack_size(&as, &err, NULL, &g_key, &lstack, &n_elements) != AEROSPIKE_OK); // Cleanup and disconnect from the database cluster. example_cleanup(&as); LOG("lstack example successfully completed"); return 0; }