1 /*PGR-GNU*****************************************************************
2 File: kruskal.c
3 Generated with Template by:
4 
5 Copyright (c) 2015 pgRouting developers
6 Mail: project@pgrouting.org
7 
8 Function's developer:
9 Copyright (c) 2018 Aditya Pratap Singh
10 Mail: adityapratap.singh28@gmail.com
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>
30 
31 #include "c_common/postgres_connection.h"
32 #include "utils/array.h"
33 
34 #include "c_common/debug_macro.h"
35 #include "c_common/e_report.h"
36 #include "c_common/time_msg.h"
37 #include "c_common/edges_input.h"
38 #include "c_common/arrays_input.h"
39 #include "c_types/pgr_mst_rt.h"
40 
41 #include "drivers/spanningTree/mst_common.h"
42 #include "drivers/spanningTree/kruskal_driver.h"
43 
44 PGDLLEXPORT Datum _pgr_kruskal(PG_FUNCTION_ARGS);
45 PG_FUNCTION_INFO_V1(_pgr_kruskal);
46 
47 
48 static
49 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)50 process(
51         char* edges_sql,
52         ArrayType *roots,
53         char * fn_suffix,
54         int64_t max_depth,
55         double distance,
56 
57         pgr_mst_rt **result_tuples,
58         size_t *result_count) {
59     pgr_SPI_connect();
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();
83     do_pgr_kruskal(
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 }
110 
_pgr_kruskal(PG_FUNCTION_ARGS)111 PGDLLEXPORT Datum _pgr_kruskal(PG_FUNCTION_ARGS) {
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 }
188