Example #1
0
PyObject * AerospikeGeospatial_Dumps(AerospikeGeospatial * self, PyObject * args, PyObject * kwds)
{

    PyObject *initresult = NULL;
	// Aerospike error object
	as_error err;
	// Initialize error object
	as_error_init(&err);

	if (!self) {
		as_error_update(&err, AEROSPIKE_ERR_PARAM, "Invalid geospatial data");
		goto CLEANUP;
	}

	initresult = AerospikeGeospatial_DoDumps(self->geo_data, &err);
    if(!initresult) {
	    as_error_update(&err, AEROSPIKE_ERR_CLIENT, "Unable to call dumps function");
		goto CLEANUP;
    }

CLEANUP:

	// If an error occurred, tell Python.
	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;
	}

	return initresult;
}
static PyObject * AerospikePredicates_GeoContains_Point(PyObject * self, PyObject * args)
{
	PyObject * py_bin = NULL;
	PyObject * py_lat = NULL;
	PyObject * py_long = NULL;
	PyObject * py_geo_object = NULL;
	PyObject * py_shape = NULL;

	as_error err;
	as_error_init(&err);

	py_geo_object = PyDict_New();

	if (PyArg_ParseTuple(args, "OOO:geo_contains_point", 
			&py_bin, &py_lat, &py_long) == false) {
		goto CLEANUP;
	}

	PyObject *py_point = PyString_FromString("Point");
	PyDict_SetItemString(py_geo_object, "type", py_point);
	Py_DECREF(py_point);

	if (PyString_Check(py_bin) && PyFloat_Check(py_lat) && PyFloat_Check(py_long)) {
		PyObject * py_list = PyList_New(2);
		PyList_SetItem(py_list, 0, py_lat);
		PyList_SetItem(py_list, 1, py_long);

		PyDict_SetItemString(py_geo_object, "coordinates", py_list);

		py_shape = AerospikeGeospatial_DoDumps(py_geo_object, &err);

		if (!py_shape) {
			as_error_update(&err, AEROSPIKE_ERR_CLIENT, "Unable to call dumps function");
			goto CLEANUP;
		}
	} else {
		as_error_update(&err, AEROSPIKE_ERR_PARAM, "Latitude and longitude should be of double type, bin of string type");
		goto CLEANUP;
	}
	
	return Py_BuildValue("iiOOi", AS_PREDICATE_RANGE, AS_INDEX_GEO2DSPHERE, py_bin, py_shape, 1);

CLEANUP:
	// If an error occurred, tell Python.
	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(Py_None);
	return Py_None;
}
static PyObject * AerospikePredicates_GeoWithin_Radius(PyObject * self, PyObject * args)
{
	PyObject * py_bin = NULL;
	PyObject * py_lat = NULL;
	PyObject * py_long = NULL;
	PyObject * py_radius = NULL;
	PyObject * py_geo_object = NULL;
	PyObject * py_shape = NULL;
	PyObject * py_indexType = NULL;

	as_error err;
	as_error_init(&err);

	py_geo_object = PyDict_New();

	if (PyArg_ParseTuple(args, "OOOO|O:geo_within_radius",
			&py_bin, &py_lat, &py_long, &py_radius, &py_indexType) == false) {
		goto CLEANUP;
	}

	if (py_indexType == NULL) {
		py_indexType = Py_BuildValue("i", AS_INDEX_TYPE_DEFAULT);
	}

	PyObject *py_circle = PyString_FromString("AeroCircle");
	PyDict_SetItemString(py_geo_object, "type", py_circle);
	Py_DECREF(py_circle);

	if (PyString_Check(py_bin) &&
			(PyFloat_Check(py_lat) || PyInt_Check(py_lat)) &&
			(PyFloat_Check(py_long) || PyInt_Check(py_long)) &&
			(PyFloat_Check(py_radius) || PyInt_Check(py_radius))) {
		PyObject * py_outer_list = PyList_New(2);
		PyObject * py_inner_list = PyList_New(2);
		PyList_SetItem(py_inner_list, 0, py_lat);
		PyList_SetItem(py_inner_list, 1, py_long);

		PyList_SetItem(py_outer_list, 0, py_inner_list);
		PyList_SetItem(py_outer_list, 1, py_radius);

		PyDict_SetItemString(py_geo_object, "coordinates", py_outer_list);

		py_shape = AerospikeGeospatial_DoDumps(py_geo_object, &err);

		if (!py_shape) {
			as_error_update(&err, AEROSPIKE_ERR_CLIENT, "Unable to call dumps function");
			goto CLEANUP;
		}
	} else {
		as_error_update(&err, AEROSPIKE_ERR_PARAM, "Latitude, longitude and radius should be integer or double type, bin of string type");
		goto CLEANUP;
	}
	
	return Py_BuildValue("iiOOOO", AS_PREDICATE_RANGE, AS_INDEX_GEO2DSPHERE, py_bin, py_shape, Py_None, py_indexType );

CLEANUP:
	// If an error occurred, tell Python.
	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(Py_None);
	return Py_None;
}
static void initialize_bin_for_strictypes(AerospikeClient *self, as_error *err, PyObject *py_value, as_binop *binop, char *bin, as_static_pool *static_pool) {
	
	as_bin *binop_bin = &binop->bin;
	if (PyInt_Check(py_value)) {
		int val = PyInt_AsLong(py_value);
		as_integer_init((as_integer *) &binop_bin->value, val);
		binop_bin->valuep = &binop_bin->value;
	} else if (PyLong_Check(py_value)) {
		long val = PyLong_AsLong(py_value);
		as_integer_init((as_integer *) &binop_bin->value, val);
		binop_bin->valuep = &binop_bin->value;
	} else if (PyString_Check(py_value)) {
		char * val = PyString_AsString(py_value);
		as_string_init((as_string *) &binop_bin->value, val, false);
		binop_bin->valuep = &binop_bin->value;	
	} else if (PyUnicode_Check(py_value)) {
		PyObject *py_ustr1 = PyUnicode_AsUTF8String(py_value);
		char * val = PyString_AsString(py_ustr1);
		as_string_init((as_string *) &binop_bin->value, val, false);
		binop_bin->valuep = &binop_bin->value;	
	} else if (PyFloat_Check(py_value)) {
		int64_t val = PyFloat_AsDouble(py_value);
		if (aerospike_has_double(self->as)) {
			as_double_init((as_double *) &binop_bin->value, val);
			binop_bin->valuep = &binop_bin->value;
		} else {
			as_bytes *bytes;
			GET_BYTES_POOL(bytes, static_pool, err);
			serialize_based_on_serializer_policy(self, SERIALIZER_PYTHON, &bytes, py_value, err);	
			((as_val *) &binop_bin->value)->type = AS_UNKNOWN;
			binop_bin->valuep = (as_bin_value *) bytes;
		}
	} else if (PyList_Check(py_value)) {
		as_list * list = NULL;
		pyobject_to_list(self, err, py_value, &list, static_pool, SERIALIZER_PYTHON);
		((as_val *) &binop_bin->value)->type = AS_UNKNOWN;
		binop_bin->valuep = (as_bin_value *) list;
	} else if (PyDict_Check(py_value)) {
		as_map * map = NULL;
		pyobject_to_map(self, err, py_value, &map, static_pool, SERIALIZER_PYTHON);
		((as_val *) &binop_bin->value)->type = AS_UNKNOWN;
		binop_bin->valuep = (as_bin_value *) map;
	} else if (!strcmp(py_value->ob_type->tp_name, "aerospike.Geospatial")) {
		PyObject* py_data = PyObject_GenericGetAttr(py_value, PyString_FromString("geo_data"));
		char *geo_value = PyString_AsString(AerospikeGeospatial_DoDumps(py_data, err));
		if (aerospike_has_geo(self->as)) {
			as_geojson_init((as_geojson *) &binop_bin->value, geo_value, false);
			binop_bin->valuep = &binop_bin->value;
		} else {
			as_bytes *bytes;
			GET_BYTES_POOL(bytes, static_pool, err);
			serialize_based_on_serializer_policy(self, SERIALIZER_PYTHON, &bytes, py_data, err);	
			((as_val *) &binop_bin->value)->type = AS_UNKNOWN;
			binop_bin->valuep = (as_bin_value *) bytes;
		}
	} else if (!strcmp(py_value->ob_type->tp_name, "aerospike.null")) {
		((as_val *) &binop_bin->value)->type = AS_UNKNOWN;
		binop_bin->valuep = (as_bin_value *) &as_nil;
	} else if (PyByteArray_Check(py_value)) {
		as_bytes *bytes;
		GET_BYTES_POOL(bytes, static_pool, err);
		serialize_based_on_serializer_policy(self, SERIALIZER_PYTHON, &bytes, py_value, err);
		as_bytes_init_wrap((as_bytes *) &binop_bin->value, bytes->value, bytes->size, false);	
		binop_bin->valuep = &binop_bin->value;
	} else {
		as_bytes *bytes;
		GET_BYTES_POOL(bytes, static_pool, err);
		serialize_based_on_serializer_policy(self, SERIALIZER_PYTHON, &bytes, py_value, err);	
		((as_val *) &binop_bin->value)->type = AS_UNKNOWN;
		binop_bin->valuep = (as_bin_value *) bytes;
	}
	strcpy(binop_bin->name, bin);
}