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