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
ksp.c
Go to the documentation of this file.
1 /*PGR-GNU*****************************************************************
2 File: ksp.c
3 
4 Copyright (c) 2015 Celia Virginia Vergara Castillo
5 vicky_vergara@hotmail.com
6 
7 ------
8 
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
13 
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18 
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 
23 ********************************************************************PGR-GNU*/
24 
25 #include "postgres.h"
26 #include "executor/spi.h"
27 #include "funcapi.h"
28 #include "catalog/pg_type.h"
29 #include "fmgr.h"
30 #if PGSQL_VERSION > 92
31 #include "access/htup_details.h"
32 #endif
33 
34 #include "./../../common/src/pgr_types.h"
35 #include "./../../common/src/debug_macro.h"
36 #include "./../../common/src/time_msg.h"
37 #include "./../../common/src/postgres_connection.h"
38 #include "./../../common/src/edges_input.h"
39 #include "./ksp_driver.h"
40 
41 PGDLLEXPORT Datum kshortest_path(PG_FUNCTION_ARGS);
42 
43 
44 static
45 void compute(char* sql, int64_t start_vertex,
46  int64_t end_vertex, int k,
47  bool directed,
48  bool heap_paths,
49  General_path_element_t **ksp_path, size_t *path_count) {
51  pgr_edge_t *edges = NULL;
52  size_t total_tuples = 0;
53 
54  char *err_msg = NULL;
55 
56  if (start_vertex == end_vertex) {
57  (*path_count) = 0;
58  *ksp_path = NULL;
60  return;
61  }
62 
63  PGR_DBG("Load data");
64  pgr_get_edges(sql, &edges, &total_tuples);
65 
66  PGR_DBG("Total %ld tuples in query:", total_tuples);
67  PGR_DBG("Calling do_pgr_ksp\n");
68  PGR_DBG("heap_paths = %i\n", heap_paths);
69 
70  clock_t start_t = clock();
71  int errcode = do_pgr_ksp(edges, total_tuples,
72  start_vertex, end_vertex,
73  k, directed, heap_paths,
74  ksp_path, path_count, &err_msg);
75  time_msg(" processing KSP", start_t, clock());
76 
77  PGR_DBG("total tuples found %ld\n", *path_count);
78  PGR_DBG("Exit Status = %i\n", errcode);
79  PGR_DBG("Returned message = %s\n", err_msg);
80 
81 
82 
83  if (err_msg) free(err_msg);
84  pfree(edges);
86 
87  if (errcode) {
88  pgr_send_error(errcode);
89  }
90 }
91 
92 
94 PGDLLEXPORT Datum
95 kshortest_path(PG_FUNCTION_ARGS) {
96  FuncCallContext *funcctx;
97  uint32_t call_cntr;
98  uint32_t max_calls;
99  TupleDesc tuple_desc;
101  // void * toDel;
102 
103  /* stuff done only on the first call of the function */
104  if (SRF_IS_FIRSTCALL()) {
105  MemoryContext oldcontext;
106  size_t path_count = 0;
107  path = NULL;
108 
109  /* create a function context for cross-call persistence */
110  funcctx = SRF_FIRSTCALL_INIT();
111 
112  /* switch to memory context appropriate for multiple function calls */
113  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
114 
115 
116  // CREATE OR REPLACE FUNCTION _pgr_ksp(sql text, start_vid bigint, end_vid bigint, k integer, directed boolean, heap_paths boolean...
117  compute(
118  pgr_text2char(PG_GETARG_TEXT_P(0)), /* SQL */
119  PG_GETARG_INT64(1), /* start_vid */
120  PG_GETARG_INT64(2), /* end_vid */
121  PG_GETARG_INT32(3), /* k */
122  PG_GETARG_BOOL(4), /* directed */
123  PG_GETARG_BOOL(5), /* heap_paths */
124  &path,
125  &path_count);
126  // toDel = path;
127 
128  PGR_DBG("Total number of tuples to be returned %ld \n", path_count);
129 
130  /* total number of tuples to be returned */
131  funcctx->max_calls = (uint32_t)path_count;
132  funcctx->user_fctx = path;
133 
134  /* Build a tuple descriptor for our result type */
135  if (get_call_result_type(fcinfo, NULL, &tuple_desc) != TYPEFUNC_COMPOSITE)
136  ereport(ERROR,
137  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
138  errmsg("function returning record called in context "
139  "that cannot accept type record\n")));
140 
141  // funcctx->tuple_desc = BlessTupleDesc(RelationNameGetTupleDesc("__pgr_2i3b2f"));
142  funcctx->tuple_desc = tuple_desc;
143  MemoryContextSwitchTo(oldcontext);
144  }
145 
146 
147  /* stuff done on every call of the function */
148  funcctx = SRF_PERCALL_SETUP();
149 
150  call_cntr = funcctx->call_cntr;
151  max_calls = funcctx->max_calls;
152  tuple_desc = funcctx->tuple_desc;
153  path = (General_path_element_t*) funcctx->user_fctx;
154 
155  if (call_cntr < max_calls) { /* do when there is more left to send */
156  PGR_DBG("returning %u \n", call_cntr);
157  HeapTuple tuple;
158  Datum result;
159  /* //Datum values[4];
160  //bool nulls[4]; */
161 
162  Datum *values;
163  bool* nulls;
164 
165  values = (Datum *)palloc(7 * sizeof(Datum));
166  nulls = (bool *) palloc(7 * sizeof(bool));
167 
168  nulls[0] = false;
169  nulls[1] = false;
170  nulls[2] = false;
171  nulls[3] = false;
172  nulls[4] = false;
173  nulls[5] = false;
174  nulls[6] = false;
175 
176  values[0] = Int32GetDatum(call_cntr + 1);
177  values[1] = Int32GetDatum(path[call_cntr].start_id + 1);
178  values[2] = Int32GetDatum(path[call_cntr].seq);
179  values[3] = Int64GetDatum(path[call_cntr].node);
180  values[4] = Int64GetDatum(path[call_cntr].edge);
181  values[5] = Float8GetDatum(path[call_cntr].cost);
182  values[6] = Float8GetDatum(path[call_cntr].agg_cost);
183 
184  tuple = heap_form_tuple(tuple_desc, values, nulls);
185 
186  /* make the tuple into a datum */
187  result = HeapTupleGetDatum(tuple);
188 
189  /* clean up (this is not really necessary) */
190  pfree(values);
191  pfree(nulls);
192 
193  SRF_RETURN_NEXT(funcctx, result);
194  } else { /* do when there is no more left */
195  if (path == (General_path_element_t *) NULL) free(path);
196  SRF_RETURN_DONE(funcctx);
197  }
198 }
199 
200 
int path_count
Definition: BDATester.cpp:51
#define PGR_DBG(...)
Definition: debug_macro.h:33
void pgr_get_edges(char *edges_sql, pgr_edge_t **edges, size_t *total_edges)
basic edge_sql
Definition: edges_input.c:535
void time_msg(char *msg, clock_t start_t, clock_t end_t)
Definition: time_msg.c:31
void pgr_SPI_finish(void)
void pgr_send_error(int errcode)
edge_astar_t * edges
Definition: BDATester.cpp:46
void pgr_SPI_connect(void)
static void compute(char *sql, int64_t start_vertex, int64_t end_vertex, int k, bool directed, bool heap_paths, General_path_element_t **ksp_path, size_t *path_count)
Definition: ksp.c:45
PGDLLEXPORT Datum kshortest_path(PG_FUNCTION_ARGS)
Definition: ksp.c:95
path_element_t * path
Definition: BDATester.cpp:49
char * err_msg
Definition: BDATester.cpp:50
int do_pgr_ksp(pgr_edge_t *data_edges, size_t total_tuples, int64_t start_vertex, int64_t end_vertex, int k, bool directedFlag, bool heap_paths, General_path_element_t **ksp_path, size_t *path_count, char **err_msg)
Definition: ksp_driver.cpp:46
PG_FUNCTION_INFO_V1(kshortest_path)
char * pgr_text2char(text *in)