pgRouting  2.2
pgRouting extends the PostGIS / PostgreSQL geospatial database to provide geospatial routing functionality.
 All Classes Functions Variables Pages
many_to_many_dijkstra.c
1 /*PGR-GNU*****************************************************************
2 
3 File: many_to_many_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_many_dijkstra_driver.h"
54 
55 PG_FUNCTION_INFO_V1(many_to_many_dijkstra);
56 #ifndef _MSC_VER
57 Datum
58 #else // _MSC_VER
59 PGDLLEXPORT Datum
60 #endif
61 many_to_many_dijkstra(PG_FUNCTION_ARGS);
62 
63 
64 /******************************************************************************/
65 /* MODIFY AS NEEDED */
66 static
67 void
68 process(
69  char* edges_sql,
70  int64_t *start_vidsArr, size_t size_start_vidsArr,
71  int64_t *end_vidsArr, 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_many_to_many_dijkstra(
97  edges,
98  total_tuples,
99  start_vidsArr,
100  size_start_vidsArr,
101  end_vidsArr,
102  size_end_vidsArr,
103  directed,
104  only_cost,
105  result_tuples,
106  result_count,
107  &err_msg);
108  time_msg(" processing Dijkstra many to many", start_t, clock());
109  PGR_DBG("Returning %ld tuples\n", *result_count);
110  PGR_DBG("Returned message = %s\n", err_msg);
111 
112  free(err_msg);
113  pfree(edges);
114  pgr_SPI_finish();
115 }
116 /* */
117 /*****************************************************************************/
118 
119 #ifndef _MSC_VER
120 Datum
121 #else // _MSC_VER
122 PGDLLEXPORT Datum
123 #endif
124 many_to_many_dijkstra(PG_FUNCTION_ARGS) {
125  FuncCallContext *funcctx;
126  uint32_t call_cntr;
127  uint32_t max_calls;
128  TupleDesc tuple_desc;
129 
130  /**************************************************************************/
131  /* MODIFY AS NEEDED */
132  /* */
133  General_path_element_t *result_tuples = 0;
134  size_t result_count = 0;
135  /* */
136  /**************************************************************************/
137 
138  if (SRF_IS_FIRSTCALL()) {
139  MemoryContext oldcontext;
140  funcctx = SRF_FIRSTCALL_INIT();
141  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
142 
143 
144  /*********************************************************************/
145  /* MODIFY AS NEEDED */
146  // CREATE OR REPLACE FUNCTION pgr_dijkstra(
147  // sql text,
148  // start_vids anyarray,
149  // end_vids anyarray,
150  // directed boolean default true,
151 
152  PGR_DBG("Initializing arrays");
153  int64_t* start_vidsArr;
154  size_t size_start_vidsArr;
155  start_vidsArr = (int64_t*)
156  pgr_get_bigIntArray(&size_start_vidsArr, PG_GETARG_ARRAYTYPE_P(1));
157  PGR_DBG("start_vidsArr size %d ", size_start_vidsArr);
158 
159  int64_t* end_vidsArr;
160  size_t size_end_vidsArr;
161  end_vidsArr = (int64_t*)
162  pgr_get_bigIntArray(&size_end_vidsArr, PG_GETARG_ARRAYTYPE_P(2));
163  PGR_DBG("end_vidsArr size %d ", size_end_vidsArr);
164 
165  PGR_DBG("Calling process");
166  process(
167  pgr_text2char(PG_GETARG_TEXT_P(0)),
168  start_vidsArr, size_start_vidsArr,
169  end_vidsArr, size_end_vidsArr,
170  PG_GETARG_BOOL(3),
171  PG_GETARG_BOOL(4),
172  &result_tuples,
173  &result_count);
174 
175  PGR_DBG("Cleaning arrays");
176  free(end_vidsArr);
177  free(start_vidsArr);
178  /* */
179  /*********************************************************************/
180 
181  funcctx->max_calls = (uint32_t)result_count;
182  funcctx->user_fctx = result_tuples;
183  if (get_call_result_type(fcinfo, NULL, &tuple_desc)
184  != TYPEFUNC_COMPOSITE) {
185  ereport(ERROR,
186  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
187  errmsg("function returning record called in context "
188  "that cannot accept type record")));
189  }
190 
191  funcctx->tuple_desc = tuple_desc;
192  MemoryContextSwitchTo(oldcontext);
193  }
194 
195  funcctx = SRF_PERCALL_SETUP();
196  call_cntr = funcctx->call_cntr;
197  max_calls = funcctx->max_calls;
198  tuple_desc = funcctx->tuple_desc;
199  result_tuples = (General_path_element_t*) funcctx->user_fctx;
200 
201  if (call_cntr < max_calls) {
202  HeapTuple tuple;
203  Datum result;
204  Datum *values;
205  char* nulls;
206 
207  /*********************************************************************/
208  /* MODIFY AS NEEDED */
209  // OUT seq integer,
210  // OUT path_seq INTEGER,
211  // OUT start_vid,
212  // OUT end_vid BIGINT,
213  // OUT node bigint,
214  // OUT edge bigint,
215  // OUT cost float,
216  // OUT agg_cost float)
217 
218  values = palloc(8 * sizeof(Datum));
219  nulls = palloc(8 * sizeof(char));
220 
221  size_t i;
222  for (i = 0; i < 8; ++i) {
223  nulls[i] = ' ';
224  }
225 
226  values[0] = Int32GetDatum(call_cntr + 1);
227  values[1] = Int32GetDatum(result_tuples[call_cntr].seq);
228  values[2] = Int64GetDatum(result_tuples[call_cntr].start_id);
229  values[3] = Int64GetDatum(result_tuples[call_cntr].end_id);
230  values[4] = Int64GetDatum(result_tuples[call_cntr].node);
231  values[5] = Int64GetDatum(result_tuples[call_cntr].edge);
232  values[6] = Float8GetDatum(result_tuples[call_cntr].cost);
233  values[7] = Float8GetDatum(result_tuples[call_cntr].agg_cost);
234  /*********************************************************************/
235 
236  tuple = heap_formtuple(tuple_desc, values, nulls);
237  result = HeapTupleGetDatum(tuple);
238  SRF_RETURN_NEXT(funcctx, result);
239  } else {
240  // cleanup
241  if (result_tuples) free(result_tuples);
242 
243  SRF_RETURN_DONE(funcctx);
244  }
245 }
246