pgRouting  2.2
pgRouting extends the PostGIS / PostgreSQL geospatial database to provide geospatial routing functionality.
 All Classes Functions Variables Pages
one_to_many_dijkstra.c
1 /*PGR-GNU*****************************************************************
2 File: one_to_many_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 
43 // #define DEBUG
44 
45 #include "fmgr.h"
46 #include "./../../common/src/debug_macro.h"
47 #include "./../../common/src/time_msg.h"
48 #include "./../../common/src/pgr_types.h"
49 #include "./../../common/src/postgres_connection.h"
50 #include "./../../common/src/edges_input.h"
51 #include "./../../common/src/arrays_input.h"
52 #include "./one_to_many_dijkstra_driver.h"
53 
54 PG_FUNCTION_INFO_V1(one_to_many_dijkstra);
55 #ifndef _MSC_VER
56 Datum
57 #else // _MSC_VER
58 PGDLLEXPORT Datum
59 #endif
60 one_to_many_dijkstra(PG_FUNCTION_ARGS);
61 
62 
63 /******************************************************************************/
64 /* MODIFY AS NEEDED */
65 static
66 void
67 process(
68  char* edges_sql,
69  int64_t start_vid,
70  int64_t *end_vidsArr,
71  size_t size_end_vidsArr,
72  bool directed,
73  bool only_cost,
74  General_path_element_t **result_tuples,
75  size_t *result_count) {
76  pgr_SPI_connect();
77 
78  PGR_DBG("Load data");
79  pgr_edge_t *edges = NULL;
80  size_t total_tuples = 0;
81  pgr_get_data_5_columns(edges_sql, &edges, &total_tuples);
82 
83  if (total_tuples == 0) {
84  PGR_DBG("No edges found");
85  (*result_count) = 0;
86  (*result_tuples) = NULL;
87  pgr_SPI_finish();
88  return;
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_one_to_many_dijkstra(
97  edges,
98  total_tuples,
99  start_vid,
100  end_vidsArr,
101  size_end_vidsArr,
102  directed,
103  only_cost,
104  result_tuples,
105  result_count,
106  &err_msg);
107  time_msg(" processing Dijkstra one to many", 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 one_to_many_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(sql text,
146  // start_vid bigint,
147  // end_vids anyarray,
148  // directed boolean default true,
149  // only_cost boolean default false
150 
151  PGR_DBG("Initializing arrays");
152  int64_t* end_vidsArr;
153  size_t size_end_vidsArr;
154  end_vidsArr = (int64_t*)
155  pgr_get_bigIntArray(&size_end_vidsArr, PG_GETARG_ARRAYTYPE_P(2));
156  PGR_DBG("targetsArr size %ld ", size_end_vidsArr);
157 
158  PGR_DBG("Calling process");
159  process(
160  pgr_text2char(PG_GETARG_TEXT_P(0)),
161  PG_GETARG_INT64(1),
162  end_vidsArr, size_end_vidsArr,
163  PG_GETARG_BOOL(3),
164  PG_GETARG_BOOL(4),
165  &result_tuples,
166  &result_count);
167 
168  PGR_DBG("Cleaning arrays");
169  free(end_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 end_vid BIGINT,
204  // OUT node bigint,
205  // OUT edge bigint,
206  // OUT cost float,
207  // OUT agg_cost float)
208 
209  values = palloc(7 * sizeof(Datum));
210  nulls = palloc(7 * sizeof(char));
211 
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].end_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