Example #1
0
void
cleanup(aerospike* p_as)
{
	example_remove_test_records(p_as);
	example_remove_index(p_as, TEST_INDEX_NAME);
	example_cleanup(p_as);
}
Example #2
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_ldt lstack;

	// Create a large stack bin to use. No need to destroy as_ldt if using
	// as_ldt_init() on stack object.
	if (! as_ldt_init(&lstack, "mystack", AS_LDT_LSTACK, NULL)) {
		LOG("unable to initialize ldt");
		example_cleanup(&as);
		exit(-1);
	}

	as_error err;

	// No need to destroy as_integer if using as_integer_init() on stack object.
	as_integer ival;
	as_integer_init(&ival, 123);

	// Push a few values onto the stack.
	if (aerospike_lstack_push(&as, &err, NULL, &g_key, &lstack,
			(const as_val*)&ival) != AEROSPIKE_OK) {
		LOG("first aerospike_lstack_push() returned %d - %s", err.code,
				err.message);
		example_cleanup(&as);
		exit(-1);
	}

	// No need to destroy as_string if using as_string_init() on stack object.
	as_string sval;
	as_string_init(&sval, "string stack value", false);

	if (aerospike_lstack_push(&as, &err, NULL, &g_key, &lstack,
			(const as_val*)&sval) != AEROSPIKE_OK) {
		LOG("second aerospike_lstack_push() returned %d - %s", err.code,
				err.message);
		example_cleanup(&as);
		exit(-1);
	}

	LOG("2 values pushed");

	uint32_t n_elements = 0;

	// Look at the stack size right now.
	if (aerospike_lstack_size(&as, &err, NULL, &g_key, &lstack, &n_elements) !=
			AEROSPIKE_OK) {
		LOG("aerospike_lstack_size() returned %d - %s", err.code, err.message);
		example_cleanup(&as);
		exit(-1);
	}

	if (n_elements != 2) {
		LOG("unexpected stack size %u", n_elements);
		example_cleanup(&as);
		exit(-1);
	}

	LOG("stack size confirmed to be %u", n_elements);

	as_ldt lstack2;
	as_ldt_init(&lstack2, "mystack", AS_LDT_LSTACK, NULL);

	uint32_t peek_count = 3;
	as_list* p_list = NULL;

	// Peek a few values back.
	if (aerospike_lstack_peek(&as, &err, NULL, &g_key, &lstack2, peek_count,
			&p_list) != AEROSPIKE_OK) {
		LOG("first aerospike_lstack_peek() returned %d - %s", err.code,
				err.message);
		as_list_destroy(p_list);
		example_cleanup(&as);
		exit(-1);
	}

	as_arraylist_iterator it;
	as_arraylist_iterator_init(&it, (const as_arraylist*)p_list);

	// 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);
		char* p_str = as_val_tostring(p_val);

		LOG("   peek - type = %d, value = %s", as_val_type(p_val), p_str);
		free(p_str);
	}

	as_list_destroy(p_list);
	p_list = NULL;

	// Push 3 more items onto the stack. By using as_arraylist_inita() we avoid
	// some but not all internal heap usage, so we must call
	// as_arraylist_destroy().
	as_arraylist vals;
	as_arraylist_inita(&vals, 3);
	as_arraylist_append_int64(&vals, 1000);
	as_arraylist_append_int64(&vals, 2000);
	as_arraylist_append_int64(&vals, 3000);

	if (aerospike_lstack_push_all(&as, &err, NULL, &g_key, &lstack,
			(const as_list*)&vals) != AEROSPIKE_OK) {
		LOG("aerospike_lstack_pushall() returned %d - %s", err.code,
				err.message);
		as_arraylist_destroy(&vals);
		example_cleanup(&as);
		exit(-1);
	}

	as_arraylist_destroy(&vals);

	LOG("3 more values pushed");

	as_ldt_init(&lstack2, "mystack", AS_LDT_LSTACK, NULL);
	peek_count = 10;

	// Peek all the values back again.
	if (aerospike_lstack_peek(&as, &err, NULL, &g_key, &lstack2, peek_count,
			&p_list) != AEROSPIKE_OK) {
		LOG("second aerospike_lstack_peek() returned %d - %s", err.code,
				err.message);
		as_list_destroy(p_list);
		example_cleanup(&as);
		exit(-1);
	}

	// See if the elements match what we expect.
	as_arraylist_iterator_init(&it, (const as_arraylist*)p_list);

	while (as_arraylist_iterator_has_next(&it)) {
		const as_val* p_val = as_arraylist_iterator_next(&it);
		char* p_str = as_val_tostring(p_val);

		LOG("   peek - type = %d, value = %s", as_val_type(p_val), p_str);
		free(p_str);
	}

	as_list_destroy(p_list);

	// Set capacity for the lstack.
	if (aerospike_lstack_set_capacity(&as, &err, NULL, &g_key, &lstack,
			10000) != AEROSPIKE_OK) {
		LOG("aerospike_lstack_set_capacity() returned %d - %s", err.code,
				err.message);
		example_cleanup(&as);
		exit(-1);
	}

	uint32_t cap_size = 0;

	// Get capacity from the lstack.
	if (aerospike_lstack_get_capacity(&as, &err, NULL, &g_key, &lstack,
			&cap_size) != AEROSPIKE_OK) {
		LOG("aerospike_lstack_get_capacity() returned %d - %s", err.code,
				err.message);
		example_cleanup(&as);
		exit(-1);
	}

	if (cap_size != 10000) {
		LOG("unexpected capacity size %u", cap_size);
		example_cleanup(&as);
		exit(-1);
	}

	// Destroy the lstack.
	if (aerospike_lstack_destroy(&as, &err, NULL, &g_key, &lstack) !=
			AEROSPIKE_OK) {
		LOG("aerospike_lstack_destroy() returned %d - %s", err.code,
				err.message);
		example_cleanup(&as);
		exit(-1);
	}

	n_elements = 0;

	// See if we can still do any lstack operations.
	if (aerospike_lstack_size(&as, &err, NULL, &g_key, &lstack, &n_elements) ==
			AEROSPIKE_OK) {
		LOG("aerospike_lstack_size() did not return error");
		example_cleanup(&as);
		exit(-1);
	}

	// Cleanup and disconnect from the database cluster.
	example_cleanup(&as);

	LOG("lstack example successfully completed");

	return 0;
}
Example #3
0
void
cleanup(aerospike* p_as)
{
	example_remove_test_records(p_as);
	example_cleanup(p_as);
}
Example #4
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_with_udf_config(&as, UDF_USER_PATH);

	// Start clean.
	example_remove_test_records(&as);
	example_remove_index(&as, TEST_INDEX_NAME);

	// Register the UDF in the database cluster.
	if (! example_register_udf(&as, UDF_FILE_PATH)) {
		example_cleanup(&as);
		exit(-1);
	}

	// Create a numeric secondary index on test-bin.
	if (! example_create_integer_index(&as, "test-bin", TEST_INDEX_NAME)) {
		cleanup(&as);
		exit(-1);
	}

	if (! insert_records(&as)) {
		cleanup(&as);
		exit(-1);
	}

	if (! example_read_test_records(&as)) {
		cleanup(&as);
		exit(-1);
	}

	as_error err;

	// Create an as_query object.
	as_query query;
	as_query_init(&query, g_namespace, g_set);

	// Generate an as_query.where condition. Note that as_query_destroy() takes
	// care of destroying all the query's member objects if necessary. However
	// using as_query_where_inita() does avoid internal heap usage.
	as_query_where_inita(&query, 1);
	as_query_where(&query, "test-bin", as_integer_range(1, 10));

	// Specify the UDF to use on the resulting stream.
	as_query_apply(&query, UDF_MODULE, "sum_test_bin", NULL);

	LOG("executing map-reduce query: where test-bin = 1 ... 10");

	// Execute the query. This call blocks - callbacks are made in the scope of
	// this call.
	if (aerospike_query_foreach(&as, &err, NULL, &query, query_cb, NULL) !=
			AEROSPIKE_OK) {
		LOG("aerospike_query_foreach() returned %d - %s", err.code,
				err.message);
		as_query_destroy(&query);
		cleanup(&as);
		exit(-1);
	}

	LOG("map-reduce query executed");

	// Reuse the as_query object for another query.
	as_query_destroy(&query);
	as_query_init(&query, g_namespace, g_set);

	// Generate an as_query.where condition.
	as_query_where_inita(&query, 1);
	as_query_where(&query, "test-bin", as_integer_range(1, 10));

	// Specify another UDF to use on the resulting stream. Like the previous UDF
	// it sums the test-bin values that satisfy the where condition, but does so
	// in a different, more efficient manner (see query_udf.lua).
	as_query_apply(&query, UDF_MODULE, "sum_test_bin_2", NULL);

	LOG("executing aggregate-reduce query: where test-bin = 1 ... 10");

	// Execute the query. This call blocks - callbacks are made in the scope of
	// this call.
	if (aerospike_query_foreach(&as, &err, NULL, &query, query_cb, NULL) !=
			AEROSPIKE_OK) {
		LOG("aerospike_query_foreach() returned %d - %s", err.code,
				err.message);
		as_query_destroy(&query);
		cleanup(&as);
		exit(-1);
	}

	LOG("aggregate-reduce query executed");

	// Reuse the as_query object for another query.
	as_query_destroy(&query);
	as_query_init(&query, g_namespace, g_set);

	// Generate an as_query.where condition.
	as_query_where_inita(&query, 1);
	as_query_where(&query, "test-bin", as_integer_range(1, 10));

	// Specify another UDF to use on the resulting stream. Like the previous
	// UDFs it sums test-bin values that satisfy the where condition, but first
	// applies a filter to sum only even values (see query_udf.lua).
	as_query_apply(&query, UDF_MODULE, "sum_test_bin_even", NULL);

	LOG("executing filter-aggregate-reduce query: where test-bin = 1 ... 10");

	// Execute the query. This call blocks - callbacks are made in the scope of
	// this call.
	if (aerospike_query_foreach(&as, &err, NULL, &query, query_cb, NULL) !=
			AEROSPIKE_OK) {
		LOG("aerospike_query_foreach() returned %d - %s", err.code,
				err.message);
		as_query_destroy(&query);
		cleanup(&as);
		exit(-1);
	}

	LOG("filter-aggregate-reduce query executed");

	// Reuse the as_query object for another query.
	as_query_destroy(&query);
	as_query_init(&query, g_namespace, g_set);

	// No as_query.where condition in this case, so we include everything.

	// Specify another UDF to use on the resulting stream. This UDF operates on
	// the numbers-bin (string) values, and demonstrates a case where the value
	// returned by the query callback is an as_map (instead of an as_integer).
	as_query_apply(&query, UDF_MODULE, "count_numbers", NULL);

	LOG("executing numbers aggregate-reduce query: all records");

	// Execute the query. This call blocks - callbacks are made in the scope of
	// this call.
	if (aerospike_query_foreach(&as, &err, NULL, &query, query_cb_map, NULL) !=
			AEROSPIKE_OK) {
		LOG("aerospike_query_foreach() returned %d - %s", err.code,
				err.message);
		as_query_destroy(&query);
		cleanup(&as);
		exit(-1);
	}

	LOG("numbers aggregate-reduce query executed");

	as_query_destroy(&query);

	// Cleanup and disconnect from the database cluster.
	cleanup(&as);

	LOG("aggregate query example successfully completed");

	return 0;
}
Example #5
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);

	// Create a large map object to use. No need to destroy lmap if using
	// as_ldt_init() on stack object.
	as_ldt lmap;
	if (! as_ldt_init(&lmap, "mylmap", AS_LDT_LMAP, NULL)) {
		LOG("unable to initialize ldt");
		example_cleanup(&as);
		exit(-1);
	}

	as_error err;
	as_boolean ldt_exists;
	as_boolean_init(&ldt_exists, false);

	// Verify that the LDT is not already there.
	if (aerospike_lmap_ldt_exists(&as, &err, NULL, &g_key, &lmap,
			&ldt_exists) != AEROSPIKE_OK) {
		int rc = example_handle_udf_error(&err, "first aerospike_lmap_ldt_exists()");
		example_cleanup(&as);
		exit(rc);
	}

	if (as_boolean_get(&ldt_exists)) {
		LOG("found ldt that should not be present");
		example_cleanup(&as);
		exit(-1);
	}

	LOG("verified that lmap ldt is not present");

	// No need to destroy ikey if using as_integer_init() on stack object.
	as_integer ikey;
	as_integer_init(&ikey, 12345);

	// No need to destroy sval if using as_string_init() on stack object with
	// free parameter false.
	as_string sval;
	as_string_init(&sval, "lmap value", false);

	// Put a string entry to the lmap.
	if (aerospike_lmap_put(&as, &err, NULL, &g_key, &lmap,
			(const as_val*)&ikey, (as_val *)&sval) != AEROSPIKE_OK) {
		LOG("first aerospike_lmap_put() returned %d - %s", err.code,
				err.message);
		example_cleanup(&as);
		exit(-1);
	}

	// Ok to reuse.
	as_integer_init(&ikey, 345);

	as_integer ival;
	as_integer_init(&ival, 1000);

	// Put an integer entry to the lmap.
	if (aerospike_lmap_put(&as, &err, NULL, &g_key, &lmap,
			(const as_val*)&ikey, (as_val*)&ival) != AEROSPIKE_OK) {
		LOG("second aerospike_lmap_put() returned %d - %s", err.code,
				err.message);
		example_cleanup(&as);
		exit(-1);
	}

	LOG("2 entries added to map");

	uint32_t n_elements = 0;

	// See how many elements we have in the lmap now.
	if (aerospike_lmap_size(&as, &err, NULL, &g_key, &lmap, &n_elements) !=
			AEROSPIKE_OK) {
		LOG("aerospike_lmap_size() returned %d - %s", err.code, err.message);
		example_cleanup(&as);
		exit(-1);
	}

	if (n_elements != 2) {
		LOG("unexpected lmap size %u", n_elements);
		example_cleanup(&as);
		exit(-1);
	}

	LOG("lmap size confirmed to be %u", n_elements);

	as_boolean_init(&ldt_exists, false);

	// Verify that the LDT is now present.
	if (aerospike_lmap_ldt_exists(&as, &err, NULL, &g_key, &lmap,
			&ldt_exists) != AEROSPIKE_OK) {
		LOG("first aerospike_lmap_ldt_exists() returned %d - %s", err.code,
				err.message);
		example_cleanup(&as);
		exit(-1);
	}

	if (! as_boolean_get(&ldt_exists)) {
		LOG("did not find ldt that should be be present");
		example_cleanup(&as);
		exit(-1);
	}

	LOG("verified that lmap ldt is present");

	as_map* p_map = NULL;

	// Get all the entries back.
	if (aerospike_lmap_get_all(&as, &err, NULL, &g_key, &lmap, &p_map) !=
			AEROSPIKE_OK) {
		LOG("aerospike_lmap_filter() returned %d - %s", err.code, err.message);
		example_cleanup(&as);
		exit(-1);
	}

	as_hashmap_iterator it;
	as_hashmap_iterator_init(&it, (const as_hashmap*)p_map);

	// See if the elements match what we expect.
	while (as_hashmap_iterator_has_next(&it)) {
		const as_val* p_val = as_hashmap_iterator_next(&it);
		char* p_str = as_val_tostring(p_val);

		LOG("   element type %d, value %s", as_val_type(p_val), p_str);
		free(p_str);
	}

	as_map_destroy(p_map);
	p_map = NULL;

	as_integer_init(&ikey, 12345);

	// Remove an entry from the map.
	if (aerospike_lmap_remove(&as, &err, NULL, &g_key, &lmap,
			(const as_val*)&ikey) != AEROSPIKE_OK) {
		LOG("aerospike_lmap_remove() returned %d - %s", err.code, err.message);
		example_cleanup(&as);
		exit(-1);
	}

	as_val* p_val = NULL;

	// Make sure we can't get the value any more.
	as_status result = aerospike_lmap_get(&as, &err, NULL, &g_key, &lmap,
			(const as_val*)&ikey, &p_val);

	if (result == AEROSPIKE_OK) {
		// Server version >= 3.4.1 returns empty map if element doesn't exist.
		if (p_val && (as_val_type(p_val) != AS_MAP ||
				as_map_size((as_map*)p_val) != 0)) {
			char* p_str = as_val_tostring(p_val);

			LOG("entry was not successfully removed");
			LOG("   element type %d, value %s", as_val_type(p_val), p_str);
			free(p_str);

			as_val_destroy(p_val);
			example_cleanup(&as);
			exit(-1);
		}
	}
	else if (result != AEROSPIKE_ERR_LARGE_ITEM_NOT_FOUND) {
		LOG("aerospike_lmap_get() returned %d - %s", err.code, err.message);
		example_cleanup(&as);
		exit(-1);
	}

	LOG("entry successfully removed");

	// Destroy the lmap.
	if (aerospike_lmap_destroy(&as, &err, NULL, &g_key, &lmap) !=
			AEROSPIKE_OK) {
		LOG("aerospike_lmap_destroy() returned %d - %s", err.code, err.message);
		example_cleanup(&as);
		exit(-1);
	}

	n_elements = 0;

	// See if we can still do any lmap operations.
	if (aerospike_lmap_size(&as, &err, NULL, &g_key, &lmap, &n_elements) ==
			AEROSPIKE_OK) {
		LOG("aerospike_lmap_size() did not return error");
		example_cleanup(&as);
		exit(-1);
	}

	LOG("lmap successfully destroyed");

	// Cleanup and disconnect from the database cluster.
	example_cleanup(&as);

	LOG("lmap example successfully completed");

	return 0;
}
Example #6
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 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;
}
Example #7
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;
}
Example #8
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_ldt lmap;

    // Create a lmap bin to use. No need to destroy as_ldt if using
    // as_ldt_init() on stack object.
    if (! as_ldt_init(&lmap, "mylmap", AS_LDT_LMAP, NULL)) {
        LOG("unable to initialize ldt");
        example_cleanup(&as);
        exit(-1);
    }

    as_error err;

    // No need to destroy as_integer if using as_integer_init() on stack object.
    as_integer ikey;
    as_integer_init(&ikey, 12345);

    // No need to destroy as_string if using as_string_init() on stack object
    // with free parameter false.
    as_string sval;
    as_string_init(&sval, "lmap value", false);

    // Put a string entry to the lmap.
    if (aerospike_lmap_put(&as, &err, NULL, &g_key, &lmap,
                           (const as_val*)&ikey, (as_val *)&sval) != AEROSPIKE_OK) {
        LOG("first aerospike_lmap_put() returned %d - %s", err.code,
            err.message);
        example_cleanup(&as);
        exit(-1);
    }

    as_integer ival;
    as_integer_init(&ival, 1000);

    // It's ok to reset the as_integer.
    as_integer_init(&ikey, 345);

    // Put an integer entry to the lmap.
    if (aerospike_lmap_put(&as, &err, NULL, &g_key, &lmap,
                           (const as_val*)&ikey, (as_val*)&ival) != AEROSPIKE_OK) {
        LOG("second aerospike_lmap_put() returned %d - %s", err.code,
            err.message);
        example_cleanup(&as);
        exit(-1);
    }

    LOG("2 entries added to map");

    uint32_t n_elements = 0;

    // See how many elements we have in the lmap now.
    if (aerospike_lmap_size(&as, &err, NULL, &g_key, &lmap, &n_elements) !=
            AEROSPIKE_OK) {
        LOG("aerospike_lmap_size() returned %d - %s", err.code, err.message);
        example_cleanup(&as);
        exit(-1);
    }

    if (n_elements != 2) {
        LOG("unexpected lmap size %u", n_elements);
        example_cleanup(&as);
        exit(-1);
    }

    LOG("lmap size confirmed to be %u", n_elements);

    as_ldt lmap2;
    as_ldt_init(&lmap2, "mylmap", AS_LDT_LMAP, NULL);

    as_map* p_map = NULL;

    // Get all the entries back.
    if (aerospike_lmap_get_all(&as, &err, NULL, &g_key, &lmap, &p_map) !=
            AEROSPIKE_OK) {
        LOG("aerospike_lmap_filter() returned %d - %s", err.code, err.message);
        example_cleanup(&as);
        exit(-1);
    }

    // See if the elements match what we expect.
    as_hashmap_iterator it;
    as_hashmap_iterator_init(&it, (const as_hashmap*)p_map);

    while (as_hashmap_iterator_has_next(&it)) {
        const as_val* p_val = as_hashmap_iterator_next(&it);
        char* p_str = as_val_tostring(p_val);

        LOG("   element - type = %d, value = %s", as_val_type(p_val), p_str);
        free(p_str);
    }

    as_map_destroy(p_map);
    p_map = NULL;

    as_integer_init(&ikey, 345);
    as_integer_init(&ival, 2000);

    // Remove an entry from the map.
    as_integer_init(&ikey, 12345);
    if (aerospike_lmap_remove(&as, &err, NULL, &g_key, &lmap,
                              (const as_val*)&ikey) != AEROSPIKE_OK) {
        LOG("aerospike_lmap_remove() returned %d - %s", err.code,
            err.message);
        example_cleanup(&as);
        exit(-1);
    }

    as_val* p_val = NULL;

    // Make sure we cannot get the value any more.
    if (aerospike_lmap_get(&as, &err, NULL, &g_key, &lmap,
                           (const as_val*)&ikey, &p_val) == AEROSPIKE_OK) {
        LOG("unexpected success getting a removed entry");
        as_val_destroy(p_val);
        example_cleanup(&as);
        exit(-1);
    }

    LOG("entry successfully removed");

    // Destroy the lmap.
    if (aerospike_lmap_destroy(&as, &err, NULL, &g_key, &lmap) !=
            AEROSPIKE_OK) {
        LOG("aerospike_lmap_destroy() returned %d - %s", err.code, err.message);
        example_cleanup(&as);
        exit(-1);
    }

    n_elements = 0;

    // See if we can still do any lmap operations.
    if (aerospike_lmap_size(&as, &err, NULL, &g_key, &lmap, &n_elements) ==
            AEROSPIKE_OK) {
        LOG("aerospike_lmap_size() did not return error");
        example_cleanup(&as);
        exit(-1);
    }

    LOG("lmap successfully destroyed");

    // Cleanup and disconnect from the database cluster.
    example_cleanup(&as);

    LOG("lmap example successfully completed");

    return 0;
}
Example #9
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;
}
Example #10
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;
}
Example #11
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;
}
Example #12
0
int
main(int argc, char* argv[])
{
	aerospike as;
	as_error err;
	as_boolean ldt_exists;
	as_ldt lstack, lstack2; 
	as_integer ival;
	as_string sval;
    as_bytes bval;
	uint32_t n_elements, cap_size;
	as_arraylist_iterator it;
	as_list* p_list = NULL;

	// Parse command line arguments.
	if (! example_get_opts(argc, argv, EXAMPLE_BASIC_OPTS)) {
		exit(-1);
	}

	// Connect to the aerospike database cluster.
	example_connect_to_aerospike(&as);

	// Start clean.
	example_remove_test_record(&as);

	// Create a large stack object to use. No need to destroy lstack if using
	// as_ldt_init() on stack object.
	as_ldt_init(&lstack, "mystack", AS_LDT_LSTACK, NULL);

	// Verify that the LDT is not already there.
	as_boolean_init(&ldt_exists, false);
	assert (aerospike_lstack_ldt_exists(&as, &err, NULL, &g_key, &lstack, &ldt_exists) == AEROSPIKE_OK);
	assert (as_boolean_get(&ldt_exists) == false);
	LOG("verified that lstack ldt is not present");

	// Push a few values onto the stack.
	// No need to destroy sval if using as_string_init() on stack object.

    // Push an integer
	as_integer_init(&ival, 123);
	assert (aerospike_lstack_push(&as, &err, NULL, &g_key, &lstack, (const as_val*)&ival) == AEROSPIKE_OK);

    // Push a string
	as_string_init(&sval, "string stack value", false);
	assert (aerospike_lstack_push(&as, &err, NULL, &g_key, &lstack, (const as_val*)&sval) == AEROSPIKE_OK);

    // Push bytes
    uint8_t buf[16] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
    as_bytes_init_wrap(&bval, buf, 16, false);
    assert (aerospike_lstack_push(&as, &err, NULL, &g_key, &lstack, (const as_val*)&bval) == AEROSPIKE_OK);


	// Look at the stack size right now.
	assert (aerospike_lstack_size(&as, &err, NULL, &g_key, &lstack, &n_elements) == AEROSPIKE_OK);
	LOG("%d values pushed", n_elements);

	// Peek a few values back.
	as_ldt_init(&lstack2, "mystack", AS_LDT_LSTACK, NULL);

	assert (aerospike_lstack_peek(&as, &err, NULL, &g_key, &lstack2, 3, &p_list) == AEROSPIKE_OK);

	// See if the elements match what we expect.
	as_arraylist_iterator_init(&it, (const as_arraylist*)p_list);
	while (as_arraylist_iterator_has_next(&it)) {
		const as_val* p_val = as_arraylist_iterator_next(&it);
		char* p_str = as_val_tostring(p_val);
		LOG("   peek - type = %d, value = %s", as_val_type(p_val), p_str);
		free(p_str);
	}
	as_list_destroy(p_list);
	p_list = NULL;

	// Push 3 more items onto the stack. By using as_arraylist_inita() we avoid
	// some but not all internal heap usage, so we must call
	// as_arraylist_destroy().
	as_arraylist vals;
	as_arraylist_inita(&vals, 3);
	as_arraylist_append_int64(&vals, 1000);
	as_arraylist_append_int64(&vals, 2000);
	as_arraylist_append_int64(&vals, 3000);

	assert (aerospike_lstack_push_all(&as, &err, NULL, &g_key, &lstack, (const as_list*)&vals) == AEROSPIKE_OK);
	as_arraylist_destroy(&vals);
	LOG("3 more values pushed");

	// Peek all the values back again.
	as_ldt_init(&lstack2, "mystack", AS_LDT_LSTACK, NULL);
	assert (aerospike_lstack_peek(&as, &err, NULL, &g_key, &lstack2, 10, &p_list) == AEROSPIKE_OK);

	// See if the elements match what we expect.
#if 0
	as_arraylist_iterator_init(&it, (const as_arraylist*)p_list);
	while (as_arraylist_iterator_has_next(&it)) {
		const as_val* p_val = as_arraylist_iterator_next(&it);
		char* p_str = as_val_tostring(p_val);

		LOG("   peek - type = %d, value = %s", as_val_type(p_val), p_str);
		free(p_str);
	}
#else
    const as_arraylist* p_array = (const as_arraylist*)p_list;
    int i;
    for (i = 0; i < p_array->size; i++) {
        const as_val* p_val = p_array->elements[i];
		char* p_str = as_val_tostring(p_val);
		LOG("   peek - type = %d, value = %s", as_val_type(p_val), p_str);
		free(p_str);
    }
#endif
	as_list_destroy(p_list);
    p_list = NULL;

	// Set capacity for the lstack.
	assert (aerospike_lstack_set_capacity(&as, &err, NULL, &g_key, &lstack, 10000) == AEROSPIKE_OK);

	// Get capacity from the lstack.
	assert (aerospike_lstack_get_capacity(&as, &err, NULL, &g_key, &lstack, &cap_size) == AEROSPIKE_OK);
	assert (cap_size == 10000);

	// Verify that the LDT is now present.
	as_boolean_init(&ldt_exists, false);
	assert (aerospike_lstack_ldt_exists(&as, &err, NULL, &g_key, &lstack, &ldt_exists) == AEROSPIKE_OK);
	assert (as_boolean_get(&ldt_exists) == true);
	LOG("verified that lstack ldt is present");

	// Destroy the lstack.
	assert (aerospike_lstack_destroy(&as, &err, NULL, &g_key, &lstack) == AEROSPIKE_OK);

	// See if we can still do any lstack operations.
	assert (aerospike_lstack_size(&as, &err, NULL, &g_key, &lstack, &n_elements) != AEROSPIKE_OK);

	// Cleanup and disconnect from the database cluster.
	example_cleanup(&as);
	LOG("lstack example successfully completed");

	return 0;
}
Example #13
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_operations ops;

	LOG("append 10 values from 0 to 9");

	// The first append will create the record and bin.
	for (int i = 0; i < 10; i++) {
		as_operations_inita(&ops, 1);
		as_operations_add_list_append_int64(&ops, "test-bin-1", i);

		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);
		}

		as_operations_destroy(&ops);
	}

	if (! example_read_test_record(&as)) {
		example_cleanup(&as);
		exit(-1);
	}

	LOG("append operations succeeded");
	LOG("pop from the tail (index -1)");

	as_operations_inita(&ops, 1);
	as_operations_add_list_pop(&ops, "test-bin-1", -1);

	as_record *p_rec = NULL;

	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);
		as_operations_destroy(&ops);
		example_cleanup(&as);
		exit(-1);
	}

	as_operations_destroy(&ops);

	LOG("pop operation succeeded");
	LOG("insert popped value at the head (index 0)");

	as_val *val = (as_val *)as_record_get(p_rec, "test-bin-1");

	as_operations_inita(&ops, 1);
	as_operations_add_list_insert(&ops, "test-bin-1", 0, val);

	if (aerospike_key_operate(&as, &err, NULL, &g_key, &ops, NULL) !=
			AEROSPIKE_OK) {
		LOG("aerospike_key_operate() returned %d - %s", err.code, err.message);
		as_operations_destroy(&ops);
		example_cleanup(&as);
		exit(-1);
	}

	as_record_destroy(p_rec);
	as_operations_destroy(&ops);

	LOG("insert operation succeeded");

	if (! example_read_test_record(&as)) {
		example_cleanup(&as);
		exit(-1);
	}

	// Cleanup and disconnect from the database cluster.
	example_cleanup(&as);

	LOG("list example successfully completed");

	return 0;
}
Example #14
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);

    // Register the UDF in the database cluster.
    if (! example_register_udf(&as, UDF_FILE_PATH)) {
        example_cleanup(&as);
        exit(-1);
    }

    // Write a record to the database.
    if (! write_record(&as)) {
        cleanup(&as);
        exit(-1);
    }

    as_error err;

    // Apply a simple UDF, with no arguments and no return value.
    if (aerospike_key_apply(&as, &err, NULL, &g_key, UDF_MODULE,
                            "test_bin_1_add_1000", NULL, NULL) != AEROSPIKE_OK) {
        LOG("aerospike_key_apply() returned %d - %s", err.code, err.message);
        cleanup(&as);
        exit(-1);
    }

    LOG("test_bin_1_add_1000() was successfully applied");

    if (! example_read_test_record(&as)) {
        example_cleanup(&as);
        exit(-1);
    }

    // Create an argument list for a (different) UDF. By using
    // as_arraylist_inita() we avoid some but not all internal heap usage, so we
    // must call as_arraylist_destroy().
    as_arraylist args;
    as_arraylist_inita(&args, 3);
    as_arraylist_append_str(&args, "test-bin-2");
    as_arraylist_append_int64(&args, 4);
    as_arraylist_append_int64(&args, 400);

    // Expect an integer return value.
    as_val* p_return_val = NULL;

    // Apply a UDF with arguments and a return value.
    if (aerospike_key_apply(&as, &err, NULL, &g_key, UDF_MODULE,
                            "bin_transform", (as_list*)&args, &p_return_val) != AEROSPIKE_OK) {
        LOG("aerospike_key_apply() returned %d - %s", err.code, err.message);
        as_arraylist_destroy(&args);
        cleanup(&as);
        exit(-1);
    }

    as_arraylist_destroy(&args);

    if (! p_return_val) {
        LOG("aerospike_key_apply() retrieved null as_val object");
        cleanup(&as);
        exit(-1);
    }

    // Extract an integer from the as_val returned.
    int64_t i_val = as_integer_getorelse(as_integer_fromval(p_return_val), -1);

    // Caller's responsibility to destroy as_val returned.
    as_val_destroy(p_return_val);

    if (i_val == -1) {
        LOG("aerospike_key_apply() retrieved non-as_integer object");
        cleanup(&as);
        exit(-1);
    }

    LOG("bin_transform() was successfully applied - returned %" PRId64, i_val);

    if (! example_read_test_record(&as)) {
        example_cleanup(&as);
        exit(-1);
    }

    // Cleanup and disconnect from the database cluster.
    cleanup(&as);

    LOG("udf example successfully completed");

    return 0;
}
Example #15
0
void
cleanup(aerospike* p_as)
{
    example_remove_udf(p_as, UDF_FILE_PATH);
    example_cleanup(p_as);
}