PGROUTING  3.2
breadthFirstSearch.c File Reference
#include <stdbool.h>
#include "c_common/postgres_connection.h"
#include "utils/array.h"
#include "c_common/debug_macro.h"
#include "c_common/e_report.h"
#include "c_common/time_msg.h"
#include "c_common/edges_input.h"
#include "c_common/arrays_input.h"
#include "drivers/breadthFirstSearch/breadthFirstSearch_driver.h"
Include dependency graph for breadthFirstSearch.c:

Go to the source code of this file.

Functions

PGDLLEXPORT Datum _pgr_breadthfirstsearch (PG_FUNCTION_ARGS)
 
 PG_FUNCTION_INFO_V1 (_pgr_breadthfirstsearch)
 
static void process (char *edges_sql, ArrayType *starts, int64_t max_depth, bool directed, pgr_mst_rt **result_tuples, size_t *result_count)
 

Function Documentation

◆ _pgr_breadthfirstsearch()

PGDLLEXPORT Datum _pgr_breadthfirstsearch ( PG_FUNCTION_ARGS  )

Definition at line 126 of file breadthFirstSearch.c.

126  {
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 }

References pgr_mst_rt::agg_cost, pgr_mst_rt::cost, pgr_mst_rt::depth, pgr_mst_rt::edge, pgr_mst_rt::from_v, if(), pgr_mst_rt::node, PGR_DBG, and process().

◆ PG_FUNCTION_INFO_V1()

PG_FUNCTION_INFO_V1 ( _pgr_breadthfirstsearch  )

◆ process()

static void process ( char *  edges_sql,
ArrayType *  starts,
int64_t  max_depth,
bool  directed,
pgr_mst_rt **  result_tuples,
size_t *  result_count 
)
static

Definition at line 48 of file breadthFirstSearch.c.

55  {
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 }

References do_pgr_breadthFirstSearch(), PGR_DBG, pgr_get_bigIntArray(), pgr_get_edges(), pgr_global_report(), pgr_SPI_connect(), pgr_SPI_finish(), and time_msg().

Referenced by _pgr_breadthfirstsearch().

pgr_mst_rt
Definition: pgr_mst_rt.h:36
pgr_edge_t
Definition: pgr_edge_t.h:37
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
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
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
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_global_report
void pgr_global_report(char *log, char *notice, char *err)
notice & error
Definition: e_report.c:93