PGROUTING  3.2
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 _pgr_linegraph (PG_FUNCTION_ARGS)
 postgres_connection.h More...
 
 PG_FUNCTION_INFO_V1 (_pgr_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

◆ _pgr_linegraph()

PGDLLEXPORT Datum _pgr_linegraph ( PG_FUNCTION_ARGS  )

postgres_connection.h

  • should always be first in the C code

Definition at line 133 of file lineGraph.c.

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

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

◆ PG_FUNCTION_INFO_V1()

PG_FUNCTION_INFO_V1 ( _pgr_linegraph  )

◆ process()

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

Definition at line 68 of file lineGraph.c.

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

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

Referenced by _pgr_linegraph().

do_pgr_lineGraph
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)
Definition: lineGraph_driver.cpp:60
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
Line_graph_rt
Definition: line_graph_rt.h:42
PGR_DBG
#define PGR_DBG(...)
Definition: debug_macro.h:34
process
static void process(char *edges_sql, bool directed, Line_graph_rt **result_tuples, size_t *result_count)
Definition: lineGraph.c:68
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
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
pgr_global_report
void pgr_global_report(char *log, char *notice, char *err)
notice & error
Definition: e_report.c:93