ERL_NIF_TERM as_ldt_lset_get(ErlNifEnv* env, handle_t* handle, void* obj) { ldt_get_args_t* args = (ldt_get_args_t*)obj; as_status res; as_error err; as_list* p_list = NULL; res = aerospike_lset_filter(&handle->instance, &err, NULL, &args->key, &args->ldt, NULL, NULL, &p_list); as_ldt_clean_get_args(env, args); if(res != AEROSPIKE_OK) return A_AS_ERROR(env, err); ERL_NIF_TERM* results; uint32_t nresults = as_list_size(p_list); results = malloc(sizeof(ERL_NIF_TERM) * nresults); as_arraylist_iterator it; as_arraylist_iterator_init(&it, (const as_arraylist*)p_list); int i = 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); results[i++] = make_nif_term_from_as_val(env, p_val); } as_list_destroy(p_list); p_list = NULL; ERL_NIF_TERM returnValue; returnValue = enif_make_list_from_array(env, results, nresults); free(results); return A_OK_VALUE(env, returnValue); }
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; }
static const as_val * _as_arraylist_iterator_next(as_iterator * i) { return as_arraylist_iterator_next((as_arraylist_iterator *) i); }
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[]) { 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; }