PGROUTING  3.2
topologicalSort.c
Go to the documentation of this file.
1 /*PGR-GNU*****************************************************************
2 
3 File: topologicalSort.c
4 
5 Generated with Template by:
6 Copyright (c) 2015 pgRouting developers
8 
9 Function's developer:
10 Copyright (c) 2019 Hang Wu
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>
32 
34 #include "utils/array.h"
35 
36 
37 #include "c_common/debug_macro.h"
38 #include "c_common/e_report.h"
39 #include "c_common/time_msg.h"
40 #include "c_common/edges_input.h"
41 #include "c_common/arrays_input.h"
43 #if 0
45 #endif
46 PGDLLEXPORT Datum _pgr_topologicalsort(PG_FUNCTION_ARGS);
48 
49 static
50 void
52  char* edges_sql,
53  pgr_topologicalSort_t **result_tuples,
54  size_t *result_count) {
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 }
91 
92 PGDLLEXPORT Datum
93 _pgr_topologicalsort(PG_FUNCTION_ARGS) {
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 }
173 
time_msg.h
postgres_connection.h
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
e_report.h
arrays_input.h
process
static void process(char *edges_sql, pgr_topologicalSort_t **result_tuples, size_t *result_count)
Definition: topologicalSort.c:51
debug_macro.h
topologicalSort_driver.h
_pgr_topologicalsort
PGDLLEXPORT Datum _pgr_topologicalsort(PG_FUNCTION_ARGS)
Definition: topologicalSort.c:93
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
PG_FUNCTION_INFO_V1
PG_FUNCTION_INFO_V1(_pgr_topologicalsort)
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
PG_MODULE_MAGIC
PG_MODULE_MAGIC
Definition: dijkstra.c:49
time_msg
void time_msg(char *msg, clock_t start_t, clock_t end_t)
Definition: time_msg.c:32
edges_input.h
pgr_global_report
void pgr_global_report(char *log, char *notice, char *err)
notice & error
Definition: e_report.c:93