// ---------------------------------------------------------------------------------- // // removing record from the cluster // // def delete(key) // // params: // key - AerospikeC::Key object // options: // policy: AerospikeC::Policy for remove // // ------ // RETURN: // 1. true if deleted // 2. nil if AEROSPIKE_ERR_RECORD_NOT_FOUND // static VALUE delete_record(int argc, VALUE * argv, VALUE self) { rb_aero_TIMED(tm); as_error err; as_status status; aerospike * as = rb_aero_CLIENT; 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_remove * policy = get_policy(options); if ( ( status = aerospike_key_remove(as, &err, policy, k) ) != AEROSPIKE_OK ) { if ( status == AEROSPIKE_ERR_RECORD_NOT_FOUND ) { rb_aero_logger(AS_LOG_LEVEL_WARN, &tm, 2, rb_str_new2("AEROSPIKE_ERR_RECORD_NOT_FOUND"), rb_aero_KEY_INFO); return Qnil; } raise_as_error(err); } rb_aero_logger(AS_LOG_LEVEL_DEBUG, &tm, 2, rb_str_new2("[Client][delete] success"), rb_aero_KEY_INFO); return Qtrue; }
/* ******************************************************************************************************* * Wrapper function to perform an aerospike_key_remove 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 options_p The user's optional policy options to be used if set, else defaults. * ******************************************************************************************************* */ extern as_status aerospike_record_operations_remove(aerospike* as_object_p, as_key* as_key_p, as_error *error_p, zval* options_p) { as_status status = AEROSPIKE_OK; as_policy_remove remove_policy; if ( (!as_key_p) || (!error_p) || (!as_object_p)) { status = AEROSPIKE_ERR; goto exit; } set_policy(NULL, NULL, NULL, &remove_policy, 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_remove(as_object_p, error_p, &remove_policy, as_key_p))) { goto exit; } exit: return(status); }
//------------------------------------------------ // Remove the test record from the database. // void example_remove_test_record(aerospike* p_as) { as_error err; // Try to remove the test record from the database. If the example has not // inserted the record, or it has already been removed, this call will // return as_status AEROSPIKE_ERR_RECORD_NOT_FOUND - which we just ignore. aerospike_key_remove(p_as, &err, NULL, &g_key); }
//------------------------------------------------ // Remove multiple-record examples' test records // from the database. // void example_remove_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); // Ignore errors - just trying to leave the database as we found it. aerospike_key_remove(p_as, &err, NULL, &key); } }
/** ******************************************************************************************************* * This function invokes csdk's API to remove particular record. * * @param self AerospikeClient object * @param py_key The key under which to store the record * @param generation The generation value * @param py_policy The optional policy parameters * * Returns 0 on success. * In case of error,appropriate exceptions will be raised. ******************************************************************************************************* */ PyObject * AerospikeClient_Remove_Invoke( AerospikeClient * self, PyObject * py_key, PyObject * py_meta, PyObject * py_policy) { // Aerospike Client Arguments as_error err; as_policy_remove remove_policy; as_policy_remove * remove_policy_p = NULL; as_key key; // 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 if (py_policy) { pyobject_to_policy_remove(&err, py_policy, &remove_policy, &remove_policy_p, &self->as->config.policies.remove); if ( err.code != AEROSPIKE_OK ) { goto CLEANUP; } else { if ( py_meta && PyDict_Check(py_meta) ) { PyObject * py_gen = PyDict_GetItemString(py_meta, "gen"); if( py_gen != NULL ){ if ( PyInt_Check(py_gen) ) { remove_policy_p->generation = (uint16_t) PyInt_AsLong(py_gen); } else if ( PyLong_Check(py_gen) ) { remove_policy_p->generation = (uint16_t) PyLong_AsLongLong(py_gen); if((uint16_t)-1 == remove_policy_p->generation) { as_error_update(&err, AEROSPIKE_ERR_PARAM, "integer value for gen exceeds sys.maxsize"); goto CLEANUP; } } else { as_error_update(&err, AEROSPIKE_ERR_PARAM, "Generation should be an int or long"); } } } } } // Invoke operation Py_BEGIN_ALLOW_THREADS aerospike_key_remove(self->as, &err, remove_policy_p, &key); Py_END_ALLOW_THREADS if(err.code != AEROSPIKE_OK) { as_error_update(&err, err.code, NULL); } CLEANUP: if (key_initialised == true){ // Destroy the key if it is initialised successfully. as_key_destroy(&key); } 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 PyLong_FromLong(0); }
as_arraylist list; as_arraylist_init(&list, 3, 0); as_arraylist_append_int64(&list, 1); as_arraylist_append_int64(&list, 2); as_arraylist_append_int64(&list, 3); as_record r, * rec = &r; as_record_init(rec, 3); as_record_set_int64(rec, "a", 123); as_record_set_str(rec, "b", "abc"); as_record_set_list(rec, "e", (as_list *) &list); as_key key; as_key_init(&key, NAMESPACE, SET, "key2"); as_status rc = aerospike_key_remove(as, &err, NULL, &key); rc = aerospike_key_put(as, &err, NULL, &key, rec); assert_int_eq( rc, AEROSPIKE_OK ); as_record_destroy(rec); as_record_init(rec, 1); as_operations ops; as_operations_inita(&ops, 2); as_operations_add_touch(&ops); as_operations_add_read(&ops, "e"); ops.ttl = 120; // Apply the operation. rc = aerospike_key_operate(as, &err, NULL, &key, &ops, &rec);
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; // Create an as_record object with two bins with different value types. By // using as_record_inita(), we won't need to destroy the record if we only // set bins using as_record_set_int64(), as_record_set_str(), and // as_record_set_nil(). as_record rec; as_record_inita(&rec, 2); as_record_set_int64(&rec, "test-bin-1", 1234); as_record_set_str(&rec, "test-bin-2", "test-bin-2-data"); // Log its contents. LOG("as_record object to write to database:"); example_dump_record(&rec); // Write the record to the database. if (aerospike_key_put(&as, &err, NULL, &g_key, &rec) != AEROSPIKE_OK) { LOG("aerospike_key_put() returned %d - %s", err.code, err.message); example_cleanup(&as); exit(-1); } LOG("write succeeded"); if (! example_read_test_record(&as)) { example_cleanup(&as); exit(-1); } // Generate a different as_record object to write. In general it's ok to // reuse the stack object by calling as_record_inita() again, as long as the // previous contents are destroyed if necessary. as_record_inita(&rec, 2); as_record_set_int64(&rec, "test-bin-2", 2222); as_record_set_str(&rec, "test-bin-3", "test-bin-3-data"); // Log its contents. LOG("as_record object to write to database:"); example_dump_record(&rec); // Write the record to the database. This will change the type and value of // test-bin-2, will add test-bin-3, and will leave test-bin-one unchanged. if (aerospike_key_put(&as, &err, NULL, &g_key, &rec) != AEROSPIKE_OK) { LOG("aerospike_key_put() returned %d - %s", err.code, err.message); example_cleanup(&as); exit(-1); } LOG("write succeeded"); if (! example_read_test_record(&as)) { example_cleanup(&as); exit(-1); } // Generate another as_record object to write. as_record_inita(&rec, 1); as_record_set_nil(&rec, "test-bin-3"); // Log its contents. LOG("as_record object to write to database:"); example_dump_record(&rec); // Write the record to the database. This will remove test-bin-3 and // will leave test-bin-1 and test-bin-2 unchanged. if (aerospike_key_put(&as, &err, NULL, &g_key, &rec) != AEROSPIKE_OK) { LOG("aerospike_key_put() returned %d - %s", err.code, err.message); example_cleanup(&as); exit(-1); } LOG("write succeeded"); if (! example_read_test_record(&as)) { example_cleanup(&as); exit(-1); } // Generate another as_record object to write. as_record_inita(&rec, 1); as_record_set_int64(&rec, "test-bin-1", 1111); // Require that the write succeeds only if the record doesn't exist. as_policy_write wpol; as_policy_write_init(&wpol); wpol.exists = AS_POLICY_EXISTS_CREATE; // Log its contents. LOG("as_record object to create in database:"); example_dump_record(&rec); // Try to create the record. This should fail since the record already // exists in the database. if (aerospike_key_put(&as, &err, &wpol, &g_key, &rec) != AEROSPIKE_ERR_RECORD_EXISTS) { LOG("aerospike_key_put() returned %d - %s, expected " "AEROSPIKE_ERR_RECORD_EXISTS", err.code, err.message); example_cleanup(&as); exit(-1); } LOG("create failed as expected"); // Remove the record from the database so we can demonstrate create success. if (aerospike_key_remove(&as, &err, NULL, &g_key) != AEROSPIKE_OK) { LOG("aerospike_key_remove() returned %d - %s", err.code, err.message); example_cleanup(&as); exit(-1); } LOG("record removed from database, trying create again"); // Try to create the record again. This should succeed since the record is // not currently in the database. if (aerospike_key_put(&as, &err, &wpol, &g_key, &rec) != AEROSPIKE_OK) { LOG("aerospike_key_put() returned %d - %s", err.code, err.message); example_cleanup(&as); exit(-1); } LOG("create succeeded"); if (! example_read_test_record(&as)) { example_cleanup(&as); exit(-1); } // Cleanup and disconnect from the database cluster. example_cleanup(&as); LOG("put example successfully completed"); return 0; }
int main(int argc, char* argv[]) { // Parse command line arguments. if (! example_get_opts(argc, argv, EXAMPLE_MULTI_KEY_OPTS)) { exit(-1); } // Connect to the aerospike database cluster. aerospike as; example_connect_to_aerospike(&as); // Start clean. example_remove_test_records(&as); if (! insert_records(&as)) { cleanup(&as); exit(-1); } as_error err; // Make a batch of all the keys we inserted. as_batch batch; as_batch_inita(&batch, g_n_keys); uint32_t i; for (i = 0; i < g_n_keys; i++) { as_key_init_int64(as_batch_keyat(&batch, i), g_namespace, g_set, (int64_t)i); } // Check existence of these keys - they should all be there. if (aerospike_batch_exists(&as, &err, NULL, &batch, batch_read_cb, NULL) != AEROSPIKE_OK) { LOG("aerospike_batch_exists() returned %d - %s", err.code, err.message); cleanup(&as); exit(-1); } LOG("batch exists call completed"); // Get all of these keys - they should all be there. if (aerospike_batch_get(&as, &err, NULL, &batch, batch_read_cb, NULL) != AEROSPIKE_OK) { LOG("aerospike_batch_get() returned %d - %s", err.code, err.message); cleanup(&as); exit(-1); } LOG("batch get call completed"); // Delete some records in the middle. uint32_t n_to_delete = g_n_keys / 5; if (n_to_delete == 0) { n_to_delete = 1; } uint32_t n_start = g_n_keys / 2; if (n_start + n_to_delete > g_n_keys) { n_start = 0; } uint32_t n_end = n_start + n_to_delete; for (i = n_start; i < n_end; i++) { // 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); if (aerospike_key_remove(&as, &err, NULL, &key) != AEROSPIKE_OK) { LOG("aerospike_key_remove() returned %d - %s", err.code, err.message); cleanup(&as); exit(-1); } } LOG("deleted records %u ... %u", n_start, n_end - 1); // Check existence of these keys - some should not be found. if (aerospike_batch_exists(&as, &err, NULL, &batch, batch_read_cb, NULL) != AEROSPIKE_OK) { LOG("aerospike_batch_exists() returned %d - %s", err.code, err.message); cleanup(&as); exit(-1); } LOG("second batch exists call completed"); // Get all of these keys - some should not be found. if (aerospike_batch_get(&as, &err, NULL, &batch, batch_read_cb, NULL) != AEROSPIKE_OK) { LOG("aerospike_batch_get() returned %d - %s", err.code, err.message); cleanup(&as); exit(-1); } LOG("second batch get call completed"); // Cleanup and disconnect from the database cluster. cleanup(&as); LOG("batch get example successfully completed"); return 0; }