// ---------------------------------------------------------------------------------- // // check if key exist in cluster // // def exists?(key, options = {}) // // params: // key - AerospikeC::Key object // options - hash of options: // policy: AerospikeC::Policy for read // // ------ // RETURN: // 1. true if exist // 2. false otherwise // static VALUE key_exists(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 key; VALUE options; rb_scan_args(argc, argv, "11", &key, &options); if ( NIL_P(options) ) options = rb_hash_new(); as_key * k = rb_aero_KEY; as_policy_read * policy = get_policy(options); if ( ( status = aerospike_key_exists(as, &err, policy, k, &rec) ) != AEROSPIKE_OK ) { as_record_destroy(rec); if ( status == AEROSPIKE_ERR_RECORD_NOT_FOUND ) { rb_aero_logger(AS_LOG_LEVEL_DEBUG, &tm, 2, rb_str_new2("[Client][exists?] success - false"), rb_aero_KEY_INFO); return Qfalse; } raise_as_error(err); } as_record_destroy(rec); rb_aero_logger(AS_LOG_LEVEL_DEBUG, &tm, 2, rb_str_new2("[Client][exists?] success - true"), rb_aero_KEY_INFO); return Qtrue; }
// ---------------------------------------------------------------------------------- // // def get_header(key, options = {}) // // params: // key - AerospikeC::Key object // options - hash of options: // policy: AerospikeC::Policy for read // // ------ // RETURN: // 1. hash representing record header // 2. nil when AEROSPIKE_ERR_RECORD_NOT_FOUND // static VALUE get_header(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 key; VALUE options; rb_scan_args(argc, argv, "11", &key, &options); if ( NIL_P(options) ) options = rb_hash_new(); as_key * k = rb_aero_KEY; as_policy_read * policy = get_policy(options); if ( ( status = aerospike_key_exists(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_header] AEROSPIKE_ERR_RECORD_NOT_FOUND"), rb_aero_KEY_INFO); return Qnil; } raise_as_error(err); } VALUE header = rb_hash_new(); rb_hash_aset(header, rb_str_new2("gen"), INT2FIX(rec->gen)); rb_hash_aset(header, rb_funcall(rb_aero_AerospikeC, rb_intern("ttl_name"), 0), INT2FIX(rec->ttl)); as_record_destroy(rec); rb_aero_logger(AS_LOG_LEVEL_DEBUG, &tm, 2, rb_str_new2("[Client][get_header] success"), rb_aero_KEY_INFO); return header; }
/* ******************************************************************************************************* * Wrapper function to perform an aerospike_key_exists 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 error_p The as_error to be populated by the function * with the encountered error if any. * @param metadata_p The return metadata for the exists/getMetadata API to be * populated by this function. * @param options_p The user's optional policy options to be used if set, else defaults. * ******************************************************************************************************* */ extern as_status aerospike_record_operations_exists(aerospike* as_object_p, as_key* as_key_p, as_error *error_p, zval* metadata_p, zval* options_p) { as_status status = AEROSPIKE_OK; as_policy_read read_policy; as_record* record_p = NULL; if ( (!as_key_p) || (!error_p) || (!as_object_p) || (!metadata_p)) { status = AEROSPIKE_ERR; goto exit; } set_policy(&read_policy, NULL, NULL, NULL, NULL, NULL, options_p, error_p); if (AEROSPIKE_OK != (status = (error_p->code))) { DEBUG_PHP_EXT_DEBUG("Unable to set policy"); goto exit; } if (AEROSPIKE_OK != (status = aerospike_key_exists(as_object_p, error_p, &read_policy, as_key_p, &record_p))) { goto exit; } add_assoc_long(metadata_p, "generation", record_p->gen); add_assoc_long(metadata_p, "ttl", record_p->ttl); exit: if (record_p) { as_record_destroy(record_p); } return(status); }
PyObject * AerospikeClient_Exists_Invoke( AerospikeClient * self, PyObject * py_key, PyObject * py_policy) { // Python Return Value PyObject * py_result = 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; } // Invoke operation aerospike_key_exists(self->as, &err, policy_p, &key, &rec); if ( err.code == AEROSPIKE_OK ) { PyObject * py_result_key = NULL; PyObject * py_result_meta = NULL; key_to_pyobject(&err, &key, &py_result_key); metadata_to_pyobject(&err, rec, &py_result_meta); py_result = PyTuple_New(2); PyTuple_SetItem(py_result, 0, py_result_key); PyTuple_SetItem(py_result, 1, py_result_meta); } else if ( err.code == AEROSPIKE_ERR_RECORD_NOT_FOUND ) { as_error_reset(&err); PyObject * py_result_key = NULL; PyObject * py_result_meta = Py_None; key_to_pyobject(&err, &key, &py_result_key); py_result = PyTuple_New(2); PyTuple_SetItem(py_result, 0, py_result_key); PyTuple_SetItem(py_result, 1, py_result_meta); Py_INCREF(py_result_meta); } CLEANUP: 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_result; }
/** * 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; }
/** ******************************************************************************************************* * This function applies a registered udf module on a particular record. * * @param self AerospikeClient object * @param py_key The key under which the record is stored. * @param py_policy The dictionary of policies * * Returns a tuple of record having key and meta sequentially. ******************************************************************************************************* */ extern PyObject * AerospikeClient_Exists_Invoke( AerospikeClient * self, PyObject * py_key, PyObject * py_policy) { // Python Return Value PyObject * py_result = 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; // Initialisation flags bool key_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 initialised successfully 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; } // Invoke operation Py_BEGIN_ALLOW_THREADS aerospike_key_exists(self->as, &err, read_policy_p, &key, &rec); Py_END_ALLOW_THREADS if ( err.code == AEROSPIKE_OK ) { PyObject * py_result_key = NULL; PyObject * py_result_meta = NULL; key_to_pyobject(&err, &key, &py_result_key); metadata_to_pyobject(&err, rec, &py_result_meta); py_result = PyTuple_New(2); PyTuple_SetItem(py_result, 0, py_result_key); PyTuple_SetItem(py_result, 1, py_result_meta); } else { as_error_update(&err, err.code, NULL); } CLEANUP: if (key_initialised == true){ // Destroy the key if it is initialised successfully. as_key_destroy(&key); } 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 py_result; }
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; }