PGROUTING  2.4
 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 "./../../common/src/postgres_connection.h"
26 
27 #include "./../../common/src/debug_macro.h"
28 #include "./../../common/src/e_report.h"
29 #include "./../../common/src/time_msg.h"
30 #include "./../../common/src/pgr_types.h"
31 #include "./../../common/src/postgres_connection.h"
32 #include "./../../common/src/edges_input.h"
33 
34 #include "./ksp_driver.h"
35 
36 PGDLLEXPORT Datum kshortest_path(PG_FUNCTION_ARGS);
38 
39 static
40 void compute(
41  char* edges_sql,
42  int64_t start_vertex,
43  int64_t end_vertex,
44  int k,
45  bool directed,
46  bool heap_paths,
47  General_path_element_t **ksp_path, size_t *path_count) {
49 
50  PGR_DBG("Load data");
51  pgr_edge_t *edges = NULL;
52  size_t total_edges = 0;
53 
54 
55  if (start_vertex == end_vertex) {
57  return;
58  }
59 
60  pgr_get_edges(edges_sql, &edges, &total_edges);
61  PGR_DBG("Total %ld edges in query:", total_edges);
62 
63  if (total_edges == 0) {
64  PGR_DBG("No edges found");
66  return;
67  }
68 
69 
70  PGR_DBG("Calling do_pgr_ksp\n");
71  PGR_DBG("heap_paths = %i\n", heap_paths);
72 
73  clock_t start_t = clock();
74  char *log_msg = NULL;
75  char *notice_msg = NULL;
76  char *err_msg = NULL;
77 
78  do_pgr_ksp(
79  edges,
80  total_edges,
81  start_vertex,
82  end_vertex,
83  k,
84  directed,
85  heap_paths,
86  ksp_path,
87  path_count,
88  &log_msg,
89  &notice_msg,
90  &err_msg);
91  time_msg(" processing KSP", start_t, clock());
92  PGR_DBG("total tuples found %ld\n", *path_count);
93 
94  if (err_msg) {
95  if (*ksp_path) free(*ksp_path);
96  }
97  pgr_global_report(log_msg, notice_msg, err_msg);
98 
99  pfree(edges);
100  pgr_SPI_finish();
101 }
102 
103 
104 PGDLLEXPORT Datum
105 kshortest_path(PG_FUNCTION_ARGS) {
106  FuncCallContext *funcctx;
107  TupleDesc tuple_desc;
109  size_t result_count = 0;
110 
111  /* stuff done only on the first call of the function */
112  if (SRF_IS_FIRSTCALL()) {
113  MemoryContext oldcontext;
114  funcctx = SRF_FIRSTCALL_INIT();
115  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
116 
117 
118  /*
119  CREATE OR REPLACE FUNCTION _pgr_ksp(
120  sql text,
121  start_vid bigint,
122  end_vid bigint,
123  k integer,
124  directed boolean,
125  heap_paths boolean
126  */
127  PGR_DBG("Calling process");
128  compute(
129  text_to_cstring(PG_GETARG_TEXT_P(0)), /* SQL */
130  PG_GETARG_INT64(1), /* start_vid */
131  PG_GETARG_INT64(2), /* end_vid */
132  PG_GETARG_INT32(3), /* k */
133  PG_GETARG_BOOL(4), /* directed */
134  PG_GETARG_BOOL(5), /* heap_paths */
135  &path,
136  &result_count);
137  PGR_DBG("Total number of tuples to be returned %ld \n", result_count);
138 
139  /* */
140  /**********************************************************************/
141 
142 #if PGSQL_VERSION > 95
143  funcctx->max_calls = result_count;
144 #else
145  funcctx->max_calls = (uint32_t)result_count;
146 #endif
147  funcctx->user_fctx = path;
148  if (get_call_result_type(fcinfo, NULL, &tuple_desc) != TYPEFUNC_COMPOSITE)
149  ereport(ERROR,
150  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
151  errmsg("function returning record called in context "
152  "that cannot accept type record\n")));
153 
154  funcctx->tuple_desc = tuple_desc;
155  MemoryContextSwitchTo(oldcontext);
156  }
157 
158 
159  funcctx = SRF_PERCALL_SETUP();
160 
161 
162  tuple_desc = funcctx->tuple_desc;
163  path = (General_path_element_t*) funcctx->user_fctx;
164 
165  if (funcctx->call_cntr < funcctx->max_calls) { /* do when there is more left to send */
166  HeapTuple tuple;
167  Datum result;
168  Datum *values;
169  bool* nulls;
170 
171  values = palloc(7 * sizeof(Datum));
172  nulls = palloc(7 * sizeof(bool));
173 
174 
175  size_t i;
176  for (i = 0; i < 7; ++i) {
177  nulls[i] = false;
178  }
179 
180  values[0] = Int32GetDatum(funcctx->call_cntr + 1);
181  values[1] = Int32GetDatum(path[funcctx->call_cntr].start_id + 1);
182  values[2] = Int32GetDatum(path[funcctx->call_cntr].seq);
183  values[3] = Int64GetDatum(path[funcctx->call_cntr].node);
184  values[4] = Int64GetDatum(path[funcctx->call_cntr].edge);
185  values[5] = Float8GetDatum(path[funcctx->call_cntr].cost);
186  values[6] = Float8GetDatum(path[funcctx->call_cntr].agg_cost);
187 
188  tuple = heap_form_tuple(tuple_desc, values, nulls);
189  result = HeapTupleGetDatum(tuple);
190  SRF_RETURN_NEXT(funcctx, result);
191  } else { /* do when there is no more left */
192  SRF_RETURN_DONE(funcctx);
193  }
194 }
int path_count
Definition: BDATester.cpp:51
#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 time_msg(char *msg, clock_t start_t, clock_t end_t)
Definition: time_msg.c:32
static void compute(char *edges_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:40
void pgr_global_report(char *log, char *notice, char *err)
notice & error
Definition: e_report.c:93
void pgr_SPI_finish(void)
edge_astar_t * edges
Definition: BDATester.cpp:46
void do_pgr_ksp(pgr_edge_t *data_edges, size_t total_edges, int64_t start_vid, int64_t end_vid, int k, bool directed, bool heap_paths, General_path_element_t **return_tuples, size_t *return_count, char **log_msg, char **notice_msg, char **err_msg)
Definition: ksp_driver.cpp:42
void pgr_SPI_connect(void)
PGDLLEXPORT Datum kshortest_path(PG_FUNCTION_ARGS)
Definition: ksp.c:105
path_element_t * path
Definition: BDATester.cpp:49
char * err_msg
Definition: BDATester.cpp:50
PG_FUNCTION_INFO_V1(kshortest_path)