PGROUTING  2.4
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
one_to_one_astar.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 "./../../common/src/postgres_connection.h"
31 #include "utils/array.h"
32 
33 #include "./../../common/src/debug_macro.h"
34 #include "./../../common/src/e_report.h"
35 #include "./../../common/src/time_msg.h"
36 #include "./../../common/src/pgr_types.h"
37 #include "./../../common/src/edges_input.h"
38 
40 
41 PGDLLEXPORT Datum astarOneToOne(PG_FUNCTION_ARGS);
43 
44 
45 static
46 void
47 process(char* edges_sql,
48  int64_t start_vid,
49  int64_t end_vid,
50  bool directed,
51  int heuristic,
52  double factor,
53  double epsilon,
54  bool only_cost,
55  General_path_element_t **result_tuples,
56  size_t *result_count) {
57  check_parameters(heuristic, factor, epsilon);
58 
60 
61  PGR_DBG("Load data");
62  Pgr_edge_xy_t *edges = NULL;
63  size_t total_edges = 0;
64 
65  pgr_get_edges_xy(edges_sql, &edges, &total_edges);
66  PGR_DBG("Total %ld edges in query:", total_edges);
67 
68  if (total_edges == 0) {
69  PGR_DBG("No edges found");
70  (*result_count) = 0;
71  (*result_tuples) = NULL;
73  return;
74  }
75 
76  PGR_DBG("Starting processing");
77  char* log_msg = NULL;
78  char* notice_msg = NULL;
79  char* err_msg = NULL;
80  clock_t start_t = clock();
82  edges, total_edges,
83  &start_vid, 1,
84  &end_vid, 1,
85  directed,
86  heuristic,
87  factor,
88  epsilon,
89  only_cost,
90  true,
91  result_tuples,
92  result_count,
93  &log_msg,
94  &notice_msg,
95  &err_msg);
96 
97  if (only_cost) {
98  time_msg("processing pgr_astarCost(one to one)", start_t, clock());
99  } else {
100  time_msg("processing pgr_astar(one to one)", start_t, clock());
101  }
102 
103  if (err_msg && (*result_tuples)) {
104  pfree(*result_tuples);
105  (*result_count) = 0;
106  (*result_tuples) = NULL;
107  }
108 
109  pgr_global_report(log_msg, notice_msg, err_msg);
110 
111  if (log_msg) pfree(log_msg);
112  if (notice_msg) pfree(notice_msg);
113  if (err_msg) pfree(err_msg);
114  if (edges) pfree(edges);
115 
116  pgr_SPI_finish();
117 }
118 
119 PGDLLEXPORT Datum
120 astarOneToOne(PG_FUNCTION_ARGS) {
121  FuncCallContext *funcctx;
122  TupleDesc tuple_desc;
123 
124  General_path_element_t *result_tuples = 0;
125  size_t result_count = 0;
126 
127  if (SRF_IS_FIRSTCALL()) {
128  MemoryContext oldcontext;
129  funcctx = SRF_FIRSTCALL_INIT();
130  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
131 
132 
133  /**********************************************************************
134  edges_sql TEXT,
135  start_vid BIGINT,
136  end_vid BIGINT,
137  directed BOOLEAN DEFAULT true,
138  heuristic INTEGER DEFAULT 0,
139  factor FLOAT DEFAULT 1.0,
140  epsilon FLOAT DEFAULT 1.0,
141 
142  **********************************************************************/
143 
144  PGR_DBG("Calling process");
145  process(
146  text_to_cstring(PG_GETARG_TEXT_P(0)),
147  PG_GETARG_INT64(1),
148  PG_GETARG_INT64(2),
149  PG_GETARG_BOOL(3),
150  PG_GETARG_INT32(4),
151  PG_GETARG_FLOAT8(5),
152  PG_GETARG_FLOAT8(6),
153  PG_GETARG_BOOL(7),
154  &result_tuples,
155  &result_count);
156 
157 
158 #if PGSQL_VERSION > 95
159  funcctx->max_calls = result_count;
160 #else
161  funcctx->max_calls = (uint32_t)result_count;
162 #endif
163  funcctx->user_fctx = result_tuples;
164  if (get_call_result_type(fcinfo, NULL, &tuple_desc)
165  != TYPEFUNC_COMPOSITE)
166  ereport(ERROR,
167  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
168  errmsg("function returning record called in context "
169  "that cannot accept type record")));
170 
171  funcctx->tuple_desc = tuple_desc;
172  MemoryContextSwitchTo(oldcontext);
173  }
174 
175  funcctx = SRF_PERCALL_SETUP();
176  tuple_desc = funcctx->tuple_desc;
177  result_tuples = (General_path_element_t*) funcctx->user_fctx;
178 
179  if (funcctx->call_cntr < funcctx->max_calls) {
180  HeapTuple tuple;
181  Datum result;
182  Datum *values;
183  bool* nulls;
184 
185  /**********************************************************************
186  OUT seq INTEGER,
187  OUT path_seq INTEGER,
188  OUT node BIGINT,
189  OUT edge BIGINT,
190  OUT cost FLOAT,
191  OUT agg_cost FLOAT
192  *********************************************************************/
193 
194 
195  values = palloc(6 * sizeof(Datum));
196  nulls = palloc(6 * sizeof(bool));
197 
198  size_t i;
199  for (i = 0; i < 6; ++i) {
200  nulls[i] = false;
201  }
202 
203  values[0] = Int32GetDatum(funcctx->call_cntr + 1);
204  values[1] = Int32GetDatum(result_tuples[funcctx->call_cntr].seq);
205  values[2] = Int64GetDatum(result_tuples[funcctx->call_cntr].node);
206  values[3] = Int64GetDatum(result_tuples[funcctx->call_cntr].edge);
207  values[4] = Float8GetDatum(result_tuples[funcctx->call_cntr].cost);
208  values[5] = Float8GetDatum(result_tuples[funcctx->call_cntr].agg_cost);
209 
210 
211  tuple = heap_form_tuple(tuple_desc, values, nulls);
212  result = HeapTupleGetDatum(tuple);
213  SRF_RETURN_NEXT(funcctx, result);
214  } else {
215  SRF_RETURN_DONE(funcctx);
216  }
217 }
218 
static void process(char *edges_sql, int64_t start_vid, 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)
#define PGR_DBG(...)
Definition: debug_macro.h:34
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, bool normal, General_path_element_t **return_tuples, size_t *return_count, char **log_msg, char **notice_msg, char **err_msg)
void time_msg(char *msg, clock_t start_t, clock_t end_t)
Definition: time_msg.c:32
PG_FUNCTION_INFO_V1(astarOneToOne)
PGDLLEXPORT Datum astarOneToOne(PG_FUNCTION_ARGS)
void pgr_global_report(char *log, char *notice, char *err)
notice & error
Definition: e_report.c:93
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:572
edge_astar_t * edges
Definition: BDATester.cpp:46
void pgr_SPI_connect(void)
char * err_msg
Definition: BDATester.cpp:50
void check_parameters(int heuristic, double factor, double epsilon)