/**
 *	Remove a record from the cluster.
 *
 *	~~~~~~~~~~{.c}
 *		as_key key;
 *		as_key_init(&key, "ns", "set", "key");
 *
 *		if ( aerospike_key_remove(&as, &err, NULL, &key) != AEROSPIKE_OK ) {
 *			fprintf(stderr, "error(%d) %s at [%s:%d]", err.code, err.message, err.file, err.line);
 *		}
 *	~~~~~~~~~~
 *
 *	@param as			The aerospike instance to use for this operation.
 *	@param err			The as_error to be populated if an error occurs.
 *	@param policy		The policy to use for this operation. If NULL, then the default policy will be used.
 *	@param key			The key of the record.
 *
 *	@return AEROSPIKE_OK if successful. Otherwise an error.
 */
as_status aerospike_key_remove(
	aerospike * as, as_error * err, const as_policy_remove * policy, 
	const as_key * key) 
{
	// we want to reset the error so, we have a clean state
	as_error_reset(err);
	
	// resolve policies
	as_policy_remove p;
	as_policy_remove_resolve(&p, &as->config.policies, policy);

	cl_write_parameters wp;
	aspolicyremove_to_clwriteparameters(&p, &wp);

	cl_rv rc = CITRUSLEAF_OK;

	switch ( p.key ) {
		case AS_POLICY_KEY_DIGEST: {
			as_digest * digest = as_key_digest((as_key *) key);
			rc = citrusleaf_delete_digest(as->cluster, key->ns, (cf_digest *) digest->value, &wp);
			break;
		}
		case AS_POLICY_KEY_SEND: {
			cl_object okey;
			asval_to_clobject((as_val *) key->valuep, &okey);
			as_digest * digest = as_key_digest((as_key *) key);
			rc = citrusleaf_delete(as->cluster, key->ns, key->set, &okey, (cf_digest*)digest->value, &wp);
			break;
		}
		default: {
			// ERROR CASE
			break;
		}
	}

	return as_error_fromrc(err,rc);
}
int main(int argc, char **argv){
	cl_cluster   *clc;
	cl_rv        return_value;
	cl_object key1;
	cl_object key2;

	printf(" STARTING TESTS\n");
	// initialize internal citrusleaf structures just once
	citrusleaf_init();

	// Create a cluster with a particular starting host
	printf(" STARTING CLUSTER CREATION TEST .... \n");
	clc = citrusleaf_cluster_create();
	if (!clc){
		printf("TEST FAILED: Could not create cluster object");
	    return(-1);
	}

	return_value = citrusleaf_cluster_add_host(clc, host, 3000, 1000);
	if( return_value != CITRUSLEAF_OK ){
		printf("TEST FAILED - cannot connect to host\n");
		return -1;
	}

	// XXX - need to do some info calls with a bigger cluster!

	printf(" DONE\n");


	// set up the key. Create a stack object, set its value to a string
	cl_object    key_obj;
	citrusleaf_object_init_str(&key_obj, myKey);

	// set up a specific bins to fetch
	// the response will be in this value
	cl_bin       values[3];
	strcpy( &values[0].bin_name[0], bin1 );
	citrusleaf_object_init_str( &values[0].object, strData);
	strcpy( &values[1].bin_name[0], bin2);
	citrusleaf_object_init_int( &values[1].object, intData );
	strcpy( &values[2].bin_name[0], bin3);
	citrusleaf_object_init_blob( &values[2].object, blobData, strlen(blobData)+1);

	printf("params to put are clc %p, ns %s, set %s, key %p, values %p\n", clc, ns, myset, &key_obj, values);
	return_value = citrusleaf_put(clc, ns, myset, &key_obj, values, 3, NULL);
	if( return_value != CITRUSLEAF_OK ){
		printf(" TEST FAILS - INITIAL PUT FAILS, value is %d\n", return_value);
		return(-1);
	}

	citrusleaf_object_init(&values[0].object);
	citrusleaf_object_init(&values[1].object);
	citrusleaf_object_init(&values[2].object);

	return_value = citrusleaf_get(clc, ns, myset, &key_obj, 
		values, 3, 0, NULL); 

	switch (return_value) {
    	case CITRUSLEAF_OK:
       		if (values[0].object.type != CL_STR) {
			printf(" TEST FAILS - value has unexpected type %d\n",values[0].object.type);
			goto cleanup;
       		} else if( strcmp(values[0].object.u.str, strData) ){
			printf("TEST FAILS - WRITE DOES NOT RETURN WHAT WAS WRITTEN: %s, %s\n", values[0].object.u.str, strData);
			goto cleanup;
		} 
	
       		if (values[1].object.type != CL_INT) {     
			printf(" TEST FAILS - value has unexpected type %d\n",values[1].object.type);
			goto cleanup;
       		} else if( values[1].object.u.i64 != intData){
			printf("TEST FAILS - WRITE DOES NOT RETURN WHAT WAS WRITTEN, %lu, %lu\n", values[1].object.u.i64, intData);
			goto cleanup;
       		} 
		if( values[2].object.type != CL_BLOB) {
			printf(" TEST FAILS - value has unexpected type %d\n",values[1].object.type);
			goto cleanup;
		}else if( strcmp(values[2].object.u.blob, blobData) ){
			printf(" TEST FAILS - WRITE DOES NOT RETURN CORRECT BLOB DATA\n");
			goto cleanup;
		}
      		break;
    	case CITRUSLEAF_FAIL_NOTFOUND:
    		printf(" TEST FAILS - citrusleaf says that key does not exist\n");
		goto cleanup;
    		break;
    	case CITRUSLEAF_FAIL_CLIENT:
    		printf(" TEST FAILS - citrusleaf client error: local error\n");
		goto cleanup;
    		break;
    	case CITRUSLEAF_FAIL_PARAMETER:
    		printf(" TEST FAILS - citrusleaf - bad parameter passed in \n");
		goto cleanup;
    		break;
    	case CITRUSLEAF_FAIL_TIMEOUT:
		printf(" TEST FAILS - citrusleaf - timeout on get\n");
		goto cleanup;
		break;
    	case CITRUSLEAF_FAIL_UNKNOWN:
		printf(" TEST _FAILS - citrusleaf - unknown server error\n");
		goto cleanup;
		break;
    	default :
		printf(" TEST_FAILS - error %d\n", return_value);
		goto cleanup;

	}
	// clean up the retrieved objects
	citrusleaf_object_free(&values[0].object);
	citrusleaf_object_free(&values[1].object);

	if( test_getall(clc) )    goto cleanup;
	if( read_mod_write(clc) ) goto cleanup;
	if( test_unique(clc) )    goto cleanup;
	if( test_operate(clc) )   goto cleanup;
	if( test_batch(clc) )     goto cleanup;
	
	printf("TEST SUCCESSFUL!\n");

cleanup:

	citrusleaf_object_init_str(&key1,myKey);
	citrusleaf_object_init_str(&key2,myKey2);
	citrusleaf_delete(clc, ns, myset, &key1, NULL);
	citrusleaf_delete(clc, ns, myset, &key2, NULL);
	// Clean up the cluster object
	citrusleaf_cluster_destroy(clc);
	// Clean up the unit
	citrusleaf_shutdown();
  
}
int
do_example(config *c)
{
	int rv;
	
	// Put some test values
	cl_object o_key;
	citrusleaf_object_init_str(&o_key, "example_key");
	cl_bin values[2];
	strcpy(values[0].bin_name, "test_bin_one");
	citrusleaf_object_init_str(&values[0].object, "example_value_one");
	strcpy(values[1].bin_name, "test_bin_two");
	citrusleaf_object_init_int(&values[1].object, 0xDEADBEEF);
	
	// set a non-default write parameter
	cl_write_parameters cl_wp;
	cl_write_parameters_set_default(&cl_wp);
	cl_wp.timeout_ms = 1000;
	
	if (0 != (rv = citrusleaf_put(c->asc, c->ns, c->set, &o_key, values, 2, &cl_wp))) {
		fprintf(stderr, "citrusleaf put failed: error %d\n",rv);
		return(-1);
	}
	fprintf(stderr, "citrusleaf put succeeded\n");
	
	// Get all the values in this key (enjoy the fine c99 standard)
	cl_bin *cl_v = 0;
	uint32_t generation;
	int 	cl_v_len;
	if (0 != (rv = citrusleaf_get_all(c->asc, c->ns, c->set, &o_key, &cl_v, &cl_v_len, c->timeout_ms, &generation))) {
		fprintf(stderr, "get after put failed, but there should be a key here - %d\n",rv);
		if (cl_v)	free(cl_v);
		return(-1);
	}
	fprintf(stderr, "get all returned %d bins\n",cl_v_len);
	for (int i=0;i<cl_v_len;i++) {
		fprintf(stderr, "%d:  bin %s ",i,cl_v[i].bin_name);
		switch (cl_v[i].object.type) {
			case CL_STR:
				fprintf(stderr, "type string: value %s\n", cl_v[i].object.u.str);
				break;
			case CL_INT:
				fprintf(stderr, "type int: value %"PRId64"\n",cl_v[i].object.u.i64);
				break;
			default:
				fprintf(stderr, "type unknown! (%d)\n",(int)cl_v[i].object.type);
				break;
		}
		// could have done this -- but let's free the objects in the bins later
		// citrusleaf_object_free(&cl_v[i].object);
	}
	if (cl_v)	{
		citrusleaf_bins_free(cl_v, cl_v_len);
		free(cl_v); // only one free for all bins
	}
	fprintf(stderr,"citrusleaf getall succeeded\n");
	
	// Delete the key you just set
	if (0 != (rv = citrusleaf_delete(c->asc, c->ns, c->set, &o_key, 0/*default write params*/))) {
		fprintf(stderr, "citrusleaf delete failed: error %d\n",rv);
		return(-1);
	}
	fprintf(stderr, "citrusleaf delete succeeded\n");
	
	return(0);
}