PGROUTING  3.2
dagShortestPath.c
Go to the documentation of this file.
1 /*PGR-GNU*****************************************************************
2 File: dagShortestPath.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 #include "c_common/edges_input.h"
39 #include "c_common/arrays_input.h"
41 #include "drivers/dagShortestPath/dagShortestPath_driver.h" // the link to the C++ code of the function
42 
43 PGDLLEXPORT Datum _pgr_dagshortestpath(PG_FUNCTION_ARGS);
45 
46 
47 static
48 void
50  char* edges_sql,
51  char* combinations_sql,
52  ArrayType *starts,
53  ArrayType *ends,
54  bool directed,
55  bool only_cost,
56  General_path_element_t **result_tuples,
57  size_t *result_count) {
58  /*
59  * https://www.postgresql.org/docs/current/static/spi-spi-connect.html
60  */
62 
63 
64 
65 
66  PGR_DBG("Initializing arrays");
67  size_t size_start_vidsArr = 0;
68  int64_t* start_vidsArr = NULL;
69 
70  size_t size_end_vidsArr = 0;
71  int64_t* end_vidsArr = NULL;
72 
73  size_t total_combinations = 0;
74  pgr_combination_t *combinations = NULL;
75 
76  if (starts && ends) {
77  start_vidsArr = (int64_t*)
78  pgr_get_bigIntArray(&size_start_vidsArr, starts);
79  end_vidsArr = (int64_t*)
80  pgr_get_bigIntArray(&size_end_vidsArr, ends);
81  } else if (combinations_sql) {
82  pgr_get_combinations(combinations_sql, &combinations, &total_combinations);
83  if (total_combinations == 0) {
84  if (combinations)
85  pfree(combinations);
87  return;
88  }
89  }
90 
91  (*result_tuples) = NULL;
92  (*result_count) = 0;
93 
94  PGR_DBG("Load data");
95  pgr_edge_t *edges = NULL;
96  size_t total_edges = 0;
97 
98  pgr_get_edges(edges_sql, &edges, &total_edges);
99  PGR_DBG("Total %ld edges in query:", total_edges);
100 
101  if (total_edges == 0) {
102  PGR_DBG("No edges found");
103  pgr_SPI_finish();
104  return;
105  }
106 
107  PGR_DBG("Starting processing");
108  clock_t start_t = clock();
109  char *log_msg = NULL;
110  char *notice_msg = NULL;
111  char *err_msg = NULL;
113  edges,
114  total_edges,
115  combinations,
116  total_combinations,
117  start_vidsArr, size_start_vidsArr,
118  end_vidsArr, size_end_vidsArr,
119 
120  directed,
121  only_cost,
122  result_tuples,
123  result_count,
124  &log_msg,
125  &notice_msg,
126  &err_msg);
127 
128  time_msg(" processing pgr_dagShortestPath", start_t, clock());
129  PGR_DBG("Returning %ld tuples", *result_count);
130 
131  if (err_msg) {
132  if (*result_tuples) pfree(*result_tuples);
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 
142  if (end_vidsArr) pfree(end_vidsArr);
143  if (start_vidsArr) pfree(start_vidsArr);
144 
145  pgr_SPI_finish();
146 }
147 /* */
148 /******************************************************************************/
149 
150 PGDLLEXPORT Datum _pgr_dagshortestpath(PG_FUNCTION_ARGS) {
151  FuncCallContext *funcctx;
152  TupleDesc tuple_desc;
153 
154  General_path_element_t *result_tuples = NULL;
155  size_t result_count = 0;
156 
157  if (SRF_IS_FIRSTCALL()) {
158  MemoryContext oldcontext;
159  funcctx = SRF_FIRSTCALL_INIT();
160  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
161 
162 
163  PGR_DBG("Calling process");
164  if (PG_NARGS() == 5) {
165  /*
166  * many to many
167  */
168  process(
169  text_to_cstring(PG_GETARG_TEXT_P(0)),
170  NULL,
171  PG_GETARG_ARRAYTYPE_P(1),
172  PG_GETARG_ARRAYTYPE_P(2),
173  PG_GETARG_BOOL(3),
174  PG_GETARG_BOOL(4),
175  &result_tuples,
176  &result_count);
177 
178  } else if (PG_NARGS() == 4) {
179  /*
180  * combinations
181  */
182  process(
183  text_to_cstring(PG_GETARG_TEXT_P(0)),
184  text_to_cstring(PG_GETARG_TEXT_P(1)),
185  NULL,
186  NULL,
187  PG_GETARG_BOOL(2),
188  PG_GETARG_BOOL(3),
189  &result_tuples,
190  &result_count);
191  }
192 
193 
194 
195 
196 #if PGSQL_VERSION > 95
197  funcctx->max_calls = result_count;
198 #else
199  funcctx->max_calls = (uint32_t)result_count;
200 #endif
201  funcctx->user_fctx = result_tuples;
202  if (get_call_result_type(fcinfo, NULL, &tuple_desc)
203  != TYPEFUNC_COMPOSITE) {
204  ereport(ERROR,
205  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
206  errmsg("function returning record called in context "
207  "that cannot accept type record")));
208  }
209 
210  funcctx->tuple_desc = tuple_desc;
211  MemoryContextSwitchTo(oldcontext);
212  }
213 
214  funcctx = SRF_PERCALL_SETUP();
215  tuple_desc = funcctx->tuple_desc;
216  result_tuples = (General_path_element_t*) funcctx->user_fctx;
217 
218  if (funcctx->call_cntr < funcctx->max_calls) {
219  HeapTuple tuple;
220  Datum result;
221  Datum *values;
222  bool* nulls;
223 
224 
225  values = palloc(6 * sizeof(Datum));
226  nulls = palloc(6 * sizeof(bool));
227 
228 
229  size_t i;
230  for (i = 0; i < 6; ++i) {
231  nulls[i] = false;
232  }
233 
234  // postgres starts counting from 1
235  values[0] = Int32GetDatum(funcctx->call_cntr + 1);
236  values[1] = Int32GetDatum(result_tuples[funcctx->call_cntr].seq);
237  values[2] = Int64GetDatum(result_tuples[funcctx->call_cntr].node);
238  values[3] = Int64GetDatum(result_tuples[funcctx->call_cntr].edge);
239  values[4] = Float8GetDatum(result_tuples[funcctx->call_cntr].cost);
240  values[5] = Float8GetDatum(result_tuples[funcctx->call_cntr].agg_cost);
241 
242  tuple = heap_form_tuple(tuple_desc, values, nulls);
243  result = HeapTupleGetDatum(tuple);
244  SRF_RETURN_NEXT(funcctx, result);
245  } else {
246  PGR_DBG("Clean up code");
247  SRF_RETURN_DONE(funcctx);
248  }
249 }
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
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
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
dagShortestPath_driver.h
edges_input.h
_pgr_dagshortestpath
PGDLLEXPORT Datum _pgr_dagshortestpath(PG_FUNCTION_ARGS)
Definition: dagShortestPath.c:150
do_pgr_dagShortestPath
void do_pgr_dagShortestPath(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: dagShortestPath_driver.cpp:74
General_path_element_t::cost
double cost
Definition: general_path_element_t.h:43
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
PG_FUNCTION_INFO_V1
PG_FUNCTION_INFO_V1(_pgr_dagshortestpath)
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: dagShortestPath.c:49