PGROUTING  3.2
binaryBreadthFirstSearch.c
Go to the documentation of this file.
1 /*PGR-GNU*****************************************************************
2 File: binaryBreadthFirstSearch.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"
42 
44 
45 PGDLLEXPORT Datum _pgr_binarybreadthfirstsearch(PG_FUNCTION_ARGS);
47 
48 static void
50  char *edges_sql,
51  char *combinations_sql,
52  ArrayType *starts,
53  ArrayType *ends,
54  bool directed,
55 
56  General_path_element_t **result_tuples,
57  size_t *result_count) {
59 
60  PGR_DBG("Initializing arrays");
61 
62  size_t size_start_vidsArr = 0;
63  int64_t *start_vidsArr = NULL;
64 
65  size_t size_end_vidsArr = 0;
66  int64_t *end_vidsArr = NULL;
67 
68  size_t total_combinations = 0;
69  pgr_combination_t *combinations = NULL;
70 
71  if (starts && ends) {
72  start_vidsArr = (int64_t*)
73  pgr_get_bigIntArray(&size_start_vidsArr, starts);
74  end_vidsArr = (int64_t*)
75  pgr_get_bigIntArray(&size_end_vidsArr, ends);
76  } else if (combinations_sql) {
77  pgr_get_combinations(combinations_sql, &combinations, &total_combinations);
78  if (total_combinations == 0) {
79  if (combinations)
80  pfree(combinations);
82  return;
83  }
84  }
85 
86  (*result_tuples) = NULL;
87  (*result_count) = 0;
88 
89  PGR_DBG("Load data");
90  pgr_edge_t *edges = NULL;
91  size_t total_edges = 0;
92 
93  pgr_get_edges(edges_sql, &edges, &total_edges);
94  PGR_DBG("Total %ld edges in query:", total_edges);
95 
96  if (total_edges == 0) {
97  if (start_vidsArr)
98  pfree(start_vidsArr);
99  if (end_vidsArr)
100  pfree(end_vidsArr);
101  pgr_SPI_finish();
102  return;
103  }
104 
105  PGR_DBG("Starting processing");
106  clock_t start_t = clock();
107  char *log_msg = NULL;
108  char *notice_msg = NULL;
109  char *err_msg = NULL;
111  edges,
112  total_edges,
113  combinations,
114  total_combinations,
115  start_vidsArr, size_start_vidsArr,
116  end_vidsArr, size_end_vidsArr,
117 
118  directed,
119 
120  result_tuples,
121  result_count,
122 
123  &log_msg,
124  &notice_msg,
125  &err_msg);
126 
127  time_msg(" processing pgr_binaryBreadthFirstSearch", start_t, clock());
128  PGR_DBG("Returning %ld tuples", *result_count);
129 
130  if (err_msg) {
131  if (*result_tuples)
132  pfree(*result_tuples);
133  }
134 
135  pgr_global_report(log_msg, notice_msg, err_msg);
136 
137  if (edges)
138  pfree(edges);
139  if (log_msg)
140  pfree(log_msg);
141  if (notice_msg)
142  pfree(notice_msg);
143  if (err_msg)
144  pfree(err_msg);
145 
146  if (start_vidsArr)
147  pfree(start_vidsArr);
148  if (end_vidsArr)
149  pfree(end_vidsArr);
150  pgr_SPI_finish();
151 }
152 
153 PGDLLEXPORT Datum _pgr_binarybreadthfirstsearch(PG_FUNCTION_ARGS) {
154  FuncCallContext *funcctx;
155  TupleDesc tuple_desc;
156 
157  /**************************************************************************/
158  General_path_element_t *result_tuples = NULL;
159  size_t result_count = 0;
160  /**************************************************************************/
161 
162  if (SRF_IS_FIRSTCALL()) {
163  MemoryContext oldcontext;
164  funcctx = SRF_FIRSTCALL_INIT();
165  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
166 
167  /**********************************************************************/
168  // pgr_binaryBreadthFirstSearch(
169  // sql TEXT,
170  // start_vids ANYARRAY,
171  // end_vids ANYARRAY,
172  // directed BOOLEAN default true,
173 
174  PGR_DBG("Calling process");
175 
176  if (PG_NARGS() == 4) {
177  /*
178  * many to many
179  */
180  process(
181  text_to_cstring(PG_GETARG_TEXT_P(0)),
182  NULL,
183  PG_GETARG_ARRAYTYPE_P(1),
184  PG_GETARG_ARRAYTYPE_P(2),
185  PG_GETARG_BOOL(3),
186  &result_tuples,
187  &result_count);
188 
189  } else if (PG_NARGS() == 3) {
190  /*
191  * combinations
192  */
193  process(
194  text_to_cstring(PG_GETARG_TEXT_P(0)),
195  text_to_cstring(PG_GETARG_TEXT_P(1)),
196  NULL,
197  NULL,
198  PG_GETARG_BOOL(2),
199  &result_tuples,
200  &result_count);
201  }
202 
203 
204  /**********************************************************************/
205 
206 #if PGSQL_VERSION > 95
207  funcctx->max_calls = result_count;
208 #else
209  funcctx->max_calls = (uint32_t)result_count;
210 #endif
211  funcctx->user_fctx = result_tuples;
212  if (get_call_result_type(fcinfo, NULL, &tuple_desc) != TYPEFUNC_COMPOSITE) {
213  ereport(ERROR,
214  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
215  errmsg("function returning record called in context "
216  "that cannot accept type record")));
217  }
218 
219  funcctx->tuple_desc = tuple_desc;
220  MemoryContextSwitchTo(oldcontext);
221  }
222 
223  funcctx = SRF_PERCALL_SETUP();
224  tuple_desc = funcctx->tuple_desc;
225  result_tuples = (General_path_element_t *)funcctx->user_fctx;
226 
227  if (funcctx->call_cntr < funcctx->max_calls) {
228  HeapTuple tuple;
229  Datum result;
230  Datum *values;
231  bool *nulls;
232 
233  /**********************************************************************/
234  /*
235  OUT seq BIGINT,
236  OUT path_seq BIGINT,
237  OUT start_vid BIGINT,
238  OUT end_vid BIGINT,
239  OUT node BIGINT,
240  OUT edge BIGINT,
241  OUT cost FLOAT,
242  OUT agg_cost FLOAT
243  */
244  /**********************************************************************/
245  size_t numb = 8;
246  values = palloc(numb * sizeof(Datum));
247  nulls = palloc(numb * sizeof(bool));
248 
249  size_t i;
250  for (i = 0; i < numb; ++i) {
251  nulls[i] = false;
252  }
253 
254  values[0] = Int32GetDatum(funcctx->call_cntr + 1);
255  values[1] = Int32GetDatum(result_tuples[funcctx->call_cntr].seq);
256  values[2] = Int64GetDatum(result_tuples[funcctx->call_cntr].start_id);
257  values[3] = Int64GetDatum(result_tuples[funcctx->call_cntr].end_id);
258  values[4] = Int64GetDatum(result_tuples[funcctx->call_cntr].node);
259  values[5] = Int64GetDatum(result_tuples[funcctx->call_cntr].edge);
260  values[6] = Float8GetDatum(result_tuples[funcctx->call_cntr].cost);
261  values[7] = Float8GetDatum(result_tuples[funcctx->call_cntr].agg_cost);
262 
263  /**********************************************************************/
264 
265  tuple = heap_form_tuple(tuple_desc, values, nulls);
266  result = HeapTupleGetDatum(tuple);
267  SRF_RETURN_NEXT(funcctx, result);
268  } else {
269  /**********************************************************************/
270 
271  PGR_DBG("Clean up code");
272 
273  /**********************************************************************/
274 
275  SRF_RETURN_DONE(funcctx);
276  }
277 }
do_pgr_binaryBreadthFirstSearch
void do_pgr_binaryBreadthFirstSearch(pgr_edge_t *data_edges, size_t total_edges, pgr_combination_t *combinations, size_t total_combinations, int64_t *start_vidsArr, size_t size_start_vidsArr, int64_t *end_vidsArr, size_t size_end_vidsArr, bool directed, General_path_element_t **return_tuples, size_t *return_count, char **log_msg, char **notice_msg, char **err_msg)
Definition: binaryBreadthFirstSearch_driver.cpp:106
combinations_input.h
General_path_element_t::edge
int64_t edge
Definition: general_path_element_t.h:42
time_msg.h
postgres_connection.h
pgr_edge_t
Definition: pgr_edge_t.h:37
process
static void process(char *edges_sql, char *combinations_sql, ArrayType *starts, ArrayType *ends, bool directed, General_path_element_t **result_tuples, size_t *result_count)
Definition: binaryBreadthFirstSearch.c:49
pgr_SPI_connect
void pgr_SPI_connect(void)
Definition: postgres_connection.c:82
General_path_element_t::seq
int seq
Definition: general_path_element_t.h:38
pgr_SPI_finish
void pgr_SPI_finish(void)
Definition: postgres_connection.c:71
e_report.h
arrays_input.h
_pgr_binarybreadthfirstsearch
PGDLLEXPORT Datum _pgr_binarybreadthfirstsearch(PG_FUNCTION_ARGS)
Definition: binaryBreadthFirstSearch.c:153
General_path_element_t::start_id
int64_t start_id
Definition: general_path_element_t.h:39
debug_macro.h
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_combination_t
Definition: pgr_combination_t.h:43
General_path_element_t::node
int64_t node
Definition: general_path_element_t.h:41
pgr_get_combinations
void pgr_get_combinations(char *combinations_sql, pgr_combination_t **combinations, size_t *total_combinations)
combinations_sql
Definition: combinations_input.c:147
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
General_path_element_t
Definition: general_path_element_t.h:37
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
General_path_element_t::end_id
int64_t end_id
Definition: general_path_element_t.h:40
binaryBreadthFirstSearch_driver.h
edges_input.h
PG_FUNCTION_INFO_V1
PG_FUNCTION_INFO_V1(_pgr_binarybreadthfirstsearch)
General_path_element_t::cost
double cost
Definition: general_path_element_t.h:43
pgr_global_report
void pgr_global_report(char *log, char *notice, char *err)
notice & error
Definition: e_report.c:93
General_path_element_t::agg_cost
double agg_cost
Definition: general_path_element_t.h:44