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_one_dijkstra.c
Go to the documentation of this file.
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 
48 #include "./../../common/src/debug_macro.h"
49 #include "./../../common/src/time_msg.h"
50 #include "./../../common/src/pgr_types.h"
51 #include "./../../common/src/postgres_connection.h"
52 #include "./../../common/src/edges_input.h"
53 #include "./../../common/src/arrays_input.h"
55 
56 PGDLLEXPORT Datum many_to_one_dijkstra(PG_FUNCTION_ARGS);
57 /******************************************************************************/
58 /* MODIFY AS NEEDED */
59 static
60 void
62  char* edges_sql,
63  int64_t *start_vidsArr,
64  size_t size_start_vidsArr,
65  int64_t end_vid,
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 
85  PGR_DBG("Total %ld tuples in query:", total_tuples);
86 
87  PGR_DBG("Starting processing");
88  clock_t start_t = clock();
89 
90  char *err_msg = NULL;
92  edges,
93  total_tuples,
94  start_vidsArr,
95  size_start_vidsArr,
96  end_vid,
97  directed,
98  only_cost,
99  result_tuples,
100  result_count,
101  &err_msg);
102  time_msg(" processing Dijkstra many to one", 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_one_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_vid BIGINT,
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  PGR_DBG("Calling process");
151  process(
152  pgr_text2char(PG_GETARG_TEXT_P(0)),
153  start_vidsArr, size_start_vidsArr,
154  PG_GETARG_INT64(2),
155  PG_GETARG_BOOL(3),
156  PG_GETARG_BOOL(4),
157  &result_tuples,
158  &result_count);
159 
160  PGR_DBG("Cleaning arrays");
161  free(start_vidsArr);
162  /* */
163  /*********************************************************************/
164 
165  funcctx->max_calls = (uint32_t) result_count;
166  funcctx->user_fctx = result_tuples;
167  if (get_call_result_type(fcinfo, NULL, &tuple_desc)
168  != TYPEFUNC_COMPOSITE) {
169  ereport(ERROR,
170  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
171  errmsg("function returning record called in context "
172  "that cannot accept type record")));
173  }
174 
175  funcctx->tuple_desc = tuple_desc;
176  MemoryContextSwitchTo(oldcontext);
177  }
178 
179  funcctx = SRF_PERCALL_SETUP();
180  call_cntr = funcctx->call_cntr;
181  max_calls = funcctx->max_calls;
182  tuple_desc = funcctx->tuple_desc;
183  result_tuples = (General_path_element_t*) funcctx->user_fctx;
184 
185  if (call_cntr < max_calls) {
186  HeapTuple tuple;
187  Datum result;
188  Datum *values;
189  bool* nulls;
190 
191  /*********************************************************************/
192  /* MODIFY AS NEEDED */
193  // OUT seq INTEGER,
194  // OUT path_seq INTEGER,
195  // OUT start_vid BIGINT,
196  // OUT end_vid BIGINT,
197  // OUT node BIGINT,
198  // OUT edge BIGINT,
199  // OUT cost FLOAT,
200  // OUT agg_cost FLOAT)
201 
202  values = palloc(7 * sizeof(Datum));
203  nulls = palloc(7 * sizeof(bool));
204 
205  size_t i;
206  for (i = 0; i < 7; ++i) {
207  nulls[i] = false;
208  }
209 
210  // postgres starts counting from 1
211  values[0] = Int32GetDatum(call_cntr + 1);
212  values[1] = Int32GetDatum(result_tuples[call_cntr].seq);
213  values[2] = Int64GetDatum(result_tuples[call_cntr].start_id);
214  values[3] = Int64GetDatum(result_tuples[call_cntr].node);
215  values[4] = Int64GetDatum(result_tuples[call_cntr].edge);
216  values[5] = Float8GetDatum(result_tuples[call_cntr].cost);
217  values[6] = Float8GetDatum(result_tuples[call_cntr].agg_cost);
218  /*********************************************************************/
219 
220  tuple = heap_form_tuple(tuple_desc, values, nulls);
221  result = HeapTupleGetDatum(tuple);
222  SRF_RETURN_NEXT(funcctx, result);
223  } else {
224  // cleanup
225  if (result_tuples) free(result_tuples);
226 
227  SRF_RETURN_DONE(funcctx);
228  }
229 }
230 
void do_pgr_many_to_one_dijkstra(pgr_edge_t *data_edges, size_t total_tuples, int64_t *start_vidsArr, size_t size_start_vidsArr, int64_t end_vid, bool directed, bool only_cost, General_path_element_t **return_tuples, size_t *return_count, char **err_msg)
#define PGR_DBG(...)
Definition: debug_macro.h:33
PG_FUNCTION_INFO_V1(many_to_one_dijkstra)
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
PGDLLEXPORT Datum many_to_one_dijkstra(PG_FUNCTION_ARGS)
void pgr_SPI_finish(void)
edge_astar_t * edges
Definition: BDATester.cpp:46
void pgr_SPI_connect(void)
char * err_msg
Definition: BDATester.cpp:50
static void process(char *edges_sql, int64_t *start_vidsArr, size_t size_start_vidsArr, int64_t end_vid, bool directed, bool only_cost, General_path_element_t **result_tuples, size_t *result_count)
char * pgr_text2char(text *in)