PGROUTING  3.2
withPoints_ksp.c File Reference
#include <stdbool.h>
#include "c_common/postgres_connection.h"
#include "utils/array.h"
#include "c_common/time_msg.h"
#include "c_common/e_report.h"
#include "c_common/edges_input.h"
#include "c_common/points_input.h"
#include "drivers/withPoints/get_new_queries.h"
#include "drivers/yen/withPoints_ksp_driver.h"
#include "c_common/debug_macro.h"
Include dependency graph for withPoints_ksp.c:

Go to the source code of this file.

Functions

PGDLLEXPORT Datum _pgr_withpointsksp (PG_FUNCTION_ARGS)
 
 PG_FUNCTION_INFO_V1 (_pgr_withpointsksp)
 
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, General_path_element_t **result_tuples, size_t *result_count)
 

Function Documentation

◆ _pgr_withpointsksp()

PGDLLEXPORT Datum _pgr_withpointsksp ( PG_FUNCTION_ARGS  )

Definition at line 165 of file withPoints_ksp.c.

165  {
166  FuncCallContext *funcctx;
167  TupleDesc tuple_desc;
168 
169  General_path_element_t *result_tuples = 0;
170  size_t result_count = 0;
171 
172  if (SRF_IS_FIRSTCALL()) {
173  MemoryContext oldcontext;
174  funcctx = SRF_FIRSTCALL_INIT();
175  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
176 
177 
178  /*
179  CREATE OR REPLACE FUNCTION pgr_withPoint(
180  edges_sql TEXT,
181  points_sql TEXT,
182  start_pid INTEGER,
183  end_pid BIGINT,
184  k BIGINT,
185 
186  directed BOOLEAN -- DEFAULT true,
187  heap_paths BOOLEAN -- DEFAULT false,
188  driving_side CHAR -- DEFAULT 'b',
189  details BOOLEAN -- DEFAULT false
190  */
191 
192  PGR_DBG("Calling process");
193  PGR_DBG("initial driving side:%s",
194  text_to_cstring(PG_GETARG_TEXT_P(7)));
195  process(
196  text_to_cstring(PG_GETARG_TEXT_P(0)),
197  text_to_cstring(PG_GETARG_TEXT_P(1)),
198  PG_GETARG_INT64(2),
199  PG_GETARG_INT64(3),
200  PG_GETARG_INT32(4),
201  PG_GETARG_BOOL(5),
202  PG_GETARG_BOOL(6),
203  text_to_cstring(PG_GETARG_TEXT_P(7)),
204  PG_GETARG_BOOL(8),
205  &result_tuples,
206  &result_count);
207 
208 
209 #if PGSQL_VERSION > 95
210  funcctx->max_calls = result_count;
211 #else
212  funcctx->max_calls = (uint32_t)result_count;
213 #endif
214  funcctx->user_fctx = result_tuples;
215  if (get_call_result_type(fcinfo, NULL, &tuple_desc)
216  != TYPEFUNC_COMPOSITE)
217  ereport(ERROR,
218  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
219  errmsg("function returning record called in context "
220  "that cannot accept type record")));
221 
222  funcctx->tuple_desc = tuple_desc;
223  MemoryContextSwitchTo(oldcontext);
224  }
225 
226  funcctx = SRF_PERCALL_SETUP();
227  tuple_desc = funcctx->tuple_desc;
228  result_tuples = (General_path_element_t*) funcctx->user_fctx;
229 
230  if (funcctx->call_cntr < funcctx->max_calls) {
231  HeapTuple tuple;
232  Datum result;
233  Datum *values;
234  bool* nulls;
235 
236  values = palloc(7 * sizeof(Datum));
237  nulls = palloc(7 * sizeof(bool));
238 
239  size_t i;
240  for (i = 0; i < 7; ++i) {
241  nulls[i] = false;
242  }
243 
244  /*
245  OUT seq INTEGER, OUT path_id INTEGER, OUT path_seq INTEGER,
246  OUT node BIGINT, OUT edge BIGINT,
247  OUT cost FLOAT, OUT agg_cost FLOAT)
248  */
249 
250 
251  // postgres starts counting from 1
252  values[0] = Int32GetDatum(funcctx->call_cntr + 1);
253  values[1] = Int32GetDatum((int)
254  (result_tuples[funcctx->call_cntr].start_id + 1));
255  values[2] = Int32GetDatum(result_tuples[funcctx->call_cntr].seq);
256  values[3] = Int64GetDatum(result_tuples[funcctx->call_cntr].node);
257  values[4] = Int64GetDatum(result_tuples[funcctx->call_cntr].edge);
258  values[5] = Float8GetDatum(result_tuples[funcctx->call_cntr].cost);
259  values[6] = Float8GetDatum(result_tuples[funcctx->call_cntr].agg_cost);
260 
261  tuple = heap_form_tuple(tuple_desc, values, nulls);
262  result = HeapTupleGetDatum(tuple);
263  SRF_RETURN_NEXT(funcctx, result);
264  } else {
265  SRF_RETURN_DONE(funcctx);
266  }
267 }

References General_path_element_t::agg_cost, General_path_element_t::cost, General_path_element_t::edge, if(), General_path_element_t::node, PGR_DBG, process(), General_path_element_t::seq, and General_path_element_t::start_id.

