/** * @brief Test whether multiple records exist in the cluster. * * @param[in] argc The argc * @param argv The argv * @param[in] self The object * * @return { description_of_the_return_value } */ static VALUE batch_read(int argc, VALUE * argv, VALUE self) { rb_aero_TIMED(tm); as_error err; as_status status; aerospike * as = rb_aero_CLIENT; VALUE keys; VALUE options; rb_scan_args(argc, argv, "11", &keys, &options); long keys_len = rb_ary_len_long(keys); as_batch batch; as_batch_inita(&batch, keys_len); VALUE return_data = rb_hash_new(); for (long i = 0; i < keys_len; ++i) { VALUE element = rb_ary_entry(keys, i); // set into hash for return values rb_hash_aset(return_data, element, Qfalse); VALUE tmp = rb_funcall(element, rb_intern("namespace"), 0); char * c_namespace = StringValueCStr( tmp ); tmp = rb_funcall(element, rb_intern("set"), 0); char * c_set = StringValueCStr( tmp ); tmp = rb_funcall(element, rb_intern("key"), 0); if ( TYPE(tmp) != T_FIXNUM ) { char * c_key = StringValueCStr( tmp ); as_key_init(as_batch_keyat(&batch,i), c_namespace, c_set, c_key); } else { as_key_init_int64(as_batch_keyat(&batch,i), c_namespace, c_set, FIX2LONG(tmp)); } } if ( aerospike_batch_get(as, &err, NULL, &batch, batch_read_callback, return_data) != AEROSPIKE_OK ) { as_batch_destroy(&batch); raise_as_error(err); } as_batch_destroy(&batch); rb_aero_logger(AS_LOG_LEVEL_DEBUG, &tm, 1, rb_str_new2("[Client][batch_read] success")); return return_data; }
static void as_put_callback1(as_error* err, void* udata, as_event_loop* event_loop) { assert_success_async(&monitor, err, udata); as_key key; as_key_init(&key, NAMESPACE, SET, "pa1"); as_error e; as_status status = aerospike_key_get_async(as, &e, NULL, &key, as_get_callback1, __result__, event_loop, NULL); assert_status_async(&monitor, status, &e); }
static int put(lua_State *L){ //Cluster aerospike* as = lua_touserdata(L, 1); //Namespace const char* nameSpace = luaL_checkstring(L, 2); //Set const char* set = luaL_checkstring(L, 3); //Key as string const char* keyString = luaL_checkstring(L, 4); // Number of bins. const int numBins = lua_tointeger(L, 5); //Bins as_record rec = add_bins_to_rec(L, 6, numBins); //const as_record * test = &rec; //if (as_val_type(as_record_get(test, "animals")) == AS_LIST) // printf("correct list\n"); //else // printf("not a list\n"); // Create key as_key key; as_error err; as_key_init(&key, nameSpace, set, keyString); // Write record aerospike_key_put(as, &err, NULL, &key, &rec); as_key_destroy(&key); as_record_destroy(&rec); // Return status lua_pushnumber(L, err.code); lua_pushstring(L, err.message); return 2; }
static void insert_data(int numrecs, const char *setname) { as_status rc; char strval[SET_STRSZ], strkey[SET_STRSZ]; as_error err; as_error_reset(&err); for (int i=0; i<numrecs; i++) { sprintf(strval, "str-%s-%d", setname ? setname : "noset", i); sprintf(strkey, "key-%s-%d", setname, i); // Map bin as_hashmap m; as_hashmap_init(&m, 8); as_stringmap_set_int64((as_map *) &m, "x", i); as_stringmap_set_int64((as_map *) &m, "y", i+1); as_stringmap_set_int64((as_map *) &m, "z", i+2); as_record r; as_record_init(&r, 3); as_record_set_int64(&r, "bin1", i); as_record_set_str(&r, "bin2", strval); as_record_set_map(&r, "bin3", (as_map *) &m); as_key k; as_key_init(&k, NS, setname, strkey); rc = aerospike_key_put(as, &err, NULL, &k, &r); if (rc != AEROSPIKE_OK) { error("digest put failed with error %d", rc); } as_hashmap_destroy(&m); as_key_destroy(&k); as_record_destroy(&r); } }
static int increment(lua_State *L){ as_error err; 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); const int numBins = lua_tointeger(L, 5); as_operations ops = add_bins_to_increment(L, 6, numBins); as_key key; as_key_init(&key, nameSpace, set, keyString); // Apply the operations. Since the record does not exist, it will be created // and the bins initialized with the ops' integer values. aerospike_key_operate(as, &err, NULL, &key, &ops, NULL); as_operations_destroy(&ops); as_key_destroy(&key); lua_pushnumber(L, err.code); lua_pushstring(L, err.message); return 2; }
/** * Creates 100 records and 9 indexes. * * Records are structured as: * {a: String, b: Integer, c: Integer, d: Integer, e: Integer} * * The key is "a-b-c-d-e" * * The values are: * a = "abc" * b = 100 * c = <current index> * d = c % 10 * e = b + (c + 1) * (d + 1) / 2 */ bool query_foreach_create() { as_error err; as_error_reset(&err); int n_recs = 100; as_status status; as_index_task task; // create index on "a" status = aerospike_index_create(as, &err, &task, NULL, NAMESPACE, SET, "a", "idx_test_a", AS_INDEX_STRING); index_process_return_code(status, &err, &task); // create index on "b" status = aerospike_index_create(as, &err, &task, NULL, NAMESPACE, SET, "b", "idx_test_b", AS_INDEX_NUMERIC); index_process_return_code(status, &err, &task); // create index on "c" status = aerospike_index_create(as, &err, &task, NULL, NAMESPACE, SET, "c", "idx_test_c", AS_INDEX_NUMERIC); index_process_return_code(status, &err, &task); // create index on "d" status = aerospike_index_create(as, &err, &task, NULL, NAMESPACE, SET, "d", "idx_test_d", AS_INDEX_NUMERIC); index_process_return_code(status, &err, &task); // create complex index on "x" status = aerospike_index_create_complex(as, &err, &task, NULL, NAMESPACE, SET, "x", "idx_test_x", AS_INDEX_TYPE_LIST, AS_INDEX_STRING); index_process_return_code(status, &err, &task); // create complex index on "y" status = aerospike_index_create_complex(as, &err, &task, NULL, NAMESPACE, SET, "y", "idx_test_y", AS_INDEX_TYPE_MAPKEYS, AS_INDEX_STRING); index_process_return_code(status, &err, &task); // create complex index on "y" status = aerospike_index_create_complex(as, &err, &task, NULL, NAMESPACE, SET, "y", "idx_test_y1", AS_INDEX_TYPE_MAPVALUES, AS_INDEX_STRING); index_process_return_code(status, &err, &task); // create complex index on "z" status = aerospike_index_create_complex(as, &err, &task, NULL, NAMESPACE, SET, "z", "idx_test_z", AS_INDEX_TYPE_LIST, AS_INDEX_NUMERIC); index_process_return_code(status, &err, &task); char* buffer = alloca(n_recs * 1024 + 1); uint32_t the_ttl = AS_RECORD_NO_EXPIRE_TTL; // insert records for ( int i = 0; i < n_recs; i++ ) { if (i == 10) { // We change the TTL from never to 100 days the_ttl = 100 * 24 * 60 * 60; } else if (i == 42) { // NOTE - We pause on the 42nd iteration for a few // milliseconds and note the time. We can then use the // as_predexp_rec_last_update_after predicate below to find // the later records. as_sleep(5); g_epochns = cf_clock_getabsolute() * 1000 * 1000; as_sleep(5); // Also on the 42nd iteration we change the TTL to // 10 days for the remaining records. the_ttl = 10 * 24 * 60 * 60; } char * a = "abc"; int b = n_recs; int c = i; int d = i % 10; int e = b + (c + 1) * (d + 1) / 2; int g = i; // Only set on odd records. char f[64]; snprintf(f, sizeof(f), "0x%04x", i); char keystr[64] = { '\0' }; snprintf(keystr, 64, "%s-%d-%d-%d-%d", a, b, c, d, e); // Make list as_arraylist list; as_arraylist_init(&list, 3, 0); if ( (i%3) == 0) { as_arraylist_append_str(&list, "x"); as_arraylist_append_str(&list, "x1"); as_arraylist_append_str(&list, "x2"); } else { as_arraylist_append_str(&list, "not_x1"); as_arraylist_append_str(&list, "not_x2"); as_arraylist_append_str(&list, "not_x3"); } // Make map as_hashmap map; as_hashmap_init(&map, 1); if ( (i%7) == 0) { as_stringmap_set_str((as_map *) &map, "ykey", "yvalue"); } else { as_stringmap_set_str((as_map *) &map, "ykey_not", "yvalue_not"); } // Make list of integers as_arraylist list2; as_arraylist_init(&list2, 5, 0); as_arraylist_append_int64(&list2, i); as_arraylist_append_int64(&list2, i+1); as_arraylist_append_int64(&list2, i+2); as_arraylist_append_int64(&list2, i+3); as_arraylist_append_int64(&list2, i+4); // Make a string of variable size for (int jj = 0; jj < i * 1024; ++jj) { buffer[jj] = 'X'; } buffer[i * 1024] = '\0'; // We only create the g bin for odd records. bool create_g_bin = i % 2 == 1; as_record r; as_record_init(&r, 10 + (create_g_bin ? 1 : 0)); as_record_set_str(&r, "a", a); as_record_set_int64(&r, "b", b); as_record_set_int64(&r, "c", c); as_record_set_int64(&r, "d", d); as_record_set_int64(&r, "e", e); as_record_set_str(&r, "f", f); if (create_g_bin) { as_record_set_int64(&r, "g", g); } as_record_set_list(&r, "x", (as_list *) &list); as_record_set_map(&r, "y", (as_map *) &map); as_record_set_list(&r, "z", (as_list *) &list2); as_record_set_str(&r, "bigstr", buffer); r.ttl = the_ttl; as_key key; as_key_init(&key, NAMESPACE, SET, keystr); aerospike_key_put(as, &err, NULL, &key, &r); as_record_destroy(&r); if (err.code != AEROSPIKE_OK) { error("aerospike_key_put() failed %d %s", err.code, err.message); return false; } as_record *r1 = NULL; aerospike_key_exists(as, &err, NULL, &key, &r1); as_key_destroy(&key); if (err.code != AEROSPIKE_OK) { error("aerospike_key_exists() failed %d %s", err.code, err.message); return false; } if (! r1) { error("key not found %s", keystr); return false; } as_record_destroy(r1); } return true; }
/** * Creates 100 records and 9 indexes. * * Records are structured as: * {a: String, b: Integer, c: Integer, d: Integer, e: Integer} * * The key is "a-b-c-d-e" * * The values are: * a = "abc" * b = 100 * c = <current index> * d = c % 10 * e = b + (c + 1) * (d + 1) / 2 */ bool query_foreach_create() { as_error err; as_error_reset(&err); int n_recs = 100; as_status status; as_index_task task; // create index on "a" status = aerospike_index_create(as, &err, &task, NULL, NAMESPACE, SET, "a", "idx_test_a", AS_INDEX_STRING); if ( status == AEROSPIKE_OK ) { aerospike_index_create_wait(&err, &task, 0); } else { info("error(%d): %s", err.code, err.message); } // create index on "b" status = aerospike_index_create(as, &err, &task, NULL, NAMESPACE, SET, "b", "idx_test_b", AS_INDEX_NUMERIC); if ( status == AEROSPIKE_OK ) { aerospike_index_create_wait(&err, &task, 0); } else { info("error(%d): %s", err.code, err.message); } // create index on "c" status = aerospike_index_create(as, &err, &task, NULL, NAMESPACE, SET, "c", "idx_test_c", AS_INDEX_NUMERIC); if ( status == AEROSPIKE_OK ) { aerospike_index_create_wait(&err, &task, 0); } else { info("error(%d): %s", err.code, err.message); } // create index on "d" status = aerospike_index_create(as, &err, &task, NULL, NAMESPACE, SET, "d", "idx_test_d", AS_INDEX_NUMERIC); if ( status == AEROSPIKE_OK ) { aerospike_index_create_wait(&err, &task, 0); } else { info("error(%d): %s", err.code, err.message); } // create complex index on "x" status = aerospike_index_create_complex(as, &err, &task, NULL, NAMESPACE, SET, "x", "idx_test_x", AS_INDEX_TYPE_LIST, AS_INDEX_STRING); if ( status == AEROSPIKE_OK ) { aerospike_index_create_wait(&err, &task, 0); } else { info("error(%d): %s", err.code, err.message); } // create complex index on "y" status = aerospike_index_create_complex(as, &err, &task, NULL, NAMESPACE, SET, "y", "idx_test_y", AS_INDEX_TYPE_MAPKEYS, AS_INDEX_STRING); if ( status == AEROSPIKE_OK ) { aerospike_index_create_wait(&err, &task, 0); } else { info("error(%d): %s", err.code, err.message); } // create complex index on "y" status = aerospike_index_create_complex(as, &err, &task, NULL, NAMESPACE, SET, "y", "idx_test_y1", AS_INDEX_TYPE_MAPVALUES, AS_INDEX_STRING); if ( status == AEROSPIKE_OK ) { aerospike_index_create_wait(&err, &task, 0); } else { info("error(%d): %s", err.code, err.message); } // create complex index on "z" status = aerospike_index_create_complex(as, &err, &task, NULL, NAMESPACE, SET, "z", "idx_test_z", AS_INDEX_TYPE_LIST, AS_INDEX_NUMERIC); if ( status == AEROSPIKE_OK ) { aerospike_index_create_wait(&err, &task, 0); } else { info("error(%d): %s", err.code, err.message); } // insert records for ( int i = 0; i < n_recs; i++ ) { char * a = "abc"; int b = n_recs; int c = i; int d = i % 10; int e = b + (c + 1) * (d + 1) / 2; char keystr[64] = { '\0' }; snprintf(keystr, 64, "%s-%d-%d-%d-%d", a, b, c, d, e); // Make list as_arraylist list; as_arraylist_init(&list, 3, 0); if ( (i%3) == 0) { as_arraylist_append_str(&list, "x"); as_arraylist_append_str(&list, "x1"); as_arraylist_append_str(&list, "x2"); } else { as_arraylist_append_str(&list, "not_x1"); as_arraylist_append_str(&list, "not_x2"); as_arraylist_append_str(&list, "not_x3"); } // Make map as_hashmap map; as_hashmap_init(&map, 1); if ( (i%7) == 0) { as_stringmap_set_str((as_map *) &map, "ykey", "yvalue"); } else { as_stringmap_set_str((as_map *) &map, "ykey_not", "yvalue_not"); } // Make list of integers as_arraylist list2; as_arraylist_init(&list2, 5, 0); as_arraylist_append_int64(&list2, i); as_arraylist_append_int64(&list2, i+1); as_arraylist_append_int64(&list2, i+2); as_arraylist_append_int64(&list2, i+3); as_arraylist_append_int64(&list2, i+4); as_record r; as_record_init(&r, 9); as_record_set_str(&r, "a", a); as_record_set_int64(&r, "b", b); as_record_set_int64(&r, "c", c); as_record_set_int64(&r, "d", d); as_record_set_int64(&r, "e", e); as_record_set_list(&r, "x", (as_list *) &list); as_record_set_map(&r, "y", (as_map *) &map); as_record_set_list(&r, "z", (as_list *) &list2); as_key key; as_key_init(&key, NAMESPACE, SET, keystr); aerospike_key_put(as, &err, NULL, &key, &r); as_record_destroy(&r); if (err.code != AEROSPIKE_OK) { error("aerospike_key_put() failed %d %s", err.code, err.message); return false; } as_record *r1 = NULL; aerospike_key_exists(as, &err, NULL, &key, &r1); as_key_destroy(&key); if (err.code != AEROSPIKE_OK) { error("aerospike_key_exists() failed %d %s", err.code, err.message); return false; } if (! r1) { error("key not found %s", keystr); return false; } as_record_destroy(r1); } return true; }
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; }
// ---------------------------------------------------------------------------------- // // getting batch of records in one call // batch size is limited on aerospike server (default: 5000) // // def batch_get(keys, specific_bins = nil, options = {}) // // params: // keys - Array of AerospikeC::Key objects // specific bins - Array of strings representing bin names // options - hash of options: // with_header: returns also generation and expire_in field (default: false) // // ------ // RETURN: Array of hashes where each hash represents record bins // // @TODO options policy // static VALUE batch_get(int argc, VALUE * argv, VALUE self) { rb_aero_TIMED(tm); as_error err; as_status status; aerospike * as = rb_aero_CLIENT; char ** bin_names; long n_bin_names; VALUE keys; VALUE specific_bins; VALUE options; rb_scan_args(argc, argv, "12", &keys, &specific_bins, &options); // default values for optional arguments if ( NIL_P(specific_bins) ) { specific_bins = Qnil; } else { if ( TYPE(specific_bins) != T_ARRAY ) rb_raise(rb_aero_OptionError, "[AerospikeC::Client][batch_get] specific_bins must be an Array"); bin_names = rb_array2bin_names(specific_bins); n_bin_names = rb_ary_len_long(specific_bins); } if ( NIL_P(options) ) { options = rb_hash_new(); rb_hash_aset(options, with_header_sym, Qfalse); } long keys_len = rb_ary_len_long(keys); VALUE records_bins = rb_ary_new(); as_batch_read_records records; as_batch_read_inita(&records, keys_len); // map array into as_batch_read_record * record for (int i = 0; i < keys_len; ++i) { VALUE element = rb_ary_entry(keys, i); VALUE tmp; tmp = rb_funcall(element, rb_intern("namespace"), 0); char * c_namespace = StringValueCStr( tmp ); tmp = rb_funcall(element, rb_intern("set"), 0); char * c_set = StringValueCStr( tmp ); as_batch_read_record * record = as_batch_read_reserve(&records); tmp = rb_funcall(element, rb_intern("key"), 0); if ( TYPE(tmp) != T_FIXNUM ) { char * c_key = StringValueCStr( tmp ); as_key_init(&record->key, c_namespace, c_set, c_key); } else { as_key_init_int64(&record->key, c_namespace, c_set, FIX2LONG(tmp)); } if ( specific_bins == Qnil ) { record->read_all_bins = true; } else { record->bin_names = bin_names; record->n_bin_names = n_bin_names; } } // read here! if ( ( status = aerospike_batch_read(as, &err, NULL, &records) ) != AEROSPIKE_OK ) { if ( status == AEROSPIKE_ERR_RECORD_NOT_FOUND ) { rb_aero_logger(AS_LOG_LEVEL_WARN, &tm, 1, rb_str_new2("[Client][batch_get] AEROSPIKE_ERR_RECORD_NOT_FOUND")); return Qnil; } as_batch_read_destroy(&records); raise_as_error(err); } as_vector list = records.list; // map records into array of hashes for (long i = 0; i < list.size; ++i) { as_batch_read_record * record = as_vector_get(&list, i); as_record rec = record->record; VALUE bins = record2hash(&rec); bins = check_with_header(bins, options, &rec); rb_ary_push(records_bins, bins); } as_batch_read_destroy(&records); if ( specific_bins != Qnil ) bin_names_destroy(bin_names, n_bin_names); rb_aero_logger(AS_LOG_LEVEL_DEBUG, &tm, 1, rb_str_new2("[Client][batch_get] success")); return records_bins; }