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
one_to_many_dijkstra.c
Go to the documentation of this file.
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"
53 
54 PGDLLEXPORT Datum one_to_many_dijkstra(PG_FUNCTION_ARGS);
55 
56 
57 /******************************************************************************/
58 /* MODIFY AS NEEDED */
59 static
60 void
62  char* edges_sql,
63  int64_t start_vid,
64  int64_t *end_vidsArr,
65  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_vid,
94  end_vidsArr,
95  size_end_vidsArr,
96  directed,
97  only_cost,
98  result_tuples,
99  result_count,
100  &err_msg);
101  time_msg(" processing Dijkstra one to many", start_t, clock());
102  PGR_DBG("Returning %ld tuples\n", *result_count);
103  PGR_DBG("Returned message = %s\n", err_msg);
104 
105  free(err_msg);
106  pfree(edges);
107  pgr_SPI_finish();
108 }
109 /* */
110 /******************************************************************************/
111 
113 PGDLLEXPORT Datum
114 one_to_many_dijkstra(PG_FUNCTION_ARGS) {
115  FuncCallContext *funcctx;
116  uint32_t call_cntr;
117  uint32_t max_calls;
118  TupleDesc tuple_desc;
119 
120  /**************************************************************************/
121  /* MODIFY AS NEEDED */
122  /* */
123  General_path_element_t *result_tuples = 0;
124  size_t result_count = 0;
125  /* */
126  /**************************************************************************/
127 
128  if (SRF_IS_FIRSTCALL()) {
129  MemoryContext oldcontext;
130  funcctx = SRF_FIRSTCALL_INIT();
131  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
132 
133 
134  /**********************************************************************/
135  /* MODIFY AS NEEDED */
136  // CREATE OR REPLACE FUNCTION pgr_dijkstra(sql text,
137  // start_vid bigint,
138  // end_vids anyarray,
139  // directed boolean default true,
140  // only_cost boolean default false
141 
142  PGR_DBG("Initializing arrays");
143  int64_t* end_vidsArr;
144  size_t size_end_vidsArr;
145  end_vidsArr = (int64_t*)
146  pgr_get_bigIntArray(&size_end_vidsArr, PG_GETARG_ARRAYTYPE_P(2));
147  PGR_DBG("targetsArr size %ld ", size_end_vidsArr);
148 
149  PGR_DBG("Calling process");
150  process(
151  pgr_text2char(PG_GETARG_TEXT_P(0)),
152  PG_GETARG_INT64(1),
153  end_vidsArr, size_end_vidsArr,
154  PG_GETARG_BOOL(3),
155  PG_GETARG_BOOL(4),
156  &result_tuples,
157  &result_count);
158 
159  PGR_DBG("Cleaning arrays");
160  free(end_vidsArr);
161  /* */
162  /**********************************************************************/
163 
164  funcctx->max_calls = (uint32_t)result_count;
165  funcctx->user_fctx = result_tuples;
166  if (get_call_result_type(fcinfo, NULL, &tuple_desc)
167  != TYPEFUNC_COMPOSITE) {
168  ereport(ERROR,
169  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
170  errmsg("function returning record called in context "
171  "that cannot accept type record")));
172  }
173 
174  funcctx->tuple_desc = tuple_desc;
175  MemoryContextSwitchTo(oldcontext);
176  }
177 
178  funcctx = SRF_PERCALL_SETUP();
179  call_cntr = funcctx->call_cntr;
180  max_calls = funcctx->max_calls;
181  tuple_desc = funcctx->tuple_desc;
182  result_tuples = (General_path_element_t*) funcctx->user_fctx;
183 
184  if (call_cntr < max_calls) {
185  HeapTuple tuple;
186  Datum result;
187  Datum *values;
188  bool* nulls;
189 
190  /*********************************************************************/
191  /* MODIFY AS NEEDED */
192  // OUT seq integer,
193  // OUT path_seq INTEGER,
194  // OUT end_vid BIGINT,
195  // OUT node bigint,
196  // OUT edge bigint,
197  // OUT cost float,
198  // OUT agg_cost float)
199 
200  values = palloc(7 * sizeof(Datum));
201  nulls = palloc(7 * sizeof(bool));
202 
203 
204  size_t i;
205  for (i = 0; i < 7; ++i) {
206  nulls[i] = false;
207  }
208 
209  // postgres starts counting from 1
210  values[0] = Int32GetDatum(call_cntr + 1);
211  values[1] = Int32GetDatum(result_tuples[call_cntr].seq);
212  values[2] = Int64GetDatum(result_tuples[call_cntr].end_id);
213  values[3] = Int64GetDatum(result_tuples[call_cntr].node);
214  values[4] = Int64GetDatum(result_tuples[call_cntr].edge);
215  values[5] = Float8GetDatum(result_tuples[call_cntr].cost);
216  values[6] = Float8GetDatum(result_tuples[call_cntr].agg_cost);
217  /*********************************************************************/
218 
219  tuple = heap_form_tuple(tuple_desc, values, nulls);
220  result = HeapTupleGetDatum(tuple);
221  SRF_RETURN_NEXT(funcctx, result);
222  } else {
223  // cleanup
224  if (result_tuples) free(result_tuples);
225 
226  SRF_RETURN_DONE(funcctx);
227  }
228 }
229 
#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
void pgr_SPI_finish(void)
PGDLLEXPORT Datum one_to_many_dijkstra(PG_FUNCTION_ARGS)
edge_astar_t * edges
Definition: BDATester.cpp:46
void pgr_SPI_connect(void)
char * err_msg
Definition: BDATester.cpp:50
void do_pgr_one_to_many_dijkstra(pgr_edge_t *data_edges, size_t total_tuples, int64_t start_vid, 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(one_to_many_dijkstra)
static void process(char *edges_sql, int64_t start_vid, int64_t *end_vidsArr, size_t size_end_vidsArr, bool directed, bool only_cost, General_path_element_t **result_tuples, size_t *result_count)
char * pgr_text2char(text *in)