char *as_pair_val_tostring(const as_val * v) { as_pair * p = as_pair_fromval(v); if ( p == NULL ) return NULL; char * a = as_val_tostring(p->_1); size_t al = strlen(a); char * b = as_val_tostring(p->_2); size_t bl = strlen(b); size_t l = al + bl + 5; char * str = (char *) cf_malloc(sizeof(char) * l); if (!str) return str; strcpy(str, "("); strcpy(str+1, a); strcpy(str+1+al,", "); strcpy(str+1+al+2, b); strcpy(str+1+al+2+bl,")"); *(str+1+al+2+bl+1) = '\0'; cf_free(a); cf_free(b); return str; }
void dump_record(const as_record* p_rec) { if (! p_rec) { info(" null as_record object"); return; } if (p_rec->key.valuep) { char* key_val_as_str = as_val_tostring(p_rec->key.valuep); info(" key: %s", key_val_as_str); free(key_val_as_str); } uint16_t num_bins = as_record_numbins(p_rec); info(" generation %u, ttl %u, %u bin%s", p_rec->gen, p_rec->ttl, num_bins, num_bins == 0 ? "s" : (num_bins == 1 ? ":" : "s:")); as_record_iterator it; as_record_iterator_init(&it, p_rec); while (as_record_iterator_has_next(&it)) { dump_bin(as_record_iterator_next(&it)); } as_record_iterator_destroy(&it); }
static bool key_basics_print_bins(const char * name, const as_val * value, void * udata) { char * sval = as_val_tostring(value); info("bin: name=%s, value=%s", name, sval); free(sval); return true; }
int lset_config_test(char * keystr, char * ldt_bin) { static char * meth = "lset_config_test()"; if( LSET_DEBUG ) { INFO(" [ENTER]:<%s:%s>:From %s", MOD, LDT, meth ); } int rc = 1; char * valstr; as_result * resultp; time_t cur_t; cur_t = time(NULL); cl_cluster * c = lset_g_config->asc; cl_object o_key; char * ns = lset_g_config->ns; char * set = lset_g_config->set; char * bname = ldt_bin; citrusleaf_object_init_str( &o_key, keystr ); //print config of lset rc = aerospike_lset_config( &resultp, c, ns, set, &o_key, bname, lset_g_config->timeout_ms); if ( rc == CITRUSLEAF_OK ) { valstr = as_val_tostring( resultp->value ); printf(" Config parameters:\n %s \n", valstr); } citrusleaf_object_free( &o_key ); return rc; }
/* * Internal Function: Entry function from UDF code path to send * success result to the caller. Performs * value translation. */ void send_result(as_result * res, udf_call * call) { as_val * v = res->value; if ( res->is_success ) { if ( cf_context_at_severity(AS_UDF, CF_DETAIL) ) { char * str = as_val_tostring(v); cf_detail(AS_UDF, "SUCCESS: %s", str); cf_free(str); } send_success(call, v); } else { // Else -- NOT success if (as_val_type(v) == AS_STRING) { send_udf_failure(call, as_string_fromval(v)); } else { char lua_err_str[1024]; size_t len = (size_t)sprintf(lua_err_str, "%s:0: in function %s() - error() argument type not handled", call->def.filename, call->def.function); call->tr->result_code = AS_PROTO_RESULT_FAIL_UDF_EXECUTION; send_failure_str(call, lua_err_str, len); } } }
static void example_dump_op(const as_binop* p_binop) { if (! p_binop) { LOG(" null as_binop object"); return; } if (p_binop->op == AS_OPERATOR_TOUCH) { LOG(" %s", AS_OPERATORS[p_binop->op]); return; } if (p_binop->op == AS_OPERATOR_READ) { LOG(" %s : %s", AS_OPERATORS[p_binop->op], p_binop->bin.name); return; } char* val_as_str = as_val_tostring(p_binop->bin.valuep); LOG(" %s : %s : %s", AS_OPERATORS[p_binop->op], p_binop->bin.name, val_as_str); free(val_as_str); }
int lset_process_read_results( char * meth, cl_rv rc, as_result * resultp, int i, int * hitsp, int * missesp, int * errsp, as_val * valp) { static char * tm = "process_read_results()"; if( LSET_DEBUG ) { INFO(" [ENTER]:<%s:%s>:%s From (%s) i(%d)", MOD, LDT, meth, tm, i ); } char * valstr; if( LSET_DEBUG ){ char * valstr = as_val_tostring( valp ); printf("<%s:%s> Result(%d) Search(%s)\n", MOD, meth, rc, valstr); free( valstr ); } if( rc == CITRUSLEAF_OK ){ if ( resultp && resultp->is_success ) { if( LSET_DEBUG ){ valstr = as_val_tostring( resultp->value ); printf("[DEBUG]<%s:%s>LSET READ SUCCESS: Val(%s)\n", MOD, meth, valstr); free( valstr ); (*hitsp)++; } } else { (*missesp)++; INFO("[ERROR]<%s:%s>LSET Read OK: Result Error: i(%d) rc(%d)", MOD, meth, i, rc); return CITRUSLEAF_FAIL_INVALID_DATA; } } else if( rc == CITRUSLEAF_FAIL_NOTFOUND ){ (*errsp)++; INFO("[ERROR]<%s:%s>LSET Read Record NOT FOUND: i(%d) rc(%d)", MOD, meth, i, rc); } else { (*errsp)++; INFO("[ERROR]<%s:%s>OTHER ERROR: i(%d) rc(%d)", MOD, meth, i, rc); } return rc; } // end process_read_results()
void dump_bin(const as_bin* p_bin) { if (! p_bin) { info(" null as_bin object"); return; } char* val_as_str = as_val_tostring(as_bin_get_value(p_bin)); info(" %s : %s", as_bin_get_name(p_bin), val_as_str); free(val_as_str); }
static void example_dump_bin(const as_bin* p_bin) { if (! p_bin) { LOG(" null as_bin object"); return; } char* val_as_str = as_val_tostring(as_bin_get_value(p_bin)); LOG(" %s : %s", as_bin_get_name(p_bin), val_as_str); free(val_as_str); }
static int mod_lua_map_tostring(lua_State * l) { mod_lua_box * box = mod_lua_checkbox(l, 1, CLASS_NAME); as_val * val = mod_lua_box_value(box); char * str = NULL; if ( val ) { str = as_val_tostring(val); } if ( str ) { lua_pushstring(l, str); cf_free(str); } else { lua_pushstring(l, "Map()"); } return 1; }
/** * Execute a query and call the callback function for each result item. * * @param as - the aerospike cluster to connect to. * @param err - the error is populated if the return value is not AEROSPIKE_OK. * @param policy - the policy to use for this operation. If NULL, then the default policy will be used. * @param query - the query to execute against the cluster * @param udata - user-data to be passed to the callback * @param callback - the callback function to call for each result item. * * @return AEROSPIKE_OK on success, otherwise an error. */ as_status aerospike_query_foreach( aerospike * as, as_error * err, const as_policy_query * policy, const as_query * query, aerospike_query_foreach_callback callback, void * udata) { // we want to reset the error so, we have a clean state as_error_reset(err); as_val * err_val = NULL; // resolve policies // as_policy_query p; // as_policy_query_resolve(&p, &as->config.policies, policy); if ( aerospike_query_init(as, err) != AEROSPIKE_OK ) { return err->code; } cl_query * clquery = as_query_toclquery(query); clquery_bridge bridge = { .udata = udata, .callback = callback }; cl_rv rc = citrusleaf_query_foreach(as->cluster, clquery, &bridge, clquery_callback, &err_val); as_status ret = as_error_fromrc(err, rc); if (CITRUSLEAF_OK != rc && err_val) { char * err_str = as_val_tostring(err_val); if(err_str) { strncat(err->message," : ",sizeof(err->message) - strlen(err->message)); strncat(err->message,err_str,sizeof(err->message) - strlen(err->message)); cf_free(err_str); } as_val_destroy(err_val); } cl_query_destroy(clquery); return ret; }
bool query_cb_map(const as_val* p_val, void* udata) { if (! p_val) { LOG("query callback returned null - query is complete"); return true; } // Because of the UDF used, we expect an as_map to be returned. if (! as_map_fromval(p_val)) { LOG("query callback returned non-as_map object"); return true; } // The map keys are number tokens ("1" to "10") and each value is the total // number of occurrences of the token in the records aggregated. char* val_as_str = as_val_tostring(p_val); LOG("query callback returned %s", val_as_str); free(val_as_str); return true; }
int main(int argc, char* argv[]) { // Parse command line arguments. if (! example_get_opts(argc, argv, EXAMPLE_BASIC_OPTS)) { exit(-1); } // Connect to the aerospike database cluster. aerospike as; example_connect_to_aerospike(&as); // Start clean. example_remove_test_record(&as); as_ldt lstack; // Create a large stack bin to use. No need to destroy as_ldt if using // as_ldt_init() on stack object. if (! as_ldt_init(&lstack, "mystack", AS_LDT_LSTACK, NULL)) { LOG("unable to initialize ldt"); example_cleanup(&as); exit(-1); } as_error err; // No need to destroy as_integer if using as_integer_init() on stack object. as_integer ival; as_integer_init(&ival, 123); // Push a few values onto the stack. if (aerospike_lstack_push(&as, &err, NULL, &g_key, &lstack, (const as_val*)&ival) != AEROSPIKE_OK) { LOG("first aerospike_lstack_push() returned %d - %s", err.code, err.message); example_cleanup(&as); exit(-1); } // No need to destroy as_string if using as_string_init() on stack object. as_string sval; as_string_init(&sval, "string stack value", false); if (aerospike_lstack_push(&as, &err, NULL, &g_key, &lstack, (const as_val*)&sval) != AEROSPIKE_OK) { LOG("second aerospike_lstack_push() returned %d - %s", err.code, err.message); example_cleanup(&as); exit(-1); } LOG("2 values pushed"); uint32_t n_elements = 0; // Look at the stack size right now. if (aerospike_lstack_size(&as, &err, NULL, &g_key, &lstack, &n_elements) != AEROSPIKE_OK) { LOG("aerospike_lstack_size() returned %d - %s", err.code, err.message); example_cleanup(&as); exit(-1); } if (n_elements != 2) { LOG("unexpected stack size %u", n_elements); example_cleanup(&as); exit(-1); } LOG("stack size confirmed to be %u", n_elements); as_ldt lstack2; as_ldt_init(&lstack2, "mystack", AS_LDT_LSTACK, NULL); uint32_t peek_count = 3; as_list* p_list = NULL; // Peek a few values back. if (aerospike_lstack_peek(&as, &err, NULL, &g_key, &lstack2, peek_count, &p_list) != AEROSPIKE_OK) { LOG("first aerospike_lstack_peek() returned %d - %s", err.code, err.message); as_list_destroy(p_list); example_cleanup(&as); exit(-1); } as_arraylist_iterator it; as_arraylist_iterator_init(&it, (const as_arraylist*)p_list); // See if the elements match what we expect. 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); if (aerospike_lstack_push_all(&as, &err, NULL, &g_key, &lstack, (const as_list*)&vals) != AEROSPIKE_OK) { LOG("aerospike_lstack_pushall() returned %d - %s", err.code, err.message); as_arraylist_destroy(&vals); example_cleanup(&as); exit(-1); } as_arraylist_destroy(&vals); LOG("3 more values pushed"); as_ldt_init(&lstack2, "mystack", AS_LDT_LSTACK, NULL); peek_count = 10; // Peek all the values back again. if (aerospike_lstack_peek(&as, &err, NULL, &g_key, &lstack2, peek_count, &p_list) != AEROSPIKE_OK) { LOG("second aerospike_lstack_peek() returned %d - %s", err.code, err.message); as_list_destroy(p_list); example_cleanup(&as); exit(-1); } // 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); // Set capacity for the lstack. if (aerospike_lstack_set_capacity(&as, &err, NULL, &g_key, &lstack, 10000) != AEROSPIKE_OK) { LOG("aerospike_lstack_set_capacity() returned %d - %s", err.code, err.message); example_cleanup(&as); exit(-1); } uint32_t cap_size = 0; // Get capacity from the lstack. if (aerospike_lstack_get_capacity(&as, &err, NULL, &g_key, &lstack, &cap_size) != AEROSPIKE_OK) { LOG("aerospike_lstack_get_capacity() returned %d - %s", err.code, err.message); example_cleanup(&as); exit(-1); } if (cap_size != 10000) { LOG("unexpected capacity size %u", cap_size); example_cleanup(&as); exit(-1); } // Destroy the lstack. if (aerospike_lstack_destroy(&as, &err, NULL, &g_key, &lstack) != AEROSPIKE_OK) { LOG("aerospike_lstack_destroy() returned %d - %s", err.code, err.message); example_cleanup(&as); exit(-1); } n_elements = 0; // See if we can still do any lstack operations. if (aerospike_lstack_size(&as, &err, NULL, &g_key, &lstack, &n_elements) == AEROSPIKE_OK) { LOG("aerospike_lstack_size() did not return error"); example_cleanup(&as); exit(-1); } // Cleanup and disconnect from the database cluster. example_cleanup(&as); LOG("lstack example successfully completed"); return 0; }
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; }
static int get(lua_State *L){ //printf("-get-\n"); aerospike* as = lua_touserdata(L, 1); const char* nameSpace = luaL_checkstring(L, 2); const char* set = luaL_checkstring(L, 3); const char* keyString = luaL_checkstring(L, 4); //printf("key-:%s\n", keyString); as_record* rec = NULL; as_key key; as_error err; as_key_init(&key, nameSpace, set, keyString); // Read the test record from the database. aerospike_key_get(as, &err, NULL, &key, &rec); // Push the error code lua_pushnumber(L, err.code); // Push the error message lua_pushstring(L, err.message); // Create an new table and push it if ( err.code == AEROSPIKE_OK){ lua_newtable(L); /* create table to hold Bins read */ /* * iterate through bin and add the bin name * and value to the table */ as_record_iterator it; as_record_iterator_init(&it, rec); while (as_record_iterator_has_next(&it)) { as_bin *bin = as_record_iterator_next(&it); as_val *value = (as_val*)as_bin_get_value(bin); char * binName = as_bin_get_name(bin); int bin_type = as_val_type(value); //Bin Type switch (bin_type){ case AS_INTEGER: //printf("--integer-%s-\n", binName); lua_pushstring(L, binName); //Bin name lua_pushnumber(L, as_integer_get(as_integer_fromval(value))); //printf("--integer-end-\n"); break; case AS_DOUBLE: //printf("--double-%s-\n", binName); lua_pushstring(L, binName); //Bin name lua_pushnumber(L, as_double_get(as_double_fromval(value))); //printf("--double-end-\n"); break; case AS_STRING: //printf("--string-%s-\n", binName); lua_pushstring(L, binName); //Bin name lua_pushstring(L, as_val_tostring(value)); //printf("--string-end-\n"); break; case AS_LIST: //printf("--list-%s-\n", binName); lua_pushstring(L, binName); //Bin name // Iterate through arraylist populating table as_list* p_list = as_list_fromval(value); as_arraylist_iterator it; as_arraylist_iterator_init(&it, (const as_arraylist*)p_list); // create a Lua inner table table for the "List" lua_newtable(L); int count = 0; // See if the elements match what we expect. while (as_arraylist_iterator_has_next(&it)) { const as_val* p_val = as_arraylist_iterator_next(&it); //Assume string char* p_str = as_val_tostring(p_val); lua_pushnumber(L, count); // table[i] lua_pushstring(L, p_str); //Value //printf("%d => %s\n", count, p_str); count++; lua_settable(L, -3); } //printf("--list-end-\n"); break; } //printf("--settable-\n"); lua_settable(L, -3); //printf("--settable-end-\n"); } } as_record_destroy(rec); as_key_destroy(&key); //printf("-get-end-\n"); return 3; }
int main(int argc, char* argv[]) { // Parse command line arguments. if (! example_get_opts(argc, argv, EXAMPLE_BASIC_OPTS)) { exit(-1); } // Connect to the aerospike database cluster. aerospike as; example_connect_to_aerospike(&as); // Start clean. example_remove_test_record(&as); as_ldt lmap; // Create a lmap bin to use. No need to destroy as_ldt if using // as_ldt_init() on stack object. if (! as_ldt_init(&lmap, "mylmap", AS_LDT_LMAP, NULL)) { LOG("unable to initialize ldt"); example_cleanup(&as); exit(-1); } as_error err; // No need to destroy as_integer if using as_integer_init() on stack object. as_integer ikey; as_integer_init(&ikey, 12345); // No need to destroy as_string if using as_string_init() on stack object // with free parameter false. as_string sval; as_string_init(&sval, "lmap value", false); // Put a string entry to the lmap. if (aerospike_lmap_put(&as, &err, NULL, &g_key, &lmap, (const as_val*)&ikey, (as_val *)&sval) != AEROSPIKE_OK) { LOG("first aerospike_lmap_put() returned %d - %s", err.code, err.message); example_cleanup(&as); exit(-1); } as_integer ival; as_integer_init(&ival, 1000); // It's ok to reset the as_integer. as_integer_init(&ikey, 345); // Put an integer entry to the lmap. if (aerospike_lmap_put(&as, &err, NULL, &g_key, &lmap, (const as_val*)&ikey, (as_val*)&ival) != AEROSPIKE_OK) { LOG("second aerospike_lmap_put() returned %d - %s", err.code, err.message); example_cleanup(&as); exit(-1); } LOG("2 entries added to map"); uint32_t n_elements = 0; // See how many elements we have in the lmap now. if (aerospike_lmap_size(&as, &err, NULL, &g_key, &lmap, &n_elements) != AEROSPIKE_OK) { LOG("aerospike_lmap_size() returned %d - %s", err.code, err.message); example_cleanup(&as); exit(-1); } if (n_elements != 2) { LOG("unexpected lmap size %u", n_elements); example_cleanup(&as); exit(-1); } LOG("lmap size confirmed to be %u", n_elements); as_ldt lmap2; as_ldt_init(&lmap2, "mylmap", AS_LDT_LMAP, NULL); as_map* p_map = NULL; // Get all the entries back. if (aerospike_lmap_get_all(&as, &err, NULL, &g_key, &lmap, &p_map) != AEROSPIKE_OK) { LOG("aerospike_lmap_filter() returned %d - %s", err.code, err.message); example_cleanup(&as); exit(-1); } // See if the elements match what we expect. as_hashmap_iterator it; as_hashmap_iterator_init(&it, (const as_hashmap*)p_map); while (as_hashmap_iterator_has_next(&it)) { const as_val* p_val = as_hashmap_iterator_next(&it); char* p_str = as_val_tostring(p_val); LOG(" element - type = %d, value = %s", as_val_type(p_val), p_str); free(p_str); } as_map_destroy(p_map); p_map = NULL; as_integer_init(&ikey, 345); as_integer_init(&ival, 2000); // Remove an entry from the map. as_integer_init(&ikey, 12345); if (aerospike_lmap_remove(&as, &err, NULL, &g_key, &lmap, (const as_val*)&ikey) != AEROSPIKE_OK) { LOG("aerospike_lmap_remove() returned %d - %s", err.code, err.message); example_cleanup(&as); exit(-1); } as_val* p_val = NULL; // Make sure we cannot get the value any more. if (aerospike_lmap_get(&as, &err, NULL, &g_key, &lmap, (const as_val*)&ikey, &p_val) == AEROSPIKE_OK) { LOG("unexpected success getting a removed entry"); as_val_destroy(p_val); example_cleanup(&as); exit(-1); } LOG("entry successfully removed"); // Destroy the lmap. if (aerospike_lmap_destroy(&as, &err, NULL, &g_key, &lmap) != AEROSPIKE_OK) { LOG("aerospike_lmap_destroy() returned %d - %s", err.code, err.message); example_cleanup(&as); exit(-1); } n_elements = 0; // See if we can still do any lmap operations. if (aerospike_lmap_size(&as, &err, NULL, &g_key, &lmap, &n_elements) == AEROSPIKE_OK) { LOG("aerospike_lmap_size() did not return error"); example_cleanup(&as); exit(-1); } LOG("lmap successfully destroyed"); // Cleanup and disconnect from the database cluster. example_cleanup(&as); LOG("lmap example successfully completed"); return 0; }
int main(int argc, char* argv[]) { // Parse command line arguments. if (! example_get_opts(argc, argv, EXAMPLE_BASIC_OPTS)) { exit(-1); } // Connect to the aerospike database cluster. aerospike as; example_connect_to_aerospike(&as); // Start clean. example_remove_test_record(&as); // Create a large map object to use. No need to destroy lmap if using // as_ldt_init() on stack object. as_ldt lmap; if (! as_ldt_init(&lmap, "mylmap", AS_LDT_LMAP, NULL)) { LOG("unable to initialize ldt"); example_cleanup(&as); exit(-1); } as_error err; as_boolean ldt_exists; as_boolean_init(&ldt_exists, false); // Verify that the LDT is not already there. if (aerospike_lmap_ldt_exists(&as, &err, NULL, &g_key, &lmap, &ldt_exists) != AEROSPIKE_OK) { int rc = example_handle_udf_error(&err, "first aerospike_lmap_ldt_exists()"); example_cleanup(&as); exit(rc); } if (as_boolean_get(&ldt_exists)) { LOG("found ldt that should not be present"); example_cleanup(&as); exit(-1); } LOG("verified that lmap ldt is not present"); // No need to destroy ikey if using as_integer_init() on stack object. as_integer ikey; as_integer_init(&ikey, 12345); // No need to destroy sval if using as_string_init() on stack object with // free parameter false. as_string sval; as_string_init(&sval, "lmap value", false); // Put a string entry to the lmap. if (aerospike_lmap_put(&as, &err, NULL, &g_key, &lmap, (const as_val*)&ikey, (as_val *)&sval) != AEROSPIKE_OK) { LOG("first aerospike_lmap_put() returned %d - %s", err.code, err.message); example_cleanup(&as); exit(-1); } // Ok to reuse. as_integer_init(&ikey, 345); as_integer ival; as_integer_init(&ival, 1000); // Put an integer entry to the lmap. if (aerospike_lmap_put(&as, &err, NULL, &g_key, &lmap, (const as_val*)&ikey, (as_val*)&ival) != AEROSPIKE_OK) { LOG("second aerospike_lmap_put() returned %d - %s", err.code, err.message); example_cleanup(&as); exit(-1); } LOG("2 entries added to map"); uint32_t n_elements = 0; // See how many elements we have in the lmap now. if (aerospike_lmap_size(&as, &err, NULL, &g_key, &lmap, &n_elements) != AEROSPIKE_OK) { LOG("aerospike_lmap_size() returned %d - %s", err.code, err.message); example_cleanup(&as); exit(-1); } if (n_elements != 2) { LOG("unexpected lmap size %u", n_elements); example_cleanup(&as); exit(-1); } LOG("lmap size confirmed to be %u", n_elements); as_boolean_init(&ldt_exists, false); // Verify that the LDT is now present. if (aerospike_lmap_ldt_exists(&as, &err, NULL, &g_key, &lmap, &ldt_exists) != AEROSPIKE_OK) { LOG("first aerospike_lmap_ldt_exists() returned %d - %s", err.code, err.message); example_cleanup(&as); exit(-1); } if (! as_boolean_get(&ldt_exists)) { LOG("did not find ldt that should be be present"); example_cleanup(&as); exit(-1); } LOG("verified that lmap ldt is present"); as_map* p_map = NULL; // Get all the entries back. if (aerospike_lmap_get_all(&as, &err, NULL, &g_key, &lmap, &p_map) != AEROSPIKE_OK) { LOG("aerospike_lmap_filter() returned %d - %s", err.code, err.message); example_cleanup(&as); exit(-1); } as_hashmap_iterator it; as_hashmap_iterator_init(&it, (const as_hashmap*)p_map); // See if the elements match what we expect. while (as_hashmap_iterator_has_next(&it)) { const as_val* p_val = as_hashmap_iterator_next(&it); char* p_str = as_val_tostring(p_val); LOG(" element type %d, value %s", as_val_type(p_val), p_str); free(p_str); } as_map_destroy(p_map); p_map = NULL; as_integer_init(&ikey, 12345); // Remove an entry from the map. if (aerospike_lmap_remove(&as, &err, NULL, &g_key, &lmap, (const as_val*)&ikey) != AEROSPIKE_OK) { LOG("aerospike_lmap_remove() returned %d - %s", err.code, err.message); example_cleanup(&as); exit(-1); } as_val* p_val = NULL; // Make sure we can't get the value any more. as_status result = aerospike_lmap_get(&as, &err, NULL, &g_key, &lmap, (const as_val*)&ikey, &p_val); if (result == AEROSPIKE_OK) { // Server version >= 3.4.1 returns empty map if element doesn't exist. if (p_val && (as_val_type(p_val) != AS_MAP || as_map_size((as_map*)p_val) != 0)) { char* p_str = as_val_tostring(p_val); LOG("entry was not successfully removed"); LOG(" element type %d, value %s", as_val_type(p_val), p_str); free(p_str); as_val_destroy(p_val); example_cleanup(&as); exit(-1); } } else if (result != AEROSPIKE_ERR_LARGE_ITEM_NOT_FOUND) { LOG("aerospike_lmap_get() returned %d - %s", err.code, err.message); example_cleanup(&as); exit(-1); } LOG("entry successfully removed"); // Destroy the lmap. if (aerospike_lmap_destroy(&as, &err, NULL, &g_key, &lmap) != AEROSPIKE_OK) { LOG("aerospike_lmap_destroy() returned %d - %s", err.code, err.message); example_cleanup(&as); exit(-1); } n_elements = 0; // See if we can still do any lmap operations. if (aerospike_lmap_size(&as, &err, NULL, &g_key, &lmap, &n_elements) == AEROSPIKE_OK) { LOG("aerospike_lmap_size() did not return error"); example_cleanup(&as); exit(-1); } LOG("lmap successfully destroyed"); // Cleanup and disconnect from the database cluster. example_cleanup(&as); LOG("lmap example successfully completed"); return 0; }
/* * Internal Function: Entry function from UDF code path to send * success result to the caller. Performs * value translation. */ void send_result(as_result * res, udf_call * call, void *udata) { // The following "no-op" line serves to quiet the compiler warning of an // otherwise unused variable. udata = udata; as_val * v = res->value; if ( res->is_success ) { if ( cf_context_at_severity(AS_UDF, CF_DETAIL) ) { char * str = as_val_tostring(v); cf_detail(AS_UDF, "SUCCESS: %s", str); cf_free(str); } if ( v != NULL ) { switch( as_val_type(v) ) { case AS_NIL: { send_success(call, AS_PARTICLE_TYPE_NULL, NULL, 0); break; } case AS_BOOLEAN: { as_boolean * b = as_boolean_fromval(v); int64_t bi = as_boolean_tobool(b) == true ? 1 : 0; send_success(call, AS_PARTICLE_TYPE_INTEGER, &bi, 8); break; } case AS_INTEGER: { as_integer * i = as_integer_fromval(v); int64_t ri = as_integer_toint(i); send_success(call, AS_PARTICLE_TYPE_INTEGER, &ri, 8); break; } case AS_STRING: { // this looks bad but it just pulls the pointer // out of the object as_string * s = as_string_fromval(v); char * rs = (char *) as_string_tostring(s); send_success(call, AS_PARTICLE_TYPE_STRING, rs, as_string_len(s)); break; } case AS_BYTES: { as_bytes * b = as_bytes_fromval(v); uint8_t * rs = as_bytes_get(b); send_success(call, AS_PARTICLE_TYPE_BLOB, rs, as_bytes_size(b)); break; } case AS_MAP: case AS_LIST: { as_buffer buf; as_buffer_init(&buf); as_serializer s; as_msgpack_init(&s); int res = as_serializer_serialize(&s, v, &buf); if (res != 0) { const char * error = "Complex Data Type Serialization failure"; cf_warning(AS_UDF, "%s (%d)", (char *)error, res); as_buffer_destroy(&buf); send_cdt_failure(call, AS_PARTICLE_TYPE_STRING, (char *)error, strlen(error)); } else { // Do not use this until after cf_detail_binary() can accept larger buffers. // cf_detail_binary(AS_UDF, buf.data, buf.size, CF_DISPLAY_HEX_COLUMNS, // "serialized %d bytes: ", buf.size); send_success(call, to_particle_type(as_val_type(v)), buf.data, buf.size); // Not needed stack allocated - unless serialize has internal state // as_serializer_destroy(&s); as_buffer_destroy(&buf); } break; } default: { cf_debug(AS_UDF, "SUCCESS: VAL TYPE UNDEFINED %d\n", as_val_type(v)); send_success(call, AS_PARTICLE_TYPE_STRING, NULL, 0); break; } } } else { send_success(call, AS_PARTICLE_TYPE_NULL, NULL, 0); } } else { // Else -- NOT success as_string * s = as_string_fromval(v); char * rs = (char *) as_string_tostring(s); cf_debug(AS_UDF, "FAILURE when calling %s %s %s", call->filename, call->function, rs); send_udf_failure(call, AS_PARTICLE_TYPE_STRING, rs, as_string_len(s)); } }