PGROUTING  3.2
kruskal.c File Reference
#include <stdbool.h>
#include "c_common/postgres_connection.h"
#include "utils/array.h"
#include "c_common/debug_macro.h"
#include "c_common/e_report.h"
#include "c_common/time_msg.h"
#include "c_common/edges_input.h"
#include "c_common/arrays_input.h"
#include "c_types/pgr_mst_rt.h"
#include "drivers/spanningTree/mst_common.h"
#include "drivers/spanningTree/kruskal_driver.h"
Include dependency graph for kruskal.c:

Go to the source code of this file.

Functions

PGDLLEXPORT Datum _pgr_kruskal (PG_FUNCTION_ARGS)
 
 PG_FUNCTION_INFO_V1 (_pgr_kruskal)
 
static void process (char *edges_sql, ArrayType *roots, char *fn_suffix, int64_t max_depth, double distance, pgr_mst_rt **result_tuples, size_t *result_count)
 

Function Documentation

◆ _pgr_kruskal()

PGDLLEXPORT Datum _pgr_kruskal ( PG_FUNCTION_ARGS  )

Definition at line 111 of file kruskal.c.

111  {
112  FuncCallContext *funcctx;
113  TupleDesc tuple_desc;
114 
115  pgr_mst_rt *result_tuples = NULL;
116  size_t result_count = 0;
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  /* Edge sql, tree roots, fn_suffix, max_depth, distance */
125  process(
126  text_to_cstring(PG_GETARG_TEXT_P(0)),
127  PG_GETARG_ARRAYTYPE_P(1),
128  text_to_cstring(PG_GETARG_TEXT_P(2)),
129  PG_GETARG_INT64(3),
130  PG_GETARG_FLOAT8(4),
131  &result_tuples,
132  &result_count);
133 
134 #if PGSQL_VERSION > 95
135  funcctx->max_calls = result_count;
136 #else
137  funcctx->max_calls = (uint32_t)result_count;
138 #endif
139  funcctx->user_fctx = result_tuples;
140  if (get_call_result_type(fcinfo, NULL, &tuple_desc)
141  != TYPEFUNC_COMPOSITE) {
142  ereport(ERROR,
143  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
144  errmsg("function returning record called in context "
145  "that cannot accept type record")));
146  }
147 
148  funcctx->tuple_desc = tuple_desc;
149  MemoryContextSwitchTo(oldcontext);
150  }
151 
152  funcctx = SRF_PERCALL_SETUP();
153  tuple_desc = funcctx->tuple_desc;
154  result_tuples = (pgr_mst_rt*) funcctx->user_fctx;
155 
156  if (funcctx->call_cntr < funcctx->max_calls) {
157  HeapTuple tuple;
158  Datum result;
159  Datum *values;
160  bool* nulls;
161 
162  size_t num = 7;
163  values = palloc(num * sizeof(Datum));
164  nulls = palloc(num * sizeof(bool));
165 
166 
167  size_t i;
168  for (i = 0; i < num; ++i) {
169  nulls[i] = false;
170  }
171 
172  values[0] = Int64GetDatum(funcctx->call_cntr + 1);
173  values[1] = Int64GetDatum(result_tuples[funcctx->call_cntr].depth);
174  values[2] = Int64GetDatum(result_tuples[funcctx->call_cntr].from_v);
175  values[3] = Int64GetDatum(result_tuples[funcctx->call_cntr].node);
176  values[4] = Int64GetDatum(result_tuples[funcctx->call_cntr].edge);
177  values[5] = Float8GetDatum(result_tuples[funcctx->call_cntr].cost);
178  values[6] = Float8GetDatum(result_tuples[funcctx->call_cntr].agg_cost);
179  /**********************************************************************/
180 
181  tuple = heap_form_tuple(tuple_desc, values, nulls);
182  result = HeapTupleGetDatum(tuple);
183  SRF_RETURN_NEXT(funcctx, result);
184  } else {
185  SRF_RETURN_DONE(funcctx);
186  }
187 }

References pgr_mst_rt::agg_cost, pgr_mst_rt::cost, pgr_mst_rt::depth, pgr_mst_rt::edge, pgr_mst_rt::from_v, if(), pgr_mst_rt::node, and process().

◆ PG_FUNCTION_INFO_V1()

PG_FUNCTION_INFO_V1 ( _pgr_kruskal  )

◆ process()

static void process ( char *  edges_sql,
ArrayType *  roots,
char *  fn_suffix,
int64_t  max_depth,
double  distance,
pgr_mst_rt **  result_tuples,
size_t *  result_count 
)
static

Definition at line 50 of file kruskal.c.

58  {
60 
61  char *log_msg = NULL;
62  char *notice_msg = NULL;
63  char *err_msg = NULL;
64 
65  char * fn_name = get_name(0, fn_suffix, &err_msg);
66  if (err_msg) {
67  pgr_global_report(log_msg, notice_msg, err_msg);
68  return;
69  }
70 
71  size_t size_rootsArr = 0;
72  int64_t* rootsArr = (int64_t*) pgr_get_bigIntArray(&size_rootsArr, roots);
73 
74  (*result_tuples) = NULL;
75  (*result_count) = 0;
76 
77  pgr_edge_t *edges = NULL;
78  size_t total_edges = 0;
79 
80  pgr_get_edges(edges_sql, &edges, &total_edges);
81 
82  clock_t start_t = clock();
84  edges, total_edges,
85  rootsArr, size_rootsArr,
86  fn_suffix,
87  max_depth,
88  distance,
89 
90  result_tuples,
91  result_count,
92  &log_msg,
93  &notice_msg,
94  &err_msg);
95 
96  time_msg(fn_name, start_t, clock());
97 
98  if (err_msg) {
99  if (*result_tuples) pfree(*result_tuples);
100  }
101  pgr_global_report(log_msg, notice_msg, err_msg);
102 
103  if (edges) pfree(edges);
104  if (log_msg) pfree(log_msg);
105  if (notice_msg) pfree(notice_msg);
106  if (err_msg) pfree(err_msg);
107 
108  pgr_SPI_finish();
109 }

References do_pgr_kruskal(), get_name(), pgr_get_bigIntArray(), pgr_get_edges(), pgr_global_report(), pgr_SPI_connect(), pgr_SPI_finish(), and time_msg().

Referenced by _pgr_kruskal().

pgr_mst_rt
Definition: pgr_mst_rt.h:36
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_get_bigIntArray
int64_t * pgr_get_bigIntArray(size_t *arrlen, ArrayType *input)
Enforces the input array to be NOT empty.
Definition: arrays_input.c:146
process
static void process(char *edges_sql, ArrayType *roots, char *fn_suffix, int64_t max_depth, double distance, pgr_mst_rt **result_tuples, size_t *result_count)
Definition: kruskal.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
get_name
char * get_name(int fn_id, char *fn_suffix, char **err_msg)
Definition: mst_common.cpp:54
pgr_global_report
void pgr_global_report(char *log, char *notice, char *err)
notice & error
Definition: e_report.c:93
do_pgr_kruskal
void do_pgr_kruskal(pgr_edge_t *data_edges, size_t total_edges, int64_t *rootsArr, size_t size_rootsArr, char *fn_suffix, int64_t max_depth, double distance, pgr_mst_rt **return_tuples, size_t *return_count, char **log_msg, char **notice_msg, char **err_msg)
Definition: kruskal_driver.cpp:44