PGROUTING  3.2
bellman_ford_neg.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 
43 #include "drivers/bellman_ford/bellman_ford_neg_driver.h" // the link to the C++ code of the function
44 
45 PGDLLEXPORT Datum _pgr_bellmanfordneg(PG_FUNCTION_ARGS);
47 
48 static
49 void
51  char* edges_sql,
52  char* neg_edges_sql,
53  char *combinations_sql,
54  ArrayType *starts,
55  ArrayType *ends,
56  bool directed,
57  bool only_cost,
58 
59  General_path_element_t **result_tuples,
60  size_t *result_count) {
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 }
163 
164 
165 PGDLLEXPORT Datum _pgr_bellmanfordneg(PG_FUNCTION_ARGS) {
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 }
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
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
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
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
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
bellman_ford_neg_driver.h
edges_input.h
PG_FUNCTION_INFO_V1
PG_FUNCTION_INFO_V1(_pgr_bellmanfordneg)
General_path_element_t::cost
double cost
Definition: general_path_element_t.h:43
_pgr_bellmanfordneg
PGDLLEXPORT Datum _pgr_bellmanfordneg(PG_FUNCTION_ARGS)
Definition: bellman_ford_neg.c:165
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
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