PGROUTING  2.5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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 **ksp_path, size_t *path_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 **  ksp_path,
size_t *  path_count 
)
static

Definition at line 39 of file ksp.c.

References do_pgr_ksp(), edges, 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  ksp_path,
86  path_count,
87  &log_msg,
88  &notice_msg,
89  &err_msg);
90  time_msg(" processing KSP", start_t, clock());
91  PGR_DBG("total tuples found %ld\n", *path_count);
92 
93  if (err_msg) {
94  if (*ksp_path) free(*ksp_path);
95  }
96  pgr_global_report(log_msg, notice_msg, err_msg);
97 
98  pfree(edges);
100 }
static edge_t edges[22573]
#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:540
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 104 of file ksp.c.

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

104  {
105  FuncCallContext *funcctx;
106  TupleDesc tuple_desc;
107  General_path_element_t *path = NULL;
108  size_t result_count = 0;
109 
110  /* stuff done only on the first call of the function */
111  if (SRF_IS_FIRSTCALL()) {
112  MemoryContext oldcontext;
113  funcctx = SRF_FIRSTCALL_INIT();
114  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
115 
116 
117  /*
118  CREATE OR REPLACE FUNCTION _pgr_ksp(
119  sql text,
120  start_vid bigint,
121  end_vid bigint,
122  k integer,
123  directed boolean,
124  heap_paths boolean
125  */
126  PGR_DBG("Calling process");
127  compute(
128  text_to_cstring(PG_GETARG_TEXT_P(0)), /* SQL */
129  PG_GETARG_INT64(1), /* start_vid */
130  PG_GETARG_INT64(2), /* end_vid */
131  PG_GETARG_INT32(3), /* k */
132  PG_GETARG_BOOL(4), /* directed */
133  PG_GETARG_BOOL(5), /* heap_paths */
134  &path,
135  &result_count);
136  PGR_DBG("Total number of tuples to be returned %ld \n", result_count);
137 
138  /* */
139  /**********************************************************************/
140 
141 #if PGSQL_VERSION > 95
142  funcctx->max_calls = result_count;
143 #else
144  funcctx->max_calls = (uint32_t)result_count;
145 #endif
146  funcctx->user_fctx = path;
147  if (get_call_result_type(fcinfo, NULL, &tuple_desc) != TYPEFUNC_COMPOSITE)
148  ereport(ERROR,
149  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
150  errmsg("function returning record called in context "
151  "that cannot accept type record\n")));
152 
153  funcctx->tuple_desc = tuple_desc;
154  MemoryContextSwitchTo(oldcontext);
155  }
156 
157 
158  funcctx = SRF_PERCALL_SETUP();
159 
160 
161  tuple_desc = funcctx->tuple_desc;
162  path = (General_path_element_t*) funcctx->user_fctx;
163 
164  if (funcctx->call_cntr < funcctx->max_calls) { /* do when there is more left to send */
165  HeapTuple tuple;
166  Datum result;
167  Datum *values;
168  bool* nulls;
169 
170  values = palloc(7 * sizeof(Datum));
171  nulls = palloc(7 * sizeof(bool));
172 
173 
174  size_t i;
175  for (i = 0; i < 7; ++i) {
176  nulls[i] = false;
177  }
178 
179  values[0] = Int32GetDatum(funcctx->call_cntr + 1);
180  values[1] = Int32GetDatum(path[funcctx->call_cntr].start_id + 1);
181  values[2] = Int32GetDatum(path[funcctx->call_cntr].seq);
182  values[3] = Int64GetDatum(path[funcctx->call_cntr].node);
183  values[4] = Int64GetDatum(path[funcctx->call_cntr].edge);
184  values[5] = Float8GetDatum(path[funcctx->call_cntr].cost);
185  values[6] = Float8GetDatum(path[funcctx->call_cntr].agg_cost);
186 
187  tuple = heap_form_tuple(tuple_desc, values, nulls);
188  result = HeapTupleGetDatum(tuple);
189  SRF_RETURN_NEXT(funcctx, result);
190  } else { /* do when there is no more left */
191  SRF_RETURN_DONE(funcctx);
192  }
193 }
#define PGR_DBG(...)
Definition: debug_macro.h:34
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:39

Here is the call graph for this function:

PG_FUNCTION_INFO_V1 ( kshortest_path  )