PyObject * AerospikeClient_HasGeo(AerospikeClient * self, PyObject * args, PyObject * kwds) { // Initialize error as_error err; as_error_init(&err); if (!self || !self->as) { as_error_update(&err, AEROSPIKE_ERR_PARAM, "Invalid aerospike object"); goto CLEANUP; } if (!self->is_conn_16) { as_error_update(&err, AEROSPIKE_ERR_CLUSTER, "No connection to aerospike cluster"); goto CLEANUP; } if (aerospike_has_geo(self->as)) { Py_INCREF(Py_True); return Py_True; } Py_INCREF(Py_False); return Py_False; 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; } return NULL; }
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); // Ensure the server supports geospatial queries. if (! aerospike_has_geo(&as)) { fprintf(stderr, "server does not support geospatial\n"); exit(0); } // Start clean. example_remove_test_records(&as); example_remove_index(&as, TEST_INDEX_NAME); // Create a numeric secondary index on test-bin. if (! example_create_2dsphere_index(&as, TEST_BIN_NAME, TEST_INDEX_NAME)) { cleanup(&as); exit(-1); } if (! insert_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); // Our query region: char const * region = "{ " " \"type\": \"Polygon\", " " \"coordinates\": [ " " [[-122.500000, 37.000000],[-121.000000, 37.000000], " " [-121.000000, 38.080000],[-122.500000, 38.080000], " " [-122.500000, 37.000000]] " " ] " " } "; // 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_NAME, as_geo_within(region)); LOG("executing query: within <rect>"); // 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("query executed"); as_query_destroy(&query); // Cleanup and disconnect from the database cluster. cleanup(&as); LOG("simple query example successfully completed"); return 0; }
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); }