pgRouting
pgRouting extends the PostGIS / PostgreSQL geospatial database to provide geospatial routing functionality.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
many_to_many_dijkstra.c
Go to the documentation of this file.
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"
54 
55 PGDLLEXPORT Datum many_to_many_dijkstra(PG_FUNCTION_ARGS);
56 
57 
58 /******************************************************************************/
59 /* MODIFY AS NEEDED */
60 static
61 void
63  char* edges_sql,
64  int64_t *start_vidsArr, size_t size_start_vidsArr,
65  int64_t *end_vidsArr, size_t size_end_vidsArr,
66  bool directed,
67  bool only_cost,
68  General_path_element_t **result_tuples,
69  size_t *result_count) {
71 
72  PGR_DBG("Load data");
73  pgr_edge_t *edges = NULL;
74  size_t total_tuples = 0;
75  pgr_get_edges(edges_sql, &edges, &total_tuples);
76 
77  if (total_tuples == 0) {
78  PGR_DBG("No edges found");
79  (*result_count) = 0;
80  (*result_tuples) = NULL;
82  return;
83  }
84  PGR_DBG("Total %ld tuples in query:", total_tuples);
85 
86  PGR_DBG("Starting processing");
87  clock_t start_t = clock();
88 
89  char *err_msg = NULL;
91  edges,
92  total_tuples,
93  start_vidsArr,
94  size_start_vidsArr,
95  end_vidsArr,
96  size_end_vidsArr,
97  directed,
98  only_cost,
99  result_tuples,
100  result_count,
101  &err_msg);
102  time_msg(" processing Dijkstra many to many", start_t, clock());
103  PGR_DBG("Returning %ld tuples\n", *result_count);
104  PGR_DBG("Returned message = %s\n", err_msg);
105 
106  free(err_msg);
107  pfree(edges);
108  pgr_SPI_finish();
109 }
110 /* */
111 /*****************************************************************************/
112 
114 PGDLLEXPORT Datum
115 many_to_many_dijkstra(PG_FUNCTION_ARGS) {
116  FuncCallContext *funcctx;
117  uint32_t call_cntr;
118  uint32_t max_calls;
119  TupleDesc tuple_desc;
120 
121  /**************************************************************************/
122  /* MODIFY AS NEEDED */
123  /* */
124  General_path_element_t *result_tuples = 0;
125  size_t result_count = 0;
126  /* */
127  /**************************************************************************/
128 
129  if (SRF_IS_FIRSTCALL()) {
130  MemoryContext oldcontext;
131  funcctx = SRF_FIRSTCALL_INIT();
132  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
133 
134 
135  /*********************************************************************/
136  /* MODIFY AS NEEDED */
137  // CREATE OR REPLACE FUNCTION pgr_dijkstra(
138  // sql text,
139  // start_vids anyarray,
140  // end_vids anyarray,
141  // directed boolean default true,
142 
143  PGR_DBG("Initializing arrays");
144  int64_t* start_vidsArr;
145  size_t size_start_vidsArr;
146  start_vidsArr = (int64_t*)
147  pgr_get_bigIntArray(&size_start_vidsArr, PG_GETARG_ARRAYTYPE_P(1));
148  PGR_DBG("start_vidsArr size %ld ", size_start_vidsArr);
149 
150  int64_t* end_vidsArr;
151  size_t size_end_vidsArr;
152  end_vidsArr = (int64_t*)
153  pgr_get_bigIntArray(&size_end_vidsArr, PG_GETARG_ARRAYTYPE_P(2));
154  PGR_DBG("end_vidsArr size %ld ", size_end_vidsArr);
155 
156  PGR_DBG("Calling process");
157  process(
158  pgr_text2char(PG_GETARG_TEXT_P(0)),
159  start_vidsArr, size_start_vidsArr,
160  end_vidsArr, size_end_vidsArr,
161  PG_GETARG_BOOL(3),
162  PG_GETARG_BOOL(4),
163  &result_tuples,
164  &result_count);
165 
166  PGR_DBG("Cleaning arrays");
167  free(end_vidsArr);
168  free(start_vidsArr);
169  /* */
170  /*********************************************************************/
171 
172  funcctx->max_calls = (uint32_t)result_count;
173  funcctx->user_fctx = result_tuples;
174  if (get_call_result_type(fcinfo, NULL, &tuple_desc)
175  != TYPEFUNC_COMPOSITE) {
176  ereport(ERROR,
177  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
178  errmsg("function returning record called in context "
179  "that cannot accept type record")));
180  }
181 
182  funcctx->tuple_desc = tuple_desc;
183  MemoryContextSwitchTo(oldcontext);
184  }
185 
186  funcctx = SRF_PERCALL_SETUP();
187  call_cntr = funcctx->call_cntr;
188  max_calls = funcctx->max_calls;
189  tuple_desc = funcctx->tuple_desc;
190  result_tuples = (General_path_element_t*) funcctx->user_fctx;
191 
192  if (call_cntr < max_calls) {
193  HeapTuple tuple;
194  Datum result;
195  Datum *values;
196  bool* nulls;
197 
198  /*********************************************************************/
199  /* MODIFY AS NEEDED */
200  // OUT seq integer,
201  // OUT path_seq INTEGER,
202  // OUT start_vid,
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(8 * sizeof(Datum));
210  nulls = palloc(8 * sizeof(bool));
211 
212  size_t i;
213  for (i = 0; i < 8; ++i) {
214  nulls[i] = false;
215  }
216 
217  values[0] = Int32GetDatum(call_cntr + 1);
218  values[1] = Int32GetDatum(result_tuples[call_cntr].seq);
219  values[2] = Int64GetDatum(result_tuples[call_cntr].start_id);
220  values[3] = Int64GetDatum(result_tuples[call_cntr].end_id);
221  values[4] = Int64GetDatum(result_tuples[call_cntr].node);
222  values[5] = Int64GetDatum(result_tuples[call_cntr].edge);
223  values[6] = Float8GetDatum(result_tuples[call_cntr].cost);
224  values[7] = Float8GetDatum(result_tuples[call_cntr].agg_cost);
225  /*********************************************************************/
226 
227  tuple = heap_form_tuple(tuple_desc, values, nulls);
228  result = HeapTupleGetDatum(tuple);
229  SRF_RETURN_NEXT(funcctx, result);
230  } else {
231  // cleanup
232  if (result_tuples) free(result_tuples);
233 
234  SRF_RETURN_DONE(funcctx);
235  }
236 }
237 
PGDLLEXPORT Datum many_to_many_dijkstra(PG_FUNCTION_ARGS)
#define PGR_DBG(...)
Definition: debug_macro.h:33
void pgr_get_edges(char *edges_sql, pgr_edge_t **edges, size_t *total_edges)
basic edge_sql
Definition: edges_input.c:535
int64_t * pgr_get_bigIntArray(size_t *arrlen, ArrayType *input)
Definition: arrays_input.c:38
void time_msg(char *msg, clock_t start_t, clock_t end_t)
Definition: time_msg.c:31
static void process(char *edges_sql, int64_t *start_vidsArr, size_t size_start_vidsArr, int64_t *end_vidsArr, size_t size_end_vidsArr, bool directed, bool only_cost, General_path_element_t **result_tuples, size_t *result_count)
void pgr_SPI_finish(void)
edge_astar_t * edges
Definition: BDATester.cpp:46
void pgr_SPI_connect(void)
void do_pgr_many_to_many_dijkstra(pgr_edge_t *data_edges, size_t total_tuples, int64_t *start_vidsArr, size_t size_start_vidsArr, int64_t *end_vidsArr, size_t size_end_vidsArr, bool directed, bool only_cost, General_path_element_t **return_tuples, size_t *return_count, char **err_msg)
PG_FUNCTION_INFO_V1(many_to_many_dijkstra)
char * err_msg
Definition: BDATester.cpp:50
char * pgr_text2char(text *in)