PGROUTING  3.2
astar.c File Reference
#include <stdbool.h>
#include "c_common/postgres_connection.h"
#include "utils/array.h"
#include "c_common/debug_macro.h"
#include "c_common/e_report.h"
#include "c_common/time_msg.h"
#include "c_common/edges_input.h"
#include "c_common/arrays_input.h"
#include "c_common/combinations_input.h"
#include "c_common/check_parameters.h"
#include "drivers/astar/astar_driver.h"
Include dependency graph for astar.c:

Go to the source code of this file.

Functions

PGDLLEXPORT Datum _pgr_astar (PG_FUNCTION_ARGS)
 
 PG_FUNCTION_INFO_V1 (_pgr_astar)
 
static void process (char *edges_sql, char *combinations_sql, ArrayType *starts, ArrayType *ends, bool directed, int heuristic, double factor, double epsilon, bool only_cost, bool normal, General_path_element_t **result_tuples, size_t *result_count)
 

Function Documentation

◆ _pgr_astar()

PGDLLEXPORT Datum _pgr_astar ( PG_FUNCTION_ARGS  )

Definition at line 176 of file astar.c.

176  {
177  FuncCallContext *funcctx;
178  TupleDesc tuple_desc;
179 
180  /**********************************************************************/
181  General_path_element_t *result_tuples = NULL;
182  size_t result_count = 0;
183  /**********************************************************************/
184 
185  if (SRF_IS_FIRSTCALL()) {
186  MemoryContext oldcontext;
187  funcctx = SRF_FIRSTCALL_INIT();
188  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
189 
190 
191  if (PG_NARGS() == 9) {
192  /*
193  * many to many
194  */
195 
196  process(
197  text_to_cstring(PG_GETARG_TEXT_P(0)),
198  NULL,
199  PG_GETARG_ARRAYTYPE_P(1),
200  PG_GETARG_ARRAYTYPE_P(2),
201  PG_GETARG_BOOL(3),
202  PG_GETARG_INT32(4),
203  PG_GETARG_FLOAT8(5),
204  PG_GETARG_FLOAT8(6),
205  PG_GETARG_BOOL(7),
206  PG_GETARG_BOOL(8),
207  &result_tuples,
208  &result_count);
209 
210  } else if (PG_NARGS() == 7) {
211  /*
212  * Combinations
213  */
214 
215  process(
216  text_to_cstring(PG_GETARG_TEXT_P(0)),
217  text_to_cstring(PG_GETARG_TEXT_P(1)),
218  NULL,
219  NULL,
220  PG_GETARG_BOOL(2),
221  PG_GETARG_INT32(3),
222  PG_GETARG_FLOAT8(4),
223  PG_GETARG_FLOAT8(5),
224  PG_GETARG_BOOL(6),
225  true,
226  &result_tuples,
227  &result_count);
228  }
229 
230 
231 #if PGSQL_VERSION > 95
232  funcctx->max_calls = result_count;
233 #else
234  funcctx->max_calls = (uint32_t)result_count;
235 #endif
236  funcctx->user_fctx = result_tuples;
237  if (get_call_result_type(fcinfo, NULL, &tuple_desc)
238  != TYPEFUNC_COMPOSITE)
239  ereport(ERROR,
240  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
241  errmsg("function returning record called in context "
242  "that cannot accept type record")));
243 
244  funcctx->tuple_desc = tuple_desc;
245  MemoryContextSwitchTo(oldcontext);
246  }
247 
248  funcctx = SRF_PERCALL_SETUP();
249  tuple_desc = funcctx->tuple_desc;
250  result_tuples = (General_path_element_t*) funcctx->user_fctx;
251 
252  if (funcctx->call_cntr < funcctx->max_calls) {
253  HeapTuple tuple;
254  Datum result;
255  Datum *values;
256  bool* nulls;
257 
258  /*********************************************************************
259  OUT seq INTEGER,
260  OUT path_seq INTEGER,
261  OUT start_vid BIGINT,
262  OUT end_vid BIGINT,
263  OUT node BIGINT,
264  OUT edge BIGINT,
265  OUT cost FLOAT,
266  OUT agg_cost FLOAT
267  **********************************************************************/
268 
269 
270  size_t numb = 8;
271  values = palloc(numb * sizeof(Datum));
272  nulls = palloc(numb * sizeof(bool));
273 
274  size_t i;
275  for (i = 0; i < numb; ++i) {
276  nulls[i] = false;
277  }
278 
279 
280  values[0] = Int32GetDatum(funcctx->call_cntr + 1);
281  values[1] = Int32GetDatum(result_tuples[funcctx->call_cntr].seq);
282  values[2] = Int64GetDatum(result_tuples[funcctx->call_cntr].start_id);
283  values[3] = Int64GetDatum(result_tuples[funcctx->call_cntr].end_id);
284  values[4] = Int64GetDatum(result_tuples[funcctx->call_cntr].node);
285  values[5] = Int64GetDatum(result_tuples[funcctx->call_cntr].edge);
286  values[6] = Float8GetDatum(result_tuples[funcctx->call_cntr].cost);
287  values[7] = Float8GetDatum(result_tuples[funcctx->call_cntr].agg_cost);
288 
289 
290  tuple = heap_form_tuple(tuple_desc, values, nulls);
291  result = HeapTupleGetDatum(tuple);
292  SRF_RETURN_NEXT(funcctx, result);
293  } else {
294  SRF_RETURN_DONE(funcctx);
295  }
296 }

