PGROUTING  3.2
dagShortestPath.c File Reference
#include <stdbool.h>
#include "c_common/postgres_connection.h"
#include "utils/array.h"
#include "c_common/debug_macro.h"
#include "c_common/e_report.h"
#include "c_common/time_msg.h"
#include "c_common/edges_input.h"
#include "c_common/arrays_input.h"
#include "c_common/combinations_input.h"
#include "drivers/dagShortestPath/dagShortestPath_driver.h"
Include dependency graph for dagShortestPath.c:

Go to the source code of this file.

Functions

PGDLLEXPORT Datum _pgr_dagshortestpath (PG_FUNCTION_ARGS)
 
 PG_FUNCTION_INFO_V1 (_pgr_dagshortestpath)
 
static void process (char *edges_sql, char *combinations_sql, ArrayType *starts, ArrayType *ends, bool directed, bool only_cost, General_path_element_t **result_tuples, size_t *result_count)
 

Function Documentation

◆ _pgr_dagshortestpath()

PGDLLEXPORT Datum _pgr_dagshortestpath ( PG_FUNCTION_ARGS  )

Definition at line 150 of file dagShortestPath.c.

150  {
151  FuncCallContext *funcctx;
152  TupleDesc tuple_desc;
153 
154  General_path_element_t *result_tuples = NULL;
155  size_t result_count = 0;
156 
157  if (SRF_IS_FIRSTCALL()) {
158  MemoryContext oldcontext;
159  funcctx = SRF_FIRSTCALL_INIT();
160  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
161 
162 
163  PGR_DBG("Calling process");
164  if (PG_NARGS() == 5) {
165  /*
166  * many to many
167  */
168  process(
169  text_to_cstring(PG_GETARG_TEXT_P(0)),
170  NULL,
171  PG_GETARG_ARRAYTYPE_P(1),
172  PG_GETARG_ARRAYTYPE_P(2),
173  PG_GETARG_BOOL(3),
174  PG_GETARG_BOOL(4),
175  &result_tuples,
176  &result_count);
177 
178  } else if (PG_NARGS() == 4) {
179  /*
180  * combinations
181  */
182  process(
183  text_to_cstring(PG_GETARG_TEXT_P(0)),
184  text_to_cstring(PG_GETARG_TEXT_P(1)),
185  NULL,
186  NULL,
187  PG_GETARG_BOOL(2),
188  PG_GETARG_BOOL(3),
189  &result_tuples,
190  &result_count);
191  }
192 
193 
194 
195 
196 #if PGSQL_VERSION > 95
197  funcctx->max_calls = result_count;
198 #else
199  funcctx->max_calls = (uint32_t)result_count;
200 #endif
201  funcctx->user_fctx = result_tuples;
202  if (get_call_result_type(fcinfo, NULL, &tuple_desc)
203  != TYPEFUNC_COMPOSITE) {
204  ereport(ERROR,
205  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
206  errmsg("function returning record called in context "
207  "that cannot accept type record")));
208  }
209 
210  funcctx->tuple_desc = tuple_desc;
211  MemoryContextSwitchTo(oldcontext);
212  }
213 
214  funcctx = SRF_PERCALL_SETUP();
215  tuple_desc = funcctx->tuple_desc;
216  result_tuples = (General_path_element_t*) funcctx->user_fctx;
217 
218  if (funcctx->call_cntr < funcctx->max_calls) {
219  HeapTuple tuple;
220  Datum result;
221  Datum *values;
222  bool* nulls;
223 
224 
225  values = palloc(6 * sizeof(Datum));
226  nulls = palloc(6 * sizeof(bool));
227 
228 
229  size_t i;
230  for (i = 0; i < 6; ++i) {
231  nulls[i] = false;
232  }
233 
234  // postgres starts counting from 1
235  values[0] = Int32GetDatum(funcctx->call_cntr + 1);
236  values[1] = Int32GetDatum(result_tuples[funcctx->call_cntr].seq);
237  values[2] = Int64GetDatum(result_tuples[funcctx->call_cntr].node);
238  values[3] = Int64GetDatum(result_tuples[funcctx->call_cntr].edge);
239  values[4] = Float8GetDatum(result_tuples[funcctx->call_cntr].cost);
240  values[5] = Float8GetDatum(result_tuples[funcctx->call_cntr].agg_cost);
241 
242  tuple = heap_form_tuple(tuple_desc, values, nulls);
243  result = HeapTupleGetDatum(tuple);
244  SRF_RETURN_NEXT(funcctx, result);
245  } else {
246  PGR_DBG("Clean up code");
247  SRF_RETURN_DONE(funcctx);
248  }
249 }

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(), and General_path_element_t::seq.

◆ PG_FUNCTION_INFO_V1()

PG_FUNCTION_INFO_V1 ( _pgr_dagshortestpath  )

