PGROUTING  2.6
lineGraph.c File Reference

Connecting code with postgres. More...

Include dependency graph for lineGraph.c:

Go to the source code of this file.

Functions

PGDLLEXPORT Datum lineGraph (PG_FUNCTION_ARGS)
 postgres_connection.h More...
 
 PG_FUNCTION_INFO_V1 (lineGraph)
 
static void process (char *edges_sql, bool directed, Line_graph_rt **result_tuples, size_t *result_count)
 

Detailed Description

Connecting code with postgres.

This file is fully documented for understanding how the postgres connectinon works

TODO Remove unnecessary comments before submiting the function. some comments are in form of PGR_DBG message

Definition in file lineGraph.c.

Function Documentation

PGDLLEXPORT Datum lineGraph ( PG_FUNCTION_ARGS  )

postgres_connection.h

  • should always be first in the C code

Definition at line 132 of file lineGraph.c.

References Line_graph_rt::cost, if(), PGR_DBG, process(), Line_graph_rt::reverse_cost, Line_graph_rt::source, and Line_graph_rt::target.

132  {
133  FuncCallContext *funcctx;
134  TupleDesc tuple_desc;
135 
136  /**************************************************************************/
137  /* MODIFY AS NEEDED */
138  /* */
139  Line_graph_rt *result_tuples = NULL;
140  size_t result_count = 0;
141  /* */
142  /**************************************************************************/
143 
144  if (SRF_IS_FIRSTCALL()) {
145  MemoryContext oldcontext;
146  funcctx = SRF_FIRSTCALL_INIT();
147  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
148 
149 
150  /**********************************************************************/
151  /* MODIFY AS NEEDED */
152  /*
153  TEXT,
154  directed BOOLEAN DEFAULT true,
155  **********************************************************************/
156 
157 
158  PGR_DBG("Calling process");
159  process(
160  text_to_cstring(PG_GETARG_TEXT_P(0)),
161  PG_GETARG_BOOL(1),
162  &result_tuples,
163  &result_count);
164 
165 
166  /* */
167  /**********************************************************************/
168 
169 #if PGSQL_VERSION > 95
170  funcctx->max_calls = result_count;
171 #else
172  funcctx->max_calls = (uint32_t)result_count;
173 #endif
174  funcctx->user_fctx = result_tuples;
175  if (get_call_result_type(fcinfo, NULL, &tuple_desc)
176  != TYPEFUNC_COMPOSITE) {
177  ereport(ERROR,
178  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
179  errmsg("function returning record called in context "
180  "that cannot accept type record")));
181  }
182 
183  funcctx->tuple_desc = tuple_desc;
184  MemoryContextSwitchTo(oldcontext);
185  }
186 
187  funcctx = SRF_PERCALL_SETUP();
188  tuple_desc = funcctx->tuple_desc;
189  result_tuples = (Line_graph_rt*) funcctx->user_fctx;
190 
191  if (funcctx->call_cntr < funcctx->max_calls) {
192  HeapTuple tuple;
193  Datum result;
194  Datum *values;
195  bool* nulls;
196 
197  values = palloc(5 * sizeof(Datum));
198  nulls = palloc(5 * sizeof(bool));
199 
200 
201  size_t i;
202  for (i = 0; i < 5; ++i) {
203  nulls[i] = false;
204  }
205 
206  // postgres starts counting from 1
207  values[0] = Int32GetDatum(funcctx->call_cntr + 1);
208  values[1] = Int64GetDatum(result_tuples[funcctx->call_cntr].source);
209  values[2] = Int64GetDatum(result_tuples[funcctx->call_cntr].target);
210  values[3] = Float8GetDatum(result_tuples[funcctx->call_cntr].cost);
211  values[4] = Float8GetDatum(result_tuples[
212  funcctx->call_cntr].reverse_cost);
213 
214  tuple = heap_form_tuple(tuple_desc, values, nulls);
215  result = HeapTupleGetDatum(tuple);
216  SRF_RETURN_NEXT(funcctx, result);
217  } else {
218  /**********************************************************************/
219  /* MODIFY AS NEEDED */
220 
221  PGR_DBG("Clean up code");
222 
223  /**********************************************************************/
224 
225  SRF_RETURN_DONE(funcctx);
226  }
227 }
int64_t source
Definition: line_graph_rt.h:65
#define PGR_DBG(...)
Definition: debug_macro.h:34
static void process(char *edges_sql, bool directed, Line_graph_rt **result_tuples, size_t *result_count)
Definition: lineGraph.c:67
double reverse_cost
Definition: line_graph_rt.h:68
if(DOXYGEN_FOUND) configure_file($
Definition: CMakeLists.txt:13
int64_t target
Definition: line_graph_rt.h:66

Here is the call graph for this function:

PG_FUNCTION_INFO_V1 ( lineGraph  )
static void process ( char *  edges_sql,
bool  directed,
Line_graph_rt **  result_tuples,
size_t *  result_count 
)
static

Definition at line 67 of file lineGraph.c.

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

Referenced by lineGraph().

71  {
72  /*
73  * https://www.postgresql.org/docs/current/static/spi-spi-connect.html
74  */
75  PGR_DBG("\nSQL QUERY: %s\n", edges_sql);
76  if (directed) {
77  PGR_DBG("\nDirectedGraph\n");
78  } else {
79  PGR_DBG("\nUndirectedGraph\n");
80  }
82 
83  (*result_tuples) = NULL;
84  (*result_count) = 0;
85 
86  PGR_DBG("Load data");
87  pgr_edge_t *edges = NULL;
88  size_t total_edges = 0;
89 
90  pgr_get_edges(edges_sql, &edges, &total_edges);
91  PGR_DBG("Total %ld edges in query:", total_edges);
92 
93  if (total_edges == 0) {
94  PGR_DBG("No edges found");
96  return;
97  }
98 
99  PGR_DBG("Starting processing");
100  clock_t start_t = clock();
101  char *log_msg = NULL;
102  char *notice_msg = NULL;
103  char *err_msg = NULL;
105  edges,
106  total_edges,
107  directed,
108  result_tuples,
109  result_count,
110  &log_msg,
111  &notice_msg,
112  &err_msg);
113 
114  time_msg(" processing pgr_lineGraph", start_t, clock());
115  PGR_DBG("Returning %ld tuples", *result_count);
116 
117  if (err_msg) {
118  if (*result_tuples) pfree(*result_tuples);
119  }
120  pgr_global_report(log_msg, notice_msg, err_msg);
121 
122  if (edges) pfree(edges);
123  if (log_msg) pfree(log_msg);
124  if (notice_msg) pfree(notice_msg);
125  if (err_msg) pfree(err_msg);
126 
127  pgr_SPI_finish();
128 }
#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 pgr_SPI_connect(void)
void do_pgr_lineGraph(pgr_edge_t *data_edges, size_t total_edges, bool directed, Line_graph_rt **return_tuples, size_t *return_count, char **log_msg, char **notice_msg, char **err_msg)

Here is the call graph for this function:

Here is the caller graph for this function: