PGROUTING  2.5
connectedComponents.c
Go to the documentation of this file.
1 /*PGR-GNU*****************************************************************
2 File: connectedComponents.c
3 
4 Generated with Template by:
5 Copyright (c) 2015 pgRouting developers
6 Mail: project@pgrouting.org
7 
8 Function's developer:
9 Copyright (c) 2017 Celia Virginia Vergara Castillo
10 Mail: vicky_vergara@hotmail.com
11 
12 
13 ------
14 
15 This program is free software; you can redistribute it and/or modify
16 it under the terms of the GNU General Public License as published by
17 the Free Software Foundation; either version 2 of the License, or
18 (at your option) any later version.
19 
20 This program is distributed in the hope that it will be useful,
21 but WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 GNU General Public License for more details.
24 
25 You should have received a copy of the GNU General Public License
26 along with this program; if not, write to the Free Software
27 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
28 
29 ********************************************************************PGR-GNU*/
30 
47 
48 
49 /* for macro PGR_DBG */
50 #include "c_common/debug_macro.h"
51 /* for pgr_global_report */
52 #include "c_common/e_report.h"
53 /* for time_msg & clock */
54 #include "c_common/time_msg.h"
55 /* for functions to get edges information */
56 #include "c_common/edges_input.h"
57 
58 #include "drivers/components/connectedComponents_driver.h" // the link to the C++ code of the function
59 
60 PGDLLEXPORT Datum connectedComponents(PG_FUNCTION_ARGS);
62 
63 
64 /******************************************************************************/
65 /* MODIFY AS NEEDED */
66 static
67 void
69  char* edges_sql,
70  pgr_components_rt **result_tuples,
71  size_t *result_count) {
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_connectedComponents", 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 }
139 /* */
140 /******************************************************************************/
141 
142 PGDLLEXPORT Datum connectedComponents(PG_FUNCTION_ARGS) {
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(6 * sizeof(Datum));
224  nulls = palloc(6 * sizeof(bool));
225 
226 
227  size_t i;
228  for (i = 0; i < 6; ++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].component);
235  values[2] = Int32GetDatum(result_tuples[funcctx->call_cntr].n_seq);
236  values[3] = Int64GetDatum(result_tuples[funcctx->call_cntr].identifier);
237  /**********************************************************************/
238 
239  tuple = heap_form_tuple(tuple_desc, values, nulls);
240  result = HeapTupleGetDatum(tuple);
241  SRF_RETURN_NEXT(funcctx, result);
242  } else {
243  /**********************************************************************/
244  /* MODIFY AS NEEDED */
245 
246  PGR_DBG("Clean up code");
247 
248  /**********************************************************************/
249 
250  SRF_RETURN_DONE(funcctx);
251  }
252 }
253 
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
PGDLLEXPORT Datum connectedComponents(PG_FUNCTION_ARGS)
postgres_connection.h
void pgr_global_report(char *log, char *notice, char *err)
notice & error
Definition: e_report.c:93
void pgr_SPI_finish(void)
PG_FUNCTION_INFO_V1(connectedComponents)
if(DOXYGEN_FOUND) configure_file($
Definition: CMakeLists.txt:13
void pgr_SPI_connect(void)
static void process(char *edges_sql, pgr_components_rt **result_tuples, size_t *result_count)
void do_pgr_connectedComponents(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)