PGROUTING  3.2
bellman_ford_neg.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_neg_driver.h"
Include dependency graph for bellman_ford_neg.c:

Go to the source code of this file.

Functions

PGDLLEXPORT Datum _pgr_bellmanfordneg (PG_FUNCTION_ARGS)
 
 PG_FUNCTION_INFO_V1 (_pgr_bellmanfordneg)
 
static void process (char *edges_sql, char *neg_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_bellmanfordneg()

PGDLLEXPORT Datum _pgr_bellmanfordneg ( PG_FUNCTION_ARGS  )

Definition at line 165 of file bellman_ford_neg.c.

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

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_bellmanfordneg  )

◆ process()

static void process ( char *  edges_sql,
char *  neg_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_neg.c.

60  {
62 
63  PGR_DBG("Initializing arrays");
64 
65  size_t size_start_vidsArr = 0;
66  int64_t* start_vidsArr = NULL;
67 
68  size_t size_end_vidsArr = 0;
69  int64_t* end_vidsArr = NULL;
70 
71  size_t total_combinations = 0;
72  pgr_combination_t *combinations = NULL;
73 
74  if (starts && ends) {
75  start_vidsArr = (int64_t*)
76  pgr_get_bigIntArray(&size_start_vidsArr, starts);
77  end_vidsArr = (int64_t*)
78  pgr_get_bigIntArray(&size_end_vidsArr, ends);
79  } else if (combinations_sql) {
80  pgr_get_combinations(combinations_sql, &combinations, &total_combinations);
81  if (total_combinations == 0) {
82  if (combinations)
83  pfree(combinations);
85  return;
86  }
87  }
88 
89  (*result_tuples) = NULL;
90  (*result_count) = 0;
91 
92  PGR_DBG("Load data");
93  pgr_edge_t *positive_edges = NULL;
94  size_t total_positive_edges = 0;
95 
96  pgr_get_edges(edges_sql, &positive_edges, &total_positive_edges);
97  PGR_DBG(
98  "Total positive weighted edges in query: %ld",
99  total_positive_edges);
100 
101  pgr_edge_t *negative_edges = NULL;
102  size_t total_negative_edges = 0;
103 
104  pgr_get_edges(neg_edges_sql, &negative_edges, &total_negative_edges);
105  PGR_DBG(
106  "Total negative weighted edges in query: %ld",
107  total_negative_edges);
108 
109  size_t total_edges = total_positive_edges + total_negative_edges;
110 
111  if (total_edges == 0) {
112  if (end_vidsArr) pfree(end_vidsArr);
113  if (start_vidsArr) pfree(start_vidsArr);
114  pgr_SPI_finish();
115  return;
116  }
117 
118  PGR_DBG("Starting processing");
119  clock_t start_t = clock();
120  char *log_msg = NULL;
121  char *notice_msg = NULL;
122  char *err_msg = NULL;
124  positive_edges,
125  total_positive_edges,
126  negative_edges,
127  total_negative_edges,
128  combinations,
129  total_combinations,
130  start_vidsArr,
131  size_start_vidsArr,
132  end_vidsArr,
133  size_end_vidsArr,
134  directed,
135  only_cost,
136 
137  result_tuples,
138  result_count,
139 
140  &log_msg,
141  &notice_msg,
142  &err_msg);
143 
144  time_msg(" processing pgr_bellman_ford", start_t, clock());
145  PGR_DBG("Returning %ld tuples", *result_count);
146 
147  if (err_msg) {
148  if (*result_tuples) pfree(*result_tuples);
149  }
150 
151  pgr_global_report(log_msg, notice_msg, err_msg);
152 
153  if (positive_edges) pfree(positive_edges);
154  if (negative_edges) pfree(negative_edges);
155  if (log_msg) pfree(log_msg);
156  if (notice_msg) pfree(notice_msg);
157  if (err_msg) pfree(err_msg);
158 
159  if (end_vidsArr) pfree(end_vidsArr);
160  if (start_vidsArr) pfree(start_vidsArr);
161  pgr_SPI_finish();
162 }

References do_pgr_bellman_ford_neg(), 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_bellmanfordneg().

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
do_pgr_bellman_ford_neg
void do_pgr_bellman_ford_neg(pgr_edge_t *positive_edges, size_t total_positive_edges, pgr_edge_t *negative_edges, size_t total_negative_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_neg_driver.cpp:80
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
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 *neg_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_neg.c:50