PGROUTING  3.2
depthFirstSearch.c
Go to the documentation of this file.
1 /*PGR-GNU*****************************************************************
2 File: depthFirstSearch.c
3 Generated with Template by:
4 
5 Copyright (c) 2020 pgRouting developers
7 
8 Function's developer:
9 Copyright (c) 2020 Ashish Kumar
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 
34 #include <stdbool.h>
36 #include "utils/array.h"
37 
38 #include "c_common/debug_macro.h"
39 #include "c_common/e_report.h"
40 #include "c_common/time_msg.h"
41 
42 #include "c_common/edges_input.h"
43 #include "c_common/arrays_input.h"
44 
46 
47 PGDLLEXPORT Datum _pgr_depthfirstsearch(PG_FUNCTION_ARGS);
49 
67 static
68 void
70  char* edges_sql,
71  ArrayType *roots,
72  bool directed,
73  int64_t max_depth,
74 
75  pgr_mst_rt **result_tuples,
76  size_t *result_count) {
78 
79  size_t size_rootsArr = 0;
80 
81  int64_t* rootsArr = (int64_t*) pgr_get_bigIntArray(&size_rootsArr, roots);
82 
83  (*result_tuples) = NULL;
84  (*result_count) = 0;
85 
86  pgr_edge_t *edges = NULL;
87  size_t total_edges = 0;
88 
89  pgr_get_edges(edges_sql, &edges, &total_edges);
90 
91  clock_t start_t = clock();
92  char *log_msg = NULL;
93  char *notice_msg = NULL;
94  char *err_msg = NULL;
96  edges, total_edges,
97  rootsArr, size_rootsArr,
98 
99  directed,
100  max_depth,
101 
102  result_tuples,
103  result_count,
104  &log_msg,
105  &notice_msg,
106  &err_msg);
107 
108  time_msg("processing pgr_depthFirstSearch", start_t, clock());
109 
110  if (err_msg && (*result_tuples)) {
111  pfree(*result_tuples);
112  (*result_tuples) = NULL;
113  (*result_count) = 0;
114  }
115 
116  pgr_global_report(log_msg, notice_msg, err_msg);
117 
118  if (log_msg) pfree(log_msg);
119  if (notice_msg) pfree(notice_msg);
120  if (err_msg) pfree(err_msg);
121  if (edges) pfree(edges);
122  if (rootsArr) pfree(rootsArr);
123 
124  pgr_SPI_finish();
125 }
126 /* */
127 /******************************************************************************/
128 
133 PGDLLEXPORT Datum _pgr_depthfirstsearch(PG_FUNCTION_ARGS) {
134  FuncCallContext *funcctx;
135  TupleDesc tuple_desc;
136 
137  /**********************************************************************/
138  pgr_mst_rt *result_tuples = NULL;
139  size_t result_count = 0;
140  /**********************************************************************/
141 
142  if (SRF_IS_FIRSTCALL()) {
143  MemoryContext oldcontext;
144  funcctx = SRF_FIRSTCALL_INIT();
145  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
146 
147  /***********************************************************************
148  *
149  * pgr_depthFirstSearch(
150  * edges_sql TEXT,
151  * root_vids ANYARRAY,
152  * directed BOOLEAN DEFAULT true
153  * max_depth BIGINT DEFAULT 9223372036854775807,
154  * );
155  *
156  **********************************************************************/
157 
158  process(
159  text_to_cstring(PG_GETARG_TEXT_P(0)),
160  PG_GETARG_ARRAYTYPE_P(1),
161  PG_GETARG_BOOL(2),
162  PG_GETARG_INT64(3),
163  &result_tuples,
164  &result_count);
165 
166  /**********************************************************************/
167 
168 
169 #if PGSQL_VERSION > 95
170  funcctx->max_calls = result_count;
171 #else
172  funcctx->max_calls = (uint32_t)result_count;
173 #endif
174  funcctx->user_fctx = result_tuples;
175  if (get_call_result_type(fcinfo, NULL, &tuple_desc)
176  != TYPEFUNC_COMPOSITE) {
177  ereport(ERROR,
178  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
179  errmsg("function returning record called in context "
180  "that cannot accept type record")));
181  }
182 
183  funcctx->tuple_desc = tuple_desc;
184  MemoryContextSwitchTo(oldcontext);
185  }
186 
187  funcctx = SRF_PERCALL_SETUP();
188  tuple_desc = funcctx->tuple_desc;
189  result_tuples = (pgr_mst_rt*) funcctx->user_fctx;
190 
191  if (funcctx->call_cntr < funcctx->max_calls) {
192  HeapTuple tuple;
193  Datum result;
194  Datum *values;
195  bool* nulls;
196 
197  /***********************************************************************
198  *
199  * OUT seq BIGINT,
200  * OUT depth BIGINT,
201  * OUT start_vid BIGINT,
202  * OUT node BIGINT,
203  * OUT edge BIGINT,
204  * OUT cost FLOAT,
205  * OUT agg_cost FLOAT
206  *
207  **********************************************************************/
208 
209  size_t num = 7;
210  values = palloc(num * sizeof(Datum));
211  nulls = palloc(num * sizeof(bool));
212 
213 
214  size_t i;
215  for (i = 0; i < num; ++i) {
216  nulls[i] = false;
217  }
218 
219  values[0] = Int64GetDatum(funcctx->call_cntr + 1);
220  values[1] = Int64GetDatum(result_tuples[funcctx->call_cntr].depth);
221  values[2] = Int64GetDatum(result_tuples[funcctx->call_cntr].from_v);
222  values[3] = Int64GetDatum(result_tuples[funcctx->call_cntr].node);
223  values[4] = Int64GetDatum(result_tuples[funcctx->call_cntr].edge);
224  values[5] = Float8GetDatum(result_tuples[funcctx->call_cntr].cost);
225  values[6] = Float8GetDatum(result_tuples[funcctx->call_cntr].agg_cost);
226 
227  /**********************************************************************/
228 
229  tuple = heap_form_tuple(tuple_desc, values, nulls);
230  result = HeapTupleGetDatum(tuple);
231  SRF_RETURN_NEXT(funcctx, result);
232  } else {
233  SRF_RETURN_DONE(funcctx);
234  }
235 }
pgr_mst_rt
Definition: pgr_mst_rt.h:36
time_msg.h
postgres_connection.h
pgr_edge_t
Definition: pgr_edge_t.h:37
pgr_mst_rt::node
int64_t node
Definition: pgr_mst_rt.h:39
pgr_SPI_connect
void pgr_SPI_connect(void)
Definition: postgres_connection.c:82
_pgr_depthfirstsearch
PGDLLEXPORT Datum _pgr_depthfirstsearch(PG_FUNCTION_ARGS)
Helps in converting postgres variables to C variables, and returns the result.
Definition: depthFirstSearch.c:133
pgr_SPI_finish
void pgr_SPI_finish(void)
Definition: postgres_connection.c:71
e_report.h
arrays_input.h
debug_macro.h
depthFirstSearch_driver.h
pgr_mst_rt::depth
int64_t depth
Definition: pgr_mst_rt.h:38
pgr_mst_rt::cost
double cost
Definition: pgr_mst_rt.h:41
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, bool directed, int64_t max_depth, pgr_mst_rt **result_tuples, size_t *result_count)
Static function, loads the data from postgres to C types for further processing.
Definition: depthFirstSearch.c:69
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_mst_rt::from_v
int64_t from_v
Definition: pgr_mst_rt.h:37
if
if(DOXYGEN_FOUND) configure_file($
Definition: doxygen/CMakeLists.txt:13
do_pgr_depthFirstSearch
void do_pgr_depthFirstSearch(pgr_edge_t *data_edges, size_t total_edges, int64_t *rootsArr, size_t size_rootsArr, bool directed, int64_t max_depth, pgr_mst_rt **return_tuples, size_t *return_count, char **log_msg, char **notice_msg, char **err_msg)
Performs exception handling and converts the results to postgres.
Definition: depthFirstSearch_driver.cpp:118
time_msg
void time_msg(char *msg, clock_t start_t, clock_t end_t)
Definition: time_msg.c:32
pgr_mst_rt::agg_cost
double agg_cost
Definition: pgr_mst_rt.h:42
PG_FUNCTION_INFO_V1
PG_FUNCTION_INFO_V1(_pgr_depthfirstsearch)
edges_input.h
pgr_mst_rt::edge
int64_t edge
Definition: pgr_mst_rt.h:40
pgr_global_report
void pgr_global_report(char *log, char *notice, char *err)
notice & error
Definition: e_report.c:93