PGROUTING  3.2
bellman_ford.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/bellman_ford/bellman_ford_driver.h"
Include dependency graph for bellman_ford.c:

Go to the source code of this file.

Functions

PGDLLEXPORT Datum _pgr_bellmanford (PG_FUNCTION_ARGS)
 
 PG_FUNCTION_INFO_V1 (_pgr_bellmanford)
 
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_bellmanford()

PGDLLEXPORT Datum _pgr_bellmanford ( PG_FUNCTION_ARGS  )

Definition at line 147 of file bellman_ford.c.

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

References General_path_element_t::agg_cost, General_path_element_t::cost, General_path_element_t::edge, General_path_element_t::end_id, 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_bellmanford  )

◆ 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 50 of file bellman_ford.c.

59  {
61 
62  PGR_DBG("Initializing arrays");
63 
64  size_t size_start_vidsArr = 0;
65  int64_t* start_vidsArr = NULL;
66 
67  size_t size_end_vidsArr = 0;
68  int64_t* end_vidsArr = NULL;
69 
70  size_t total_combinations = 0;
71  pgr_combination_t *combinations = NULL;
72 
73  if (starts && ends) {
74  start_vidsArr = (int64_t*)
75  pgr_get_bigIntArray(&size_start_vidsArr, starts);
76  end_vidsArr = (int64_t*)
77  pgr_get_bigIntArray(&size_end_vidsArr, ends);
78  } else if (combinations_sql) {
79  pgr_get_combinations(combinations_sql, &combinations, &total_combinations);
80  if (total_combinations == 0) {
81  if (combinations)
82  pfree(combinations);
84  return;
85  }
86  }
87 
88  (*result_tuples) = NULL;
89  (*result_count) = 0;
90 
91  PGR_DBG("Load data");
92  pgr_edge_t *edges = NULL;
93  size_t total_edges = 0;
94 
95  pgr_get_edges(edges_sql, &edges, &total_edges);
96  PGR_DBG("Total %ld edges in query:", total_edges);
97 
98  if (total_edges == 0) {
99  if (end_vidsArr) pfree(end_vidsArr);
100  if (start_vidsArr) pfree(start_vidsArr);
101  pgr_SPI_finish();
102  return;
103  }
104 
105  PGR_DBG("Starting processing");
106  clock_t start_t = clock();
107  char *log_msg = NULL;
108  char *notice_msg = NULL;
109  char *err_msg = NULL;
111  edges,
112  total_edges,
113  combinations,
114  total_combinations,
115  start_vidsArr, size_start_vidsArr,
116  end_vidsArr, size_end_vidsArr,
117  directed,
118  only_cost,
119 
120  result_tuples,
121  result_count,
122 
123  &log_msg,
124  &notice_msg,
125  &err_msg);
126 
127  time_msg(" processing pgr_bellman_ford", start_t, clock());
128  PGR_DBG("Returning %ld tuples", *result_count);
129 
130  if (err_msg) {
131  if (*result_tuples) pfree(*result_tuples);
132  }
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  if (end_vidsArr) pfree(end_vidsArr);
142  if (start_vidsArr) pfree(start_vidsArr);
143  pgr_SPI_finish();
144 }

References do_pgr_bellman_ford(), 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_bellmanford().

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
do_pgr_bellman_ford
void do_pgr_bellman_ford(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: bellman_ford_driver.cpp:83
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
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: bellman_ford.c:50
pgr_global_report
void pgr_global_report(char *log, char *notice, char *err)
notice & error
Definition: e_report.c:93