bool write_record(aerospike* p_as) { as_error err; // Create an as_record object with four 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_raw(). as_record rec; as_record_inita(&rec, 4); as_record_set_int64(&rec, "test-bin-1", 1111); as_record_set_int64(&rec, "test-bin-2", 2222); as_record_set_str(&rec, "test-bin-3", "test-bin-3-data"); static const uint8_t bytes[] = { 1, 2, 3 }; as_record_set_raw(&rec, "test-bin-4", bytes, 3); // 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(p_as, &err, NULL, &g_key, &rec) != AEROSPIKE_OK) { LOG("aerospike_key_put() returned %d - %s", err.code, err.message); return false; } LOG("write succeeded"); return true; }
//------------------------------------------------ // 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; }
bool write_record(aerospike* p_as) { as_error err; // Create an as_record object with two (integer type) bins. 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 rec; as_record_inita(&rec, 2); as_record_set_int64(&rec, "test-bin-1", 1000); as_record_set_int64(&rec, "test-bin-2", 1000); // 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(p_as, &err, NULL, &g_key, &rec) != AEROSPIKE_OK) { LOG("aerospike_key_put() returned %d - %s", err.code, err.message); return false; } LOG("write succeeded"); return true; }
bool batch_read_cb(const as_batch_read* results, uint32_t n, void* udata) { LOG("batch read callback returned %u/%u record results:", n, g_n_keys); uint32_t n_found = 0; uint32_t i; for (i = 0; i < n; i++) { LOG("index %u, key %" PRId64 ":", i, as_integer_getorelse((as_integer*)results[i].key->valuep, -1)); if (results[i].result == AEROSPIKE_OK) { LOG(" AEROSPIKE_OK"); // For aerospike_batch_exists() calls, there should be record // metadata but no bins. example_dump_record(&results[i].record); n_found++; } else if (results[i].result == AEROSPIKE_ERR_RECORD_NOT_FOUND) { // The transaction succeeded but the record doesn't exist. LOG(" AEROSPIKE_ERR_RECORD_NOT_FOUND"); } else { // The transaction didn't succeed. LOG(" error %d", results[i].result); } } LOG("... found %u/%u records", n_found, n); return true; }
bool scan_cb(const as_val* p_val, void* udata) { if (! p_val) { LOG("scan callback returned null - scan is complete"); return true; } // The scan didn't use a UDF, so the as_val object should be an as_record. as_record* p_rec = as_record_fromval(p_val); if (! p_rec) { LOG("scan callback returned non-as_record object"); return true; } LOG("scan callback returned record:"); example_dump_record(p_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; }
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_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 one (integer value) bin. 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 rec; as_record_inita(&rec, 1); as_record_set_int64(&rec, TEST_BIN, 1001); // Log its contents. LOG("as_record object to write to database:"); example_dump_record(&rec); // Write the record to the database. If the record isn't already in the // database, it will be created with generation = 1. 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"); uint16_t gen; // Read the record back, and get its generation. if (! read_generation(&as, &gen)) { example_cleanup(&as); exit(-1); } // Update the as_record object with a different bin value. In general it's // ok to do this - all as_record_set_... calls destroy any previous value. as_record_set_int64(&rec, TEST_BIN, 1002); // Set its generation equal to that of the record in the database. rec.gen = gen; // Require that the next write will only succeed if generations match. as_policy_write wpol; as_policy_write_init(&wpol); wpol.gen = AS_POLICY_GEN_EQ; // Log its contents. LOG("as_record object to write to database:"); example_dump_record(&rec); // Re-write the record in the database. The write should succeed, and // increment the generation. 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("re-write requiring generation = %u succeeded", rec.gen); // Read the record back, and get its generation. if (! read_generation(&as, &gen)) { example_cleanup(&as); exit(-1); } // Update the record object with a different bin value. as_record_set_int64(&rec, TEST_BIN, 1003); // Set its generation way past that of the record in the database. rec.gen = gen + 10; // Log its contents. LOG("as_record object to write to database:"); example_dump_record(&rec); // Try to re-write the record in the database. Use the same write policy, // requiring generations to match. This write should fail. if (aerospike_key_put(&as, &err, &wpol, &g_key, &rec) != AEROSPIKE_ERR_RECORD_GENERATION) { LOG("aerospike_key_put() returned %d - %s, expected " "AEROSPIKE_ERR_RECORD_GENERATION", err.code, err.message); example_cleanup(&as); exit(-1); } LOG("re-write requiring generation = %u failed as expected", rec.gen); // Now require that the next write will only succeed if the specified // generation is greater than that of the record in the database. wpol.gen = AS_POLICY_GEN_GT; // Log its contents. LOG("as_record object to write to database:"); example_dump_record(&rec); // Try again. This write should succeed, and increment the generation. (Note // that it does not write the record with the local generation!) 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("re-write requiring generation < %u succeeded", rec.gen); // Read the record back, and get its generation. if (! read_generation(&as, &gen)) { example_cleanup(&as); exit(-1); } // Cleanup and disconnect from the database cluster. example_cleanup(&as); LOG("generation example successfully completed"); return 0; }
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_operations object with a pair of bin arithmetic operations. // Generally, if using as_operations_inita(), we won't need to destroy the // object unless we call as_operations_add_write() with an externally // allocated as_bin_value. as_operations ops; as_operations_inita(&ops, 2); as_operations_add_incr(&ops, "test-bin-1", 1001); as_operations_add_incr(&ops, "test-bin-2", 1002); // Log the operations. LOG("as_operations object to apply to database:"); example_dump_operations(&ops); // Apply the operations. Since the record does not exist, it will be created // and the bins initialized with the ops' integer values. if (aerospike_key_operate(&as, &err, NULL, &g_key, &ops, NULL) != AEROSPIKE_OK) { LOG("aerospike_key_operate() returned %d - %s", err.code, err.message); example_cleanup(&as); exit(-1); } LOG("operations succeeded"); if (! example_read_test_record(&as)) { example_cleanup(&as); exit(-1); } // Generate a different set of arithmetic operations. as_operations_inita(&ops, 3); as_operations_add_incr(&ops, "test-bin-1", 1); as_operations_add_incr(&ops, "test-bin-2", -2); as_operations_add_incr(&ops, "test-bin-3", 3); // Log the operations. LOG("as_operations object to apply to database:"); example_dump_operations(&ops); // Apply the operations. The first two bins exist, so those ops' values will // be added to the existing values. The third (non-existent) bin will be // created and initialized with the op's integer value. if (aerospike_key_operate(&as, &err, NULL, &g_key, &ops, NULL) != AEROSPIKE_OK) { LOG("aerospike_key_operate() returned %d - %s", err.code, err.message); example_cleanup(&as); exit(-1); } LOG("operations succeeded"); if (! example_read_test_record(&as)) { example_cleanup(&as); exit(-1); } // Create an as_record object with one string value bin. as_record rec; as_record_inita(&rec, 1); as_record_set_str(&rec, "test-bin-1", "test-bin-1-data"); // Log its contents. LOG("as_record object to write to database:"); example_dump_record(&rec); // Write the record to the database, to change the value type of the bin. 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); } // Log the operations. (Same operations as last time.) LOG("as_operations object to apply to database:"); example_dump_operations(&ops); // Try to apply the three arithmetic operations again. This will fail, since // we can't increment the string value. Note that if any operation in the // transaction is rejected, none will be applied. if (aerospike_key_operate(&as, &err, NULL, &g_key, &ops, NULL) != AEROSPIKE_ERR_BIN_INCOMPATIBLE_TYPE) { LOG("aerospike_key_operate() returned %d - %s, expected " "AEROSPIKE_ERR_BIN_INCOMPATIBLE_TYPE", err.code, err.message); example_cleanup(&as); exit(-1); } LOG("operations failed as expected"); if (! example_read_test_record(&as)) { example_cleanup(&as); exit(-1); } // Generate a pair of operations to do an atomic increment and read. as_operations_inita(&ops, 2); as_operations_add_incr(&ops, "test-bin-3", 1); as_operations_add_read(&ops, "test-bin-3"); // Log the operations. LOG("as_operations object to apply to database:"); example_dump_operations(&ops); as_record* p_rec = NULL; // Apply the operations. The first will add the op's value to the existing // value, and the second will return the result. The pair of operations will // be atomic on the server. if (aerospike_key_operate(&as, &err, NULL, &g_key, &ops, &p_rec) != AEROSPIKE_OK) { LOG("aerospike_key_operate() returned %d - %s", err.code, err.message); example_cleanup(&as); exit(-1); } LOG("operations succeeded"); example_dump_record(p_rec); as_record_destroy(p_rec); if (! example_read_test_record(&as)) { example_cleanup(&as); exit(-1); } // Cleanup and disconnect from the database cluster. example_cleanup(&as); LOG("incr example successfully completed"); return 0; }
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; }
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 one (integer value) bin. 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 rec; as_record_inita(&rec, 1); as_record_set_int64(&rec, "test-bin", 1234); // Set the TTL of the record so it will last a minute. rec.ttl = 60; // 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); } // Create an as_operations object with a touch operation. Generally, if // using as_operations_inita(), we won't need to destroy the object unless // we call as_operations_add_write() with an externally allocated // as_bin_value. as_operations ops; as_operations_inita(&ops, 1); as_operations_add_touch(&ops); // Set the TTL of the record so it will last two minutes. ops.ttl = 120; // Log the operation. LOG("as_operations object to apply to database:"); example_dump_operations(&ops); // Apply the operation. Note that it does increment the record generation. if (aerospike_key_operate(&as, &err, NULL, &g_key, &ops, NULL) != AEROSPIKE_OK) { LOG("aerospike_key_operate() returned %d - %s", err.code, err.message); example_cleanup(&as); exit(-1); } LOG("operation succeeded"); if (! example_read_test_record(&as)) { example_cleanup(&as); exit(-1); } // Cleanup and disconnect from the database cluster. example_cleanup(&as); LOG("touch example successfully completed"); return 0; }