PGROUTING  2.6-dev
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
6 Mail: project@pgrouting.org
7 
8 Function's developer:
9 Copyright (c) 2017 Vidhan Jain
10 Mail: vidhanj1307@gmail.com
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 
46 
47 
48 /* for macro PGR_DBG */
49 #include "c_common/debug_macro.h"
50 /* for pgr_global_report */
51 #include "c_common/e_report.h"
52 /* for time_msg & clock */
53 #include "c_common/time_msg.h"
54 /* for functions to get edges information */
55 #include "c_common/edges_input.h"
56 
57 #include "drivers/lineGraph/lineGraph_driver.h" // the link to the C++ code of the function
58 
59 PGDLLEXPORT Datum lineGraph(PG_FUNCTION_ARGS);
61 
62 
63 /******************************************************************************/
64 /* MODIFY AS NEEDED */
65 static
66 void
68  char* edges_sql,
69  bool directed,
70  Line_graph_rt **result_tuples,
71  size_t *result_count) {
72  /*
73  * https://www.postgresql.org/docs/current/static/spi-spi-connect.html
74  */
75  PGR_DBG("\nSQL QUERY: %s\n", edges_sql);
76  if (directed) {
77  PGR_DBG("\nDirectedGraph\n");
78  } else {
79  PGR_DBG("\nUndirectedGraph\n");
80  }
82 
83  (*result_tuples) = NULL;
84  (*result_count) = 0;
85 
86  PGR_DBG("Load data");
87  pgr_edge_t *edges = NULL;
88  size_t total_edges = 0;
89 
90  pgr_get_edges(edges_sql, &edges, &total_edges);
91  PGR_DBG("Total %ld edges in query:", total_edges);
92 
93  if (total_edges == 0) {
94  PGR_DBG("No edges found");
96  return;
97  }
98 
99  PGR_DBG("Starting processing");
100  clock_t start_t = clock();
101  char *log_msg = NULL;
102  char *notice_msg = NULL;
103  char *err_msg = NULL;
105  edges,
106  total_edges,
107  directed,
108  result_tuples,
109  result_count,
110  &log_msg,
111  &notice_msg,
112  &err_msg);
113 
114  time_msg(" processing pgr_lineGraph", start_t, clock());
115  PGR_DBG("Returning %ld tuples", *result_count);
116 
117  if (err_msg) {
118  if (*result_tuples) pfree(*result_tuples);
119  }
120  pgr_global_report(log_msg, notice_msg, err_msg);
121 
122  if (edges) pfree(edges);
123  if (log_msg) pfree(log_msg);
124  if (notice_msg) pfree(notice_msg);
125  if (err_msg) pfree(err_msg);
126 
127  pgr_SPI_finish();
128 }
129 /* */
130 /******************************************************************************/
131 
132 PGDLLEXPORT Datum lineGraph(PG_FUNCTION_ARGS) {
133  FuncCallContext *funcctx;
134  TupleDesc tuple_desc;
135 
136  /**************************************************************************/
137  /* MODIFY AS NEEDED */
138  /* */
139  Line_graph_rt *result_tuples = NULL;
140  size_t result_count = 0;
141  /* */
142  /**************************************************************************/
143 
144  if (SRF_IS_FIRSTCALL()) {
145  MemoryContext oldcontext;
146  funcctx = SRF_FIRSTCALL_INIT();
147  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
148 
149 
150  /**********************************************************************/
151  /* MODIFY AS NEEDED */
152  /*
153  TEXT,
154  directed BOOLEAN DEFAULT true,
155  **********************************************************************/
156 
157 
158  PGR_DBG("Calling process");
159  process(
160  text_to_cstring(PG_GETARG_TEXT_P(0)),
161  PG_GETARG_BOOL(1),
162  &result_tuples,
163  &result_count);
164 
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 = (Line_graph_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  values = palloc(5 * sizeof(Datum));
198  nulls = palloc(5 * sizeof(bool));
199 
200 
201  size_t i;
202  for (i = 0; i < 5; ++i) {
203  nulls[i] = false;
204  }
205 
206  // postgres starts counting from 1
207  values[0] = Int32GetDatum(funcctx->call_cntr + 1);
208  values[1] = Int64GetDatum(result_tuples[funcctx->call_cntr].source);
209  values[2] = Int64GetDatum(result_tuples[funcctx->call_cntr].target);
210  values[3] = Float8GetDatum(result_tuples[funcctx->call_cntr].cost);
211  values[4] = Float8GetDatum(result_tuples[
212  funcctx->call_cntr].reverse_cost);
213 
214  tuple = heap_form_tuple(tuple_desc, values, nulls);
215  result = HeapTupleGetDatum(tuple);
216  SRF_RETURN_NEXT(funcctx, result);
217  } else {
218  /**********************************************************************/
219  /* MODIFY AS NEEDED */
220 
221  PGR_DBG("Clean up code");
222 
223  /**********************************************************************/
224 
225  SRF_RETURN_DONE(funcctx);
226  }
227 }
int64_t source
Definition: line_graph_rt.h:65
PG_FUNCTION_INFO_V1(lineGraph)
#define PGR_DBG(...)
Definition: debug_macro.h:34
PGDLLEXPORT Datum lineGraph(PG_FUNCTION_ARGS)
postgres_connection.h
Definition: lineGraph.c:132
void pgr_get_edges(char *edges_sql, pgr_edge_t **edges, size_t *total_edges)
basic edge_sql
Definition: edges_input.c:545
static void process(char *edges_sql, bool directed, Line_graph_rt **result_tuples, size_t *result_count)
Definition: lineGraph.c:67
void time_msg(char *msg, clock_t start_t, clock_t end_t)
Definition: time_msg.c:32
void pgr_global_report(char *log, char *notice, char *err)
notice & error
Definition: e_report.c:93
void pgr_SPI_finish(void)
double reverse_cost
Definition: line_graph_rt.h:68
if(DOXYGEN_FOUND) configure_file($
Definition: CMakeLists.txt:13
void pgr_SPI_connect(void)
int64_t target
Definition: line_graph_rt.h:66
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)