◆ process()

static void process ( char *  edges_sql,
char *  combinations_sql,
ArrayType *  starts,
ArrayType *  ends,
bool  directed,
bool  only_cost,
General_path_element_t **  result_tuples,
size_t *  result_count 
)
static

Definition at line 49 of file dagShortestPath.c.

57  {
58  /*
59  * https://www.postgresql.org/docs/current/static/spi-spi-connect.html
60  */
62 
63 
64 
65 
66  PGR_DBG("Initializing arrays");
67  size_t size_start_vidsArr = 0;
68  int64_t* start_vidsArr = NULL;
69 
70  size_t size_end_vidsArr = 0;
71  int64_t* end_vidsArr = NULL;
72 
73  size_t total_combinations = 0;
74  pgr_combination_t *combinations = NULL;
75 
76  if (starts && ends) {
77  start_vidsArr = (int64_t*)
78  pgr_get_bigIntArray(&size_start_vidsArr, starts);
79  end_vidsArr = (int64_t*)
80  pgr_get_bigIntArray(&size_end_vidsArr, ends);
81  } else if (combinations_sql) {
82  pgr_get_combinations(combinations_sql, &combinations, &total_combinations);
83  if (total_combinations == 0) {
84  if (combinations)
85  pfree(combinations);
87  return;
88  }
89  }
90 
91  (*result_tuples) = NULL;
92  (*result_count) = 0;
93 
94  PGR_DBG("Load data");
95  pgr_edge_t *edges = NULL;
96  size_t total_edges = 0;
97 
98  pgr_get_edges(edges_sql, &edges, &total_edges);
99  PGR_DBG("Total %ld edges in query:", total_edges);
100 
101  if (total_edges == 0) {
102  PGR_DBG("No edges found");
103  pgr_SPI_finish();
104  return;
105  }
106 
107  PGR_DBG("Starting processing");
108  clock_t start_t = clock();
109  char *log_msg = NULL;
110  char *notice_msg = NULL;
111  char *err_msg = NULL;
113  edges,
114  total_edges,
115  combinations,
116  total_combinations,
117  start_vidsArr, size_start_vidsArr,
118  end_vidsArr, size_end_vidsArr,
119 
120  directed,
121  only_cost,
122  result_tuples,
123  result_count,
124  &log_msg,
125  &notice_msg,
126  &err_msg);
127 
128  time_msg(" processing pgr_dagShortestPath", start_t, clock());
129  PGR_DBG("Returning %ld tuples", *result_count);
130 
131  if (err_msg) {
132  if (*result_tuples) pfree(*result_tuples);
133  }
134  pgr_global_report(log_msg, notice_msg, err_msg);
135 
136  if (edges) pfree(edges);
137  if (log_msg) pfree(log_msg);
138  if (notice_msg) pfree(notice_msg);
139  if (err_msg) pfree(err_msg);
140 
141 
142  if (end_vidsArr) pfree(end_vidsArr);
143  if (start_vidsArr) pfree(start_vidsArr);
144 
145  pgr_SPI_finish();
146 }

References do_pgr_dagShortestPath(), PGR_DBG, pgr_get_bigIntArray(), pgr_get_combinations(), pgr_get_edges(), pgr_global_report(), pgr_SPI_connect(), pgr_SPI_finish(), and time_msg().

Referenced by _pgr_dagshortestpath().

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
pgr_get_bigIntArray
int64_t * pgr_get_bigIntArray(size_t *arrlen, ArrayType *input)
Enforces the input array to be NOT empty.
Definition: arrays_input.c:146
PGR_DBG
#define PGR_DBG(...)
Definition: debug_macro.h:34
pgr_combination_t
Definition: pgr_combination_t.h:43
pgr_get_combinations
void pgr_get_combinations(char *combinations_sql, pgr_combination_t **combinations, size_t *total_combinations)
combinations_sql
Definition: combinations_input.c:147
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
do_pgr_dagShortestPath
void do_pgr_dagShortestPath(pgr_edge_t *data_edges, size_t total_edges, pgr_combination_t *combinations, size_t total_combinations, int64_t *start_vidsArr, size_t size_start_vidsArr, int64_t *end_vidsArr, size_t size_end_vidsArr, bool directed, bool only_cost, General_path_element_t **return_tuples, size_t *return_count, char **log_msg, char **notice_msg, char **err_msg)
Definition: dagShortestPath_driver.cpp:74
pgr_global_report
void pgr_global_report(char *log, char *notice, char *err)
notice & error
Definition: e_report.c:93
process
static void process(char *edges_sql, char *combinations_sql, ArrayType *starts, ArrayType *ends, bool directed, bool only_cost, General_path_element_t **result_tuples, size_t *result_count)
Definition: dagShortestPath.c:49