PGROUTING  3.2
depthFirstSearch.c File Reference

Connecting code with postgres. More...

#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/traversal/depthFirstSearch_driver.h"
Include dependency graph for depthFirstSearch.c:

Go to the source code of this file.

Functions

PGDLLEXPORT Datum _pgr_depthfirstsearch (PG_FUNCTION_ARGS)
 Helps in converting postgres variables to C variables, and returns the result. More...
 
 PG_FUNCTION_INFO_V1 (_pgr_depthfirstsearch)
 
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. More...
 

Detailed Description

Connecting code with postgres.

Definition in file depthFirstSearch.c.

Function Documentation

◆ _pgr_depthfirstsearch()

PGDLLEXPORT Datum _pgr_depthfirstsearch ( PG_FUNCTION_ARGS  )

Helps in converting postgres variables to C variables, and returns the result.

Definition at line 133 of file depthFirstSearch.c.

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

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, and process().

◆ PG_FUNCTION_INFO_V1()

PG_FUNCTION_INFO_V1 ( _pgr_depthfirstsearch  )

◆ 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

Static function, loads the data from postgres to C types for further processing.

It first connects the C function to the SPI manager. Then converts the postgres array to C array and loads the edges belonging to the graph in C types. Then it calls the function do_pgr_depthFirstSearch defined in the depthFirstSearch_driver.h file for further processing. Finally, it frees the memory and disconnects the C function to the SPI manager.

Parameters
edges_sqlthe edges of the SQL query
rootsthe root vertices
directedwhether the graph is directed or undirected
max_depththe maximum depth of traversal
result_tuplesthe rows in the result
result_countthe count of rows in the result
Returns
void

Definition at line 69 of file depthFirstSearch.c.

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

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

Referenced by _pgr_depthfirstsearch().

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