◆ PG_FUNCTION_INFO_V1()

PG_FUNCTION_INFO_V1 ( _pgr_withpointsksp  )

◆ process()

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,
General_path_element_t **  result_tuples,
size_t *  result_count 
)
static

Definition at line 50 of file withPoints_ksp.c.

63  {
64  if (p_k < 0) {
65  return;
66  }
67 
68  size_t k = (size_t)p_k;
69 
70  driving_side[0] = (char) tolower(driving_side[0]);
71  PGR_DBG("driving side:%c", driving_side[0]);
72  if (!((driving_side[0] == 'r')
73  || (driving_side[0] == 'l'))) {
74  driving_side[0] = 'b';
75  }
76 
78 
79  Point_on_edge_t *points = NULL;
80  size_t total_points = 0;
81  pgr_get_points(points_sql, &points, &total_points);
82 
83  char *edges_of_points_query = NULL;
84  char *edges_no_points_query = NULL;
86  edges_sql, points_sql,
87  &edges_of_points_query,
88  &edges_no_points_query);
89 
90 
91  pgr_edge_t *edges_of_points = NULL;
92  size_t total_edges_of_points = 0;
93  pgr_get_edges(edges_of_points_query,
94  &edges_of_points,
95  &total_edges_of_points);
96 
97  pgr_edge_t *edges = NULL;
98  size_t total_edges = 0;
99  pgr_get_edges(edges_no_points_query, &edges, &total_edges);
100 
101  PGR_DBG("freeing allocated memory not used anymore");
102  pfree(edges_of_points_query);
103  pfree(edges_no_points_query);
104 
105  if ((total_edges + total_edges_of_points) == 0) {
106  PGR_DBG("No edges found");
107  (*result_count) = 0;
108  (*result_tuples) = NULL;
109  pgr_SPI_finish();
110  return;
111  }
112 
113  PGR_DBG("Starting processing");
114  clock_t start_t = clock();
115 
116  char *log_msg = NULL;
117  char *notice_msg = NULL;
118  char *err_msg = NULL;
120  edges,
121  total_edges,
122  points,
123  total_points,
124  edges_of_points,
125  total_edges_of_points,
126  start_pid,
127  end_pid,
128  k,
129 
130  directed,
131  heap_paths,
132  driving_side[0],
133  details,
134 
135  result_tuples,
136  result_count,
137 
138  &log_msg,
139  &notice_msg,
140  &err_msg);
141  time_msg(" processing withPointsKSP", start_t, clock());
142 
143  if (err_msg && (*result_tuples)) {
144  pfree(*result_tuples);
145  (*result_tuples) = NULL;
146  (*result_count) = 0;
147  }
148 
149  pgr_global_report(log_msg, notice_msg, err_msg);
150 
151  if (log_msg) pfree(log_msg);
152  if (notice_msg) pfree(notice_msg);
153  if (err_msg) pfree(err_msg);
154 
155  pfree(edges);
156  pfree(edges_of_points);
157  pfree(points);
158 
159  pgr_SPI_finish();
160 }

References do_pgr_withPointsKsp(), get_new_queries(), PGR_DBG, pgr_get_edges(), pgr_get_points(), pgr_global_report(), pgr_SPI_connect(), pgr_SPI_finish(), and time_msg().

Referenced by _pgr_withpointsksp().

pgr_edge_t
Definition: pgr_edge_t.h:37
pgr_SPI_connect
void pgr_SPI_connect(void)
Definition: postgres_connection.c:82
pgr_SPI_finish
void pgr_SPI_finish(void)
Definition: postgres_connection.c:71
process
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, General_path_element_t **result_tuples, size_t *result_count)
Definition: withPoints_ksp.c:50
pgr_get_points
void pgr_get_points(char *points_sql, Point_on_edge_t **points, size_t *total_points)
pgr_get_points
Definition: points_input.c:69
get_new_queries
void get_new_queries(char *edges_sql, char *points_sql, char **edges_of_points_query, char **edges_no_points_query)
Definition: get_new_queries.cpp:46
PGR_DBG
#define PGR_DBG(...)
Definition: debug_macro.h:34
do_pgr_withPointsKsp
int do_pgr_withPointsKsp(pgr_edge_t *edges, size_t total_edges, Point_on_edge_t *points_p, size_t total_points, pgr_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, General_path_element_t **return_tuples, size_t *return_count, char **log_msg, char **notice_msg, char **err_msg)
Definition: withPoints_ksp_driver.cpp:54
Point_on_edge_t
Definition: point_on_edge_t.h:37
pgr_get_edges
void pgr_get_edges(char *edges_sql, pgr_edge_t **edges, size_t *total_edges)
basic edge_sql
Definition: edges_input.c:711
General_path_element_t
Definition: general_path_element_t.h:37
if
if(DOXYGEN_FOUND) configure_file($
Definition: doxygen/CMakeLists.txt:13
time_msg
void time_msg(char *msg, clock_t start_t, clock_t end_t)
Definition: time_msg.c:32
pgr_global_report
void pgr_global_report(char *log, char *notice, char *err)
notice & error
Definition: e_report.c:93