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