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