static int check_bin4(as_record * rec, scan_check * check) { as_val * bin = (as_val *) as_record_get(rec, "bin4"); if ( !bin ) { error("Expected a value in bin('%s'), but got null", "bin4"); return !(check->failed = true); } as_list * list = as_list_fromval(bin); if ( !list ) { error("Expected a list in bin('%s'), but got type %d", "bin4", as_val_type(bin)); return !(check->failed = true); } int sz = as_list_size(list); if ( sz < 3 ) { error("Expected list size of %d, but got %d", 3, sz); return !(check->failed = true); } for ( int i = 0; i < sz; i++ ) { as_val * val = as_list_get(list, i); if ( !val ) { error("Expecting value at %d, but got null", i); return !(check->failed = true); } as_integer * ival = as_integer_fromval(val); if ( !ival ) { error("Expecting integer at %d, but got type %d", i, as_val_type(val)); return !(check->failed = true); } } return !(check->failed = false); }
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; }
/** * Get specified bin's value as an as_list. * as_list * value = as_record_get_list(rec, "bin"); * @param rec - the record containing the bin * @param name - the name of the bin * @return the value if it exists, otherwise NULL. */ as_list * as_record_get_list(const as_record * rec, const as_bin_name name) { return as_list_fromval((as_val *) as_record_get(rec, name)); }