PGROUTING  3.2
ksp.c File Reference
#include <stdbool.h>
#include "c_common/postgres_connection.h"
#include "c_common/debug_macro.h"
#include "c_common/e_report.h"
#include "c_common/time_msg.h"
#include "c_common/edges_input.h"
#include "drivers/yen/ksp_driver.h"
Include dependency graph for ksp.c:

Go to the source code of this file.

Functions

PGDLLEXPORT Datum _pgr_ksp (PG_FUNCTION_ARGS)
 
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)
 
 PG_FUNCTION_INFO_V1 (_pgr_ksp)
 

Function Documentation

◆ _pgr_ksp()

PGDLLEXPORT Datum _pgr_ksp ( PG_FUNCTION_ARGS  )

Definition at line 120 of file ksp.c.

120  {
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 }

References General_path_element_t::agg_cost, compute(), General_path_element_t::cost, General_path_element_t::edge, if(), General_path_element_t::node, PGR_DBG, General_path_element_t::seq, and General_path_element_t::start_id.

◆ 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 
)
static

Definition at line 40 of file ksp.c.

48  {
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 }

References do_pgr_ksp(), PGR_DBG, pgr_get_edges(), pgr_global_report(), pgr_SPI_connect(), pgr_SPI_finish(), and time_msg().

Referenced by _pgr_ksp().

◆ PG_FUNCTION_INFO_V1()

PG_FUNCTION_INFO_V1 ( _pgr_ksp  )
pgr_edge_t
Definition: pgr_edge_t.h:37
pgr_SPI_connect
void pgr_SPI_connect(void)
Definition: postgres_connection.c:82
pgr_SPI_finish
void pgr_SPI_finish(void)
Definition: postgres_connection.c:71
PGR_DBG
#define PGR_DBG(...)
Definition: debug_macro.h:34
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
pgr_global_report
void pgr_global_report(char *log, char *notice, char *err)
notice & error
Definition: e_report.c:93
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