References General_path_element_t::agg_cost, General_path_element_t::cost, General_path_element_t::edge, General_path_element_t::end_id, if(), General_path_element_t::node, process(), General_path_element_t::seq, and General_path_element_t::start_id.

◆ PG_FUNCTION_INFO_V1()

PG_FUNCTION_INFO_V1 ( _pgr_astar  )

◆ process()

static void process ( char *  edges_sql,
char *  combinations_sql,
ArrayType *  starts,
ArrayType *  ends,
bool  directed,
int  heuristic,
double  factor,
double  epsilon,
bool  only_cost,
bool  normal,
General_path_element_t **  result_tuples,
size_t *  result_count 
)
static

Definition at line 73 of file astar.c.

84  {
85  check_parameters(heuristic, factor, epsilon);
86 
88 
89  int64_t* start_vidsArr = NULL;
90  size_t size_start_vidsArr = 0;
91 
92  int64_t* end_vidsArr = NULL;
93  size_t size_end_vidsArr = 0;
94 
95  Pgr_edge_xy_t *edges = NULL;
96  size_t total_edges = 0;
97 
98  pgr_combination_t *combinations = NULL;
99  size_t total_combinations = 0;
100 
101  if (normal) {
102  pgr_get_edges_xy(edges_sql, &edges, &total_edges);
103  if (starts && ends) {
104  start_vidsArr = (int64_t*)
105  pgr_get_bigIntArray(&size_start_vidsArr, starts);
106  end_vidsArr = (int64_t*)
107  pgr_get_bigIntArray(&size_end_vidsArr, ends);
108  } else if (combinations_sql) {
109  pgr_get_combinations(combinations_sql, &combinations, &total_combinations);
110  }
111  } else {
112  pgr_get_edges_xy_reversed(edges_sql, &edges, &total_edges);
113  end_vidsArr = (int64_t*)
114  pgr_get_bigIntArray(&size_end_vidsArr, starts);
115  start_vidsArr = (int64_t*)
116  pgr_get_bigIntArray(&size_start_vidsArr, ends);
117  }
118 
119  if (total_edges == 0) {
120  PGR_DBG("No edges found");
121  (*result_count) = 0;
122  (*result_tuples) = NULL;
123  pgr_SPI_finish();
124  return;
125  }
126 
127  PGR_DBG("Starting processing");
128  char *log_msg = NULL;
129  char *notice_msg = NULL;
130  char *err_msg = NULL;
131  clock_t start_t = clock();
133  edges, total_edges,
134 
135  combinations, total_combinations,
136 
137  start_vidsArr, size_start_vidsArr,
138  end_vidsArr, size_end_vidsArr,
139  directed,
140  heuristic,
141  factor,
142  epsilon,
143  only_cost,
144  normal,
145  result_tuples, result_count,
146  &log_msg,
147  &notice_msg,
148  &err_msg);
149 
150  if (only_cost) {
151  time_msg("processing pgr_astarCost", start_t, clock());
152  } else {
153  time_msg("processing pgr_astar", start_t, clock());
154  }
155 
156 
157  if (err_msg && (*result_tuples)) {
158  pfree(*result_tuples);
159  (*result_tuples) = NULL;
160  (*result_count) = 0;
161  }
162 
163  pgr_global_report(log_msg, notice_msg, err_msg);
164 
165  if (log_msg) pfree(log_msg);
166  if (notice_msg) pfree(notice_msg);
167  if (err_msg) pfree(err_msg);
168  if (edges) pfree(edges);
169  if (start_vidsArr) pfree(start_vidsArr);
170  if (end_vidsArr) pfree(end_vidsArr);
171 
172  pgr_SPI_finish();
173 }

