PGROUTING  2.6-dev
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
7 Mail: project@pgrouting.org
8 
9 Function's developer:
10 Copyright (c) 2015 Celia Virginia Vergara Castillo
11 Mail: vicky_vergara@hotmail.com
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 
32 #include "utils/array.h"
33 
34 #include "c_common/debug_macro.h"
35 #include "c_common/e_report.h"
36 #include "c_common/time_msg.h"
37 #include "c_common/edges_input.h"
38 #include "c_common/arrays_input.h"
40 
42 
43 PGDLLEXPORT Datum many_to_many_dijkstra(PG_FUNCTION_ARGS);
45 
46 static
47 void
49  char* edges_sql,
50  ArrayType *starts,
51  ArrayType *ends,
52  bool directed,
53  bool only_cost,
54  bool normal,
55  General_path_element_t **result_tuples,
56  size_t *result_count) {
58 
59  int64_t* start_vidsArr = NULL;
60  size_t size_start_vidsArr = 0;
61 
62  int64_t* end_vidsArr = NULL;
63  size_t size_end_vidsArr = 0;
64 
65  pgr_edge_t *edges = NULL;
66  size_t total_edges = 0;
67  if (normal) {
68  pgr_get_edges(edges_sql, &edges, &total_edges);
69  start_vidsArr = (int64_t*)
70  pgr_get_bigIntArray(&size_start_vidsArr, starts);
71  end_vidsArr = (int64_t*)
72  pgr_get_bigIntArray(&size_end_vidsArr, ends);
73  } else {
74  pgr_get_edges_reversed(edges_sql, &edges, &total_edges);
75  end_vidsArr = (int64_t*)
76  pgr_get_bigIntArray(&size_end_vidsArr, starts);
77  start_vidsArr = (int64_t*)
78  pgr_get_bigIntArray(&size_start_vidsArr, ends);
79  }
80 
81  if (total_edges == 0) {
82  if (end_vidsArr) pfree(end_vidsArr);
83  if (start_vidsArr) pfree(start_vidsArr);
85  return;
86  }
87 
88  PGR_DBG("Starting timer");
89  clock_t start_t = clock();
90  char* log_msg = NULL;
91  char* notice_msg = NULL;
92  char* err_msg = NULL;
94  edges, total_edges,
95  start_vidsArr, size_start_vidsArr,
96  end_vidsArr, size_end_vidsArr,
97 
98  directed,
99  only_cost,
100  normal,
101 
102  result_tuples,
103  result_count,
104 
105  &log_msg,
106  &notice_msg,
107  &err_msg);
108 
109  if (only_cost) {
110  time_msg("processing pgr_dijkstraCost", start_t, clock());
111  } else {
112  time_msg("processing pgr_dijkstra", start_t, clock());
113  }
114 
115 
116  if (err_msg && (*result_tuples)) {
117  pfree(*result_tuples);
118  (*result_tuples) = NULL;
119  (*result_count) = 0;
120  }
121 
122  pgr_global_report(log_msg, notice_msg, err_msg);
123 
124  if (log_msg) pfree(log_msg);
125  if (notice_msg) pfree(notice_msg);
126  if (err_msg) pfree(err_msg);
127  if (edges) pfree(edges);
128  if (start_vidsArr) pfree(start_vidsArr);
129  if (end_vidsArr) pfree(end_vidsArr);
130  pgr_SPI_finish();
131 }
132 
133 PGDLLEXPORT Datum
134 many_to_many_dijkstra(PG_FUNCTION_ARGS) {
135  FuncCallContext *funcctx;
136  TupleDesc tuple_desc;
137 
138  /**********************************************************************/
139  General_path_element_t *result_tuples = NULL;
140  size_t result_count = 0;
141  /**********************************************************************/
142 
143  if (SRF_IS_FIRSTCALL()) {
144  MemoryContext oldcontext;
145  funcctx = SRF_FIRSTCALL_INIT();
146  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
147 
148 
149  /**********************************************************************/
150  // pgr_dijkstra(
151  // sql TEXT,
152  // start_vids ANYARRAY,
153  // end_vids ANYARRAY,
154  // directed BOOLEAN default true,
155  // only_cost BOOLEAN default false
156  // normal BOOLEAN default true
157 
158  process(
159  text_to_cstring(PG_GETARG_TEXT_P(0)),
160  PG_GETARG_ARRAYTYPE_P(1),
161  PG_GETARG_ARRAYTYPE_P(2),
162  PG_GETARG_BOOL(3),
163  PG_GETARG_BOOL(4),
164  PG_GETARG_BOOL(5),
165  &result_tuples,
166  &result_count);
167 
168  /**********************************************************************/
169 
170 #if PGSQL_VERSION > 95
171  funcctx->max_calls = result_count;
172 #else
173  funcctx->max_calls = (uint32_t)result_count;
174 #endif
175 
176  funcctx->user_fctx = result_tuples;
177  if (get_call_result_type(fcinfo, NULL, &tuple_desc)
178  != TYPEFUNC_COMPOSITE) {
179  ereport(ERROR,
180  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
181  errmsg("function returning record called in context "
182  "that cannot accept type record")));
183  }
184 
185  funcctx->tuple_desc = tuple_desc;
186  MemoryContextSwitchTo(oldcontext);
187  }
188 
189  funcctx = SRF_PERCALL_SETUP();
190  tuple_desc = funcctx->tuple_desc;
191  result_tuples = (General_path_element_t*) funcctx->user_fctx;
192 
193  if (funcctx->call_cntr < funcctx->max_calls) {
194  HeapTuple tuple;
195  Datum result;
196  Datum *values;
197  bool* nulls;
198  size_t call_cntr = funcctx->call_cntr;
199 
200  /**********************************************************************/
201  // OUT seq INTEGER,
202  // OUT path_seq INTEGER,
203  // OUT start_vid BIGINT,
204  // OUT end_vid BIGINT,
205  // OUT node BIGINT,
206  // OUT edge BIGINT,
207  // OUT cost FLOAT,
208  // OUT agg_cost FLOAT)
209 
210  size_t numb = 8;
211  values = palloc(numb * sizeof(Datum));
212  nulls = palloc(numb * sizeof(bool));
213 
214  size_t i;
215  for (i = 0; i < numb; ++i) {
216  nulls[i] = false;
217  }
218 
219  values[0] = Int32GetDatum(call_cntr + 1);
220  values[1] = Int32GetDatum(result_tuples[call_cntr].seq);
221  values[2] = Int64GetDatum(result_tuples[call_cntr].start_id);
222  values[3] = Int64GetDatum(result_tuples[call_cntr].end_id);
223  values[4] = Int64GetDatum(result_tuples[call_cntr].node);
224  values[5] = Int64GetDatum(result_tuples[call_cntr].edge);
225  values[6] = Float8GetDatum(result_tuples[call_cntr].cost);
226  values[7] = Float8GetDatum(result_tuples[call_cntr].agg_cost);
227  /**********************************************************************/
228 
229  tuple = heap_form_tuple(tuple_desc, values, nulls);
230  result = HeapTupleGetDatum(tuple);
231  SRF_RETURN_NEXT(funcctx, result);
232  } else {
233  SRF_RETURN_DONE(funcctx);
234  }
235 }
236 
Definition: trsp.h:31
PG_FUNCTION_INFO_V1(many_to_many_dijkstra)
#define PGR_DBG(...)
Definition: debug_macro.h:34
void pgr_get_edges(char *edges_sql, pgr_edge_t **edges, size_t *total_edges)
basic edge_sql
Definition: edges_input.c:545
static void process(char *edges_sql, ArrayType *starts, ArrayType *ends, bool directed, bool only_cost, bool normal, General_path_element_t **result_tuples, size_t *result_count)
Definition: dijkstra.c:48
int64_t * pgr_get_bigIntArray(size_t *arrlen, ArrayType *input)
enforces the input array to be NOT empty
Definition: arrays_input.c:124
void time_msg(char *msg, clock_t start_t, clock_t end_t)
Definition: time_msg.c:32
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, General_path_element_t **return_tuples, size_t *return_count, char **log_msg, char **notice_msg, char **err_msg)
void pgr_global_report(char *log, char *notice, char *err)
notice & error
Definition: e_report.c:93
void pgr_SPI_finish(void)
if(DOXYGEN_FOUND) configure_file($
Definition: CMakeLists.txt:13
void pgr_SPI_connect(void)
PGDLLEXPORT Datum many_to_many_dijkstra(PG_FUNCTION_ARGS)
Definition: dijkstra.c:134
PG_MODULE_MAGIC
Definition: dijkstra.c:41
void pgr_get_edges_reversed(char *edges_sql, pgr_edge_t **edges, size_t *total_edges)
Definition: edges_input.c:556