Skip to content

Commit

Permalink
Added old code for backward compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
AbhinJ committed Jul 10, 2023
1 parent ca6a3df commit b555ff1
Show file tree
Hide file tree
Showing 6 changed files with 411 additions and 29 deletions.
2 changes: 2 additions & 0 deletions docqueries/withPoints/test.conf
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@
doc-pgr_withPointsDD
withPointsVia
withPointsKSP
doc-pgr_withPointsKSP
)],
'documentation' => [qw(
doc-pgr_withPoints
doc-pgr_withPointsCost
doc-pgr_withPointsCostMatrix
doc-pgr_withPointsDD
withPointsKSP
doc-pgr_withPointsKSP
withPointsVia
)],
},
Expand Down
19 changes: 19 additions & 0 deletions include/drivers/yen/withPoints_ksp_driver.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,25 @@ extern "C" {

Path_rt**, size_t*,
char**, char**, char**);

/*TODO remove on v4*/
int do_pgr_withPointsKsp(
Edge_t *edges, size_t total_edges,
Point_on_edge_t *points, size_t total_points,
Edge_t *edges_of_points, size_t total_edges_of_points,
int64_t start_pid,
int64_t end_pid,
size_t k,
bool directed,
bool heap_paths,
char driving_side,
bool details,

Path_rt **return_tuples,
size_t *return_count,
char ** log_msg,
char ** notice_msg,
char ** err_msg);


#ifdef __cplusplus
Expand Down
1 change: 1 addition & 0 deletions sql/ksp/_withPointsKSP.sql
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ IS 'pgRouting internal function';
COMMENT ON FUNCTION _pgr_v4withPointsKSP(TEXT, TEXT, TEXT, INTEGER, CHAR, BOOLEAN, BOOLEAN, BOOLEAN)
IS 'pgRouting internal function';

