PGROUTING  3.2
makeConnected.c
Go to the documentation of this file.
1 /*PGR-GNU*****************************************************************
2 File: makeConnected.c
3 
4 Generated with Template by:
5 Copyright (c) 2019 pgRouting developers
7 
8 Function's developer:
9 Copyright (c) 2020 Himanshu Raj
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 
31 #include <stdbool.h>
33 #include "utils/array.h"
34 
35 #include "c_common/debug_macro.h"
36 #include "c_common/e_report.h"
37 #include "c_common/time_msg.h"
38 
39 #include "c_common/edges_input.h"
40 #include "c_common/arrays_input.h"
41 
43 PGDLLEXPORT Datum _pgr_makeconnected(PG_FUNCTION_ARGS);
45 
46 static void
48  char *edges_sql,
49 
50  pgr_makeConnected_t **result_tuples,
51  size_t *result_count) {
53 
54  PGR_DBG("Initializing arrays");
55 
56 
57  (*result_tuples) = NULL;
58  (*result_count) = 0;
59 
60  PGR_DBG("Load data");
61  pgr_edge_t *edges = NULL;
62  size_t total_edges = 0;
63 
64  pgr_get_edges(edges_sql, &edges, &total_edges);
65  PGR_DBG("Total %ld edges in query:", total_edges);
66 
67  if (total_edges == 0) {
69  return;
70  }
71 
72  PGR_DBG("Starting processing");
73  clock_t start_t = clock();
74  char *log_msg = NULL;
75  char *notice_msg = NULL;
76  char *err_msg = NULL;
78  edges,
79  total_edges,
80 
81  result_tuples,
82  result_count,
83 
84  &log_msg,
85  &notice_msg,
86  &err_msg);
87  time_msg(" processing pgr_makeConnected", start_t, clock());
88  PGR_DBG("Returning %ld tuples", *result_count);
89 
90  if (err_msg) {
91  if (*result_tuples)
92  pfree(*result_tuples);
93  }
94 
95  pgr_global_report(log_msg, notice_msg, err_msg);
96 
97  if (edges)
98  pfree(edges);
99  if (log_msg)
100  pfree(log_msg);
101  if (notice_msg)
102  pfree(notice_msg);
103  if (err_msg)
104  pfree(err_msg);
105 
106  pgr_SPI_finish();
107 }
108 
109 PGDLLEXPORT Datum _pgr_makeconnected(PG_FUNCTION_ARGS) {
110  FuncCallContext *funcctx;
111  TupleDesc tuple_desc;
112 
113  /**************************************************************************/
114  pgr_makeConnected_t *result_tuples = NULL;
115  size_t result_count = 0;
116  /**************************************************************************/
117 
118  if (SRF_IS_FIRSTCALL()) {
119  MemoryContext oldcontext;
120  funcctx = SRF_FIRSTCALL_INIT();
121  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
122 
123  /**********************************************************************/
124  /*
125  pgr_makeConnected(
126  edge_sql TEXT)
127  */
128  /**********************************************************************/
129 
130  PGR_DBG("Calling process");
131  process(
132  text_to_cstring(PG_GETARG_TEXT_P(0)),
133  &result_tuples,
134  &result_count);
135 
136  /**********************************************************************/
137 
138 #if PGSQL_VERSION > 95
139  funcctx->max_calls = result_count;
140 #else
141  funcctx->max_calls = (uint32_t)result_count;
142 #endif
143  funcctx->user_fctx = result_tuples;
144  if (get_call_result_type(fcinfo, NULL, &tuple_desc) != TYPEFUNC_COMPOSITE) {
145  ereport(ERROR,
146  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
147  errmsg("function returning record called in context "
148  "that cannot accept type record")));
149  }
150 
151  funcctx->tuple_desc = tuple_desc;
152  MemoryContextSwitchTo(oldcontext);
153  }
154 
155  funcctx = SRF_PERCALL_SETUP();
156  tuple_desc = funcctx->tuple_desc;
157  result_tuples = (pgr_makeConnected_t *)funcctx->user_fctx;
158 
159  if (funcctx->call_cntr < funcctx->max_calls) {
160  HeapTuple tuple;
161  Datum result;
162  Datum *values;
163  bool *nulls;
164 
165  /**********************************************************************/
166  /*
167  OUT start_vid BIGINT,
168  OUT end_vid BIGINT
169  */
170  /**********************************************************************/
171  size_t numb = 3;
172  values = palloc(numb * sizeof(Datum));
173  nulls = palloc(numb * sizeof(bool));
174 
175  size_t i;
176  for (i = 0; i < numb; ++i) {
177  nulls[i] = false;
178  }
179 
180  values[0] = Int32GetDatum(funcctx->call_cntr + 1);
181  values[1] = Int64GetDatum(result_tuples[funcctx->call_cntr].start_vid);
182  values[2] = Int64GetDatum(result_tuples[funcctx->call_cntr].end_vid);
183 
184  /**********************************************************************/
185 
186  tuple = heap_form_tuple(tuple_desc, values, nulls);
187  result = HeapTupleGetDatum(tuple);
188  SRF_RETURN_NEXT(funcctx, result);
189  } else {
190  /**********************************************************************/
191 
192  PGR_DBG("Clean up code");
193 
194  /**********************************************************************/
195 
196  SRF_RETURN_DONE(funcctx);
197  }
198 }
time_msg.h
_pgr_makeconnected
PGDLLEXPORT Datum _pgr_makeconnected(PG_FUNCTION_ARGS)
Definition: makeConnected.c:109
postgres_connection.h
pgr_edge_t
Definition: pgr_edge_t.h:37
process
static void process(char *edges_sql, pgr_makeConnected_t **result_tuples, size_t *result_count)
Definition: makeConnected.c:47
pgr_makeConnected_t
Definition: pgr_makeConnected_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
e_report.h
arrays_input.h
pgr_makeConnected_t::end_vid
int64_t end_vid
Definition: pgr_makeConnected_t.h:39
debug_macro.h
makeConnected_driver.h
PGR_DBG
#define PGR_DBG(...)
Definition: debug_macro.h:34
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
PG_FUNCTION_INFO_V1
PG_FUNCTION_INFO_V1(_pgr_makeconnected)
time_msg
void time_msg(char *msg, clock_t start_t, clock_t end_t)
Definition: time_msg.c:32
do_pgr_makeConnected
void do_pgr_makeConnected(pgr_edge_t *data_edges, size_t total_edges, pgr_makeConnected_t **return_tuples, size_t *return_count, char **log_msg, char **notice_msg, char **err_msg)
Definition: makeConnected_driver.cpp:43
edges_input.h
pgr_makeConnected_t::start_vid
int64_t start_vid
Definition: pgr_makeConnected_t.h:38
pgr_global_report
void pgr_global_report(char *log, char *notice, char *err)
notice & error
Definition: e_report.c:93