pgRouting  2.2
pgRouting extends the PostGIS / PostgreSQL geospatial database to provide geospatial routing functionality.
 All Classes Functions Variables Pages
ksp.c
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 "./../../common/src/pgr_types.h"
26 #include "postgres.h"
27 #include "executor/spi.h"
28 #include "funcapi.h"
29 #include "catalog/pg_type.h"
30 #include "fmgr.h"
31 #if PGSQL_VERSION > 92
32 #include "access/htup_details.h"
33 #endif
34 
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 
42 PG_FUNCTION_INFO_V1(kshortest_path);
43 #ifndef _MSC_VER
44 Datum
45 #else // _MSC_VER
46 PGDLLEXPORT Datum
47 #endif // _MSC_VER
48 kshortest_path(PG_FUNCTION_ARGS);
49 
50 
51 static
52 void compute(char* sql, int64_t start_vertex,
53  int64_t end_vertex, int k,
54  bool directed,
55  bool heap_paths,
56  General_path_element_t **ksp_path, size_t *path_count) {
57  pgr_SPI_connect();
58  pgr_edge_t *edges = NULL;
59  size_t total_tuples = 0;
60 
61  char *err_msg = NULL;
62 
63  if (start_vertex == end_vertex) {
64  (*path_count) = 0;
65  *ksp_path = NULL;
66  pgr_SPI_finish();
67  return;
68  }
69 
70  PGR_DBG("Load data");
71  pgr_get_data_5_columns(sql, &edges, &total_tuples);
72 
73  PGR_DBG("Total %ld tuples in query:", total_tuples);
74  PGR_DBG("Calling do_pgr_ksp\n");
75  PGR_DBG("heap_paths = %i\n", heap_paths);
76 
77  clock_t start_t = clock();
78  int errcode = do_pgr_ksp(edges, total_tuples,
79  start_vertex, end_vertex,
80  k, directed, heap_paths,
81  ksp_path, path_count, &err_msg);
82  time_msg(" processing KSP", start_t, clock());
83 
84  PGR_DBG("total tuples found %ld\n", *path_count);
85  PGR_DBG("Exist Status = %i\n", ret);
86  PGR_DBG("Returned message = %s\n", err_msg);
87 
88 
89 
90  if (err_msg) free(err_msg);
91  pfree(edges);
92  pgr_SPI_finish();
93 
94  if (errcode) {
95  pgr_send_error(errcode);
96  }
97 }
98 
99 
100 #ifndef _MSC_VER
101 Datum
102 #else // _MSC_VER
103 PGDLLEXPORT Datum
104 #endif // _MSC_VER
105 kshortest_path(PG_FUNCTION_ARGS) {
106  FuncCallContext *funcctx;
107  uint32_t call_cntr;
108  uint32_t max_calls;
109  TupleDesc tuple_desc;
111  // void * toDel;
112 
113  /* stuff done only on the first call of the function */
114  if (SRF_IS_FIRSTCALL()) {
115  MemoryContext oldcontext;
116  size_t path_count = 0;
117  path = NULL;
118 
119  /* create a function context for cross-call persistence */
120  funcctx = SRF_FIRSTCALL_INIT();
121 
122  /* switch to memory context appropriate for multiple function calls */
123  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
124 
125 
126  // CREATE OR REPLACE FUNCTION _pgr_ksp(sql text, start_vid bigint, end_vid bigint, k integer, directed boolean, heap_paths boolean...
127  compute(
128  pgr_text2char(PG_GETARG_TEXT_P(0)), /* SQL */
129  PG_GETARG_INT64(1), /* start_vid */
130  PG_GETARG_INT64(2), /* end_vid */
131  PG_GETARG_INT32(3), /* k */
132  PG_GETARG_BOOL(4), /* directed */
133  PG_GETARG_BOOL(5), /* heap_paths */
134  &path,
135  &path_count);
136  // toDel = path;
137 
138  PGR_DBG("Total number of tuples to be returned %ld \n", path_count);
139 
140  /* total number of tuples to be returned */
141  funcctx->max_calls = (uint32_t)path_count;
142  funcctx->user_fctx = path;
143 
144  /* Build a tuple descriptor for our result type */
145  if (get_call_result_type(fcinfo, NULL, &tuple_desc) != TYPEFUNC_COMPOSITE)
146  ereport(ERROR,
147  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
148  errmsg("function returning record called in context "
149  "that cannot accept type record\n")));
150 
151  // funcctx->tuple_desc = BlessTupleDesc(RelationNameGetTupleDesc("__pgr_2i3b2f"));
152  funcctx->tuple_desc = tuple_desc;
153  MemoryContextSwitchTo(oldcontext);
154  }
155 
156 
157  /* stuff done on every call of the function */
158  funcctx = SRF_PERCALL_SETUP();
159 
160  call_cntr = funcctx->call_cntr;
161  max_calls = funcctx->max_calls;
162  tuple_desc = funcctx->tuple_desc;
163  path = (General_path_element_t*) funcctx->user_fctx;
164 
165  if (call_cntr < max_calls) { /* do when there is more left to send */
166  PGR_DBG("returning %ld \n", call_cntr);
167  HeapTuple tuple;
168  Datum result;
169  /* //Datum values[4];
170  //bool nulls[4]; */
171 
172  Datum *values;
173  bool* nulls;
174 
175  values = (Datum *)palloc(7 * sizeof(Datum));
176  nulls = (bool *) palloc(7 * sizeof(bool));
177 
178  nulls[0] = false;
179  nulls[1] = false;
180  nulls[2] = false;
181  nulls[3] = false;
182  nulls[4] = false;
183  nulls[5] = false;
184  nulls[6] = false;
185 
186  values[0] = Int32GetDatum(call_cntr + 1);
187  values[1] = Int32GetDatum(path[call_cntr].start_id + 1);
188  values[2] = Int32GetDatum(path[call_cntr].seq);
189  values[3] = Int64GetDatum(path[call_cntr].node);
190  values[4] = Int64GetDatum(path[call_cntr].edge);
191  values[5] = Float8GetDatum(path[call_cntr].cost);
192  values[6] = Float8GetDatum(path[call_cntr].agg_cost);
193 
194  tuple = heap_form_tuple(tuple_desc, values, nulls);
195 
196  /* make the tuple into a datum */
197  result = HeapTupleGetDatum(tuple);
198 
199  /* clean up (this is not really necessary) */
200  pfree(values);
201  pfree(nulls);
202 
203  SRF_RETURN_NEXT(funcctx, result);
204  } else { /* do when there is no more left */
205  if (path == (General_path_element_t *) NULL) free(path);
206  SRF_RETURN_DONE(funcctx);
207  }
208 }
209 
210