//------------------------------------------------ // Read the whole test record from the database. // bool example_read_test_record(aerospike* p_as) { as_error err; as_record* p_rec = NULL; // Read the test record from the database. if (aerospike_key_get(p_as, &err, NULL, &g_key, &p_rec) != AEROSPIKE_OK) { LOG("aerospike_key_get() returned %d - %s", err.code, err.message); return false; } // If we didn't get an as_record object back, something's wrong. if (! p_rec) { LOG("aerospike_key_get() retrieved null as_record object"); return false; } // Log the result. LOG("record was successfully read from database:"); example_dump_record(p_rec); // Destroy the as_record object. as_record_destroy(p_rec); return true; }
static bool asc_raw_read(aerospike* p_as, as_key* p_key, uint8_t *buf, uint32_t size) { as_status status; as_error err; // Read the (whole) test record from the database. as_record *rec = NULL; status = aerospike_key_get(p_as, &err, NULL, p_key, &rec); if (status != AEROSPIKE_OK) { ERROR("aerospike_key_get() returned %d - %s", err.code, err.message); return false; } // Read the record as_bytes *bytes = as_record_get_bytes(rec, "data"); memcpy(buf, bytes->value, MIN(bytes->size, size)); as_record_destroy(rec); return true; }
bool asc_size(aerospike *p_as, char *ns, char *file, uint32_t *size) { as_status status; as_error err; // Prepare the key as_key key; as_key_init_str(&key, ns, SET, file); // Read metadata as_record *rec = NULL; status = aerospike_key_get(p_as, &err, NULL, &key, &rec); if (status != AEROSPIKE_OK) { ERROR("aerospike_key_get() returned %d - %s", err.code, err.message); return false; } *size = as_record_get_int64(rec, "size", 0); as_record_destroy(rec); return true; }
//------------------------------------------------ // Read multiple-record examples' test records // from the database. // bool example_read_test_records(aerospike* p_as) { // Multiple-record examples insert g_n_keys records, using integer keys from // 0 to (g_n_keys - 1). for (uint32_t i = 0; i < g_n_keys; i++) { as_error err; // No need to destroy a stack as_key object, if we only use // as_key_init_int64(). as_key key; as_key_init_int64(&key, g_namespace, g_set, (int64_t)i); as_record* p_rec = NULL; // Read a test record from the database. if (aerospike_key_get(p_as, &err, NULL, &key, &p_rec) != AEROSPIKE_OK) { LOG("aerospike_key_get() returned %d - %s", err.code, err.message); return false; } // If we didn't get an as_record object back, something's wrong. if (! p_rec) { LOG("aerospike_key_get() retrieved null as_record object"); return false; } // Log the result. LOG("read record with key %u from database:", i); example_dump_record(p_rec); // Destroy the as_record object. as_record_destroy(p_rec); } return true; }
/** ******************************************************************************************************* * This function read a record with a given key, and return the record. * * @param self AerospikeClient object * @param py_key The key under which to store the record. * @param py_policy The dictionary of policies to be given while * reading a record. * * Returns the record on success. * In case of error,appropriate exceptions will be raised. ******************************************************************************************************* */ PyObject * AerospikeClient_Get_Invoke( AerospikeClient * self, PyObject * py_key, PyObject * py_policy) { // Python Return Value PyObject * py_rec = NULL; // Aerospike Client Arguments as_error err; as_policy_read read_policy; as_policy_read * read_policy_p = NULL; as_key key; as_record * rec = NULL; // Initialised flags bool key_initialised = false; bool record_initialised = false; // Initialize error as_error_init(&err); if (!self || !self->as) { as_error_update(&err, AEROSPIKE_ERR_PARAM, "Invalid aerospike object"); goto CLEANUP; } if (!self->is_conn_16) { as_error_update(&err, AEROSPIKE_ERR_CLUSTER, "No connection to aerospike cluster"); goto CLEANUP; } // Convert python key object to as_key pyobject_to_key(&err, py_key, &key); if ( err.code != AEROSPIKE_OK ) { goto CLEANUP; } // Key is successfully initialised. key_initialised = true; // Convert python policy object to as_policy_exists pyobject_to_policy_read(&err, py_policy, &read_policy, &read_policy_p, &self->as->config.policies.read); if ( err.code != AEROSPIKE_OK ) { goto CLEANUP; } // Initialize record as_record_init(rec, 0); // Record initialised successfully. record_initialised = true; // Invoke operation aerospike_key_get(self->as, &err, read_policy_p, &key, &rec); if ( err.code == AEROSPIKE_OK ) { record_to_pyobject(&err, rec, &key, &py_rec); if ( read_policy_p == NULL || ( read_policy_p != NULL && read_policy_p->key == AS_POLICY_KEY_DIGEST)){ // This is a special case. // C-client returns NULL key, so to the user // response will be (<ns>, <set>, None, <digest>) // Using the same input key, just making primary key part to be None // Only in case of POLICY_KEY_DIGEST or no policy specified PyObject * p_key = PyTuple_GetItem( py_rec, 0 ); Py_INCREF(Py_None); PyTuple_SetItem(p_key, 2, Py_None); } } else if( err.code == AEROSPIKE_ERR_RECORD_NOT_FOUND ) { as_error_reset(&err); PyObject * py_rec_key = NULL; PyObject * py_rec_meta = Py_None; PyObject * py_rec_bins = Py_None; key_to_pyobject(&err, &key, &py_rec_key); py_rec = PyTuple_New(3); PyTuple_SetItem(py_rec, 0, py_rec_key); PyTuple_SetItem(py_rec, 1, py_rec_meta); PyTuple_SetItem(py_rec, 2, py_rec_bins); Py_INCREF(py_rec_meta); Py_INCREF(py_rec_bins); } else { as_error_update(&err, err.code, NULL); } CLEANUP: if (key_initialised == true){ // Destroy key only if it is initialised. as_key_destroy(&key); } if (record_initialised == true){ // Destroy record only if it is initialised. as_record_destroy(rec); } if ( err.code != AEROSPIKE_OK ) { PyObject * py_err = NULL; error_to_pyobject(&err, &py_err); PyObject *exception_type = raise_exception(&err); if(PyObject_HasAttrString(exception_type, "key")) { PyObject_SetAttrString(exception_type, "key", py_key); } if(PyObject_HasAttrString(exception_type, "bin")) { PyObject_SetAttrString(exception_type, "bin", Py_None); } PyErr_SetObject(exception_type, py_err); Py_DECREF(py_err); return NULL; } return py_rec; }
PyObject * AerospikeClient_Get_Invoke( AerospikeClient * self, PyObject * py_key, PyObject * py_policy) { // Python Return Value PyObject * py_rec = NULL; // Aerospike Client Arguments as_error err; as_policy_read policy; as_policy_read * policy_p = NULL; as_key key; as_record * rec = NULL; // Initialize error as_error_init(&err); // Convert python key object to as_key pyobject_to_key(&err, py_key, &key); if ( err.code != AEROSPIKE_OK ) { goto CLEANUP; } // Convert python policy object to as_policy_exists pyobject_to_policy_read(&err, py_policy, &policy, &policy_p); if ( err.code != AEROSPIKE_OK ) { goto CLEANUP; } // Initialize record as_record_init(rec, 0); // Invoke operation aerospike_key_get(self->as, &err, policy_p, &key, &rec); if ( err.code == AEROSPIKE_OK ) { record_to_pyobject(&err, rec, &key, &py_rec); } else if ( err.code == AEROSPIKE_ERR_RECORD_NOT_FOUND ) { as_error_reset(&err); PyObject * py_rec_key = NULL; PyObject * py_rec_meta = Py_None; PyObject * py_rec_bins = Py_None; key_to_pyobject(&err, &key, &py_rec_key); py_rec = PyTuple_New(3); PyTuple_SetItem(py_rec, 0, py_rec_key); PyTuple_SetItem(py_rec, 1, py_rec_meta); PyTuple_SetItem(py_rec, 2, py_rec_bins); Py_INCREF(py_rec_meta); Py_INCREF(py_rec_bins); } CLEANUP: // as_key_destroy(&key); as_record_destroy(rec); if ( err.code != AEROSPIKE_OK ) { PyObject * py_err = NULL; error_to_pyobject(&err, &py_err); PyErr_SetObject(PyExc_Exception, py_err); return NULL; } return py_rec; }
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_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; }
// ---------------------------------------------------------------------------------- // // 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; }