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