diff --git a/docqueries/driving_distance/doc-pgr_drivingDistance.result b/docqueries/driving_distance/doc-pgr_drivingDistance.result deleted file mode 100644 index 77efb0c90a..0000000000 --- a/docqueries/driving_distance/doc-pgr_drivingDistance.result +++ /dev/null @@ -1,80 +0,0 @@ -BEGIN; -BEGIN -SET client_min_messages TO NOTICE; -SET -/* --q5 */ -SELECT * FROM pgr_drivingDistance( - 'SELECT id, source, target, cost, reverse_cost FROM edges', - 11, 3.0); - seq | node | edge | cost | agg_cost ------+------+------+------+---------- - 1 | 11 | -1 | 0 | 0 - 2 | 7 | 8 | 1 | 1 - 3 | 12 | 11 | 1 | 1 - 4 | 16 | 9 | 1 | 1 - 5 | 3 | 7 | 1 | 2 - 6 | 6 | 4 | 1 | 2 - 7 | 8 | 10 | 1 | 2 - 8 | 15 | 16 | 1 | 2 - 9 | 17 | 15 | 1 | 2 - 10 | 1 | 6 | 1 | 3 - 11 | 5 | 1 | 1 | 3 - 12 | 9 | 14 | 1 | 3 - 13 | 10 | 3 | 1 | 3 -(13 rows) - -/* --q6 */ -SELECT * FROM pgr_drivingDistance( - 'SELECT id, source, target, cost, reverse_cost FROM edges', - array[11, 16], 3.0, equicost => true); - seq | from_v | node | edge | cost | agg_cost ------+--------+------+------+------+---------- - 1 | 11 | 11 | -1 | 0 | 0 - 2 | 11 | 7 | 8 | 1 | 1 - 3 | 11 | 12 | 11 | 1 | 1 - 4 | 11 | 3 | 7 | 1 | 2 - 5 | 11 | 6 | 4 | 1 | 2 - 6 | 11 | 8 | 10 | 1 | 2 - 7 | 11 | 1 | 6 | 1 | 3 - 8 | 11 | 5 | 1 | 1 | 3 - 9 | 11 | 9 | 14 | 1 | 3 - 10 | 16 | 16 | -1 | 0 | 0 - 11 | 16 | 15 | 16 | 1 | 1 - 12 | 16 | 17 | 15 | 1 | 1 - 13 | 16 | 10 | 3 | 1 | 2 -(13 rows) - -/* --q10 */ -SELECT * FROM pgr_drivingDistance( - 'SELECT id, source, target, cost, reverse_cost FROM edges', - array[11, 16], 3.0, directed => false); - seq | from_v | node | edge | cost | agg_cost ------+--------+------+------+------+---------- - 1 | 11 | 11 | -1 | 0 | 0 - 2 | 11 | 7 | 8 | 1 | 1 - 3 | 11 | 10 | 5 | 1 | 1 - 4 | 11 | 12 | 11 | 1 | 1 - 5 | 11 | 16 | 9 | 1 | 1 - 6 | 11 | 3 | 7 | 1 | 2 - 7 | 11 | 6 | 2 | 1 | 2 - 8 | 11 | 8 | 10 | 1 | 2 - 9 | 11 | 15 | 3 | 1 | 2 - 10 | 11 | 17 | 15 | 1 | 2 - 11 | 11 | 1 | 6 | 1 | 3 - 12 | 11 | 5 | 1 | 1 | 3 - 13 | 11 | 9 | 14 | 1 | 3 - 14 | 16 | 16 | -1 | 0 | 0 - 15 | 16 | 11 | 9 | 1 | 1 - 16 | 16 | 15 | 16 | 1 | 1 - 17 | 16 | 17 | 15 | 1 | 1 - 18 | 16 | 7 | 8 | 1 | 2 - 19 | 16 | 10 | 5 | 1 | 2 - 20 | 16 | 12 | 13 | 1 | 2 - 21 | 16 | 3 | 7 | 1 | 3 - 22 | 16 | 6 | 4 | 1 | 3 - 23 | 16 | 8 | 10 | 1 | 3 -(23 rows) - -/* --q15 */ -ROLLBACK; -ROLLBACK diff --git a/docqueries/driving_distance/doc-pgr_drivingDistance.test.sql b/docqueries/driving_distance/doc-pgr_drivingDistance.test.sql deleted file mode 100644 index c5edda4786..0000000000 --- a/docqueries/driving_distance/doc-pgr_drivingDistance.test.sql +++ /dev/null @@ -1,15 +0,0 @@ --- CopyRight(c) pgRouting developers --- Creative Commons Attribution-Share Alike 3.0 License : https://creativecommons.org/licenses/by-sa/3.0/ -/* --q5 */ -SELECT * FROM pgr_drivingDistance( - 'SELECT id, source, target, cost, reverse_cost FROM edges', - 11, 3.0); -/* --q6 */ -SELECT * FROM pgr_drivingDistance( - 'SELECT id, source, target, cost, reverse_cost FROM edges', - array[11, 16], 3.0, equicost => true); -/* --q10 */ -SELECT * FROM pgr_drivingDistance( - 'SELECT id, source, target, cost, reverse_cost FROM edges', - array[11, 16], 3.0, directed => false); -/* --q15 */ diff --git a/docqueries/driving_distance/pgr_drivingDistance.result b/docqueries/driving_distance/pgr_drivingDistance.result index 3f0fd3f201..27c6f7a85e 100644 --- a/docqueries/driving_distance/pgr_drivingDistance.result +++ b/docqueries/driving_distance/pgr_drivingDistance.result @@ -73,7 +73,7 @@ SELECT * FROM pgr_drivingDistance( 21 | 3 | 16 | 3 | 7 | 1 | 3 22 | 3 | 16 | 6 | 4 | 1 | 3 23 | 3 | 16 | 8 | 10 | 1 | 3 -(23 rows) +(23 rows) /* --q15 */ ROLLBACK; diff --git a/docqueries/driving_distance/pgr_drivingDistance.test.sql b/docqueries/driving_distance/pgr_drivingDistance.test.sql index fa586918bd..c5edda4786 100644 --- a/docqueries/driving_distance/pgr_drivingDistance.test.sql +++ b/docqueries/driving_distance/pgr_drivingDistance.test.sql @@ -12,4 +12,4 @@ SELECT * FROM pgr_drivingDistance( SELECT * FROM pgr_drivingDistance( 'SELECT id, source, target, cost, reverse_cost FROM edges', array[11, 16], 3.0, directed => false); -/* --q15 */ \ No newline at end of file +/* --q15 */ diff --git a/src/driving_distance/CMakeLists.txt b/src/driving_distance/CMakeLists.txt index 033899093d..9d568dfd70 100644 --- a/src/driving_distance/CMakeLists.txt +++ b/src/driving_distance/CMakeLists.txt @@ -1,5 +1,5 @@ ADD_LIBRARY(driving_distance OBJECT - driving_distance.c + many_to_dist_driving_distance.c many_to_dist_withPointsDD.c drivedist_driver.cpp diff --git a/src/driving_distance/driving_distance.c b/src/driving_distance/driving_distance.c deleted file mode 100644 index 8ec55dcbfc..0000000000 --- a/src/driving_distance/driving_distance.c +++ /dev/null @@ -1,278 +0,0 @@ -/*PGR-GNU***************************************************************** -File: driving_distance.c - -Copyright (c) 2015 Celia Virginia Vergara Castillo -Mail: vicky at erosion.dev - -Copyright (c) 2023 Aryan Gupta -guptaaryan1010 AT gmail.com ------- - -This program is free software; you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation; either version 2 of the License, or -(at your option) any later version. - -This program is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. - -You should have received a copy of the GNU General Public License -along with this program; if not, write to the Free Software -Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - - ********************************************************************PGR-GNU*/ - -#include -#include "c_common/postgres_connection.h" - -#include "c_types/path_rt.h" -#include "c_types/mst_rt.h" -#include "c_common/debug_macro.h" -#include "c_common/e_report.h" -#include "c_common/time_msg.h" - -#include "c_common/pgdata_getters.h" -#include "drivers/driving_distance/drivedist_driver.h" - - -PGDLLEXPORT Datum _pgr_drivingdistance(PG_FUNCTION_ARGS); -PG_FUNCTION_INFO_V1(_pgr_drivingdistance); - -PGDLLEXPORT Datum _pgr_v4drivingdistance(PG_FUNCTION_ARGS); -PG_FUNCTION_INFO_V1(_pgr_v4drivingdistance); - -static -void process( - char* edges_sql, - ArrayType *starts, - float8 distance, - bool directed, - bool equicost, - bool is_new, - Path_rt **result_old_tuples, - size_t *result_old_count, - MST_rt **result_tuples, - size_t *result_count) { - pgr_SPI_connect(); - char* log_msg = NULL; - char* notice_msg = NULL; - char* err_msg = NULL; - - size_t size_start_vidsArr = 0; - int64_t* start_vidsArr = pgr_get_bigIntArray(&size_start_vidsArr, starts, false, &err_msg); - throw_error(err_msg, "While getting start vids"); - - Edge_t *edges = NULL; - size_t total_tuples = 0; - pgr_get_edges(edges_sql, &edges, &total_tuples, true, false, &err_msg); - throw_error(err_msg, edges_sql); - - if (total_tuples == 0) { - return; - } - - PGR_DBG("Starting timer"); - clock_t start_t = clock(); - do_pgr_driving_many_to_dist( - edges, total_tuples, - start_vidsArr, size_start_vidsArr, - distance, - directed, - equicost, - is_new, - result_old_tuples, result_old_count, - result_tuples, result_count, - &log_msg, - ¬ice_msg, - &err_msg); - - time_msg("processing pgr_v4drivingDistance()", - start_t, clock()); - - if (err_msg && (*result_tuples)) { - pfree(*result_tuples); - (*result_tuples) = NULL; - (*result_count) = 0; - } - - pgr_global_report(log_msg, notice_msg, err_msg); - - if (log_msg) pfree(log_msg); - if (notice_msg) pfree(notice_msg); - if (err_msg) pfree(err_msg); - if (edges) pfree(edges); - if (start_vidsArr) pfree(start_vidsArr); - - pgr_SPI_finish(); -} - - -PGDLLEXPORT Datum -_pgr_v4drivingdistance(PG_FUNCTION_ARGS) { - FuncCallContext *funcctx; - TupleDesc tuple_desc; - - Path_rt *result_old_tuples = 0; - size_t result_old_count = 0; - MST_rt *result_tuples = 0; - size_t result_count = 0; - - if (SRF_IS_FIRSTCALL()) { - MemoryContext oldcontext; - - funcctx = SRF_FIRSTCALL_INIT(); - oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx); - - process( - text_to_cstring(PG_GETARG_TEXT_P(0)), - PG_GETARG_ARRAYTYPE_P(1), - PG_GETARG_FLOAT8(2), - PG_GETARG_BOOL(3), - PG_GETARG_BOOL(4), - true, - &result_old_tuples, &result_old_count, - &result_tuples, &result_count); - - - funcctx->max_calls = result_count; - - funcctx->user_fctx = result_tuples; - if (get_call_result_type(fcinfo, NULL, &tuple_desc) - != TYPEFUNC_COMPOSITE) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("function returning record called in context " - "that cannot accept type record"))); - - funcctx->tuple_desc = tuple_desc; - - MemoryContextSwitchTo(oldcontext); - } - - funcctx = SRF_PERCALL_SETUP(); - - tuple_desc = funcctx->tuple_desc; - result_tuples = (MST_rt*) funcctx->user_fctx; - - if (funcctx->call_cntr < funcctx->max_calls) { - HeapTuple tuple; - Datum result; - Datum *values; - bool* nulls; - - size_t numb = 7; - values = palloc(numb * sizeof(Datum)); - nulls = palloc(numb * sizeof(bool)); - - size_t i; - for (i = 0; i < numb; ++i) { - nulls[i] = false; - } - values[0] = Int32GetDatum(funcctx->call_cntr + 1); - values[1] = Int64GetDatum(result_tuples[funcctx->call_cntr].depth); - values[2] = Int64GetDatum(result_tuples[funcctx->call_cntr].from_v); - values[3] = Int64GetDatum(result_tuples[funcctx->call_cntr].node); - values[4] = Int64GetDatum(result_tuples[funcctx->call_cntr].edge); - values[5] = Float8GetDatum(result_tuples[funcctx->call_cntr].cost); - values[6] = Float8GetDatum(result_tuples[funcctx->call_cntr].agg_cost); - - tuple = heap_form_tuple(tuple_desc, values, nulls); - result = HeapTupleGetDatum(tuple); - - pfree(values); - pfree(nulls); - - SRF_RETURN_NEXT(funcctx, result); - } else { - SRF_RETURN_DONE(funcctx); - } -} - - -/* Old code starts here - * TODO(v4) remove old code - * its code that is used when there is an old version of SQL 3.5 and under - */ -PGDLLEXPORT Datum -_pgr_drivingdistance(PG_FUNCTION_ARGS) { - FuncCallContext *funcctx; - TupleDesc tuple_desc; - - Path_rt* result_tuples = 0; - size_t result_count = 0; - MST_rt *result_new_tuples = 0; - size_t result_new_count = 0; - - if (SRF_IS_FIRSTCALL()) { - MemoryContext oldcontext; - - funcctx = SRF_FIRSTCALL_INIT(); - oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx); - - - PGR_DBG("Calling driving_many_to_dist_driver"); - process( - text_to_cstring(PG_GETARG_TEXT_P(0)), - PG_GETARG_ARRAYTYPE_P(1), - PG_GETARG_FLOAT8(2), - PG_GETARG_BOOL(3), - PG_GETARG_BOOL(4), - false, - &result_tuples, &result_count, - &result_new_tuples, &result_new_count); - - - funcctx->max_calls = result_count; - - funcctx->user_fctx = result_tuples; - if (get_call_result_type(fcinfo, NULL, &tuple_desc) - != TYPEFUNC_COMPOSITE) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("function returning record called in context " - "that cannot accept type record"))); - - funcctx->tuple_desc = tuple_desc; - - MemoryContextSwitchTo(oldcontext); - } - - funcctx = SRF_PERCALL_SETUP(); - - tuple_desc = funcctx->tuple_desc; - result_tuples = (Path_rt*) funcctx->user_fctx; - - if (funcctx->call_cntr < funcctx->max_calls) { - HeapTuple tuple; - Datum result; - Datum *values; - bool* nulls; - - size_t numb = 6; - values = palloc(numb * sizeof(Datum)); - nulls = palloc(numb * sizeof(bool)); - - size_t i; - for (i = 0; i < numb; ++i) { - nulls[i] = false; - } - values[0] = Int32GetDatum((int32_t)funcctx->call_cntr + 1); - values[1] = Int64GetDatum(result_tuples[funcctx->call_cntr].start_id); - values[2] = Int64GetDatum(result_tuples[funcctx->call_cntr].node); - values[3] = Int64GetDatum(result_tuples[funcctx->call_cntr].edge); - values[4] = Float8GetDatum(result_tuples[funcctx->call_cntr].cost); - values[5] = Float8GetDatum(result_tuples[funcctx->call_cntr].agg_cost); - - tuple = heap_form_tuple(tuple_desc, values, nulls); - result = HeapTupleGetDatum(tuple); - - pfree(values); - pfree(nulls); - - SRF_RETURN_NEXT(funcctx, result); - } else { - SRF_RETURN_DONE(funcctx); - } -} diff --git a/src/driving_distance/many_to_dist_driving_distance.c b/src/driving_distance/many_to_dist_driving_distance.c index b5dae07feb..7d082cc113 100644 --- a/src/driving_distance/many_to_dist_driving_distance.c +++ b/src/driving_distance/many_to_dist_driving_distance.c @@ -2,8 +2,10 @@ File: many_to_dist_driving_distance.c Copyright (c) 2015 Celia Virginia Vergara Castillo -Mail: vicky_Vergara@hotmail.com +Mail: vicky at erosion.dev +Copyright (c) 2023 Aryan Gupta +guptaaryan1010 AT gmail.com ------ This program is free software; you can redistribute it and/or modify @@ -26,6 +28,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "c_common/postgres_connection.h" #include "c_types/path_rt.h" +#include "c_types/mst_rt.h" #include "c_common/debug_macro.h" #include "c_common/e_report.h" #include "c_common/time_msg.h" @@ -37,6 +40,8 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. PGDLLEXPORT Datum _pgr_drivingdistance(PG_FUNCTION_ARGS); PG_FUNCTION_INFO_V1(_pgr_drivingdistance); +PGDLLEXPORT Datum _pgr_v4drivingdistance(PG_FUNCTION_ARGS); +PG_FUNCTION_INFO_V1(_pgr_v4drivingdistance); static void process( @@ -45,7 +50,10 @@ void process( float8 distance, bool directed, bool equicost, - Path_rt **result_tuples, + bool is_new, + Path_rt **result_old_tuples, + size_t *result_old_count, + MST_rt **result_tuples, size_t *result_count) { pgr_SPI_connect(); char* log_msg = NULL; @@ -73,12 +81,14 @@ void process( distance, directed, equicost, + is_new, + result_old_tuples, result_old_count, result_tuples, result_count, &log_msg, ¬ice_msg, &err_msg); - time_msg("processing pgr_drivingDistance()", + time_msg("processing pgr_v4drivingDistance()", start_t, clock()); if (err_msg && (*result_tuples)) { @@ -99,15 +109,101 @@ void process( } +PGDLLEXPORT Datum +_pgr_v4drivingdistance(PG_FUNCTION_ARGS) { + FuncCallContext *funcctx; + TupleDesc tuple_desc; + + Path_rt *result_old_tuples = 0; + size_t result_old_count = 0; + MST_rt *result_tuples = 0; + size_t result_count = 0; + + if (SRF_IS_FIRSTCALL()) { + MemoryContext oldcontext; + + funcctx = SRF_FIRSTCALL_INIT(); + oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx); + + process( + text_to_cstring(PG_GETARG_TEXT_P(0)), + PG_GETARG_ARRAYTYPE_P(1), + PG_GETARG_FLOAT8(2), + PG_GETARG_BOOL(3), + PG_GETARG_BOOL(4), + true, + &result_old_tuples, &result_old_count, + &result_tuples, &result_count); + + + funcctx->max_calls = result_count; + + funcctx->user_fctx = result_tuples; + if (get_call_result_type(fcinfo, NULL, &tuple_desc) + != TYPEFUNC_COMPOSITE) + ereport(ERROR, + (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("function returning record called in context " + "that cannot accept type record"))); + + funcctx->tuple_desc = tuple_desc; + + MemoryContextSwitchTo(oldcontext); + } + + funcctx = SRF_PERCALL_SETUP(); + + tuple_desc = funcctx->tuple_desc; + result_tuples = (MST_rt*) funcctx->user_fctx; + + if (funcctx->call_cntr < funcctx->max_calls) { + HeapTuple tuple; + Datum result; + Datum *values; + bool* nulls; + + size_t numb = 7; + values = palloc(numb * sizeof(Datum)); + nulls = palloc(numb * sizeof(bool)); + + size_t i; + for (i = 0; i < numb; ++i) { + nulls[i] = false; + } + values[0] = Int32GetDatum(funcctx->call_cntr + 1); + values[1] = Int64GetDatum(result_tuples[funcctx->call_cntr].depth); + values[2] = Int64GetDatum(result_tuples[funcctx->call_cntr].from_v); + values[3] = Int64GetDatum(result_tuples[funcctx->call_cntr].node); + values[4] = Int64GetDatum(result_tuples[funcctx->call_cntr].edge); + values[5] = Float8GetDatum(result_tuples[funcctx->call_cntr].cost); + values[6] = Float8GetDatum(result_tuples[funcctx->call_cntr].agg_cost); + + tuple = heap_form_tuple(tuple_desc, values, nulls); + result = HeapTupleGetDatum(tuple); + + pfree(values); + pfree(nulls); + + SRF_RETURN_NEXT(funcctx, result); + } else { + SRF_RETURN_DONE(funcctx); + } +} + + +/* Old code starts here + * TODO(v4) remove old code + * its code that is used when there is an old version of SQL 3.5 and under + */ PGDLLEXPORT Datum _pgr_drivingdistance(PG_FUNCTION_ARGS) { FuncCallContext *funcctx; TupleDesc tuple_desc; - /**********************************************************************/ Path_rt* result_tuples = 0; size_t result_count = 0; - /**********************************************************************/ + MST_rt *result_new_tuples = 0; + size_t result_new_count = 0; if (SRF_IS_FIRSTCALL()) { MemoryContext oldcontext; @@ -115,7 +211,6 @@ _pgr_drivingdistance(PG_FUNCTION_ARGS) { funcctx = SRF_FIRSTCALL_INIT(); oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx); - /**********************************************************************/ PGR_DBG("Calling driving_many_to_dist_driver"); process( @@ -124,9 +219,10 @@ _pgr_drivingdistance(PG_FUNCTION_ARGS) { PG_GETARG_FLOAT8(2), PG_GETARG_BOOL(3), PG_GETARG_BOOL(4), - &result_tuples, &result_count); + false, + &result_tuples, &result_count, + &result_new_tuples, &result_new_count); - /**********************************************************************/ funcctx->max_calls = result_count; @@ -154,7 +250,6 @@ _pgr_drivingdistance(PG_FUNCTION_ARGS) { Datum *values; bool* nulls; - /**********************************************************************/ size_t numb = 6; values = palloc(numb * sizeof(Datum)); nulls = palloc(numb * sizeof(bool)); @@ -170,7 +265,6 @@ _pgr_drivingdistance(PG_FUNCTION_ARGS) { values[4] = Float8GetDatum(result_tuples[funcctx->call_cntr].cost); values[5] = Float8GetDatum(result_tuples[funcctx->call_cntr].agg_cost); - /**********************************************************************/ tuple = heap_form_tuple(tuple_desc, values, nulls); result = HeapTupleGetDatum(tuple); @@ -182,4 +276,3 @@ _pgr_drivingdistance(PG_FUNCTION_ARGS) { SRF_RETURN_DONE(funcctx); } } -