pgRouting  2.2
pgRouting extends the PostGIS / PostgreSQL geospatial database to provide geospatial routing functionality.
 All Classes Functions Variables Pages
many_to_one_dijkstra.c
1 /*PGR-GNU*****************************************************************
2 
3 File: many_to_one_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 
31 #include "postgres.h"
32 #include "executor/spi.h"
33 #include "funcapi.h"
34 #include "utils/array.h"
35 #include "catalog/pg_type.h"
36 #if PGSQL_VERSION > 92
37 #include "access/htup_details.h"
38 #endif
39 
40 /*
41  * Uncomment when needed
42  */
43 
44 // #define DEBUG
45 
46 #include "fmgr.h"
47 #include "./../../common/src/debug_macro.h"
48 #include "./../../common/src/time_msg.h"
49 #include "./../../common/src/pgr_types.h"
50 #include "./../../common/src/postgres_connection.h"
51 #include "./../../common/src/edges_input.h"
52 #include "./../../common/src/arrays_input.h"
53 #include "./many_to_one_dijkstra_driver.h"
54 
55 PG_FUNCTION_INFO_V1(many_to_one_dijkstra);
56 #ifndef _MSC_VER
57 Datum
58 #else // _MSC_VER
59 PGDLLEXPORT Datum
60 #endif
61 many_to_one_dijkstra(PG_FUNCTION_ARGS);
62 /******************************************************************************/
63 /* MODIFY AS NEEDED */
64 static
65 void
66 process(
67  char* edges_sql,
68  int64_t *start_vidsArr,
69  size_t size_start_vidsArr,
70  int64_t end_vid,
71  bool directed,
72  bool only_cost,
73  General_path_element_t **result_tuples,
74  size_t *result_count) {
75  pgr_SPI_connect();
76 
77  PGR_DBG("Load data");
78  pgr_edge_t *edges = NULL;
79  size_t total_tuples = 0;
80  pgr_get_data_5_columns(edges_sql, &edges, &total_tuples);
81 
82  if (total_tuples == 0) {
83  PGR_DBG("No edges found");
84  (*result_count) = 0;
85  (*result_tuples) = NULL;
86  pgr_SPI_finish();
87  return;
88  }
89 
90  PGR_DBG("Total %ld tuples in query:", total_tuples);
91 
92  PGR_DBG("Starting processing");
93  clock_t start_t = clock();
94 
95  char *err_msg = NULL;
96  do_pgr_many_to_one_dijkstra(
97  edges,
98  total_tuples,
99  start_vidsArr,
100  size_start_vidsArr,
101  end_vid,
102  directed,
103  only_cost,
104  result_tuples,
105  result_count,
106  &err_msg);
107  time_msg(" processing Dijkstra many to one", start_t, clock());
108  PGR_DBG("Returning %ld tuples\n", *result_count);
109  PGR_DBG("Returned message = %s\n", err_msg);
110 
111  free(err_msg);
112  pfree(edges);
113  pgr_SPI_finish();
114 }
115 /* */
116 /******************************************************************************/
117 
118 #ifndef _MSC_VER
119 Datum
120 #else // _MSC_VER
121 PGDLLEXPORT Datum
122 #endif
123 many_to_one_dijkstra(PG_FUNCTION_ARGS) {
124  FuncCallContext *funcctx;
125  uint32_t call_cntr;
126  uint32_t max_calls;
127  TupleDesc tuple_desc;
128 
129  /**************************************************************************/
130  /* MODIFY AS NEEDED */
131  /* */
132  General_path_element_t *result_tuples = 0;
133  size_t result_count = 0;
134  /* */
135  /**************************************************************************/
136 
137  if (SRF_IS_FIRSTCALL()) {
138  MemoryContext oldcontext;
139  funcctx = SRF_FIRSTCALL_INIT();
140  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
141 
142 
143  /*********************************************************************/
144  /* MODIFY AS NEEDED */
145  // CREATE OR REPLACE FUNCTION pgr_dijkstra(
146  // sql text,
147  // start_vids anyarray,
148  // end_vid BIGINT,
149  // directed BOOLEAN default true,
150 
151  PGR_DBG("Initializing arrays");
152  int64_t* start_vidsArr;
153  size_t size_start_vidsArr;
154  start_vidsArr = (int64_t*)
155  pgr_get_bigIntArray(&size_start_vidsArr, PG_GETARG_ARRAYTYPE_P(1));
156  PGR_DBG("start_vidsArr size %ld ", size_start_vidsArr);
157 
158  PGR_DBG("Calling process");
159  process(
160  pgr_text2char(PG_GETARG_TEXT_P(0)),
161  start_vidsArr, size_start_vidsArr,
162  PG_GETARG_INT64(2),
163  PG_GETARG_BOOL(3),
164  PG_GETARG_BOOL(4),
165  &result_tuples,
166  &result_count);
167 
168  PGR_DBG("Cleaning arrays");
169  free(start_vidsArr);
170  /* */
171  /*********************************************************************/
172 
173  funcctx->max_calls = (uint32_t) result_count;
174  funcctx->user_fctx = result_tuples;
175  if (get_call_result_type(fcinfo, NULL, &tuple_desc)
176  != TYPEFUNC_COMPOSITE) {
177  ereport(ERROR,
178  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
179  errmsg("function returning record called in context "
180  "that cannot accept type record")));
181  }
182 
183  funcctx->tuple_desc = tuple_desc;
184  MemoryContextSwitchTo(oldcontext);
185  }
186 
187  funcctx = SRF_PERCALL_SETUP();
188  call_cntr = funcctx->call_cntr;
189  max_calls = funcctx->max_calls;
190  tuple_desc = funcctx->tuple_desc;
191  result_tuples = (General_path_element_t*) funcctx->user_fctx;
192 
193  if (call_cntr < max_calls) {
194  HeapTuple tuple;
195  Datum result;
196  Datum *values;
197  char* nulls;
198 
199  /*********************************************************************/
200  /* MODIFY AS NEEDED */
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  values = palloc(7 * sizeof(Datum));
211  nulls = palloc(7 * sizeof(char));
212 
213  size_t i;
214  for (i = 0; i < 7; ++i) {
215  nulls[i] = ' ';
216  }
217 
218  // postgres starts counting from 1
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].node);
223  values[4] = Int64GetDatum(result_tuples[call_cntr].edge);
224  values[5] = Float8GetDatum(result_tuples[call_cntr].cost);
225  values[6] = Float8GetDatum(result_tuples[call_cntr].agg_cost);
226  /*********************************************************************/
227 
228  tuple = heap_formtuple(tuple_desc, values, nulls);
229  result = HeapTupleGetDatum(tuple);
230  SRF_RETURN_NEXT(funcctx, result);
231  } else {
232  // cleanup
233  if (result_tuples) free(result_tuples);
234 
235  SRF_RETURN_DONE(funcctx);
236  }
237 }
238