PGROUTING  3.2
randomSpanningTree.c
Go to the documentation of this file.
1 /*PGR-GNU*****************************************************************
2 File: randomSpanningTree.c
3 Generated with Template by:
4 
5 Copyright (c) 2015 pgRouting developers
7 
8 Function's developer:
9 Copyright (c) 2018 Aditya Pratap Singh
11 ------
12 
13 This program is free software; you can redistribute it and/or modify
14 it under the terms of the GNU General Public License as published by
15 the Free Software Foundation; either version 2 of the License, or
16 (at your option) any later version.
17 
18 This program is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
22 
23 You should have received a copy of the GNU General Public License
24 along with this program; if not, write to the Free Software
25 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 
27  ********************************************************************PGR-GNU*/
28 
29 #include <stdbool.h>
31 
32 #include "c_common/debug_macro.h"
33 #include "c_common/e_report.h"
34 #include "c_common/time_msg.h"
35 
36 #include "c_common/edges_input.h"
37 
39 
40 PGDLLEXPORT Datum randomSpanningTree(PG_FUNCTION_ARGS);
42 
43 static
44 void
46  char* edges_sql,
47  int64_t root_vertex,
48  bool directed,
49  pgr_randomSpanningTree_t **result_tuples,
50  size_t *result_count) {
51  /*
52  * https://www.postgresql.org/docs/current/static/spi-spi-connect.html
53  */
55 
56  (*result_tuples) = NULL;
57  (*result_count) = 0;
58 
59  PGR_DBG("Load data");
60  pgr_edge_t *edges = NULL;
61  size_t total_edges = 0;
62 
63  pgr_get_edges(edges_sql, &edges, &total_edges);
64  PGR_DBG("Total %ld edges in query:", total_edges);
65 
66  if (total_edges == 0) {
67  PGR_DBG("No edges found");
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  root_vertex,
81  directed,
82  result_tuples,
83  result_count,
84  &log_msg,
85  &notice_msg,
86  &err_msg);
87 
88  time_msg(" processing pgr_randomSpanningTree", start_t, clock());
89  PGR_DBG("Returning %ld tuples", *result_count);
90 
91  if (err_msg) {
92  if (*result_tuples) pfree(*result_tuples);
93  }
94  pgr_global_report(log_msg, notice_msg, err_msg);
95 
96  if (edges) pfree(edges);
97  if (log_msg) pfree(log_msg);
98  if (notice_msg) pfree(notice_msg);
99  if (err_msg) pfree(err_msg);
100 
101  pgr_SPI_finish();
102 }
103 /* */
104 /******************************************************************************/
105 
106 PGDLLEXPORT Datum randomSpanningTree(PG_FUNCTION_ARGS) {
107  FuncCallContext *funcctx;
108  TupleDesc tuple_desc;
109 
110  pgr_randomSpanningTree_t *result_tuples = NULL;
111  size_t result_count = 0;
112 
113  if (SRF_IS_FIRSTCALL()) {
114  MemoryContext oldcontext;
115  funcctx = SRF_FIRSTCALL_INIT();
116  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
117 
118 
119  PGR_DBG("Calling process");
120  process(
121  text_to_cstring(PG_GETARG_TEXT_P(0)),
122  PG_GETARG_INT64(1),
123  PG_GETARG_BOOL(2),
124  &result_tuples,
125  &result_count);
126 
127 #if PGSQL_VERSION > 94
128  funcctx->max_calls = (uint32_t)result_count;
129 #else
130  funcctx->max_calls = (uint32_t)result_count;
131 #endif
132  funcctx->user_fctx = result_tuples;
133  if (get_call_result_type(fcinfo, NULL, &tuple_desc)
134  != TYPEFUNC_COMPOSITE) {
135  ereport(ERROR,
136  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
137  errmsg("function returning record called in context "
138  "that cannot accept type record")));
139  }
140 
141  funcctx->tuple_desc = tuple_desc;
142  MemoryContextSwitchTo(oldcontext);
143  }
144 
145  funcctx = SRF_PERCALL_SETUP();
146  tuple_desc = funcctx->tuple_desc;
147  result_tuples = (pgr_randomSpanningTree_t*) funcctx->user_fctx;
148 
149  if (funcctx->call_cntr < funcctx->max_calls) {
150  HeapTuple tuple;
151  Datum result;
152  Datum *values;
153  bool* nulls;
154 
155  values = palloc(5 * sizeof(Datum));
156  nulls = palloc(5 * sizeof(bool));
157 
158 
159  size_t i;
160  for (i = 0; i < 5; ++i) {
161  nulls[i] = false;
162  }
163 
164  // postgres starts counting from 1
165  values[0] = Int64GetDatum(funcctx->call_cntr + 1);
166  values[1] =
167  Int64GetDatum(result_tuples[funcctx->call_cntr].root_vertex);
168  values[2] = Int64GetDatum(result_tuples[funcctx->call_cntr].edge);
169  values[3] = Float8GetDatum(result_tuples[funcctx->call_cntr].cost);
170  values[4] = Float8GetDatum(result_tuples[funcctx->call_cntr].tree_cost);
171  /**********************************************************************/
172 
173  tuple = heap_form_tuple(tuple_desc, values, nulls);
174  result = HeapTupleGetDatum(tuple);
175  SRF_RETURN_NEXT(funcctx, result);
176  } else {
177  PGR_DBG("Clean up code");
178 
179  SRF_RETURN_DONE(funcctx);
180  }
181 }
PG_FUNCTION_INFO_V1
PG_FUNCTION_INFO_V1(randomSpanningTree)
do_pgr_randomSpanningTree
void do_pgr_randomSpanningTree(pgr_edge_t *data_edges, size_t total_edges, int64_t root_vertex, bool directed, pgr_randomSpanningTree_t **return_tuples, size_t *return_count, char **log_msg, char **notice_msg, char **err_msg)
Definition: randomSpanningTree_driver.cpp:52
time_msg.h
postgres_connection.h
pgr_edge_t
Definition: pgr_edge_t.h:37
process
static void process(char *edges_sql, int64_t root_vertex, bool directed, pgr_randomSpanningTree_t **result_tuples, size_t *result_count)
Definition: randomSpanningTree.c:45
pgr_SPI_connect
void pgr_SPI_connect(void)
Definition: postgres_connection.c:82
pgr_randomSpanningTree_t::root_vertex
int64_t root_vertex
Definition: pgr_randomSpanningTree_t.h:39
randomSpanningTree_driver.h
pgr_SPI_finish
void pgr_SPI_finish(void)
Definition: postgres_connection.c:71
e_report.h
debug_macro.h
pgr_randomSpanningTree_t::cost
double cost
Definition: pgr_randomSpanningTree_t.h:41
PGR_DBG
#define PGR_DBG(...)
Definition: debug_macro.h:34
pgr_randomSpanningTree_t
Definition: pgr_randomSpanningTree_t.h:37
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
pgr_randomSpanningTree_t::edge
int64_t edge
Definition: pgr_randomSpanningTree_t.h:40
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
randomSpanningTree
PGDLLEXPORT Datum randomSpanningTree(PG_FUNCTION_ARGS)
Definition: randomSpanningTree.c:106
edges_input.h
pgr_global_report
void pgr_global_report(char *log, char *notice, char *err)
notice & error
Definition: e_report.c:93
pgr_randomSpanningTree_t::tree_cost
double tree_cost
Definition: pgr_randomSpanningTree_t.h:42