PGROUTING  3.2
connectedComponents.c File Reference
Include dependency graph for connectedComponents.c:

Go to the source code of this file.

Functions

PGDLLEXPORT Datum _pgr_connectedcomponents (PG_FUNCTION_ARGS)
 
 PG_FUNCTION_INFO_V1 (_pgr_connectedcomponents)
 
static void process (char *edges_sql, pgr_components_rt **result_tuples, size_t *result_count)
 

Function Documentation

◆ _pgr_connectedcomponents()

PGDLLEXPORT Datum _pgr_connectedcomponents ( PG_FUNCTION_ARGS  )

Definition at line 98 of file connectedComponents.c.

98  {
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 
146  size_t numb = 3;
147  values = palloc(numb * sizeof(Datum));
148  nulls = palloc(numb * sizeof(bool));
149 
150  size_t i;
151  for (i = 0; i < numb; ++i) {
152  nulls[i] = false;
153  }
154 
155  values[0] = Int64GetDatum(funcctx->call_cntr + 1);
156  values[1] = Int64GetDatum(result_tuples[funcctx->call_cntr].component);
157  values[2] = Int64GetDatum(result_tuples[funcctx->call_cntr].identifier);
158 
159  tuple = heap_form_tuple(tuple_desc, values, nulls);
160  result = HeapTupleGetDatum(tuple);
161  SRF_RETURN_NEXT(funcctx, result);
162  } else {
163  SRF_RETURN_DONE(funcctx);
164  }
165 }

References pgr_components_rt::component, pgr_components_rt::identifier, if(), and process().

◆ PG_FUNCTION_INFO_V1()

PG_FUNCTION_INFO_V1 ( _pgr_connectedcomponents  )

◆ process()

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

Definition at line 50 of file connectedComponents.c.

53  {
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_connectedComponents", 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 }

References do_pgr_connectedComponents(), pgr_get_edges(), pgr_global_report(), pgr_SPI_connect(), pgr_SPI_finish(), and time_msg().

Referenced by _pgr_connectedcomponents().

pgr_edge_t
Definition: pgr_edge_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
pgr_components_rt
Definition: pgr_components_rt.h:39
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
do_pgr_connectedComponents
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)
Definition: connectedComponents_driver.cpp:47
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
pgr_global_report
void pgr_global_report(char *log, char *notice, char *err)
notice & error
Definition: e_report.c:93
process
static void process(char *edges_sql, pgr_components_rt **result_tuples, size_t *result_count)
Definition: connectedComponents.c:50