PGROUTING  2.5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
bridges.c File Reference

Connecting code with postgres. More...

Include dependency graph for bridges.c:

Go to the source code of this file.

Functions

PGDLLEXPORT Datum bridges (PG_FUNCTION_ARGS)
 postgres_connection.h More...
 
 PG_FUNCTION_INFO_V1 (bridges)
 
static void process (char *edges_sql, pgr_components_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 bridges.c.

Function Documentation

PGDLLEXPORT Datum bridges ( PG_FUNCTION_ARGS  )

postgres_connection.h

  • should always be first in the C code

Definition at line 142 of file bridges.c.

References pgr_components_rt::identifier, PGR_DBG, and process().

142  {
143  FuncCallContext *funcctx;
144  TupleDesc tuple_desc;
145 
146  /**************************************************************************/
147  /* MODIFY AS NEEDED */
148  /* */
149  pgr_components_rt *result_tuples = NULL;
150  size_t result_count = 0;
151  /* */
152  /**************************************************************************/
153 
154  if (SRF_IS_FIRSTCALL()) {
155  MemoryContext oldcontext;
156  funcctx = SRF_FIRSTCALL_INIT();
157  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
158 
159 
160  /**********************************************************************/
161  /* MODIFY AS NEEDED */
162  /*
163  TEXT,
164  BIGINT,
165  BIGINT,
166  **********************************************************************/
167 
168 
169  PGR_DBG("Calling process");
170  process(
171  text_to_cstring(PG_GETARG_TEXT_P(0)),
172 #if 0
173  /*
174  * handling arrays example
175  */
176 
177  PG_GETARG_ARRAYTYPE_P(1),
178  PG_GETARG_ARRAYTYPE_P(2),
179 #endif
180  &result_tuples,
181  &result_count);
182 
183  /* */
184  /**********************************************************************/
185 
186 #if PGSQL_VERSION > 94
187  funcctx->max_calls = (uint32_t)result_count;
188 #else
189  funcctx->max_calls = (uint32_t)result_count;
190 #endif
191  funcctx->user_fctx = result_tuples;
192  if (get_call_result_type(fcinfo, NULL, &tuple_desc)
193  != TYPEFUNC_COMPOSITE) {
194  ereport(ERROR,
195  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
196  errmsg("function returning record called in context "
197  "that cannot accept type record")));
198  }
199 
200  funcctx->tuple_desc = tuple_desc;
201  MemoryContextSwitchTo(oldcontext);
202  }
203 
204  funcctx = SRF_PERCALL_SETUP();
205  tuple_desc = funcctx->tuple_desc;
206  result_tuples = (pgr_components_rt*) funcctx->user_fctx;
207 
208  if (funcctx->call_cntr < funcctx->max_calls) {
209  HeapTuple tuple;
210  Datum result;
211  Datum *values;
212  bool* nulls;
213 
214  /**********************************************************************/
215  /* MODIFY AS NEEDED */
216  /*
217  OUT seq INTEGER,
218  OUT component BIGINT,
219  OUT n_seq INTEGER,
220  OUT node BIGINT
221  ***********************************************************************/
222 
223  values = palloc(2 * sizeof(Datum));
224  nulls = palloc(2 * sizeof(bool));
225 
226 
227  size_t i;
228  for (i = 0; i < 2; ++i) {
229  nulls[i] = false;
230  }
231 
232  // postgres starts counting from 1
233  values[0] = Int32GetDatum(funcctx->call_cntr + 1);
234  values[1] = Int64GetDatum(result_tuples[funcctx->call_cntr].identifier);
235  /*********************************************************************/
236 
237  tuple = heap_form_tuple(tuple_desc, values, nulls);
238  result = HeapTupleGetDatum(tuple);
239  SRF_RETURN_NEXT(funcctx, result);
240  } else {
241  /**********************************************************************/
242  /* MODIFY AS NEEDED */
243 
244  PGR_DBG("Clean up code");
245 
246  /**********************************************************************/
247 
248  SRF_RETURN_DONE(funcctx);
249  }
250 }
#define PGR_DBG(...)
Definition: debug_macro.h:34
static void process(char *edges_sql, pgr_components_rt **result_tuples, size_t *result_count)
Definition: bridges.c:68

Here is the call graph for this function:

PG_FUNCTION_INFO_V1 ( bridges  )
static void process ( char *  edges_sql,
pgr_components_rt **  result_tuples,
size_t *  result_count 
)
static

Definition at line 68 of file bridges.c.

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

Referenced by bridges().

71  {
72  /*
73  * https://www.postgresql.org/docs/current/static/spi-spi-connect.html
74  */
76 
77  (*result_tuples) = NULL;
78  (*result_count) = 0;
79 
80  PGR_DBG("Load data");
81  pgr_edge_t *edges = NULL;
82  size_t total_edges = 0;
83 
84  pgr_get_edges(edges_sql, &edges, &total_edges);
85  PGR_DBG("Total %ld edges in query:", total_edges);
86 
87  if (total_edges == 0) {
88  PGR_DBG("No edges found");
90  return;
91  }
92 
93  PGR_DBG("Starting processing");
94  clock_t start_t = clock();
95  char *log_msg = NULL;
96  char *notice_msg = NULL;
97  char *err_msg = NULL;
99  edges,
100  total_edges,
101 #if 0
102  /*
103  * handling arrays example
104  */
105 
106  start_vidsArr, size_start_vidsArr,
107  end_vidsArr, size_end_vidsArr,
108 #endif
109 
110  result_tuples,
111  result_count,
112  &log_msg,
113  &notice_msg,
114  &err_msg);
115 
116  time_msg(" processing pgr_bridges", start_t, clock());
117  PGR_DBG("Returning %ld tuples", *result_count);
118 
119  if (err_msg) {
120  if (*result_tuples) pfree(*result_tuples);
121  }
122  pgr_global_report(log_msg, notice_msg, err_msg);
123 
124  if (edges) pfree(edges);
125  if (log_msg) pfree(log_msg);
126  if (notice_msg) pfree(notice_msg);
127  if (err_msg) pfree(err_msg);
128 #if 0
129  /*
130  * handling arrays example
131  */
132 
133  if (end_vidsArr) pfree(end_vidsArr);
134  if (start_vidsArr) pfree(start_vidsArr);
135 #endif
136 
137  pgr_SPI_finish();
138 }
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_bridges(pgr_edge_t *data_edges, size_t total_edges, pgr_components_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: