bool insert_records(aerospike* p_as) { // Create an as_record object with up to three integer value 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, 3); // Re-using rec, write records into the database such that each record's key // and (test-bin) value is based on the loop index. for (uint32_t i = 0; i < g_n_keys; i++) { as_error err; // Set up a default as_policy_write object. as_policy_write wpol; as_policy_write_init(&wpol); // 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); // In general it's ok to reset a bin value - all as_record_set_... calls // destroy any previous value. if (i < 10) { // Only write one bin in the first ten records. as_record_set_int64(&rec, "test-bin-1", (int64_t)i); // By default, we don't store the key with the record in the // database. For these records, the key will not be returned in the // scan callback. } else { // Write three bins in all remaining records. as_record_set_int64(&rec, "test-bin-1", (int64_t)i); as_record_set_int64(&rec, "test-bin-2", (int64_t)(100 + i)); as_record_set_int64(&rec, "test-bin-3", (int64_t)(1000 + i)); // If we want the key to be returned in the scan callback, we must // store it with the record in the database. AS_POLICY_KEY_SEND // causes the key to be stored. wpol.key = AS_POLICY_KEY_SEND; } // Write a record to the database. if (aerospike_key_put(p_as, &err, &wpol, &key, &rec) != AEROSPIKE_OK) { LOG("aerospike_key_put() returned %d - %s", err.code, err.message); return false; } } LOG("insert succeeded"); 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; }