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
astarManyToMany.c
Go to the documentation of this file.
1 /*PGR-GNU*****************************************************************
2 File: astarOneToOne.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:
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 #include "fmgr.h"
39 
40 #include "./../../common/src/debug_macro.h"
41 #include "./../../common/src/time_msg.h"
42 #include "./../../common/src/pgr_types.h"
43 #include "./../../common/src/postgres_connection.h"
44 #include "./../../common/src/edges_input.h"
45 #include "./../../common/src/arrays_input.h"
46 
48 
50 PGDLLEXPORT Datum
51 astarManyToMany(PG_FUNCTION_ARGS);
52 
53 
54 static
55 void
56 process(char* edges_sql,
57  int64_t *start_vidsArr, size_t size_start_vidsArr,
58  int64_t *end_vidsArr, size_t size_end_vidsArr,
59  bool directed,
60  int heuristic,
61  double factor,
62  double epsilon,
63  bool only_cost,
64  General_path_element_t **result_tuples,
65  size_t *result_count) {
66  if (heuristic > 5 || heuristic < 0) {
67  free(start_vidsArr);
68  free(end_vidsArr);
69  ereport(ERROR,
70  (errmsg("Unknown heuristic"),
71  errhint("Valid values: 0~5")));
72  }
73  if (factor <= 0) {
74  free(start_vidsArr);
75  free(end_vidsArr);
76  ereport(ERROR,
77  (errmsg("Factor value out of range"),
78  errhint("Valid values: positive non zero")));
79  }
80  if (epsilon < 1) {
81  free(start_vidsArr);
82  free(end_vidsArr);
83  ereport(ERROR,
84  (errmsg("Epsilon value out of range"),
85  errhint("Valid values: 1 or greater than 1")));
86  }
87 
88 
89 
91 
92  Pgr_edge_xy_t *edges = NULL;
93  size_t total_edges = 0;
94 
95  pgr_get_edges_xy(edges_sql, &edges, &total_edges);
96 
97  if (total_edges == 0) {
98  PGR_DBG("No edges found");
99  (*result_count) = 0;
100  (*result_tuples) = NULL;
101  pgr_SPI_finish();
102  return;
103  }
104 
105  PGR_DBG("Starting processing");
106  char *err_msg = NULL;
107  char *log_msg = NULL;
108  clock_t start_t = clock();
110  edges,
111  total_edges,
112  start_vidsArr, size_start_vidsArr,
113  end_vidsArr, size_end_vidsArr,
114  directed,
115  heuristic,
116  factor,
117  epsilon,
118  only_cost,
119  result_tuples,
120  result_count,
121  &log_msg,
122  &err_msg);
123  time_msg(" processing pgr_astar(many to many)", start_t, clock());
124  PGR_DBG("Returning %ld tuples\n", *result_count);
125  PGR_DBG("LOG: %s\n", log_msg);
126  if (log_msg) free(log_msg);
127 
128  if (err_msg) {
129  if (*result_tuples) free(*result_tuples);
130  elog(ERROR, "%s", err_msg);
131  free(err_msg);
132  }
133 
134  pfree(edges);
135  pgr_SPI_finish();
136 }
137 
138 PGDLLEXPORT Datum
139 astarManyToMany(PG_FUNCTION_ARGS) {
140  FuncCallContext *funcctx;
141  uint32_t call_cntr;
142  uint32_t max_calls;
143  TupleDesc tuple_desc;
144 
145  General_path_element_t *result_tuples = 0;
146  size_t result_count = 0;
147 
148  if (SRF_IS_FIRSTCALL()) {
149  MemoryContext oldcontext;
150  funcctx = SRF_FIRSTCALL_INIT();
151  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
152 
153 
154  /**********************************************************************/
155  /*
156  edges_sql TEXT,
157  start_vids ARRAY[ANY_INTEGER], -- anyarray
158  end_vids ARRAY[ANY_INTEGER], -- anyarray
159  directed BOOLEAN DEFAULT true,
160  heuristic INTEGER DEFAULT 0,
161  factor FLOAT DEFAULT 1.0,
162  epsilon FLOAT DEFAULT 1.0,
163 
164  **********************************************************************/
165 
166  int64_t* start_vidsArr;
167  size_t size_start_vidsArr;
168  start_vidsArr = (int64_t*)
169  pgr_get_bigIntArray(&size_start_vidsArr, PG_GETARG_ARRAYTYPE_P(1));
170 
171  int64_t* end_vidsArr;
172  size_t size_end_vidsArr;
173  end_vidsArr = (int64_t*)
174  pgr_get_bigIntArray(&size_end_vidsArr, PG_GETARG_ARRAYTYPE_P(2));
175 
176  process(
177  pgr_text2char(PG_GETARG_TEXT_P(0)),
178  start_vidsArr, size_start_vidsArr,
179  end_vidsArr, size_end_vidsArr,
180  PG_GETARG_BOOL(3),
181  PG_GETARG_INT32(4),
182  PG_GETARG_FLOAT8(5),
183  PG_GETARG_FLOAT8(6),
184  PG_GETARG_BOOL(7),
185  &result_tuples,
186  &result_count);
187 
188  free(end_vidsArr);
189  free(start_vidsArr);
190 
191 
192  funcctx->max_calls = (uint32_t) result_count;
193  funcctx->user_fctx = result_tuples;
194  if (get_call_result_type(fcinfo, NULL, &tuple_desc) != TYPEFUNC_COMPOSITE)
195  ereport(ERROR,
196  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
197  errmsg("function returning record called in context "
198  "that cannot accept type record")));
199 
200  funcctx->tuple_desc = tuple_desc;
201  MemoryContextSwitchTo(oldcontext);
202  }
203 
204  funcctx = SRF_PERCALL_SETUP();
205  call_cntr = funcctx->call_cntr;
206  max_calls = funcctx->max_calls;
207  tuple_desc = funcctx->tuple_desc;
208  result_tuples = (General_path_element_t*) funcctx->user_fctx;
209 
210  if (call_cntr < max_calls) {
211  HeapTuple tuple;
212  Datum result;
213  Datum *values;
214  bool* nulls;
215 
216  /*********************************************************************
217  OUT seq INTEGER,
218  OUT path_seq INTEGER,
219  OUT start_vid BIGINT,
220  OUT end_vid BIGINT,
221  OUT node BIGINT,
222  OUT edge BIGINT,
223  OUT cost FLOAT,
224  OUT agg_cost FLOAT
225  **********************************************************************/
226 
227 
228  values = palloc(8 * sizeof(Datum));
229  nulls = palloc(8 * sizeof(bool));
230 
231  size_t i;
232  for (i = 0; i < 8; ++i) {
233  nulls[i] = false;
234  }
235 
236 
237  values[0] = Int32GetDatum(call_cntr + 1);
238  values[1] = Int32GetDatum(result_tuples[call_cntr].seq);
239  values[2] = Int64GetDatum(result_tuples[call_cntr].start_id);
240  values[3] = Int64GetDatum(result_tuples[call_cntr].end_id);
241  values[4] = Int64GetDatum(result_tuples[call_cntr].node);
242  values[5] = Int64GetDatum(result_tuples[call_cntr].edge);
243  values[6] = Float8GetDatum(result_tuples[call_cntr].cost);
244  values[7] = Float8GetDatum(result_tuples[call_cntr].agg_cost);
245 
246 
247  tuple = heap_form_tuple(tuple_desc, values, nulls);
248  result = HeapTupleGetDatum(tuple);
249  SRF_RETURN_NEXT(funcctx, result);
250  } else {
251  if (result_tuples) free(result_tuples);
252 
253  SRF_RETURN_DONE(funcctx);
254  }
255 }
256 
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, int heuristic, double factor, double epsilon, bool only_cost, General_path_element_t **result_tuples, size_t *result_count)
#define PGR_DBG(...)
Definition: debug_macro.h:33
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 do_pgr_astarManyToMany(Pgr_edge_xy_t *edges, size_t total_edges, int64_t *start_vidsArr, size_t size_start_vidsArr, int64_t *end_vidsArr, size_t size_end_vidsArr, bool directed, int heuristic, double factor, double epsilon, bool only_cost, General_path_element_t **return_tuples, size_t *return_count, char **log_msg, char **err_msg)
PG_FUNCTION_INFO_V1(astarManyToMany)
PGDLLEXPORT Datum astarManyToMany(PG_FUNCTION_ARGS)
void pgr_SPI_finish(void)
void pgr_get_edges_xy(char *edges_sql, Pgr_edge_xy_t **edges, size_t *total_edges)
Edges with x, y vertices values.
Definition: edges_input.c:553
edge_astar_t * edges
Definition: BDATester.cpp:46
void pgr_SPI_connect(void)
char * err_msg
Definition: BDATester.cpp:50
char * pgr_text2char(text *in)