PGROUTING  3.2
prim.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/prim_driver.h"
Include dependency graph for prim.c:

Go to the source code of this file.

Functions

PGDLLEXPORT Datum _pgr_prim (PG_FUNCTION_ARGS)
 
 PG_FUNCTION_INFO_V1 (_pgr_prim)
 
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_prim()

PGDLLEXPORT Datum _pgr_prim ( PG_FUNCTION_ARGS  )

Definition at line 116 of file prim.c.

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

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_prim  )

◆ 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 49 of file prim.c.

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

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

Referenced by _pgr_prim().

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
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
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: prim.c:49
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
do_pgr_prim
void do_pgr_prim(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: prim_driver.cpp:44
pgr_global_report
void pgr_global_report(char *log, char *notice, char *err)
notice & error
Definition: e_report.c:93