PGROUTING  3.2
topologicalSort.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/topologicalSort/topologicalSort_driver.h"
Include dependency graph for topologicalSort.c:

Go to the source code of this file.

Functions

PGDLLEXPORT Datum _pgr_topologicalsort (PG_FUNCTION_ARGS)
 
 PG_FUNCTION_INFO_V1 (_pgr_topologicalsort)
 
static void process (char *edges_sql, pgr_topologicalSort_t **result_tuples, size_t *result_count)
 

Function Documentation

◆ _pgr_topologicalsort()

PGDLLEXPORT Datum _pgr_topologicalsort ( PG_FUNCTION_ARGS  )

Definition at line 93 of file topologicalSort.c.

93  {
94  FuncCallContext *funcctx;
95  TupleDesc tuple_desc;
96 
97  /**********************************************************************/
98  pgr_topologicalSort_t *result_tuples = NULL;
99  size_t result_count = 0;
100  /**********************************************************************/
101 
102  if (SRF_IS_FIRSTCALL()) {
103  MemoryContext oldcontext;
104  funcctx = SRF_FIRSTCALL_INIT();
105  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
106 
107 
108  /**********************************************************************/
109  // pgr_topologicalSort(
110  // sql TEXT,
111 
112  process(
113  text_to_cstring(PG_GETARG_TEXT_P(0)),
114  &result_tuples,
115  &result_count);
116 
117  /**********************************************************************/
118 
119 #if PGSQL_VERSION > 95
120  funcctx->max_calls = result_count;
121 #else
122  funcctx->max_calls = (uint32_t)result_count;
123 #endif
124 
125  funcctx->user_fctx = result_tuples;
126  if (get_call_result_type(fcinfo, NULL, &tuple_desc)
127  != TYPEFUNC_COMPOSITE) {
128  ereport(ERROR,
129  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
130  errmsg("function returning record called in context "
131  "that cannot accept type record")));
132  }
133 
134  funcctx->tuple_desc = tuple_desc;
135  MemoryContextSwitchTo(oldcontext);
136  }
137 
138  funcctx = SRF_PERCALL_SETUP();
139  tuple_desc = funcctx->tuple_desc;
140  result_tuples = (pgr_topologicalSort_t*) funcctx->user_fctx;
141 
142  if (funcctx->call_cntr < funcctx->max_calls) {
143  HeapTuple tuple;
144  Datum result;
145  Datum *values;
146  bool* nulls;
147  size_t call_cntr = funcctx->call_cntr;
148 
149  /**********************************************************************/
150  // OUT seq INTEGER,
151  // OUT sorted_v BIGINT)
152 
153  size_t numb = 2;
154  values = palloc(numb * sizeof(Datum));
155  nulls = palloc(numb * sizeof(bool));
156 
157  size_t i;
158  for (i = 0; i < numb; ++i) {
159  nulls[i] = false;
160  }
161 
162  values[0] = Int32GetDatum(call_cntr + 1);
163  values[1] = Int64GetDatum(result_tuples[call_cntr].sorted_v);
164  /**********************************************************************/
165 
166  tuple = heap_form_tuple(tuple_desc, values, nulls);
167  result = HeapTupleGetDatum(tuple);
168  SRF_RETURN_NEXT(funcctx, result);
169  } else {
170  SRF_RETURN_DONE(funcctx);
171  }
172 }

References if(), and process().

◆ PG_FUNCTION_INFO_V1()

PG_FUNCTION_INFO_V1 ( _pgr_topologicalsort  )

◆ process()

static void process ( char *  edges_sql,
pgr_topologicalSort_t **  result_tuples,
size_t *  result_count 
)
static

Definition at line 51 of file topologicalSort.c.

54  {
56 
57  pgr_edge_t *edges = NULL;
58  size_t total_edges = 0;
59  pgr_get_edges(edges_sql, &edges, &total_edges);
60 
61  PGR_DBG("Starting timer");
62  clock_t start_t = clock();
63  char* log_msg = NULL;
64  char* notice_msg = NULL;
65  char* err_msg = NULL;
67  edges, total_edges,
68  result_tuples,
69  result_count,
70  &log_msg,
71  &notice_msg,
72  &err_msg);
73 
74  time_msg("processing pgr_topologicalSort", start_t, clock());
75 
76 
77  if (err_msg && (*result_tuples)) {
78  pfree(*result_tuples);
79  (*result_tuples) = NULL;
80  (*result_count) = 0;
81  }
82 
83  pgr_global_report(log_msg, notice_msg, err_msg);
84 
85  if (log_msg) pfree(log_msg);
86  if (notice_msg) pfree(notice_msg);
87  if (err_msg) pfree(err_msg);
88  if (edges) pfree(edges);
90 }

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

Referenced by _pgr_topologicalsort().

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
process
static void process(char *edges_sql, pgr_topologicalSort_t **result_tuples, size_t *result_count)
Definition: topologicalSort.c:51
PGR_DBG
#define PGR_DBG(...)
Definition: debug_macro.h:34
do_pgr_topologicalSort
void do_pgr_topologicalSort(pgr_edge_t *data_edges, size_t total_edges, pgr_topologicalSort_t **return_tuples, size_t *return_count, char **log_msg, char **notice_msg, char **err_msg)
Definition: topologicalSort_driver.cpp:57
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_topologicalSort_t
Definition: pgr_topologicalSort_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
pgr_global_report
void pgr_global_report(char *log, char *notice, char *err)
notice & error
Definition: e_report.c:93