示例#1
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);
	}

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

	as_error err;

	// Specify the namespace and set to use during the scan.
	as_scan scan;
	as_scan_init(&scan, g_namespace, g_set);

	LOG("starting scan all ...");

	// Do the scan. This call blocks while the scan is running - callbacks are
	// made in the scope of this call.
	if (aerospike_scan_foreach(&as, &err, NULL, &scan, scan_cb, NULL) !=
			AEROSPIKE_OK) {
		LOG("aerospike_scan_foreach() returned %d - %s", err.code, err.message);
		as_scan_destroy(&scan);
		cleanup(&as);
		exit(-1);
	}

	LOG("... scan all completed");

	// Now specify that only two bins are to be returned by the scan. The first
	// ten records do not have these two bins, so they should not be returned by
	// the scan. The remaining records should be returned without test-bin-1.
	as_scan_select_inita(&scan, 2);
	as_scan_select(&scan, "test-bin-2");
	as_scan_select(&scan, "test-bin-3");

	LOG("starting scan with select ...");

	// Do the scan. This call blocks while the scan is running - callbacks are
	// made in the scope of this call.
	if (aerospike_scan_foreach(&as, &err, NULL, &scan, scan_cb, NULL) !=
			AEROSPIKE_OK) {
		LOG("aerospike_scan_foreach() returned %d - %s", err.code, err.message);
		as_scan_destroy(&scan);
		cleanup(&as);
		exit(-1);
	}

	LOG("... scan with select completed");

	// Destroy the as_scan object.
	as_scan_destroy(&scan);

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

	LOG("standard scan example successfully completed");

	return 0;
}
示例#2
0
AerospikeScan * AerospikeScan_Select(AerospikeScan * self, PyObject * args, PyObject * kwds)
{
	TRACE();

	char * bin = NULL; 
	PyObject * py_ustr = NULL;
	as_error err;
	as_error_init(&err);

	if (!self || !self->client->as) {
		as_error_update(&err, AEROSPIKE_ERR_PARAM, "Invalid aerospike object");
		goto CLEANUP;
	}

	if (!self->client->is_conn_16) {
		as_error_update(&err, AEROSPIKE_ERR_CLUSTER, "No connection to aerospike cluster");
		goto CLEANUP;
	}

	int nbins = (int) PyTuple_Size(args);
	as_scan_select_init(&self->scan, nbins);

	for ( int i = 0; i < nbins; i++ ) {
		PyObject * py_bin = PyTuple_GetItem(args, i);
		if ( py_bin) {
			TRACE();
			if (PyUnicode_Check(py_bin)) {
				py_ustr = PyUnicode_AsUTF8String(py_bin);
				bin = PyString_AsString(py_ustr);
			} else if (PyString_Check(py_bin)) {
				bin = PyString_AsString(py_bin);
			} else {
				as_error_update(&err, AEROSPIKE_ERR_PARAM, "Bin name should be of type string");
				PyObject * py_err = NULL;
				error_to_pyobject(&err, &py_err);
				PyObject *exception_type = raise_exception(&err);
				PyErr_SetObject(exception_type, py_err);
				Py_DECREF(py_err);
				return NULL;

			}
		} else {
			TRACE();
		}
		as_scan_select(&self->scan, bin);
		if (py_ustr) {
			Py_DECREF(py_ustr);
			py_ustr = NULL;
		}
	}

CLEANUP:

	if ( err.code != AEROSPIKE_OK ) {
		PyObject * py_err = NULL;
		error_to_pyobject(&err, &py_err);
		PyObject *exception_type = raise_exception(&err);
		PyErr_SetObject(exception_type, py_err);
		Py_DECREF(py_err);
		return NULL;
	}

	Py_INCREF(self);
	return self;
}