PGROUTING  2.4
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
withPoints_ksp.c
Go to the documentation of this file.
1 /*PGR-GNU*****************************************************************
2 File: withPoints_ksp.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/pgr_types.h"
34 #include "./../../common/src/time_msg.h"
35 #include "./../../common/src/e_report.h"
36 #include "./../../common/src/edges_input.h"
37 #include "./../../common/src/points_input.h"
38 #include "./../../withPoints/src/get_new_queries.h"
40 #include "./../../common/src/debug_macro.h"
41 
42 PGDLLEXPORT Datum withPoints_ksp(PG_FUNCTION_ARGS);
44 
45 
46 /*******************************************************************************/
47 /* MODIFY AS NEEDED */
48 static
49 void
51  char* edges_sql,
52  char* points_sql,
53  int64_t start_pid,
54  int64_t end_pid,
55  int k,
56 
57  bool directed,
58  bool heap_paths,
59  char *driving_side,
60  bool details,
61 
62  General_path_element_t **result_tuples,
63  size_t *result_count) {
64 
65  driving_side[0] = (char) tolower(driving_side[0]);
66  PGR_DBG("driving side:%c",driving_side[0]);
67  if (! ((driving_side[0] == 'r')
68  || (driving_side[0] == 'l'))) {
69  driving_side[0] = 'b';
70  }
71 
73 
74  Point_on_edge_t *points = NULL;
75  size_t total_points = 0;
76  pgr_get_points(points_sql, &points, &total_points);
77 
78  char *edges_of_points_query = NULL;
79  char *edges_no_points_query = NULL;
81  edges_sql, points_sql,
82  &edges_of_points_query,
83  &edges_no_points_query);
84 
85 
86  pgr_edge_t *edges_of_points = NULL;
87  size_t total_edges_of_points = 0;
88  pgr_get_edges(edges_of_points_query, &edges_of_points, &total_edges_of_points);
89 
90  pgr_edge_t *edges = NULL;
91  size_t total_edges = 0;
92  pgr_get_edges(edges_no_points_query, &edges, &total_edges);
93 
94  PGR_DBG("freeing allocated memory not used anymore");
95  free(edges_of_points_query);
96  free(edges_no_points_query);
97 
98  if ((total_edges + total_edges_of_points) == 0) {
99  PGR_DBG("No edges found");
100  (*result_count) = 0;
101  (*result_tuples) = NULL;
102  pgr_SPI_finish();
103  return;
104  }
105 
106  PGR_DBG("Starting processing");
107  clock_t start_t = clock();
108 
109  char *log_msg = NULL;
110  char *notice_msg = NULL;
111  char *err_msg = NULL;
113  edges,
114  total_edges,
115  points,
116  total_points,
117  edges_of_points,
118  total_edges_of_points,
119  start_pid,
120  end_pid,
121  k,
122 
123  directed,
124  heap_paths,
125  driving_side[0],
126  details,
127 
128  result_tuples,
129  result_count,
130 
131  &log_msg,
132  &notice_msg,
133  &err_msg);
134  time_msg(" processing withPointsKSP", start_t, clock());
135 
136  PGR_DBG("Returned message = %s\n", err_msg);
137 
138  if (err_msg) {
139  if (*result_tuples) free(*result_tuples);
140  }
141  pgr_global_report(log_msg, notice_msg, err_msg);
142 
143  pfree(edges);
144  pfree(edges_of_points);
145  pfree(points);
146 
147  pgr_SPI_finish();
148 
149 }
150 
151 /* */
152 /*******************************************************************************/
153 
154 
155 
156 PGDLLEXPORT Datum withPoints_ksp(PG_FUNCTION_ARGS) {
157  FuncCallContext *funcctx;
158  TupleDesc tuple_desc;
159 
160  /*******************************************************************************/
161  /* MODIFY AS NEEDED */
162  /* */
163  General_path_element_t *result_tuples = 0;
164  size_t result_count = 0;
165  /* */
166  /*******************************************************************************/
167 
168  if (SRF_IS_FIRSTCALL()) {
169  MemoryContext oldcontext;
170  funcctx = SRF_FIRSTCALL_INIT();
171  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
172 
173 
174  /*******************************************************************************/
175  /* MODIFY AS NEEDED */
176  // CREATE OR REPLACE FUNCTION pgr_withPoint(
177  // edges_sql TEXT,
178  // points_sql TEXT,
179  // start_pid INTEGER,
180  // end_pid BIGINT,
181  // k BIGINT,
182  //
183  // directed BOOLEAN -- DEFAULT true,
184  // heap_paths BOOLEAN -- DEFAULT false,
185  // driving_side CHAR -- DEFAULT 'b',
186  // details BOOLEAN -- DEFAULT false,
187 
188  PGR_DBG("Calling process");
189  PGR_DBG("initial driving side:%s", text_to_cstring(PG_GETARG_TEXT_P(7)));
190  process(
191  text_to_cstring(PG_GETARG_TEXT_P(0)),
192  text_to_cstring(PG_GETARG_TEXT_P(1)),
193  PG_GETARG_INT64(2),
194  PG_GETARG_INT64(3),
195  PG_GETARG_INT32(4),
196  PG_GETARG_BOOL(5),
197  PG_GETARG_BOOL(6),
198  text_to_cstring(PG_GETARG_TEXT_P(7)),
199  PG_GETARG_BOOL(8),
200  &result_tuples,
201  &result_count);
202 
203  /* */
204  /*******************************************************************************/
205 
206 #if PGSQL_VERSION > 95
207  funcctx->max_calls = result_count;
208 #else
209  funcctx->max_calls = (uint32_t)result_count;
210 #endif
211  funcctx->user_fctx = result_tuples;
212  if (get_call_result_type(fcinfo, NULL, &tuple_desc) != TYPEFUNC_COMPOSITE)
213  ereport(ERROR,
214  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
215  errmsg("function returning record called in context "
216  "that cannot accept type record")));
217 
218  funcctx->tuple_desc = tuple_desc;
219  MemoryContextSwitchTo(oldcontext);
220  }
221 
222  funcctx = SRF_PERCALL_SETUP();
223  tuple_desc = funcctx->tuple_desc;
224  result_tuples = (General_path_element_t*) funcctx->user_fctx;
225 
226  if (funcctx->call_cntr < funcctx->max_calls) {
227  HeapTuple tuple;
228  Datum result;
229  Datum *values;
230  bool* nulls;
231 
232  /*******************************************************************************/
233  /* MODIFY AS NEEDED */
234  values = palloc(7 * sizeof(Datum));
235  nulls = palloc(7 * sizeof(bool));
236 
237  size_t i;
238  for(i = 0; i < 7; ++i) {
239  nulls[i] = false;
240  }
241 
242  /*
243  OUT seq INTEGER, OUT path_id INTEGER, OUT path_seq INTEGER,
244  OUT node BIGINT, OUT edge BIGINT,
245  OUT cost FLOAT, OUT agg_cost FLOAT)
246  */
247 
248 
249  // postgres starts counting from 1
250  values[0] = Int32GetDatum(funcctx->call_cntr + 1);
251  values[1] = Int32GetDatum((int)(result_tuples[funcctx->call_cntr].start_id + 1));
252  values[2] = Int32GetDatum(result_tuples[funcctx->call_cntr].seq);
253  values[3] = Int64GetDatum(result_tuples[funcctx->call_cntr].node);
254  values[4] = Int64GetDatum(result_tuples[funcctx->call_cntr].edge);
255  values[5] = Float8GetDatum(result_tuples[funcctx->call_cntr].cost);
256  values[6] = Float8GetDatum(result_tuples[funcctx->call_cntr].agg_cost);
257  /*******************************************************************************/
258 
259  tuple =heap_form_tuple(tuple_desc, values, nulls);
260  result = HeapTupleGetDatum(tuple);
261  SRF_RETURN_NEXT(funcctx, result);
262  } else {
263  SRF_RETURN_DONE(funcctx);
264  }
265 }
266 
PG_FUNCTION_INFO_V1(withPoints_ksp)
#define PGR_DBG(...)
Definition: debug_macro.h:34
void pgr_get_edges(char *edges_sql, pgr_edge_t **edges, size_t *total_edges)
basic edge_sql
Definition: edges_input.c:539
void pgr_get_points(char *points_sql, Point_on_edge_t **points, size_t *total_points)
pgr_get_points
Definition: points_input.c:62
PGDLLEXPORT Datum withPoints_ksp(PG_FUNCTION_ARGS)
void time_msg(char *msg, clock_t start_t, clock_t end_t)
Definition: time_msg.c:32
int do_pgr_withPointsKsp(pgr_edge_t *edges, size_t total_edges, Point_on_edge_t *points_p, size_t total_points, pgr_edge_t *edges_of_points, size_t total_edges_of_points, int64_t start_pid, int64_t end_pid, int k, bool directed, bool heap_paths, char driving_side, bool details, General_path_element_t **return_tuples, size_t *return_count, char **log_msg, char **notice_msg, char **err_msg)
void pgr_global_report(char *log, char *notice, char *err)
notice & error
Definition: e_report.c:93
void pgr_SPI_finish(void)
void get_new_queries(char *edges_sql, char *points_sql, char **edges_of_points_query, char **edges_no_points_query)
edge_astar_t * edges
Definition: BDATester.cpp:46
void pgr_SPI_connect(void)
char * err_msg
Definition: BDATester.cpp:50
static void process(char *edges_sql, char *points_sql, int64_t start_pid, int64_t end_pid, int k, bool directed, bool heap_paths, char *driving_side, bool details, General_path_element_t **result_tuples, size_t *result_count)