PGROUTING  3.2
dijkstra.c
Go to the documentation of this file.
1 /*PGR-GNU*****************************************************************
2 
3 File: many_to_many_dijkstra.c
4 
5 Generated with Template by:
6 Copyright (c) 2015 pgRouting developers
8 
9 Function's developer:
10 Copyright (c) 2015 Celia Virginia Vergara Castillo
12 
13 Copyright (c) 2020 The combinations_sql signature is added by Mahmoud SAKR
14 and Esteban ZIMANYI
16 
17 ------
18 
19 This program is free software; you can redistribute it and/or modify
20 it under the terms of the GNU General Public License as published by
21 the Free Software Foundation; either version 2 of the License, or
22 (at your option) any later version.
23 
24 This program is distributed in the hope that it will be useful,
25 but WITHOUT ANY WARRANTY; without even the implied warranty of
26 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 GNU General Public License for more details.
28 
29 You should have received a copy of the GNU General Public License
30 along with this program; if not, write to the Free Software
31 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
32 
33  ********************************************************************PGR-GNU*/
34 
35 #include <stdbool.h>
36 
38 #include "utils/array.h"
39 
40 
41 #include "c_common/debug_macro.h"
42 #include "c_common/e_report.h"
43 #include "c_common/time_msg.h"
44 #include "c_common/edges_input.h"
45 #include "c_common/arrays_input.h"
48 
50 
51 PGDLLEXPORT Datum _pgr_dijkstra(PG_FUNCTION_ARGS);
53 
54 static
55 void
57  char* edges_sql,
58  ArrayType *starts,
59  ArrayType *ends,
60  bool directed,
61  bool only_cost,
62  bool normal,
63  int64_t n_goals,
64  bool global,
65  General_path_element_t **result_tuples,
66  size_t *result_count) {
68 
69  int64_t* start_vidsArr = NULL;
70  size_t size_start_vidsArr = 0;
71 
72  int64_t* end_vidsArr = NULL;
73  size_t size_end_vidsArr = 0;
74 
75  pgr_edge_t *edges = NULL;
76  size_t total_edges = 0;
77  if (normal) {
78  pgr_get_edges(edges_sql, &edges, &total_edges);
79  start_vidsArr = (int64_t*)
80  pgr_get_bigIntArray(&size_start_vidsArr, starts);
81  end_vidsArr = (int64_t*)
82  pgr_get_bigIntArray(&size_end_vidsArr, ends);
83  } else {
84  pgr_get_edges_reversed(edges_sql, &edges, &total_edges);
85  end_vidsArr = (int64_t*)
86  pgr_get_bigIntArray(&size_end_vidsArr, starts);
87  start_vidsArr = (int64_t*)
88  pgr_get_bigIntArray(&size_start_vidsArr, ends);
89  }
90 
91  if (total_edges == 0) {
92  if (end_vidsArr) pfree(end_vidsArr);
93  if (start_vidsArr) pfree(start_vidsArr);
95  return;
96  }
97 
98  clock_t start_t = clock();
99  char* log_msg = NULL;
100  char* notice_msg = NULL;
101  char* err_msg = NULL;
103  edges, total_edges,
104  start_vidsArr, size_start_vidsArr,
105  end_vidsArr, size_end_vidsArr,
106 
107  directed,
108  only_cost,
109  normal,
110  n_goals,
111  global,
112 
113  result_tuples,
114  result_count,
115 
116  &log_msg,
117  &notice_msg,
118  &err_msg);
119 
120  if (only_cost) {
121  if (n_goals > 0) {
122  time_msg("processing pgr_dijkstraNearCost", start_t, clock());
123  } else {
124  time_msg("processing pgr_dijkstraCost", start_t, clock());
125  }
126  } else {
127  if (n_goals > 0) {
128  time_msg("processing pgr_dijkstraNear", start_t, clock());
129  } else {
130  time_msg("processing pgr_dijkstra", start_t, clock());
131  }
132  }
133 
134 
135  if (err_msg && (*result_tuples)) {
136  pfree(*result_tuples);
137  (*result_tuples) = NULL;
138  (*result_count) = 0;
139  }
140 
141  pgr_global_report(log_msg, notice_msg, err_msg);
142 
143  if (log_msg) pfree(log_msg);
144  if (notice_msg) pfree(notice_msg);
145  if (err_msg) pfree(err_msg);
146  if (edges) pfree(edges);
147  if (start_vidsArr) pfree(start_vidsArr);
148  if (end_vidsArr) pfree(end_vidsArr);
149  pgr_SPI_finish();
150 }
151 
152 
153 
154 static
155 void
157  char* edges_sql,
158  char* combinations_sql,
159  bool directed,
160  bool only_cost,
161  int64_t n_goals,
162  bool global,
163  General_path_element_t **result_tuples,
164  size_t *result_count) {
165  pgr_SPI_connect();
166 
167  pgr_edge_t *edges = NULL;
168  size_t total_edges = 0;
169 
170  pgr_combination_t *combinations = NULL;
171  size_t total_combinations = 0;
172 
173  pgr_get_edges(edges_sql, &edges, &total_edges);
174 
175  if (total_edges == 0) {
176  pgr_SPI_finish();
177  return;
178  }
179  pgr_get_combinations(combinations_sql, &combinations, &total_combinations);
180  if (total_combinations == 0) {
181  if (edges) pfree(edges);
182  pgr_SPI_finish();
183  return;
184  }
185 
186  clock_t start_t = clock();
187  char* log_msg = NULL;
188  char* notice_msg = NULL;
189  char* err_msg = NULL;
191  edges, total_edges,
192  combinations, total_combinations,
193  directed,
194  only_cost,
195  true,
196  n_goals,
197  global,
198 
199  result_tuples,
200  result_count,
201 
202  &log_msg,
203  &notice_msg,
204  &err_msg);
205 
206  if (only_cost) {
207  if (n_goals > 0) {
208  time_msg("Processing pgr_dijkstraNearCost", start_t, clock());
209  } else {
210  time_msg("Processing pgr_dijkstraCost", start_t, clock());
211  }
212  } else {
213  if (n_goals > 0) {
214  time_msg("Processing pgr_dijkstraNear", start_t, clock());
215  } else {
216  time_msg("Processing pgr_dijkstra", start_t, clock());
217  }
218  }
219 
220  if (err_msg && (*result_tuples)) {
221  pfree(*result_tuples);
222  (*result_tuples) = NULL;
223  (*result_count) = 0;
224  }
225 
226  pgr_global_report(log_msg, notice_msg, err_msg);
227 
228  if (log_msg) pfree(log_msg);
229  if (notice_msg) pfree(notice_msg);
230  if (err_msg) pfree(err_msg);
231  if (edges) pfree(edges);
232  if (combinations) pfree(combinations);
233  pgr_SPI_finish();
234 }
235 
236 
237 
238 
239 PGDLLEXPORT Datum
240 _pgr_dijkstra(PG_FUNCTION_ARGS) {
241  FuncCallContext *funcctx;
242  TupleDesc tuple_desc;
243 
244  /**********************************************************************/
245  General_path_element_t *result_tuples = NULL;
246  size_t result_count = 0;
247  /**********************************************************************/
248 
249  if (SRF_IS_FIRSTCALL()) {
250  MemoryContext oldcontext;
251  funcctx = SRF_FIRSTCALL_INIT();
252  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
253  if (PG_NARGS() == 7) {
254  /**********************************************************************/
255  // pgr_dijkstra(
256  // sql TEXT,
257  // start_vids ANYARRAY,
258  // end_vids ANYARRAY,
259  // directed BOOLEAN default true,
260  // only_cost BOOLEAN default false
261  // normal BOOLEAN default true
262 
263  process(
264  text_to_cstring(PG_GETARG_TEXT_P(0)),
265  PG_GETARG_ARRAYTYPE_P(1),
266  PG_GETARG_ARRAYTYPE_P(2),
267  PG_GETARG_BOOL(3),
268  PG_GETARG_BOOL(4),
269  PG_GETARG_BOOL(5),
270  PG_GETARG_INT64(6),
271  true,
272  &result_tuples,
273  &result_count);
274 
275  /**********************************************************************/
276  } else if (PG_NARGS() == 5) {
277  /**********************************************************************/
278  // pgr_dijkstra(
279  // edge_sql TEXT,
280  // combinations_sql TEXT,
281  // directed BOOLEAN default true,
282  // only_cost BOOLEAN default false
283 
285  text_to_cstring(PG_GETARG_TEXT_P(0)),
286  text_to_cstring(PG_GETARG_TEXT_P(1)),
287  PG_GETARG_BOOL(2),
288  PG_GETARG_BOOL(3),
289  0, true,
290  &result_tuples,
291  &result_count);
292 
293  } else if (PG_NARGS() == 8) {
294  process(
295  text_to_cstring(PG_GETARG_TEXT_P(0)),
296  PG_GETARG_ARRAYTYPE_P(1),
297  PG_GETARG_ARRAYTYPE_P(2),
298  PG_GETARG_BOOL(3),
299  PG_GETARG_BOOL(4),
300  PG_GETARG_BOOL(5),
301  PG_GETARG_INT64(6),
302  PG_GETARG_BOOL(7),
303  &result_tuples,
304  &result_count);
305 
306  /**********************************************************************/
307  } else /* (PG_NARGS() == 6) */ {
308  /**********************************************************************/
309  // pgr_dijkstra(
310  // edge_sql TEXT,
311  // combinations_sql TEXT,
312  // directed BOOLEAN default true,
313  // only_cost BOOLEAN default false
314 
316  text_to_cstring(PG_GETARG_TEXT_P(0)),
317  text_to_cstring(PG_GETARG_TEXT_P(1)),
318  PG_GETARG_BOOL(2),
319  PG_GETARG_BOOL(3),
320  PG_GETARG_INT64(4),
321  PG_GETARG_BOOL(5),
322  &result_tuples,
323  &result_count);
324 
325  /**********************************************************************/
326  }
327 #if PGSQL_VERSION > 95
328  funcctx->max_calls = result_count;
329 #else
330  funcctx->max_calls = (uint32_t)result_count;
331 #endif
332 
333  funcctx->user_fctx = result_tuples;
334  if (get_call_result_type(fcinfo, NULL, &tuple_desc)
335  != TYPEFUNC_COMPOSITE) {
336  ereport(ERROR,
337  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
338  errmsg("function returning record called in context "
339  "that cannot accept type record")));
340  }
341 
342  funcctx->tuple_desc = tuple_desc;
343  MemoryContextSwitchTo(oldcontext);
344  }
345 
346  funcctx = SRF_PERCALL_SETUP();
347  tuple_desc = funcctx->tuple_desc;
348  result_tuples = (General_path_element_t*) funcctx->user_fctx;
349 
350  if (funcctx->call_cntr < funcctx->max_calls) {
351  HeapTuple tuple;
352  Datum result;
353  Datum *values;
354  bool* nulls;
355  size_t call_cntr = funcctx->call_cntr;
356 
357  /**********************************************************************/
358  // OUT seq INTEGER,
359  // OUT path_seq INTEGER,
360  // OUT start_vid BIGINT,
361  // OUT end_vid BIGINT,
362  // OUT node BIGINT,
363  // OUT edge BIGINT,
364  // OUT cost FLOAT,
365  // OUT agg_cost FLOAT)
366 
367  size_t numb = 8;
368  values = palloc(numb * sizeof(Datum));
369  nulls = palloc(numb * sizeof(bool));
370 
371  size_t i;
372  for (i = 0; i < numb; ++i) {
373  nulls[i] = false;
374  }
375 
376  values[0] = Int32GetDatum(call_cntr + 1);
377  values[1] = Int32GetDatum(result_tuples[call_cntr].seq);
378  values[2] = Int64GetDatum(result_tuples[call_cntr].start_id);
379  values[3] = Int64GetDatum(result_tuples[call_cntr].end_id);
380  values[4] = Int64GetDatum(result_tuples[call_cntr].node);
381  values[5] = Int64GetDatum(result_tuples[call_cntr].edge);
382  values[6] = Float8GetDatum(result_tuples[call_cntr].cost);
383  values[7] = Float8GetDatum(result_tuples[call_cntr].agg_cost);
384  /**********************************************************************/
385 
386  tuple = heap_form_tuple(tuple_desc, values, nulls);
387  result = HeapTupleGetDatum(tuple);
388  SRF_RETURN_NEXT(funcctx, result);
389  } else {
390  SRF_RETURN_DONE(funcctx);
391  }
392 }
393 
combinations_input.h
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
process_combinations
static void process_combinations(char *edges_sql, char *combinations_sql, bool directed, bool only_cost, int64_t n_goals, bool global, General_path_element_t **result_tuples, size_t *result_count)
Definition: dijkstra.c:156
pgr_SPI_finish
void pgr_SPI_finish(void)
Definition: postgres_connection.c:71
_pgr_dijkstra
PGDLLEXPORT Datum _pgr_dijkstra(PG_FUNCTION_ARGS)
Definition: dijkstra.c:240
e_report.h
process
static void process(char *edges_sql, ArrayType *starts, ArrayType *ends, bool directed, bool only_cost, bool normal, int64_t n_goals, bool global, General_path_element_t **result_tuples, size_t *result_count)
Definition: dijkstra.c:56
arrays_input.h
pgr_get_edges_reversed
void pgr_get_edges_reversed(char *edges_sql, pgr_edge_t **edges, size_t *total_edges)
Definition: edges_input.c:722
edge
Definition: trsp.h:41
debug_macro.h
do_pgr_many_to_many_dijkstra
void do_pgr_many_to_many_dijkstra(pgr_edge_t *data_edges, size_t total_edges, int64_t *start_vidsArr, size_t size_start_vidsArr, int64_t *end_vidsArr, size_t size_end_vidsArr, bool directed, bool only_cost, bool normal, int64_t n_goals, bool global, General_path_element_t **return_tuples, size_t *return_count, char **log_msg, char **notice_msg, char **err_msg)
Definition: dijkstra_driver.cpp:158
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_combination_t
Definition: pgr_combination_t.h:43
do_pgr_combinations_dijkstra
void do_pgr_combinations_dijkstra(pgr_edge_t *data_edges, size_t total_edges, pgr_combination_t *combinations, size_t total_combinations, bool directed, bool only_cost, bool normal, int64_t n_goals, bool global, General_path_element_t **return_tuples, size_t *return_count, char **log_msg, char **notice_msg, char **err_msg)
Definition: dijkstra_driver.cpp:262
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
PG_MODULE_MAGIC
PG_MODULE_MAGIC
Definition: dijkstra.c:49
time_msg
void time_msg(char *msg, clock_t start_t, clock_t end_t)
Definition: time_msg.c:32
edges_input.h
dijkstra_driver.h
PG_FUNCTION_INFO_V1
PG_FUNCTION_INFO_V1(_pgr_dijkstra)
pgr_global_report
void pgr_global_report(char *log, char *notice, char *err)
notice & error
Definition: e_report.c:93