PGROUTING  3.2
lineGraph.c
Go to the documentation of this file.
1 /*PGR-GNU*****************************************************************
2 File: lineGraph.c
3 
4 Generated with Template by:
5 Copyright (c) 2015 pgRouting developers
7 
8 Function's developer:
9 Copyright (c) 2017 Vidhan Jain
11 
12 ------
13 
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation; either version 2 of the License, or
17 (at your option) any later version.
18 
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
23 
24 You should have received a copy of the GNU General Public License
25 along with this program; if not, write to the Free Software
26 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 
28  ********************************************************************PGR-GNU*/
29 
45 #include <stdbool.h>
47 
48 
49 /* for macro PGR_DBG */
50 #include "c_common/debug_macro.h"
51 /* for pgr_global_report */
52 #include "c_common/e_report.h"
53 /* for time_msg & clock */
54 #include "c_common/time_msg.h"
55 /* for functions to get edges information */
56 #include "c_common/edges_input.h"
57 
58 #include "drivers/lineGraph/lineGraph_driver.h" // the link to the C++ code of the function
59 
60 PGDLLEXPORT Datum _pgr_linegraph(PG_FUNCTION_ARGS);
62 
63 
64 /******************************************************************************/
65 /* MODIFY AS NEEDED */
66 static
67 void
69  char* edges_sql,
70  bool directed,
71  Line_graph_rt **result_tuples,
72  size_t *result_count) {
73  /*
74  * https://www.postgresql.org/docs/current/static/spi-spi-connect.html
75  */
76  PGR_DBG("\nSQL QUERY: %s\n", edges_sql);
77  if (directed) {
78  PGR_DBG("\nDirectedGraph\n");
79  } else {
80  PGR_DBG("\nUndirectedGraph\n");
81  }
83 
84  (*result_tuples) = NULL;
85  (*result_count) = 0;
86 
87  PGR_DBG("Load data");
88  pgr_edge_t *edges = NULL;
89  size_t total_edges = 0;
90 
91  pgr_get_edges(edges_sql, &edges, &total_edges);
92  PGR_DBG("Total %ld edges in query:", total_edges);
93 
94  if (total_edges == 0) {
95  PGR_DBG("No edges found");
97  return;
98  }
99 
100  PGR_DBG("Starting processing");
101  clock_t start_t = clock();
102  char *log_msg = NULL;
103  char *notice_msg = NULL;
104  char *err_msg = NULL;
106  edges,
107  total_edges,
108  directed,
109  result_tuples,
110  result_count,
111  &log_msg,
112  &notice_msg,
113  &err_msg);
114 
115  time_msg(" processing pgr_lineGraph", start_t, clock());
116  PGR_DBG("Returning %ld tuples", *result_count);
117 
118  if (err_msg) {
119  if (*result_tuples) pfree(*result_tuples);
120  }
121  pgr_global_report(log_msg, notice_msg, err_msg);
122 
123  if (edges) pfree(edges);
124  if (log_msg) pfree(log_msg);
125  if (notice_msg) pfree(notice_msg);
126  if (err_msg) pfree(err_msg);
127 
128  pgr_SPI_finish();
129 }
130 /* */
131 /******************************************************************************/
132 
133 PGDLLEXPORT Datum _pgr_linegraph(PG_FUNCTION_ARGS) {
134  FuncCallContext *funcctx;
135  TupleDesc tuple_desc;
136 
137  /**************************************************************************/
138  /* MODIFY AS NEEDED */
139  /* */
140  Line_graph_rt *result_tuples = NULL;
141  size_t result_count = 0;
142  /* */
143  /**************************************************************************/
144 
145  if (SRF_IS_FIRSTCALL()) {
146  MemoryContext oldcontext;
147  funcctx = SRF_FIRSTCALL_INIT();
148  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
149 
150 
151  /**********************************************************************/
152  /* MODIFY AS NEEDED */
153  /*
154  TEXT,
155  directed BOOLEAN DEFAULT true,
156  **********************************************************************/
157 
158 
159  PGR_DBG("Calling process");
160  process(
161  text_to_cstring(PG_GETARG_TEXT_P(0)),
162  PG_GETARG_BOOL(1),
163  &result_tuples,
164  &result_count);
165 
166 
167  /* */
168  /**********************************************************************/
169 
170 #if PGSQL_VERSION > 95
171  funcctx->max_calls = result_count;
172 #else
173  funcctx->max_calls = (uint32_t)result_count;
174 #endif
175  funcctx->user_fctx = result_tuples;
176  if (get_call_result_type(fcinfo, NULL, &tuple_desc)
177  != TYPEFUNC_COMPOSITE) {
178  ereport(ERROR,
179  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
180  errmsg("function returning record called in context "
181  "that cannot accept type record")));
182  }
183 
184  funcctx->tuple_desc = tuple_desc;
185  MemoryContextSwitchTo(oldcontext);
186  }
187 
188  funcctx = SRF_PERCALL_SETUP();
189  tuple_desc = funcctx->tuple_desc;
190  result_tuples = (Line_graph_rt*) funcctx->user_fctx;
191 
192  if (funcctx->call_cntr < funcctx->max_calls) {
193  HeapTuple tuple;
194  Datum result;
195  Datum *values;
196  bool* nulls;
197 
198  values = palloc(5 * sizeof(Datum));
199  nulls = palloc(5 * sizeof(bool));
200 
201 
202  size_t i;
203  for (i = 0; i < 5; ++i) {
204  nulls[i] = false;
205  }
206 
207  // postgres starts counting from 1
208  values[0] = Int32GetDatum(funcctx->call_cntr + 1);
209  values[1] = Int64GetDatum(result_tuples[funcctx->call_cntr].source);
210  values[2] = Int64GetDatum(result_tuples[funcctx->call_cntr].target);
211  values[3] = Float8GetDatum(result_tuples[funcctx->call_cntr].cost);
212  values[4] = Float8GetDatum(result_tuples[
213  funcctx->call_cntr].reverse_cost);
214 
215  tuple = heap_form_tuple(tuple_desc, values, nulls);
216  result = HeapTupleGetDatum(tuple);
217  SRF_RETURN_NEXT(funcctx, result);
218  } else {
219  /**********************************************************************/
220  /* MODIFY AS NEEDED */
221 
222  PGR_DBG("Clean up code");
223 
224  /**********************************************************************/
225 
226  SRF_RETURN_DONE(funcctx);
227  }
228 }
time_msg.h
Line_graph_rt::reverse_cost
double reverse_cost
Definition: line_graph_rt.h:47
do_pgr_lineGraph
void do_pgr_lineGraph(pgr_edge_t *data_edges, size_t total_edges, bool directed, Line_graph_rt **return_tuples, size_t *return_count, char **log_msg, char **notice_msg, char **err_msg)
Definition: lineGraph_driver.cpp:60
postgres_connection.h
Line_graph_rt::cost
double cost
Definition: line_graph_rt.h:46
pgr_edge_t
Definition: pgr_edge_t.h:37
pgr_SPI_connect
void pgr_SPI_connect(void)
Definition: postgres_connection.c:82
PG_FUNCTION_INFO_V1
PG_FUNCTION_INFO_V1(_pgr_linegraph)
pgr_SPI_finish
void pgr_SPI_finish(void)
Definition: postgres_connection.c:71
e_report.h
Line_graph_rt
Definition: line_graph_rt.h:42
debug_macro.h
PGR_DBG
#define PGR_DBG(...)
Definition: debug_macro.h:34
process
static void process(char *edges_sql, bool directed, Line_graph_rt **result_tuples, size_t *result_count)
Definition: lineGraph.c:68
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
Line_graph_rt::target
int64_t target
Definition: line_graph_rt.h:45
time_msg
void time_msg(char *msg, clock_t start_t, clock_t end_t)
Definition: time_msg.c:32
Line_graph_rt::source
int64_t source
Definition: line_graph_rt.h:44
edges_input.h
lineGraph_driver.h
pgr_global_report
void pgr_global_report(char *log, char *notice, char *err)
notice & error
Definition: e_report.c:93
_pgr_linegraph
PGDLLEXPORT Datum _pgr_linegraph(PG_FUNCTION_ARGS)
postgres_connection.h
Definition: lineGraph.c:133