PGROUTING  3.2
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 <stdbool.h>
30 #include "utils/array.h"
31 
32 #include "c_common/debug_macro.h"
33 #include "c_common/e_report.h"
34 #include "c_common/time_msg.h"
35 #include "c_common/edges_input.h"
36 #include "c_common/arrays_input.h"
38 
39 PGDLLEXPORT Datum _pgr_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  size_t size_via_vidsArr = 0;
54  int64_t* via_vidsArr = (int64_t*) pgr_get_bigIntArray(&size_via_vidsArr, vias);
55 
56  pgr_edge_t* edges = NULL;
57  size_t total_edges = 0;
58  pgr_get_edges(edges_sql, &edges, &total_edges);
59 
60  if (total_edges == 0) {
61  if (via_vidsArr) pfree(via_vidsArr);
63  return;
64  }
65 
66  PGR_DBG("Starting timer");
67  clock_t start_t = clock();
68  char* log_msg = NULL;
69  char* notice_msg = NULL;
70  char* err_msg = NULL;
72  edges, total_edges,
73  via_vidsArr, size_via_vidsArr,
74  directed,
75  strict,
76  U_turn_on_edge,
77  result_tuples,
78  result_count,
79  &log_msg,
80  &notice_msg,
81  &err_msg);
82  time_msg("processing pgr_dijkstraVia", start_t, clock());
83 
84  if (err_msg && (*result_tuples)) {
85  pfree(*result_tuples);
86  (*result_tuples) = NULL;
87  (*result_count) = 0;
88  }
89 
90  pgr_global_report(log_msg, notice_msg, err_msg);
91 
92  if (log_msg) pfree(log_msg);
93  if (notice_msg) pfree(notice_msg);
94  if (err_msg) pfree(err_msg);
95  if (edges) pfree(edges);
96  if (via_vidsArr) pfree(via_vidsArr);
98 }
99 
100 
101 PGDLLEXPORT Datum
102 _pgr_dijkstravia(PG_FUNCTION_ARGS) {
103  FuncCallContext *funcctx;
104  TupleDesc tuple_desc;
105 
106  /**********************************************************************/
107  Routes_t *result_tuples = 0;
108  size_t result_count = 0;
109  /**********************************************************************/
110 
111  if (SRF_IS_FIRSTCALL()) {
112  MemoryContext oldcontext;
113  funcctx = SRF_FIRSTCALL_INIT();
114  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
115 
116 
117  /**********************************************************************
118  * pgr_dijkstraVia(edges_sql text,
119  * vertices anyarray,
120  * directed boolean default true,
121  * strict boolean default false,
122  * U_turn_on_edge boolean default false,
123  **********************************************************************/
124 
125  process(
126  text_to_cstring(PG_GETARG_TEXT_P(0)),
127  PG_GETARG_ARRAYTYPE_P(1),
128  PG_GETARG_BOOL(2),
129  PG_GETARG_BOOL(3),
130  PG_GETARG_BOOL(4),
131  &result_tuples,
132  &result_count);
133 
134  /**********************************************************************/
135 
136 #if PGSQL_VERSION > 95
137  funcctx->max_calls = result_count;
138 #else
139  funcctx->max_calls = (uint32_t)result_count;
140 #endif
141  funcctx->user_fctx = result_tuples;
142  if (get_call_result_type(fcinfo, NULL, &tuple_desc)
143  != TYPEFUNC_COMPOSITE)
144  ereport(ERROR,
145  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
146  errmsg("function returning record called in context "
147  "that cannot accept type record")));
148 
149  funcctx->tuple_desc = tuple_desc;
150  MemoryContextSwitchTo(oldcontext);
151  }
152 
153  funcctx = SRF_PERCALL_SETUP();
154  tuple_desc = funcctx->tuple_desc;
155  result_tuples = (Routes_t*) funcctx->user_fctx;
156 
157  if (funcctx->call_cntr < funcctx->max_calls) {
158  HeapTuple tuple;
159  Datum result;
160  Datum *values;
161  bool* nulls;
162  size_t call_cntr = funcctx->call_cntr;
163 
164  /**********************************************************************/
165  /*
166  OUT seq INTEGER,
167  OUT path_id INTEGER,
168  OUT path_seq INTEGER,
169  OUT start_vid BIGINT,
170  OUT end_vid BIGINT,
171  OUT node BIGINT,
172  OUT edge BIGINT,
173  OUT cost FLOAT,
174  OUT agg_cost FLOAT,
175  OUT route_agg_cost FLOAT
176  */
177 
178  size_t numb_out = 10;
179  values = palloc(numb_out * sizeof(Datum));
180  nulls = palloc(numb_out * sizeof(bool));
181  size_t i;
182  for (i = 0; i< numb_out; ++i) {
183  nulls[i] = false;
184  }
185 
186  // postgres starts counting from 1
187  values[0] = Int32GetDatum(call_cntr + 1);
188  values[1] = Int32GetDatum(result_tuples[call_cntr].path_id);
189  values[2] = Int32GetDatum(result_tuples[call_cntr].path_seq + 1);
190  values[3] = Int64GetDatum(result_tuples[call_cntr].start_vid);
191  values[4] = Int64GetDatum(result_tuples[call_cntr].end_vid);
192  values[5] = Int64GetDatum(result_tuples[call_cntr].node);
193  values[6] = Int64GetDatum(result_tuples[call_cntr].edge);
194  values[7] = Float8GetDatum(result_tuples[call_cntr].cost);
195  values[8] = Float8GetDatum(result_tuples[call_cntr].agg_cost);
196  values[9] = Float8GetDatum(result_tuples[call_cntr].route_agg_cost);
197 
198  /**********************************************************************/
199 
200  tuple = heap_form_tuple(tuple_desc, values, nulls);
201  result = HeapTupleGetDatum(tuple);
202  SRF_RETURN_NEXT(funcctx, result);
203  } else {
204  SRF_RETURN_DONE(funcctx);
205  }
206 }
PG_FUNCTION_INFO_V1
PG_FUNCTION_INFO_V1(_pgr_dijkstravia)
_pgr_dijkstravia
PGDLLEXPORT Datum _pgr_dijkstravia(PG_FUNCTION_ARGS)
Definition: dijkstraVia.c:102
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
pgr_SPI_finish
void pgr_SPI_finish(void)
Definition: postgres_connection.c:71
e_report.h
arrays_input.h
edge
Definition: trsp.h:41
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_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
Routes_t
Definition: routes_t.h:37
dijkstraVia_driver.h
if
if(DOXYGEN_FOUND) configure_file($
Definition: doxygen/CMakeLists.txt:13
process
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
time_msg
void time_msg(char *msg, clock_t start_t, clock_t end_t)
Definition: time_msg.c:32
do_pgr_dijkstraVia
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)
Definition: dijkstraVia_driver.cpp:90
edges_input.h
pgr_global_report
void pgr_global_report(char *log, char *notice, char *err)
notice & error
Definition: e_report.c:93