PGROUTING  3.2
bellman_ford.c
Go to the documentation of this file.
1 /*PGR-GNU*****************************************************************
2 File: bellman_ford.c
3 
4 Generated with Template by:
5 Copyright (c) 2015 pgRouting developers
7 
8 Function's developer:
9 Copyright (c) 2018 Sourabh Garg
11 
12 
13 ------
14 
15 This program is free software; you can redistribute it and/or modify
16 it under the terms of the GNU General Public License as published by
17 the Free Software Foundation; either version 2 of the License, or
18 (at your option) any later version.
19 
20 This program is distributed in the hope that it will be useful,
21 but WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 GNU General Public License for more details.
24 
25 You should have received a copy of the GNU General Public License
26 along with this program; if not, write to the Free Software
27 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
28 
29  ********************************************************************PGR-GNU*/
30 
31 #include <stdbool.h>
33 #include "utils/array.h"
34 
35 #include "c_common/debug_macro.h"
36 #include "c_common/e_report.h"
37 #include "c_common/time_msg.h"
38 
39 #include "c_common/edges_input.h"
40 #include "c_common/arrays_input.h"
42 
44 
45 PGDLLEXPORT Datum _pgr_bellmanford(PG_FUNCTION_ARGS);
47 
48 static
49 void
51  char* edges_sql,
52  char *combinations_sql,
53  ArrayType *starts,
54  ArrayType *ends,
55  bool directed,
56  bool only_cost,
57 
58  General_path_element_t **result_tuples,
59  size_t *result_count) {
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 }
145 
146 
147 PGDLLEXPORT Datum _pgr_bellmanford(PG_FUNCTION_ARGS) {
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 }
combinations_input.h
General_path_element_t::edge
int64_t edge
Definition: general_path_element_t.h:42
time_msg.h
postgres_connection.h
pgr_edge_t
Definition: pgr_edge_t.h:37
pgr_SPI_connect
void pgr_SPI_connect(void)
Definition: postgres_connection.c:82
General_path_element_t::seq
int seq
Definition: general_path_element_t.h:38
pgr_SPI_finish
void pgr_SPI_finish(void)
Definition: postgres_connection.c:71
_pgr_bellmanford
PGDLLEXPORT Datum _pgr_bellmanford(PG_FUNCTION_ARGS)
Definition: bellman_ford.c:147
e_report.h
arrays_input.h
General_path_element_t::start_id
int64_t start_id
Definition: general_path_element_t.h:39
debug_macro.h
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
General_path_element_t::node
int64_t node
Definition: general_path_element_t.h:41
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
PG_FUNCTION_INFO_V1
PG_FUNCTION_INFO_V1(_pgr_bellmanford)
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
General_path_element_t::end_id
int64_t end_id
Definition: general_path_element_t.h:40
edges_input.h
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
General_path_element_t::cost
double cost
Definition: general_path_element_t.h:43
bellman_ford_driver.h
pgr_global_report
void pgr_global_report(char *log, char *notice, char *err)
notice & error
Definition: e_report.c:93
General_path_element_t::agg_cost
double agg_cost
Definition: general_path_element_t.h:44