PGROUTING  2.5
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 129 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.

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

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(), edges, 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) PGR_DBG("\nDirectedGraph\n");
77  else PGR_DBG("\nUndirectedGraph\n");
79 
80  (*result_tuples) = NULL;
81  (*result_count) = 0;
82 
83  PGR_DBG("Load data");
84  pgr_edge_t *edges = NULL;
85  size_t total_edges = 0;
86 
87  pgr_get_edges(edges_sql, &edges, &total_edges);
88  PGR_DBG("Total %ld edges in query:", total_edges);
89 
90  if (total_edges == 0) {
91  PGR_DBG("No edges found");
93  return;
94  }
95 
96  PGR_DBG("Starting processing");
97  clock_t start_t = clock();
98  char *log_msg = NULL;
99  char *notice_msg = NULL;
100  char *err_msg = NULL;
102  edges,
103  total_edges,
104  directed,
105  result_tuples,
106  result_count,
107  &log_msg,
108  &notice_msg,
109  &err_msg);
110 
111  time_msg(" processing pgr_lineGraph", start_t, clock());
112  PGR_DBG("Returning %ld tuples", *result_count);
113 
114  if (err_msg) {
115  if (*result_tuples) pfree(*result_tuples);
116  }
117  pgr_global_report(log_msg, notice_msg, err_msg);
118 
119  if (edges) pfree(edges);
120  if (log_msg) pfree(log_msg);
121  if (notice_msg) pfree(notice_msg);
122  if (err_msg) pfree(err_msg);
123 
124  pgr_SPI_finish();
125 }
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 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: