pgRouting  2.2
pgRouting extends the PostGIS / PostgreSQL geospatial database to provide geospatial routing functionality.
 All Classes Functions Variables Pages
one_to_one_dijkstra.c
1 /*PGR-GNU*****************************************************************
2 File: one_to_one_dijkstra.c
3 
4 Generated with Template by:
5 Copyright (c) 2015 pgRouting developers
6 Mail: project@pgrouting.org
7 
8 Function's developer:
9 Copyright (c) 2015 Celia Virginia Vergara Castillo
10 Mail: vicky_vergara@hotmail.com
11 
12 ------
13 
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation; either version 2 of the License, or
17 (at your option) any later version.
18 
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
23 
24 You should have received a copy of the GNU General Public License
25 along with this program; if not, write to the Free Software
26 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 
28 ********************************************************************PGR-GNU*/
29 
30 #include "postgres.h"
31 #include "executor/spi.h"
32 #include "funcapi.h"
33 #include "utils/array.h"
34 #include "catalog/pg_type.h"
35 #if PGSQL_VERSION > 92
36 #include "access/htup_details.h"
37 #endif
38 
39 /*
40  Uncomment when needed
41 */
42 // #define DEBUG
43 
44 #include "fmgr.h"
45 #include "./../../common/src/debug_macro.h"
46 #include "./../../common/src/time_msg.h"
47 #include "./../../common/src/pgr_types.h"
48 #include "./../../common/src/postgres_connection.h"
49 #include "./../../common/src/edges_input.h"
50 #include "./one_to_one_dijkstra_driver.h"
51 
52 PG_MODULE_MAGIC;
53 
54 PG_FUNCTION_INFO_V1(one_to_one_dijkstra);
55 #ifndef _MSC_VER
56 Datum
57 #else // _MSC_VER
58 PGDLLEXPORT Datum
59 #endif
60 one_to_one_dijkstra(PG_FUNCTION_ARGS);
61 
62 /******************************************************************************/
63 /* MODIFY AS NEEDED */
64 static
65 void
66 process(
67  char* edges_sql,
68  int64_t start_vid,
69  int64_t end_vid,
70  bool directed,
71  bool only_cost,
72  General_path_element_t **result_tuples,
73  size_t *result_count) {
74  pgr_SPI_connect();
75 
76  PGR_DBG("Load data");
77  pgr_edge_t *edges = NULL;
78 
79  if (start_vid == end_vid) {
80  (*result_count) = 0;
81  (*result_tuples) = NULL;
82  pgr_SPI_finish();
83  return;
84  }
85 
86  size_t total_tuples = 0;
87  pgr_get_data_5_columns(edges_sql, &edges, &total_tuples);
88 
89  if (total_tuples == 0) {
90  PGR_DBG("No edges found");
91  (*result_count) = 0;
92  (*result_tuples) = NULL;
93  pgr_SPI_finish();
94  return;
95  }
96  PGR_DBG("Total %ld tuples in query:", total_tuples);
97 
98  PGR_DBG("Starting processing");
99  clock_t start_t = clock();
100  char *err_msg = NULL;
101  do_pgr_one_to_one_dijkstra(
102  edges,
103  total_tuples,
104  start_vid,
105  end_vid,
106  directed,
107  only_cost,
108  result_tuples,
109  result_count,
110  &err_msg);
111 
112  time_msg(" processing Dijkstra one to one", start_t, clock());
113  PGR_DBG("Returning %ld tuples\n", *result_count);
114  PGR_DBG("Returned message = %s\n", err_msg);
115 
116  free(err_msg);
117  pfree(edges);
118  pgr_SPI_finish();
119 }
120 /* */
121 /******************************************************************************/
122 
123 #ifndef _MSC_VER
124 Datum
125 #else // _MSC_VER
126 PGDLLEXPORT Datum
127 #endif
128 one_to_one_dijkstra(PG_FUNCTION_ARGS) {
129  FuncCallContext *funcctx;
130  uint32_t call_cntr;
131  uint32_t max_calls;
132  TupleDesc tuple_desc;
133 
134  /**************************************************************************/
135  /* MODIFY AS NEEDED */
136  /* */
137  General_path_element_t *result_tuples = 0;
138  size_t result_count = 0;
139  /* */
140  /**************************************************************************/
141 
142  if (SRF_IS_FIRSTCALL()) {
143  MemoryContext oldcontext;
144  funcctx = SRF_FIRSTCALL_INIT();
145  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
146 
147 
148  /**********************************************************************/
149  /* MODIFY AS NEEDED */
150  // CREATE OR REPLACE FUNCTION pgr_dijkstra(
151  // sql text, start_vids BIGINT,
152  // end_vid BIGINT,
153  // directed BOOLEAN default true,
154 
155  PGR_DBG("Calling process");
156  process(
157  pgr_text2char(PG_GETARG_TEXT_P(0)),
158  PG_GETARG_INT64(1),
159  PG_GETARG_INT64(2),
160  PG_GETARG_BOOL(3),
161  PG_GETARG_BOOL(4),
162  &result_tuples,
163  &result_count);
164 
165  /* */
166  /**********************************************************************/
167 
168  funcctx->max_calls = (uint32_t)result_count;
169  funcctx->user_fctx = result_tuples;
170  if (get_call_result_type(fcinfo, NULL, &tuple_desc)
171  != TYPEFUNC_COMPOSITE) {
172  ereport(ERROR,
173  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
174  errmsg("function returning record called in context "
175  "that cannot accept type record")));
176  }
177 
178  funcctx->tuple_desc = tuple_desc;
179  MemoryContextSwitchTo(oldcontext);
180  }
181 
182  funcctx = SRF_PERCALL_SETUP();
183  call_cntr = funcctx->call_cntr;
184  max_calls = funcctx->max_calls;
185  tuple_desc = funcctx->tuple_desc;
186  result_tuples = (General_path_element_t*) funcctx->user_fctx;
187 
188  if (call_cntr < max_calls) {
189  HeapTuple tuple;
190  Datum result;
191  Datum *values;
192  char* nulls;
193 
194  /**********************************************************************/
195  /* MODIFY AS NEEDED */
196  // OUT seq INTEGER,
197  // OUT path_seq INTEGER,
198  // OUT node BIGINT,
199  // OUT edge BIGINT,
200  // OUT cost FLOAT,
201  // OUT agg_cost FLOAT
202 
203  values = palloc(6 * sizeof(Datum));
204  nulls = palloc(6 * sizeof(char));
205 
206 
207  size_t i;
208  for (i = 0; i < 6; ++i) {
209  nulls[i] = ' ';
210  }
211 
212  // postgres starts counting from 1
213  values[0] = Int32GetDatum(call_cntr + 1);
214  values[1] = Int32GetDatum(result_tuples[call_cntr].seq);
215  values[2] = Int64GetDatum(result_tuples[call_cntr].node);
216  values[3] = Int64GetDatum(result_tuples[call_cntr].edge);
217  values[4] = Float8GetDatum(result_tuples[call_cntr].cost);
218  values[5] = Float8GetDatum(result_tuples[call_cntr].agg_cost);
219  /**********************************************************************/
220 
221  tuple = heap_formtuple(tuple_desc, values, nulls);
222  result = HeapTupleGetDatum(tuple);
223  SRF_RETURN_NEXT(funcctx, result);
224  } else {
225  // cleanup
226  if (result_tuples) free(result_tuples);
227 
228  SRF_RETURN_DONE(funcctx);
229  }
230 }
231