PGROUTING  3.2
lineGraphFull.c
Go to the documentation of this file.
1 /*PGR-GNU*****************************************************************
2 File: lineGraphFull.c
3 
4 
5 Generated with Template by:
6 Copyright (c) 2015 pgRouting developers
8 
9 Function's developer:
10 Copyright (c) 2017 Anthony Nicola Tasca
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 "c_common/debug_macro.h"
34 #include "c_common/e_report.h"
35 #include "c_common/time_msg.h"
36 #include "c_common/edges_input.h"
38 
39 PGDLLEXPORT Datum _pgr_linegraphfull(PG_FUNCTION_ARGS);
41 
42 static
43 void
45  char* edges_sql,
46  Line_graph_full_rt **result_tuples,
47  size_t *result_count) {
48  PGR_DBG("\nSQL QUERY: %s\n", edges_sql);
49  PGR_DBG("\nDirectedGraph\n");
51 
52  (*result_tuples) = NULL;
53  (*result_count) = 0;
54 
55  PGR_DBG("Load data");
56  pgr_edge_t *edges = NULL;
57  size_t total_edges = 0;
58 
59  pgr_get_edges(edges_sql, &edges, &total_edges);
60  PGR_DBG("Total %ld edges in query:", total_edges);
61 
62  if (total_edges == 0) {
63  PGR_DBG("No edges found");
65  return;
66  }
67 
68  PGR_DBG("Starting processing");
69  clock_t start_t = clock();
70  char *log_msg = NULL;
71  char *notice_msg = NULL;
72  char *err_msg = NULL;
74  edges,
75  total_edges,
76  result_tuples,
77  result_count,
78  &log_msg,
79  &notice_msg,
80  &err_msg);
81 
82  time_msg(" processing pgr_lineGraphFull", start_t, clock());
83  PGR_DBG("Returning %ld tuples", *result_count);
84 
85  if (err_msg) {
86  if (*result_tuples) pfree(*result_tuples);
87  }
88 
89  pgr_global_report(log_msg, notice_msg, err_msg);
90 
91  if (edges) pfree(edges);
92  if (log_msg) pfree(log_msg);
93  if (notice_msg) pfree(notice_msg);
94  if (err_msg) pfree(err_msg);
95 
97 }
98 /* */
99 /******************************************************************************/
100 
101 PGDLLEXPORT Datum _pgr_linegraphfull(PG_FUNCTION_ARGS) {
102  FuncCallContext *funcctx;
103  TupleDesc tuple_desc;
104 
105  Line_graph_full_rt *result_tuples = NULL;
106  size_t result_count = 0;
107 
108  if (SRF_IS_FIRSTCALL()) {
109  MemoryContext oldcontext;
110  funcctx = SRF_FIRSTCALL_INIT();
111  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
112 
113  PGR_DBG("Calling process");
114  process(text_to_cstring(PG_GETARG_TEXT_P(0)),
115  &result_tuples,
116  &result_count);
117 
118 #if PGSQL_VERSION > 95
119  funcctx->max_calls = result_count;
120 #else
121  funcctx->max_calls = (uint32_t)result_count;
122 #endif
123  funcctx->user_fctx = result_tuples;
124  if (get_call_result_type(fcinfo, NULL, &tuple_desc)
125  != TYPEFUNC_COMPOSITE) {
126  ereport(ERROR,
127  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
128  errmsg("function returning record called in context "
129  "that cannot accept type record")));
130  }
131 
132  funcctx->tuple_desc = tuple_desc;
133  MemoryContextSwitchTo(oldcontext);
134  }
135 
136  funcctx = SRF_PERCALL_SETUP();
137  tuple_desc = funcctx->tuple_desc;
138  result_tuples = (Line_graph_full_rt*) funcctx->user_fctx;
139 
140  if (funcctx->call_cntr < funcctx->max_calls) {
141  HeapTuple tuple;
142  Datum result;
143  Datum *values;
144  bool* nulls;
145 
146  values = palloc(5 * sizeof(Datum));
147  nulls = palloc(5 * sizeof(bool));
148 
149 
150  size_t i;
151  for (i = 0; i < 5; ++i) {
152  nulls[i] = false;
153  }
154 
155  size_t c_cntr = funcctx->call_cntr;
156 
157  values[0] = Int32GetDatum(c_cntr + 1);
158  values[1] = Int64GetDatum(result_tuples[c_cntr].source);
159  values[2] = Int64GetDatum(result_tuples[c_cntr].target);
160  values[3] = Float8GetDatum(result_tuples[c_cntr].cost);
161  values[4] = Int64GetDatum(result_tuples[c_cntr].edge);
162 
163  tuple = heap_form_tuple(tuple_desc, values, nulls);
164  result = HeapTupleGetDatum(tuple);
165  SRF_RETURN_NEXT(funcctx, result);
166  } else {
167  PGR_DBG("Clean up code");
168  SRF_RETURN_DONE(funcctx);
169  }
170 }
time_msg.h
postgres_connection.h
_pgr_linegraphfull
PGDLLEXPORT Datum _pgr_linegraphfull(PG_FUNCTION_ARGS)
Definition: lineGraphFull.c:101
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
process
static void process(char *edges_sql, Line_graph_full_rt **result_tuples, size_t *result_count)
Definition: lineGraphFull.c:44
edge
Definition: trsp.h:41
debug_macro.h
PG_FUNCTION_INFO_V1
PG_FUNCTION_INFO_V1(_pgr_linegraphfull)
PGR_DBG
#define PGR_DBG(...)
Definition: debug_macro.h:34
lineGraphFull_driver.h
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
do_pgr_lineGraphFull
void do_pgr_lineGraphFull(pgr_edge_t *data_edges, size_t total_edges, Line_graph_full_rt **return_tuples, size_t *return_count, char **log_msg, char **notice_msg, char **err_msg)
Definition: lineGraphFull_driver.cpp:62
edges_input.h
pgr_global_report
void pgr_global_report(char *log, char *notice, char *err)
notice & error
Definition: e_report.c:93
Line_graph_full_rt
Definition: line_graph_full_rt.h:42