References check_parameters(), do_pgr_astarManyToMany(), PGR_DBG, pgr_get_bigIntArray(), pgr_get_combinations(), pgr_get_edges_xy(), pgr_get_edges_xy_reversed(), pgr_global_report(), pgr_SPI_connect(), pgr_SPI_finish(), and time_msg().

Referenced by _pgr_astar().

check_parameters
void check_parameters(int heuristic, double factor, double epsilon)
Definition: check_parameters.c:34
pgr_get_edges_xy
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:744
process
static void process(char *edges_sql, char *combinations_sql, ArrayType *starts, ArrayType *ends, bool directed, int heuristic, double factor, double epsilon, bool only_cost, bool normal, General_path_element_t **result_tuples, size_t *result_count)
Definition: astar.c:73
pgr_SPI_connect
void pgr_SPI_connect(void)
Definition: postgres_connection.c:82
pgr_SPI_finish
void pgr_SPI_finish(void)
Definition: postgres_connection.c:71
pgr_get_bigIntArray
int64_t * pgr_get_bigIntArray(size_t *arrlen, ArrayType *input)
Enforces the input array to be NOT empty.
Definition: arrays_input.c:146
pgr_get_edges_xy_reversed
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:757
do_pgr_astarManyToMany
void do_pgr_astarManyToMany(Pgr_edge_xy_t *edges, size_t total_edges, pgr_combination_t *combinations, size_t total_combinations, 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, bool normal, General_path_element_t **return_tuples, size_t *return_count, char **log_msg, char **notice_msg, char **err_msg)
Definition: astar_driver.cpp:86
PGR_DBG
#define PGR_DBG(...)
Definition: debug_macro.h:34
pgr_combination_t
Definition: pgr_combination_t.h:43
pgr_get_combinations
void pgr_get_combinations(char *combinations_sql, pgr_combination_t **combinations, size_t *total_combinations)
combinations_sql
Definition: combinations_input.c:147
General_path_element_t
Definition: general_path_element_t.h:37
if
if(DOXYGEN_FOUND) configure_file($
Definition: doxygen/CMakeLists.txt:13
time_msg
void time_msg(char *msg, clock_t start_t, clock_t end_t)
Definition: time_msg.c:32
Pgr_edge_xy_t
Definition: pgr_edge_xy_t.h:38
pgr_global_report
void pgr_global_report(char *log, char *notice, char *err)
notice & error
Definition: e_report.c:93