PGROUTING  3.2
breadthFirstSearch.c
Go to the documentation of this file.
1 /*PGR-GNU*****************************************************************
2 File: breadthFirstSearch.c
3 
4 Generated with Template by:
5 Copyright (c) 2019 pgRouting developers
7 
8 Function's developer:
9 Copyright (c) 2019 Gudesa Venkata Sai Akhil
11 
12 
13 ------
14 
15 This program is free software; you can redistribute it and/or modify
16 it under the terms of the GNU General Public License as published by
17 the Free Software Foundation; either version 2 of the License, or
18 (at your option) any later version.
19 
20 This program is distributed in the hope that it will be useful,
21 but WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 GNU General Public License for more details.
24 
25 You should have received a copy of the GNU General Public License
26 along with this program; if not, write to the Free Software
27 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
28 
29 ********************************************************************PGR-GNU*/
30 
31 #include <stdbool.h>
33 #include "utils/array.h"
34 
35 #include "c_common/debug_macro.h"
36 #include "c_common/e_report.h"
37 #include "c_common/time_msg.h"
38 
39 #include "c_common/edges_input.h"
40 #include "c_common/arrays_input.h"
41 
43 
44 PGDLLEXPORT Datum _pgr_breadthfirstsearch(PG_FUNCTION_ARGS);
46 
47 static void
49  char *edges_sql,
50  ArrayType *starts,
51  int64_t max_depth,
52  bool directed,
53 
54  pgr_mst_rt **result_tuples,
55  size_t *result_count) {
57 
58  PGR_DBG("Initializing arrays");
59 
60  size_t size_start_vidsArr = 0;
61  int64_t *start_vidsArr = (int64_t *)
62  pgr_get_bigIntArray(&size_start_vidsArr, starts);
63  PGR_DBG("start_vidsArr size %ld ", size_start_vidsArr);
64 
65 
66  (*result_tuples) = NULL;
67  (*result_count) = 0;
68 
69  PGR_DBG("Load data");
70  pgr_edge_t *edges = NULL;
71  size_t total_edges = 0;
72 
73  pgr_get_edges(edges_sql, &edges, &total_edges);
74  PGR_DBG("Total %ld edges in query:", total_edges);
75 
76  if (total_edges == 0) {
77  if (start_vidsArr)
78  pfree(start_vidsArr);
80  return;
81  }
82 
83  PGR_DBG("Starting processing");
84  clock_t start_t = clock();
85  char *log_msg = NULL;
86  char *notice_msg = NULL;
87  char *err_msg = NULL;
89  edges,
90  total_edges,
91  start_vidsArr, size_start_vidsArr,
92  max_depth,
93  directed,
94 
95  result_tuples,
96  result_count,
97 
98  &log_msg,
99  &notice_msg,
100  &err_msg);
101 
102  time_msg(" processing pgr_breadthFirstSearch", start_t, clock());
103  PGR_DBG("Returning %ld tuples", *result_count);
104 
105  if (err_msg) {
106  if (*result_tuples)
107  pfree(*result_tuples);
108  }
109 
110  pgr_global_report(log_msg, notice_msg, err_msg);
111 
112  if (edges)
113  pfree(edges);
114  if (log_msg)
115  pfree(log_msg);
116  if (notice_msg)
117  pfree(notice_msg);
118  if (err_msg)
119  pfree(err_msg);
120 
121  if (start_vidsArr)
122  pfree(start_vidsArr);
123  pgr_SPI_finish();
124 }
125 
126 PGDLLEXPORT Datum _pgr_breadthfirstsearch(PG_FUNCTION_ARGS) {
127  FuncCallContext *funcctx;
128  TupleDesc tuple_desc;
129 
130  /**************************************************************************/
131  pgr_mst_rt *result_tuples = NULL;
132  size_t result_count = 0;
133  /**************************************************************************/
134 
135  if (SRF_IS_FIRSTCALL()) {
136  MemoryContext oldcontext;
137  funcctx = SRF_FIRSTCALL_INIT();
138  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
139 
140  /**********************************************************************/
141  /*
142  pgr_breadthFirstSearch(
143  edge_sql TEXT,
144  start_vids ANYARRAY,
145  max_depth BIGINT DEFAULT 9223372036854775807,
146  directed BOOLEAN DEFAULT true)
147  */
148  /**********************************************************************/
149 
150  PGR_DBG("Calling process");
151  process(
152  text_to_cstring(PG_GETARG_TEXT_P(0)),
153  PG_GETARG_ARRAYTYPE_P(1),
154  PG_GETARG_INT64(2),
155  PG_GETARG_BOOL(3),
156  &result_tuples,
157  &result_count);
158 
159  /**********************************************************************/
160 
161 #if PGSQL_VERSION > 95
162  funcctx->max_calls = result_count;
163 #else
164  funcctx->max_calls = (uint32_t)result_count;
165 #endif
166  funcctx->user_fctx = result_tuples;
167  if (get_call_result_type(fcinfo, NULL, &tuple_desc) != TYPEFUNC_COMPOSITE) {
168  ereport(ERROR,
169  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
170  errmsg("function returning record called in context "
171  "that cannot accept type record")));
172  }
173 
174  funcctx->tuple_desc = tuple_desc;
175  MemoryContextSwitchTo(oldcontext);
176  }
177 
178  funcctx = SRF_PERCALL_SETUP();
179  tuple_desc = funcctx->tuple_desc;
180  result_tuples = (pgr_mst_rt *)funcctx->user_fctx;
181 
182  if (funcctx->call_cntr < funcctx->max_calls) {
183  HeapTuple tuple;
184  Datum result;
185  Datum *values;
186  bool *nulls;
187 
188  /**********************************************************************/
189  /*
190  OUT seq BIGINT,
191  OUT depth BIGINT,
192  OUT start_vid BIGINT,
193  OUT node BIGINT,
194  OUT edge BIGINT,
195  OUT cost FLOAT,
196  OUT agg_cost FLOAT
197  */
198  /**********************************************************************/
199  size_t numb = 7;
200  values = palloc(numb * sizeof(Datum));
201  nulls = palloc(numb * sizeof(bool));
202 
203  size_t i;
204  for (i = 0; i < numb; ++i) {
205  nulls[i] = false;
206  }
207 
208  values[0] = Int32GetDatum(funcctx->call_cntr + 1);
209  values[1] = Int64GetDatum(result_tuples[funcctx->call_cntr].depth);
210  values[2] = Int64GetDatum(result_tuples[funcctx->call_cntr].from_v);
211  values[3] = Int64GetDatum(result_tuples[funcctx->call_cntr].node);
212  values[4] = Int64GetDatum(result_tuples[funcctx->call_cntr].edge);
213  values[5] = Float8GetDatum(result_tuples[funcctx->call_cntr].cost);
214  values[6] = Float8GetDatum(result_tuples[funcctx->call_cntr].agg_cost);
215 
216  /**********************************************************************/
217 
218  tuple = heap_form_tuple(tuple_desc, values, nulls);
219  result = HeapTupleGetDatum(tuple);
220  SRF_RETURN_NEXT(funcctx, result);
221  } else {
222  /**********************************************************************/
223 
224  PGR_DBG("Clean up code");
225 
226  /**********************************************************************/
227 
228  SRF_RETURN_DONE(funcctx);
229  }
230 }
pgr_mst_rt
Definition: pgr_mst_rt.h:36
time_msg.h
postgres_connection.h
pgr_edge_t
Definition: pgr_edge_t.h:37
PG_FUNCTION_INFO_V1
PG_FUNCTION_INFO_V1(_pgr_breadthfirstsearch)
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_SPI_finish
void pgr_SPI_finish(void)
Definition: postgres_connection.c:71
do_pgr_breadthFirstSearch
void do_pgr_breadthFirstSearch(pgr_edge_t *data_edges, size_t total_edges, int64_t *start_vidsArr, size_t size_start_vidsArr, int64_t max_depth, bool directed, pgr_mst_rt **return_tuples, size_t *return_count, char **log_msg, char **notice_msg, char **err_msg)
Definition: breadthFirstSearch_driver.cpp:60
e_report.h
process
static void process(char *edges_sql, ArrayType *starts, int64_t max_depth, bool directed, pgr_mst_rt **result_tuples, size_t *result_count)
Definition: breadthFirstSearch.c:48
arrays_input.h
_pgr_breadthfirstsearch
PGDLLEXPORT Datum _pgr_breadthfirstsearch(PG_FUNCTION_ARGS)
Definition: breadthFirstSearch.c:126
debug_macro.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
PGR_DBG
#define PGR_DBG(...)
Definition: debug_macro.h:34
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
breadthFirstSearch_driver.h
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
pgr_mst_rt::agg_cost
double agg_cost
Definition: pgr_mst_rt.h:42
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