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; }
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 char * pyobject_to_str(PyObject * py_obj) { if ( PyStr_Check(py_obj) ) { return PyStr_AsString(py_obj); } else { return NULL; } } */ static int AerospikeQuery_Where_Add(AerospikeQuery * self, as_predicate_type predicate, as_index_datatype in_datatype, PyObject * py_bin, PyObject * py_val1, PyObject * py_val2, int index_type) { as_error err; char * val = NULL, * bin = NULL; PyObject * py_ubin = NULL; switch (predicate) { case AS_PREDICATE_EQUAL: { if ( in_datatype == AS_INDEX_STRING ){ if (PyUnicode_Check(py_bin)){ py_ubin = PyUnicode_AsUTF8String(py_bin); bin = PyStr_AsString(py_ubin); } else if (PyStr_Check(py_bin) ){ bin = PyStr_AsString(py_bin); } else { return 1; } if (PyUnicode_Check(py_val1)){ val = strdup(PyStr_AsString( StoreUnicodePyObject( self, PyUnicode_AsUTF8String(py_val1) ))); } else if (PyStr_Check(py_val1) ){ val = strdup(PyStr_AsString(py_val1)); } else { return 1; } as_query_where_init(&self->query, 1); if(index_type == 0) { as_query_where(&self->query, bin, as_equals( STRING, val )); } else if(index_type == 1) { as_query_where(&self->query, bin, as_contains( LIST, STRING, val )); } else if(index_type == 2) { as_query_where(&self->query, bin, as_contains( MAPKEYS, STRING, val )); } else if(index_type == 3) { as_query_where(&self->query, bin, as_contains( MAPVALUES, STRING, val )); } else { return 1; } if (py_ubin){ Py_DECREF(py_ubin); py_ubin = NULL; } } else if ( in_datatype == AS_INDEX_NUMERIC ){ if (PyUnicode_Check(py_bin)){ py_ubin = PyUnicode_AsUTF8String(py_bin); bin = PyStr_AsString(py_ubin); } else if (PyStr_Check(py_bin) ){ bin = PyStr_AsString(py_bin); } else { return 1; } int64_t val = pyobject_to_int64(py_val1); as_query_where_init(&self->query, 1); if(index_type == 0) { as_query_where(&self->query, bin, as_equals( NUMERIC, val )); } else if(index_type == 1) { as_query_where(&self->query, bin, as_contains( LIST, NUMERIC, val )); } else if(index_type == 2) { as_query_where(&self->query, bin, as_contains( MAPKEYS, NUMERIC, val )); } else if(index_type == 3) { as_query_where(&self->query, bin, as_contains( MAPVALUES, NUMERIC, val )); } else { return 1; } if (py_ubin){ Py_DECREF(py_ubin); py_ubin = NULL; } } else { // If it ain't expected, raise and error as_error_update(&err, AEROSPIKE_ERR_PARAM, "predicate 'equals' expects a string or integer value."); PyObject * py_err = NULL; error_to_pyobject(&err, &py_err); PyErr_SetObject(PyExc_Exception, py_err); return 1; } break; } case AS_PREDICATE_RANGE: { if ( in_datatype == AS_INDEX_NUMERIC) { if (PyUnicode_Check(py_bin)){ py_ubin = PyUnicode_AsUTF8String(py_bin); bin = PyStr_AsString(py_ubin); } else if (PyStr_Check(py_bin)){ bin = PyStr_AsString(py_bin); } else { return 1; } int64_t min = pyobject_to_int64(py_val1); int64_t max = pyobject_to_int64(py_val2); as_query_where_init(&self->query, 1); if(index_type == 0) { as_query_where(&self->query, bin, as_range( DEFAULT, NUMERIC, min, max )); } else if(index_type == 1) { as_query_where(&self->query, bin, as_range( LIST, NUMERIC, min, max )); } else if(index_type == 2) { as_query_where(&self->query, bin, as_range( MAPKEYS, NUMERIC, min, max )); } else if(index_type == 3) { as_query_where(&self->query, bin, as_range( MAPVALUES, NUMERIC, min, max )); } else { return 1; } if (py_ubin){ Py_DECREF(py_ubin); py_ubin = NULL; } } else if ( in_datatype == AS_INDEX_STRING) { // NOT IMPLEMENTED } else { // If it ain't right, raise and error as_error_update(&err, AEROSPIKE_ERR_PARAM, "predicate 'between' expects two integer values."); PyObject * py_err = NULL; error_to_pyobject(&err, &py_err); PyErr_SetObject(PyExc_Exception, py_err); return 1; } break; } default: { // If it ain't supported, raise and error as_error_update(&err, AEROSPIKE_ERR_PARAM, "unknown predicate type"); PyObject * py_err = NULL; error_to_pyobject(&err, &py_err); PyErr_SetObject(PyExc_Exception, py_err); return 1; } } return 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); example_remove_index(&as, TEST_INDEX_NAME); // 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_equals(7)); LOG("executing query: where test-bin = 7"); // 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; }