PGROUTING  2.6
ksp.c File Reference
Include dependency graph for ksp.c:

Go to the source code of this file.

Functions

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

Function Documentation

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

Definition at line 39 of file ksp.c.

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

Referenced by kshortest_path().

46  {
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 }
#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)
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)

Here is the call graph for this function:

Here is the caller graph for this function:

PGDLLEXPORT Datum kshortest_path ( PG_FUNCTION_ARGS  )

Definition at line 113 of file ksp.c.

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.

113  {
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
if(DOXYGEN_FOUND) configure_file($
Definition: CMakeLists.txt:13
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

Here is the call graph for this function:

PG_FUNCTION_INFO_V1 ( kshortest_path  )