|
| 1 | +/* |
| 2 | + This is free and unencumbered software released into the public domain. |
| 3 | + Anyone is free to copy, modify, publish, use, compile, sell, or |
| 4 | + distribute this software, either in source code form or as a compiled |
| 5 | + binary, for any purpose, commercial or non-commercial, and by any |
| 6 | + means. |
| 7 | + In jurisdictions that recognize copyright laws, the author or authors |
| 8 | + of this software dedicate any and all copyright interest in the |
| 9 | + software to the public domain. We make this dedication for the benefit |
| 10 | + of the public at large and to the detriment of our heirs and |
| 11 | + successors. We intend this dedication to be an overt act of |
| 12 | + relinquishment in perpetuity of all present and future rights to this |
| 13 | + software under copyright law. |
| 14 | + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 15 | + EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 16 | + MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 17 | + IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR |
| 18 | + OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
| 19 | + ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
| 20 | + OTHER DEALINGS IN THE SOFTWARE. |
| 21 | + For more information, please refer to <http://unlicense.org/> |
| 22 | +*/ |
| 23 | + |
| 24 | +#include <dse.h> |
| 25 | + |
| 26 | +#include <stdarg.h> |
| 27 | +#include <stdio.h> |
| 28 | +#include <string.h> |
| 29 | + |
| 30 | +void print_error(CassFuture* future) { |
| 31 | + const char* message; |
| 32 | + size_t message_length; |
| 33 | + cass_future_error_message(future, &message, &message_length); |
| 34 | + fprintf(stderr, "Error: %.*s\n", (int)message_length, message); |
| 35 | +} |
| 36 | + |
| 37 | +CassCluster* create_cluster() { |
| 38 | + CassCluster* cluster = cass_cluster_new(); |
| 39 | + cass_cluster_set_contact_points(cluster, "127.0.0.1"); |
| 40 | + return cluster; |
| 41 | +} |
| 42 | + |
| 43 | +CassError connect_session(CassSession* session, const CassCluster* cluster) { |
| 44 | + CassError rc = CASS_OK; |
| 45 | + CassFuture* future = cass_session_connect(session, cluster); |
| 46 | + |
| 47 | + cass_future_wait(future); |
| 48 | + rc = cass_future_error_code(future); |
| 49 | + if (rc != CASS_OK) { |
| 50 | + print_error(future); |
| 51 | + } |
| 52 | + cass_future_free(future); |
| 53 | + |
| 54 | + return rc; |
| 55 | +} |
| 56 | + |
| 57 | +CassError execute_query(CassSession* session, const char* query) { |
| 58 | + CassError rc = CASS_OK; |
| 59 | + CassFuture* future = NULL; |
| 60 | + CassStatement* statement = cass_statement_new(query, 0); |
| 61 | + |
| 62 | + future = cass_session_execute(session, statement); |
| 63 | + cass_future_wait(future); |
| 64 | + |
| 65 | + rc = cass_future_error_code(future); |
| 66 | + if (rc != CASS_OK) { |
| 67 | + print_error(future); |
| 68 | + } |
| 69 | + |
| 70 | + cass_future_free(future); |
| 71 | + cass_statement_free(statement); |
| 72 | + |
| 73 | + return rc; |
| 74 | +} |
| 75 | + |
| 76 | +void insert_point(CassSession* session, const char* key, |
| 77 | + cass_double_t x, cass_double_t y) { |
| 78 | + CassFuture* future = NULL; |
| 79 | + CassStatement* statement |
| 80 | + = cass_statement_new("INSERT INTO examples.geotypes " |
| 81 | + "(key, point) VALUES (?, ?)", 2); |
| 82 | + |
| 83 | + cass_statement_bind_string(statement, 0, key); |
| 84 | + cass_statement_bind_dse_point(statement, 1, x, y); |
| 85 | + |
| 86 | + future = cass_session_execute(session, statement); |
| 87 | + |
| 88 | + if (cass_future_error_code(future) != CASS_OK) { |
| 89 | + print_error(future); |
| 90 | + } |
| 91 | + |
| 92 | + cass_future_free(future); |
| 93 | + cass_statement_free(statement); |
| 94 | +} |
| 95 | + |
| 96 | +void select_point(CassSession* session, const char* key) { |
| 97 | + CassFuture* future = NULL; |
| 98 | + CassStatement* statement |
| 99 | + = cass_statement_new("SELECT point FROM examples.geotypes WHERE key = ?", 1); |
| 100 | + |
| 101 | + cass_statement_bind_string(statement, 0, key); |
| 102 | + |
| 103 | + future = cass_session_execute(session, statement); |
| 104 | + |
| 105 | + if (cass_future_error_code(future) == CASS_OK) { |
| 106 | + cass_double_t x = 0.0, y = 0.0; |
| 107 | + const CassResult* result = cass_future_get_result(future); |
| 108 | + if (result && cass_result_column_count(result) > 0) { |
| 109 | + const CassRow* row = cass_result_first_row(result); |
| 110 | + const CassValue* value = cass_row_get_column_by_name(row, "point"); |
| 111 | + |
| 112 | + cass_value_get_dse_point(value, &x, &y); |
| 113 | + printf("%s : POINT(%.1f %.1f)\n", key, x, y); |
| 114 | + |
| 115 | + cass_result_free(result); |
| 116 | + } |
| 117 | + } else { |
| 118 | + print_error(future); |
| 119 | + } |
| 120 | + |
| 121 | + cass_future_free(future); |
| 122 | + cass_statement_free(statement); |
| 123 | +} |
| 124 | + |
| 125 | +void insert_line_string(CassSession* session, const char* key, int num_points, ...) { |
| 126 | + int i; |
| 127 | + va_list args; |
| 128 | + CassFuture* future = NULL; |
| 129 | + CassStatement* statement |
| 130 | + = cass_statement_new("INSERT INTO examples.geotypes " |
| 131 | + "(key, linestring) VALUES (?, ?)", 2); |
| 132 | + DseLineString* line_string = dse_line_string_new(); |
| 133 | + |
| 134 | + dse_line_string_reserve(line_string, num_points); |
| 135 | + |
| 136 | + va_start(args, num_points); |
| 137 | + for (i = 0; i < num_points; ++i) { |
| 138 | + cass_double_t x = va_arg(args, cass_double_t); |
| 139 | + cass_double_t y = va_arg(args, cass_double_t); |
| 140 | + dse_line_string_add_point(line_string, x, y); |
| 141 | + } |
| 142 | + va_end(args); |
| 143 | + |
| 144 | + dse_line_string_finish(line_string); |
| 145 | + |
| 146 | + cass_statement_bind_string(statement, 0, key); |
| 147 | + cass_statement_bind_dse_line_string(statement, 1, line_string); |
| 148 | + |
| 149 | + future = cass_session_execute(session, statement); |
| 150 | + |
| 151 | + if (cass_future_error_code(future) != CASS_OK) { |
| 152 | + print_error(future); |
| 153 | + } |
| 154 | + |
| 155 | + cass_future_free(future); |
| 156 | + dse_line_string_free(line_string); |
| 157 | + cass_statement_free(statement); |
| 158 | +} |
| 159 | + |
| 160 | +void select_line_string(CassSession* session, const char* key) { |
| 161 | + CassFuture* future = NULL; |
| 162 | + CassStatement* statement |
| 163 | + = cass_statement_new("SELECT linestring FROM examples.geotypes WHERE key = ?", 1); |
| 164 | + |
| 165 | + cass_statement_bind_string(statement, 0, key); |
| 166 | + |
| 167 | + future = cass_session_execute(session, statement); |
| 168 | + |
| 169 | + if (cass_future_error_code(future) == CASS_OK) { |
| 170 | + const CassResult* result = cass_future_get_result(future); |
| 171 | + if (result && cass_result_column_count(result) > 0) { |
| 172 | + cass_uint32_t i, num_points; |
| 173 | + const CassRow* row = cass_result_first_row(result); |
| 174 | + const CassValue* value = cass_row_get_column_by_name(row, "linestring"); |
| 175 | + |
| 176 | + DseLineStringIterator* iterator = dse_line_string_iterator_new(); |
| 177 | + |
| 178 | + dse_line_string_iterator_reset(iterator, value); |
| 179 | + |
| 180 | + num_points = dse_line_string_iterator_num_points(iterator); |
| 181 | + |
| 182 | + printf("%s : LINESTRING(", key); |
| 183 | + for (i = 0; i < num_points; ++i) { |
| 184 | + cass_double_t x = 0.0, y = 0.0; |
| 185 | + dse_line_string_iterator_next_point(iterator, &x, &y); |
| 186 | + if (i > 0) printf(", "); |
| 187 | + printf("%.1f %.1f", x, y); |
| 188 | + } |
| 189 | + printf(")\n"); |
| 190 | + |
| 191 | + dse_line_string_iterator_free(iterator); |
| 192 | + cass_result_free(result); |
| 193 | + } |
| 194 | + } else { |
| 195 | + print_error(future); |
| 196 | + } |
| 197 | + |
| 198 | + cass_future_free(future); |
| 199 | + cass_statement_free(statement); |
| 200 | +} |
| 201 | + |
| 202 | +void insert_polygon(CassSession* session, const char* key, int num_rings, ...) { |
| 203 | + int i, j; |
| 204 | + va_list args; |
| 205 | + CassFuture* future = NULL; |
| 206 | + CassStatement* statement |
| 207 | + = cass_statement_new("INSERT INTO examples.geotypes " |
| 208 | + "(key, polygon) VALUES (?, ?)", 2); |
| 209 | + |
| 210 | + DsePolygon* polygon = dse_polygon_new(); |
| 211 | + |
| 212 | + va_start(args, num_rings); |
| 213 | + for (i = 0; i < num_rings; ++i) { |
| 214 | + int num_points = va_arg(args, int); |
| 215 | + dse_polygon_start_ring(polygon); |
| 216 | + for (j = 0; j < num_points; ++j) { |
| 217 | + cass_double_t x = va_arg(args, cass_double_t); |
| 218 | + cass_double_t y = va_arg(args, cass_double_t); |
| 219 | + dse_polygon_add_point(polygon, x, y); |
| 220 | + } |
| 221 | + } |
| 222 | + va_end(args); |
| 223 | + |
| 224 | + dse_polygon_finish(polygon); |
| 225 | + |
| 226 | + cass_statement_bind_string(statement, 0, key); |
| 227 | + cass_statement_bind_dse_polygon(statement, 1, polygon); |
| 228 | + |
| 229 | + future = cass_session_execute(session, statement); |
| 230 | + |
| 231 | + if (cass_future_error_code(future) != CASS_OK) { |
| 232 | + print_error(future); |
| 233 | + } |
| 234 | + |
| 235 | + cass_future_free(future); |
| 236 | + dse_polygon_free(polygon); |
| 237 | + cass_statement_free(statement); |
| 238 | +} |
| 239 | + |
| 240 | +void select_polygon(CassSession* session, const char* key) { |
| 241 | + CassFuture* future = NULL; |
| 242 | + CassStatement* statement |
| 243 | + = cass_statement_new("SELECT polygon FROM examples.geotypes WHERE key = ?", 1); |
| 244 | + |
| 245 | + cass_statement_bind_string(statement, 0, key); |
| 246 | + |
| 247 | + future = cass_session_execute(session, statement); |
| 248 | + |
| 249 | + if (cass_future_error_code(future) == CASS_OK) { |
| 250 | + const CassResult* result = cass_future_get_result(future); |
| 251 | + if (result && cass_result_column_count(result) > 0) { |
| 252 | + cass_uint32_t i, j, num_rings; |
| 253 | + const CassRow* row = cass_result_first_row(result); |
| 254 | + const CassValue* value = cass_row_get_column_by_name(row, "polygon"); |
| 255 | + |
| 256 | + DsePolygonIterator* iterator = dse_polygon_iterator_new(); |
| 257 | + |
| 258 | + dse_polygon_iterator_reset(iterator, value); |
| 259 | + |
| 260 | + num_rings = dse_polygon_iterator_num_rings(iterator); |
| 261 | + |
| 262 | + printf("%s : POLYGON(", key); |
| 263 | + for (i = 0; i < num_rings; ++i) { |
| 264 | + cass_uint32_t num_points = 0; |
| 265 | + dse_polygon_iterator_next_num_points(iterator, &num_points); |
| 266 | + printf("("); |
| 267 | + for (j = 0; j < num_points; ++j) { |
| 268 | + cass_double_t x = 0.0, y; |
| 269 | + dse_polygon_iterator_next_point(iterator, &x, &y); |
| 270 | + if (j > 0) printf(", "); |
| 271 | + printf("%.1f %.1f", x, y); |
| 272 | + } |
| 273 | + printf(")"); |
| 274 | + } |
| 275 | + printf(")\n"); |
| 276 | + |
| 277 | + dse_polygon_iterator_free(iterator); |
| 278 | + cass_result_free(result); |
| 279 | + } |
| 280 | + } else { |
| 281 | + print_error(future); |
| 282 | + } |
| 283 | + |
| 284 | + cass_future_free(future); |
| 285 | + cass_statement_free(statement); |
| 286 | +} |
| 287 | + |
| 288 | +int main() { |
| 289 | + CassCluster* cluster = create_cluster(); |
| 290 | + CassSession* session = cass_session_new(); |
| 291 | + CassFuture* close_future = NULL; |
| 292 | + |
| 293 | + if (connect_session(session, cluster) != CASS_OK) { |
| 294 | + cass_cluster_free(cluster); |
| 295 | + cass_session_free(session); |
| 296 | + return -1; |
| 297 | + } |
| 298 | + |
| 299 | + execute_query(session, |
| 300 | + "CREATE KEYSPACE IF NOT EXISTS examples " |
| 301 | + "WITH replication = { 'class': 'SimpleStrategy', 'replication_factor': '3' };"); |
| 302 | + |
| 303 | + execute_query(session, "CREATE TABLE IF NOT EXISTS examples.geotypes (" |
| 304 | + "key text PRIMARY KEY, " |
| 305 | + "point 'PointType', " |
| 306 | + "linestring 'LineStringType', " |
| 307 | + "polygon 'PolygonType')"); |
| 308 | + |
| 309 | + insert_point(session, "pnt1", 0.1, 0.1); |
| 310 | + select_point(session, "pnt1"); |
| 311 | + |
| 312 | + insert_line_string(session, "lnstr1", 0); |
| 313 | + select_line_string(session, "lnstr1"); |
| 314 | + |
| 315 | + insert_line_string(session, "lnstr2", 2, |
| 316 | + 0.0, 0.0, 1.0, 1.0); |
| 317 | + select_line_string(session, "lnstr2"); |
| 318 | + |
| 319 | + insert_line_string(session, "lnstr3", 3, |
| 320 | + 0.0, 0.0, 1.0, 0.0, |
| 321 | + 2.0, 0.0); |
| 322 | + select_line_string(session, "lnstr3"); |
| 323 | + |
| 324 | + insert_line_string(session, "lnstr4", 4, |
| 325 | + 0.0, 0.0, 1.0, 0.0, |
| 326 | + 2.0, 0.0, 3.0, 0.0); |
| 327 | + select_line_string(session, "lnstr4"); |
| 328 | + |
| 329 | + insert_polygon(session, "poly1", 0); |
| 330 | + select_polygon(session, "poly1"); |
| 331 | + |
| 332 | + insert_polygon(session, "poly2", 1, 0); |
| 333 | + select_polygon(session, "poly2"); |
| 334 | + |
| 335 | + insert_polygon(session, "poly3", 1, |
| 336 | + 3, |
| 337 | + 0.0, 0.0, 0.5, 0.0, |
| 338 | + 1.0, 1.0); |
| 339 | + select_polygon(session, "poly3"); |
| 340 | + |
| 341 | + insert_polygon(session, "poly4", 2, |
| 342 | + 5, |
| 343 | + 35.0, 10.0, 45.0, 45.0, |
| 344 | + 15.0, 40.0, 10.0, 20.0, |
| 345 | + 35.0, 10.0, |
| 346 | + 4, |
| 347 | + 20.0, 30.0, 35.0, 35.0, |
| 348 | + 30.0, 20.0, 20.0, 30.0); |
| 349 | + select_polygon(session, "poly4"); |
| 350 | + |
| 351 | + close_future = cass_session_close(session); |
| 352 | + cass_future_wait(close_future); |
| 353 | + cass_future_free(close_future); |
| 354 | + |
| 355 | + cass_cluster_free(cluster); |
| 356 | + cass_session_free(session); |
| 357 | + |
| 358 | + return 0; |
| 359 | +} |
| 360 | + |
0 commit comments