PGROUTING  3.2
strongComponents.c
Go to the documentation of this file.
1 /*PGR-GNU*****************************************************************
2 File: strongComponents.c
3 
4 Generated with Template by:
5 Copyright (c) 2015 pgRouting developers
7 
8 Function's developer:
9 Copyright (c) 2017 Maoguang Wang
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 
33 #include <stdbool.h>
35 
36 
37 #include "c_common/debug_macro.h"
38 #include "c_common/e_report.h"
39 #include "c_common/time_msg.h"
40 #include "c_common/edges_input.h"
41 
43 
44 PGDLLEXPORT Datum _pgr_strongcomponents(PG_FUNCTION_ARGS);
46 
47 
48 static
49 void
51  char* edges_sql,
52  pgr_components_rt **result_tuples,
53  size_t *result_count) {
55 
56  (*result_tuples) = NULL;
57  (*result_count) = 0;
58 
59  pgr_edge_t *edges = NULL;
60  size_t total_edges = 0;
61 
62  pgr_get_edges(edges_sql, &edges, &total_edges);
63 
64  if (total_edges == 0) {
66  return;
67  }
68 
69  clock_t start_t = clock();
70  char *log_msg = NULL;
71  char *notice_msg = NULL;
72  char *err_msg = NULL;
74  edges,
75  total_edges,
76 
77  result_tuples,
78  result_count,
79  &log_msg,
80  &notice_msg,
81  &err_msg);
82 
83  time_msg(" processing pgr_strongComponents", start_t, clock());
84 
85  if (err_msg) {
86  if (*result_tuples) pfree(*result_tuples);
87  }
88  pgr_global_report(log_msg, notice_msg, err_msg);
89 
90  if (edges) pfree(edges);
91  if (log_msg) pfree(log_msg);
92  if (notice_msg) pfree(notice_msg);
93  if (err_msg) pfree(err_msg);
94 
96 }
97 
98 PGDLLEXPORT Datum _pgr_strongcomponents(PG_FUNCTION_ARGS) {
99  FuncCallContext *funcctx;
100  TupleDesc tuple_desc;
101 
102  pgr_components_rt *result_tuples = NULL;
103  size_t result_count = 0;
104 
105  if (SRF_IS_FIRSTCALL()) {
106  MemoryContext oldcontext;
107  funcctx = SRF_FIRSTCALL_INIT();
108  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
109 
110 
111  process(
112  text_to_cstring(PG_GETARG_TEXT_P(0)),
113  &result_tuples,
114  &result_count);
115 
116 
117 #if PGSQL_VERSION > 94
118  funcctx->max_calls = (uint32_t)result_count;
119 #else
120  funcctx->max_calls = (uint32_t)result_count;
121 #endif
122  funcctx->user_fctx = result_tuples;
123  if (get_call_result_type(fcinfo, NULL, &tuple_desc)
124  != TYPEFUNC_COMPOSITE) {
125  ereport(ERROR,
126  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
127  errmsg("function returning record called in context "
128  "that cannot accept type record")));
129  }
130 
131  funcctx->tuple_desc = tuple_desc;
132  MemoryContextSwitchTo(oldcontext);
133  }
134 
135  funcctx = SRF_PERCALL_SETUP();
136  tuple_desc = funcctx->tuple_desc;
137  result_tuples = (pgr_components_rt*) funcctx->user_fctx;
138 
139  if (funcctx->call_cntr < funcctx->max_calls) {
140  HeapTuple tuple;
141  Datum result;
142  Datum *values;
143  bool* nulls;
144 
145  size_t numb = 3;
146  values = palloc(numb * sizeof(Datum));
147  nulls = palloc(numb * sizeof(bool));
148 
149  size_t i;
150  for (i = 0; i < numb; ++i) {
151  nulls[i] = false;
152  }
153 
154  values[0] = Int64GetDatum(funcctx->call_cntr + 1);
155  values[1] = Int64GetDatum(result_tuples[funcctx->call_cntr].component);
156  values[2] = Int64GetDatum(result_tuples[funcctx->call_cntr].identifier);
157 
158  tuple = heap_form_tuple(tuple_desc, values, nulls);
159  result = HeapTupleGetDatum(tuple);
160  SRF_RETURN_NEXT(funcctx, result);
161  } else {
162  SRF_RETURN_DONE(funcctx);
163  }
164 }
165 
time_msg.h
pgr_components_rt::identifier
int64_t identifier
Definition: pgr_components_rt.h:41
postgres_connection.h
pgr_edge_t
Definition: pgr_edge_t.h:37
PG_FUNCTION_INFO_V1
PG_FUNCTION_INFO_V1(_pgr_strongcomponents)
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
debug_macro.h
pgr_components_rt
Definition: pgr_components_rt.h:39
process
static void process(char *edges_sql, pgr_components_rt **result_tuples, size_t *result_count)
Definition: strongComponents.c:50
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
edges_input.h
pgr_components_rt::component
int64_t component
Definition: pgr_components_rt.h:40
do_pgr_strongComponents
void do_pgr_strongComponents(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)
Definition: strongComponents_driver.cpp:45
pgr_global_report
void pgr_global_report(char *log, char *notice, char *err)
notice & error
Definition: e_report.c:93
_pgr_strongcomponents
PGDLLEXPORT Datum _pgr_strongcomponents(PG_FUNCTION_ARGS)
Definition: strongComponents.c:98
strongComponents_driver.h