// ---------------------------------------------------------------------------------- // // getting record for specifed key // read all bins of record or given in specific_bins argument // // def get(key, 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) // policy: AerospikeC::Policy for read // // ------ // RETURN: // 1. hash representing record // 2. nil when AEROSPIKE_ERR_RECORD_NOT_FOUND // static VALUE get(int argc, VALUE * argv, VALUE self) { rb_aero_TIMED(tm); as_error err; as_status status; aerospike * as = rb_aero_CLIENT; as_record * rec = NULL; VALUE bins; VALUE key; VALUE specific_bins; VALUE options; rb_scan_args(argc, argv, "12", &key, &specific_bins, &options); // default values for optional arguments if ( NIL_P(specific_bins) ) specific_bins = Qnil; if ( NIL_P(options) ) { options = rb_hash_new(); rb_hash_aset(options, with_header_sym, Qfalse); } as_key * k = rb_aero_KEY; as_policy_read * policy = get_policy(options); // read specific bins if ( specific_bins != Qnil && rb_ary_len_int(specific_bins) > 0 ) { if ( TYPE(specific_bins) != T_ARRAY ) { rb_raise(rb_aero_OptionError, "[AerospikeC::Client][get] specific_bins must be an Array"); } char ** inputArray = rb_array2inputArray(specific_bins); // convert ruby array to char ** if ( ( status = aerospike_key_select(as, &err, policy, k, inputArray, &rec) ) != AEROSPIKE_OK) { as_record_destroy(rec); inputArray_destroy(inputArray); if ( status == AEROSPIKE_ERR_RECORD_NOT_FOUND ) { rb_aero_logger(AS_LOG_LEVEL_WARN, &tm, 2, rb_str_new2("[Client][get] AEROSPIKE_ERR_RECORD_NOT_FOUND"), rb_aero_KEY_INFO); return Qnil; } raise_as_error(err); } bins = record2hash(rec); bins = check_with_header(bins, options, rec); as_record_destroy(rec); inputArray_destroy(inputArray); // check_for_llist_workaround(self, key, bins); rb_aero_logger(AS_LOG_LEVEL_DEBUG, &tm, 2, rb_str_new2("[Client][get] success"), rb_aero_KEY_INFO); return bins; } // read all bins if ( ( status = aerospike_key_get(as, &err, policy, k, &rec) ) != AEROSPIKE_OK) { as_record_destroy(rec); if ( status == AEROSPIKE_ERR_RECORD_NOT_FOUND ) { rb_aero_logger(AS_LOG_LEVEL_WARN, &tm, 2, rb_str_new2("[Client][get] AEROSPIKE_ERR_RECORD_NOT_FOUND"), rb_aero_KEY_INFO); return Qnil; } raise_as_error(err); } bins = record2hash(rec); bins = check_with_header(bins, options, rec); as_record_destroy(rec); // check_for_llist_workaround(self, key, bins); rb_aero_logger(AS_LOG_LEVEL_DEBUG, &tm, 2, rb_str_new2("[Client][get] success"), rb_aero_KEY_INFO); return bins; }
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_error err; as_record* p_rec = NULL; // Try to read the test record from the database. This should fail since the // record is not there. if (aerospike_key_get(&as, &err, NULL, &g_key, &p_rec) != AEROSPIKE_ERR_RECORD_NOT_FOUND) { LOG("aerospike_key_get() returned %d - %s, expected " "AEROSPIKE_ERR_RECORD_NOT_FOUND", err.code, err.message); as_record_destroy(p_rec); example_cleanup(&as); exit(-1); } // Note that p_rec will still be NULL here. LOG("get (non-existent record) failed as expected"); // Write a record to the database so we can demonstrate read success. if (! write_record(&as)) { example_cleanup(&as); exit(-1); } // Read the (whole) test record from the database. if (aerospike_key_get(&as, &err, NULL, &g_key, &p_rec) != AEROSPIKE_OK) { LOG("aerospike_key_get() returned %d - %s", err.code, err.message); example_cleanup(&as); exit(-1); } // Log the result and recycle the as_record object. LOG("record was successfully read from database:"); example_dump_record(p_rec); as_record_destroy(p_rec); p_rec = NULL; // Select bins 1 and 3 to read. static const char* bins_1_3[] = { "test-bin-1", "test-bin-3", NULL }; // Read only these two bins of the test record from the database. if (aerospike_key_select(&as, &err, NULL, &g_key, bins_1_3, &p_rec) != AEROSPIKE_OK) { LOG("aerospike_key_select() returned %d - %s", err.code, err.message); example_cleanup(&as); exit(-1); } // Log the result and recycle the as_record object. LOG("bins 1 and 3 were read from database:"); example_dump_record(p_rec); as_record_destroy(p_rec); p_rec = NULL; // Select non-existent bin 5 to read. static const char* bins_5[] = { "test-bin-5", NULL }; // Read only this bin from the database. This call should return an // as_record object with one bin, with null as_bin_value. if (aerospike_key_select(&as, &err, NULL, &g_key, bins_5, &p_rec) != AEROSPIKE_OK) { LOG("aerospike_key_select() returned %d - %s", err.code, err.message); example_cleanup(&as); exit(-1); } // Log the result and destroy the as_record object. LOG("non-existent bin 5 was read from database:"); example_dump_record(p_rec); as_record_destroy(p_rec); p_rec = NULL; // Sleep 2 seconds, just to show the TTL decrease. LOG("waiting 2 seconds ..."); sleep(2); // Use aerospike_key_exists() to get only record metadata. if (aerospike_key_exists(&as, &err, NULL, &g_key, &p_rec) != AEROSPIKE_OK) { LOG("aerospike_key_exists() returned %d - %s", err.code, err.message); example_cleanup(&as); exit(-1); } // Log the result, which will only have metadata. LOG("existence check found record metadata:"); example_dump_record(p_rec); as_record_destroy(p_rec); // Cleanup and disconnect from the database cluster. example_cleanup(&as); LOG("get example successfully completed"); return 0; }
/* ******************************************************************************************************* * Wrapper function to perform an aerospike_key_oeprate within the C client. * * @param as_object_p The C client's aerospike object. * @param as_key_p The C client's as_key that identifies the record. * @param options_p The user's optional policy options to be used if set, else defaults. * @param error_p The as_error to be populated by the function * with the encountered error if any. * @param bin_name_p The bin name to perform operation upon. * @param str The string to be appended in case of operation: append. * @param offset The offset to be incremented by in case of operation: increment. * @param initial_value The initial value to be set if record is absent * in case of operation: increment. * @param time_to_live The ttl for the record in case of operation: touch. * @param operation The operation type. * ******************************************************************************************************* */ extern as_status aerospike_record_operations_ops(aerospike* as_object_p, as_key* as_key_p, zval* options_p, as_error* error_p, int8_t* bin_name_p, int8_t* str, u_int64_t offset, u_int64_t initial_value, u_int64_t time_to_live, u_int64_t operation) { as_status status = AEROSPIKE_OK; as_policy_operate operate_policy; uint32_t serializer_policy; as_record* get_rec = NULL; as_operations ops; as_val* value_p = NULL; as_integer initial_int_val; int16_t initialize_int = 0; const char *select[] = {bin_name_p, NULL}; as_operations_inita(&ops, 1); as_policy_operate_init(&operate_policy); if ((!as_object_p) || (!error_p) || (!as_key_p)) { status = AEROSPIKE_ERR; goto exit; } set_policy(NULL, NULL, &operate_policy, NULL, NULL, &serializer_policy, options_p, error_p); if (AEROSPIKE_OK != (status = (error_p->code))) { DEBUG_PHP_EXT_DEBUG("Unable to set policy"); goto exit; } switch(operation) { case AS_OPERATOR_APPEND: as_operations_add_append_str(&ops, bin_name_p, str); break; case AS_OPERATOR_PREPEND: as_operations_add_prepend_str(&ops, bin_name_p, str); break; case AS_OPERATOR_INCR: if (AEROSPIKE_OK != (status = aerospike_key_select(as_object_p, error_p, NULL, as_key_p, select, &get_rec))) { goto exit; } else { if (NULL != (value_p = (as_val *) as_record_get (get_rec, bin_name_p))) { if (AS_NIL == value_p->type) { as_integer_init(&initial_int_val, initial_value); initialize_int = 1; if (!as_operations_add_write(&ops, bin_name_p, (as_bin_value*) &initial_int_val)) { status = AEROSPIKE_ERR; goto exit; } } else { as_operations_add_incr(&ops, bin_name_p, offset); } } else { status = AEROSPIKE_ERR; goto exit; } } break; case AS_OPERATOR_TOUCH: ops.ttl = time_to_live; as_operations_add_touch(&ops); break; default: status = AEROSPIKE_ERR; goto exit; break; } if (AEROSPIKE_OK != (status = aerospike_key_operate(as_object_p, error_p, &operate_policy, as_key_p, &ops, &get_rec))) { goto exit; } exit: as_operations_destroy(&ops); if (get_rec) { as_record_destroy(get_rec); } if (initialize_int) { as_integer_destroy(&initial_int_val); } return status; }