PGROUTING  2.4
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
dijkstraVia.c
Go to the documentation of this file.
1 /*PGR-GNU*****************************************************************
2 File: dijkstraViaVertex.c
3 
4 Generated with Template by:
5 Copyright (c) 2015 pgRouting developers
6 
7 Function's developer:
8 Copyright (c) 2015 Celia Virginia Vergara Castillo
9 
10 ------
11 
12 This program is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 2 of the License, or
15 (at your option) any later version.
16 
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
21 
22 You should have received a copy of the GNU General Public License
23 along with this program; if not, write to the Free Software
24 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 
26 ********************************************************************PGR-GNU*/
27 
28 #include "./../../common/src/postgres_connection.h"
29 #include "utils/array.h"
30 
31 #include "./../../common/src/debug_macro.h"
32 #include "./../../common/src/e_report.h"
33 #include "./../../common/src/time_msg.h"
34 #include "./../../common/src/pgr_types.h"
35 #include "./../../common/src/edges_input.h"
36 #include "./../../common/src/arrays_input.h"
37 #include "./dijkstraVia_driver.h"
38 
39 PGDLLEXPORT Datum dijkstraVia(PG_FUNCTION_ARGS);
41 
42 static
43 void
44 process(char* edges_sql,
45  ArrayType *vias,
46  bool directed,
47  bool strict,
48  bool U_turn_on_edge,
49  Routes_t **result_tuples,
50  size_t *result_count) {
52 
53  int64_t* via_vidsArr = NULL;
54  size_t size_via_vidsArr = 0;
55  via_vidsArr = (int64_t*) pgr_get_bigIntArray(&size_via_vidsArr, vias);
56 
57  pgr_edge_t* edges = NULL;
58  size_t total_edges = 0;
59  pgr_get_edges(edges_sql, &edges, &total_edges);
60 
61  if (total_edges == 0) {
62  if (via_vidsArr) pfree(via_vidsArr);
64  return;
65  }
66 
67  PGR_DBG("Starting timer");
68  clock_t start_t = clock();
69  char* log_msg = NULL;
70  char* notice_msg = NULL;
71  char* err_msg = NULL;
73  edges, total_edges,
74  via_vidsArr, size_via_vidsArr,
75  directed,
76  strict,
77  U_turn_on_edge,
78  result_tuples,
79  result_count,
80  &log_msg,
81  &notice_msg,
82  &err_msg);
83  time_msg("processing pgr_dijkstraVia", start_t, clock());
84 
85  if (err_msg && (*result_tuples)) {
86  pfree(*result_tuples);
87  (*result_tuples) = NULL;
88  (*result_count) = 0;
89  }
90 
91  pgr_global_report(log_msg, notice_msg, err_msg);
92 
93  if (log_msg) pfree(log_msg);
94  if (notice_msg) pfree(notice_msg);
95  if (err_msg) pfree(err_msg);
96  if (edges) pfree(edges);
97  if (via_vidsArr) pfree(via_vidsArr);
99 }
100 
101 
102 PGDLLEXPORT Datum
103 dijkstraVia(PG_FUNCTION_ARGS) {
104  FuncCallContext *funcctx;
105  TupleDesc tuple_desc;
106 
107  /**********************************************************************/
108  Routes_t *result_tuples = 0;
109  size_t result_count = 0;
110  /**********************************************************************/
111 
112  if (SRF_IS_FIRSTCALL()) {
113  MemoryContext oldcontext;
114  funcctx = SRF_FIRSTCALL_INIT();
115  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
116 
117 
118  /**********************************************************************
119  * pgr_dijkstraVia(edges_sql text,
120  * vertices anyarray,
121  * directed boolean default true,
122  * strict boolean default false,
123  * U_turn_on_edge boolean default false,
124  **********************************************************************/
125 
126  process(
127  text_to_cstring(PG_GETARG_TEXT_P(0)),
128  PG_GETARG_ARRAYTYPE_P(1),
129  PG_GETARG_BOOL(2),
130  PG_GETARG_BOOL(3),
131  PG_GETARG_BOOL(4),
132  &result_tuples,
133  &result_count);
134 
135  /**********************************************************************/
136 
137 #if PGSQL_VERSION > 95
138  funcctx->max_calls = result_count;
139 #else
140  funcctx->max_calls = (uint32_t)result_count;
141 #endif
142  funcctx->user_fctx = result_tuples;
143  if (get_call_result_type(fcinfo, NULL, &tuple_desc)
144  != TYPEFUNC_COMPOSITE)
145  ereport(ERROR,
146  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
147  errmsg("function returning record called in context "
148  "that cannot accept type record")));
149 
150  funcctx->tuple_desc = tuple_desc;
151  MemoryContextSwitchTo(oldcontext);
152  }
153 
154  funcctx = SRF_PERCALL_SETUP();
155  tuple_desc = funcctx->tuple_desc;
156  result_tuples = (Routes_t*) funcctx->user_fctx;
157 
158  if (funcctx->call_cntr < funcctx->max_calls) {
159  HeapTuple tuple;
160  Datum result;
161  Datum *values;
162  bool* nulls;
163  size_t call_cntr = funcctx->call_cntr;
164 
165  /**********************************************************************/
166  /*
167  OUT seq INTEGER,
168  OUT path_id INTEGER,
169  OUT path_seq INTEGER,
170  OUT start_vid BIGINT,
171  OUT end_vid BIGINT,
172  OUT node BIGINT,
173  OUT edge BIGINT,
174  OUT cost FLOAT,
175  OUT agg_cost FLOAT,
176  OUT route_agg_cost FLOAT
177  */
178 
179  size_t numb_out = 10;
180  values = palloc(numb_out * sizeof(Datum));
181  nulls = palloc(numb_out * sizeof(bool));
182  size_t i;
183  for (i = 0; i< numb_out; ++i) {
184  nulls[i] = false;
185  }
186 
187  // postgres starts counting from 1
188  values[0] = Int32GetDatum(call_cntr + 1);
189  values[1] = Int32GetDatum(result_tuples[call_cntr].path_id);
190  values[2] = Int32GetDatum(result_tuples[call_cntr].path_seq + 1);
191  values[3] = Int64GetDatum(result_tuples[call_cntr].start_vid);
192  values[4] = Int64GetDatum(result_tuples[call_cntr].end_vid);
193  values[5] = Int64GetDatum(result_tuples[call_cntr].node);
194  values[6] = Int64GetDatum(result_tuples[call_cntr].edge);
195  values[7] = Float8GetDatum(result_tuples[call_cntr].cost);
196  values[8] = Float8GetDatum(result_tuples[call_cntr].agg_cost);
197  values[9] = Float8GetDatum(result_tuples[call_cntr].route_agg_cost);
198 
199  /**********************************************************************/
200 
201  tuple = heap_form_tuple(tuple_desc, values, nulls);
202  result = HeapTupleGetDatum(tuple);
203  SRF_RETURN_NEXT(funcctx, result);
204  } else {
205  SRF_RETURN_DONE(funcctx);
206  }
207 }
#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:539
void do_pgr_dijkstraVia(pgr_edge_t *data_edges, size_t total_edges, int64_t *via_vidsArr, size_t size_via_vidsArr, bool directed, bool strict, bool U_turn_on_edge, Routes_t **return_tuples, size_t *return_count, char **log_msg, char **notice_msg, char **err_msg)
int64_t * pgr_get_bigIntArray(size_t *arrlen, ArrayType *input)
Definition: arrays_input.c:127
void time_msg(char *msg, clock_t start_t, clock_t end_t)
Definition: time_msg.c:32
PG_FUNCTION_INFO_V1(dijkstraVia)
PGDLLEXPORT Datum dijkstraVia(PG_FUNCTION_ARGS)
Definition: dijkstraVia.c:103
void pgr_global_report(char *log, char *notice, char *err)
notice & error
Definition: e_report.c:93
void pgr_SPI_finish(void)
edge_astar_t * edges
Definition: BDATester.cpp:46
void pgr_SPI_connect(void)
char * err_msg
Definition: BDATester.cpp:50
static void process(char *edges_sql, ArrayType *vias, bool directed, bool strict, bool U_turn_on_edge, Routes_t **result_tuples, size_t *result_count)
Definition: dijkstraVia.c:44