/*TODO remove on v4*/
--v3.0
CREATE FUNCTION _pgr_withPointsKSP(
edges_sql TEXT,
Expand Down
10 changes: 5 additions & 5 deletions sql/ksp/withPointsKSP.sql
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ ROWS 1000;

-- COMMENTS

COMMENT ON FUNCTION pgr_v4withPointsKSP(TEXT, TEXT, BIGINT, BIGINT, INTEGER, CHAR, BOOLEAN, BOOLEAN, BOOLEAN)
COMMENT ON FUNCTION pgr_withPointsKSP(TEXT, TEXT, BIGINT, BIGINT, INTEGER, CHAR, BOOLEAN, BOOLEAN, BOOLEAN)
IS 'pgr_withPointsKSP
- PROPOSED
- Parameters:
Expand All @@ -203,7 +203,7 @@ IS 'pgr_withPointsKSP
- Documentation:
- ${PROJECT_DOC_LINK}/pgr_withPointsKSP.html';

COMMENT ON FUNCTION pgr_v4withPointsKSP(TEXT, TEXT, BIGINT, ANYARRAY, INTEGER, CHAR, BOOLEAN, BOOLEAN, BOOLEAN)
COMMENT ON FUNCTION pgr_withPointsKSP(TEXT, TEXT, BIGINT, ANYARRAY, INTEGER, CHAR, BOOLEAN, BOOLEAN, BOOLEAN)
IS 'pgr_withPointsKSP
- PROPOSED
- Parameters:
Expand All @@ -220,7 +220,7 @@ IS 'pgr_withPointsKSP
- Documentation:
- ${PROJECT_DOC_LINK}/pgr_withPointsKSP.html';

COMMENT ON FUNCTION pgr_v4withPointsKSP(TEXT, TEXT, ANYARRAY, BIGINT, INTEGER, CHAR, BOOLEAN, BOOLEAN, BOOLEAN)
COMMENT ON FUNCTION pgr_withPointsKSP(TEXT, TEXT, ANYARRAY, BIGINT, INTEGER, CHAR, BOOLEAN, BOOLEAN, BOOLEAN)
IS 'pgr_withPointsKSP
- PROPOSED
- Parameters:
Expand All @@ -237,7 +237,7 @@ IS 'pgr_withPointsKSP
- Documentation:
- ${PROJECT_DOC_LINK}/pgr_withPointsKSP.html';

COMMENT ON FUNCTION pgr_v4withPointsKSP(TEXT, TEXT, ANYARRAY, ANYARRAY, INTEGER, CHAR, BOOLEAN, BOOLEAN, BOOLEAN)
COMMENT ON FUNCTION pgr_withPointsKSP(TEXT, TEXT, ANYARRAY, ANYARRAY, INTEGER, CHAR, BOOLEAN, BOOLEAN, BOOLEAN)
IS 'pgr_withPointsKSP
- PROPOSED
- Parameters:
Expand All @@ -254,7 +254,7 @@ IS 'pgr_withPointsKSP
- Documentation:
- ${PROJECT_DOC_LINK}/pgr_withPointsKSP.html';

COMMENT ON FUNCTION pgr_v4withPointsKSP(TEXT, TEXT, TEXT, INTEGER, CHAR, BOOLEAN, BOOLEAN, BOOLEAN)
COMMENT ON FUNCTION pgr_withPointsKSP(TEXT, TEXT, TEXT, INTEGER, CHAR, BOOLEAN, BOOLEAN, BOOLEAN)
IS 'pgr_withPointsKSP
- PROPOSED
- Parameters:
Expand Down
241 changes: 225 additions & 16 deletions src/ksp/withPoints_ksp.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,12 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
PGDLLEXPORT Datum _pgr_withpointsksp(PG_FUNCTION_ARGS);
PG_FUNCTION_INFO_V1(_pgr_withpointsksp);

PGDLLEXPORT Datum _pgr_v4withpointsksp(PG_FUNCTION_ARGS);
PG_FUNCTION_INFO_V1(_pgr_v4withpointsksp);

static
void
process(
processv4(
char* edges_sql,
char* points_sql,
char* combinations_sql,
Expand Down Expand Up @@ -200,7 +202,7 @@ process(



PGDLLEXPORT Datum _pgr_withpointsksp(PG_FUNCTION_ARGS) {
PGDLLEXPORT Datum _pgr_v4withpointsksp(PG_FUNCTION_ARGS) {
FuncCallContext *funcctx;
TupleDesc tuple_desc;

Expand All @@ -213,7 +215,7 @@ PGDLLEXPORT Datum _pgr_withpointsksp(PG_FUNCTION_ARGS) {
oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);

if(PG_NARGS() == 9){
process(
processv4(
text_to_cstring(PG_GETARG_TEXT_P(0)),
text_to_cstring(PG_GETARG_TEXT_P(1)),
NULL,
Expand All @@ -227,7 +229,7 @@ PGDLLEXPORT Datum _pgr_withpointsksp(PG_FUNCTION_ARGS) {
&result_tuples,
&result_count);
} else/* (PG_NARGS() == 8) */{
process(
processv4(
text_to_cstring(PG_GETARG_TEXT_P(0)),
text_to_cstring(PG_GETARG_TEXT_P(1)),
text_to_cstring(PG_GETARG_TEXT_P(2)),
Expand Down Expand Up @@ -274,18 +276,6 @@ PGDLLEXPORT Datum _pgr_withpointsksp(PG_FUNCTION_ARGS) {
nulls[i] = false;
}

/*
OUT seq INTEGER,
OUT path_id INTEGER,
OUT path_seq INTEGER,
OUT start_vid BIGINT,
OUT end_vid BIGINT,
OUT node BIGINT,
OUT edge BIGINT,
OUT cost FLOAT,
OUT agg_cost FLOAT
*/

int64_t path_id = 1;
if (funcctx->call_cntr != 0) {
if (result_tuples[funcctx->call_cntr - 1].edge == -1) {
Expand Down Expand Up @@ -315,3 +305,222 @@ PGDLLEXPORT Datum _pgr_withpointsksp(PG_FUNCTION_ARGS) {
}
}


/*TODO remove on v4*/
static
void
process(
char* edges_sql,
char* points_sql,
int64_t start_pid,
int64_t end_pid,
int p_k,

bool directed,
bool heap_paths,
char *driving_side,
bool details,

Path_rt **result_tuples,
size_t *result_count) {
if (p_k < 0) {
return;
}

size_t k = (size_t)p_k;

driving_side[0] = (char) tolower(driving_side[0]);
PGR_DBG("driving side:%c", driving_side[0]);
if (!((driving_side[0] == 'r')
|| (driving_side[0] == 'l'))) {
driving_side[0] = 'b';
}

pgr_SPI_connect();
char* log_msg = NULL;
char* notice_msg = NULL;
char* err_msg = NULL;

Point_on_edge_t *points = NULL;
size_t total_points = 0;
pgr_get_points(points_sql, &points, &total_points, &err_msg);
throw_error(err_msg, points_sql);

char *edges_of_points_query = NULL;
char *edges_no_points_query = NULL;
get_new_queries(
edges_sql, points_sql,
&edges_of_points_query,
&edges_no_points_query);


Edge_t *edges_of_points = NULL;
size_t total_edges_of_points = 0;
pgr_get_edges(edges_of_points_query, &edges_of_points, &total_edges_of_points, true, false, &err_msg);
throw_error(err_msg, edges_of_points_query);

Edge_t *edges = NULL;
size_t total_edges = 0;
pgr_get_edges(edges_no_points_query, &edges, &total_edges, true, false, &err_msg);
throw_error(err_msg, edges_no_points_query);

PGR_DBG("freeing allocated memory not used anymore");
pfree(edges_of_points_query);
pfree(edges_no_points_query);

if ((total_edges + total_edges_of_points) == 0) {
PGR_DBG("No edges found");
(*result_count) = 0;
(*result_tuples) = NULL;
pgr_SPI_finish();
return;
}

PGR_DBG("Starting processing");
clock_t start_t = clock();

do_pgr_withPointsKsp(
edges,
total_edges,
points,
total_points,
edges_of_points,
total_edges_of_points,
start_pid,
end_pid,
k,

directed,
heap_paths,
driving_side[0],
details,

result_tuples,
result_count,

&log_msg,
&notice_msg,
&err_msg);
time_msg(" processing withPointsKSP", 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);

pfree(edges);
pfree(edges_of_points);
pfree(points);

pgr_SPI_finish();
}




PGDLLEXPORT Datum _pgr_withpointsksp(PG_FUNCTION_ARGS) {
FuncCallContext *funcctx;
TupleDesc tuple_desc;

Path_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);


/*
CREATE OR REPLACE FUNCTION pgr_withPoint(
edges_sql TEXT,
points_sql TEXT,
start_pid INTEGER,
end_pid BIGINT,
k BIGINT,
directed BOOLEAN -- DEFAULT true,
heap_paths BOOLEAN -- DEFAULT false,
driving_side CHAR -- DEFAULT 'b',
details BOOLEAN -- DEFAULT false
*/

PGR_DBG("Calling process");
PGR_DBG("initial driving side:%s",
text_to_cstring(PG_GETARG_TEXT_P(7)));
process(
text_to_cstring(PG_GETARG_TEXT_P(0)),
text_to_cstring(PG_GETARG_TEXT_P(1)),
PG_GETARG_INT64(2),
PG_GETARG_INT64(3),
PG_GETARG_INT32(4),
PG_GETARG_BOOL(5),
PG_GETARG_BOOL(6),
text_to_cstring(PG_GETARG_TEXT_P(7)),
PG_GETARG_BOOL(8),
&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 = (Path_rt*) funcctx->user_fctx;

if (funcctx->call_cntr < funcctx->max_calls) {
HeapTuple tuple;
Datum result;
Datum *values;
bool* nulls;

values = palloc(7 * sizeof(Datum));
nulls = palloc(7 * sizeof(bool));

size_t i;
for (i = 0; i < 7; ++i) {
nulls[i] = false;
}

/*
OUT seq INTEGER, OUT path_id INTEGER, OUT path_seq INTEGER,
OUT node BIGINT, OUT edge BIGINT,
OUT cost FLOAT, OUT agg_cost FLOAT)
*/


// postgres starts counting from 1
values[0] = Int32GetDatum(funcctx->call_cntr + 1);
values[1] = Int32GetDatum((int)
(result_tuples[funcctx->call_cntr].start_id + 1));
values[2] = Int32GetDatum(result_tuples[funcctx->call_cntr].seq);
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);
SRF_RETURN_NEXT(funcctx, result);
} else {
SRF_RETURN_DONE(funcctx);
}
}
Loading

0 comments on commit b555ff1

Please sign in to comment.