PGROUTING  2.5
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) PGR_DBG("\nDirectedGraph\n");
77  else PGR_DBG("\nUndirectedGraph\n");
79 
80  (*result_tuples) = NULL;
81  (*result_count) = 0;
82 
83  PGR_DBG("Load data");
84  pgr_edge_t *edges = NULL;
85  size_t total_edges = 0;
86 
87  pgr_get_edges(edges_sql, &edges, &total_edges);
88  PGR_DBG("Total %ld edges in query:", total_edges);
89 
90  if (total_edges == 0) {
91  PGR_DBG("No edges found");
93  return;
94  }
95 
96  PGR_DBG("Starting processing");
97  clock_t start_t = clock();
98  char *log_msg = NULL;
99  char *notice_msg = NULL;
100  char *err_msg = NULL;
102  edges,
103  total_edges,
104  directed,
105  result_tuples,
106  result_count,
107  &log_msg,
108  &notice_msg,
109  &err_msg);
110 
111  time_msg(" processing pgr_lineGraph", start_t, clock());
112  PGR_DBG("Returning %ld tuples", *result_count);
113 
114  if (err_msg) {
115  if (*result_tuples) pfree(*result_tuples);
116  }
117  pgr_global_report(log_msg, notice_msg, err_msg);
118 
119  if (edges) pfree(edges);
120  if (log_msg) pfree(log_msg);
121  if (notice_msg) pfree(notice_msg);
122  if (err_msg) pfree(err_msg);
123 
124  pgr_SPI_finish();
125 }
126 /* */
127 /******************************************************************************/
128 
129 PGDLLEXPORT Datum lineGraph(PG_FUNCTION_ARGS) {
130  FuncCallContext *funcctx;
131  TupleDesc tuple_desc;
132 
133  /**************************************************************************/
134  /* MODIFY AS NEEDED */
135  /* */
136  Line_graph_rt *result_tuples = NULL;
137  size_t result_count = 0;
138  /* */
139  /**************************************************************************/
140 
141  if (SRF_IS_FIRSTCALL()) {
142  MemoryContext oldcontext;
143  funcctx = SRF_FIRSTCALL_INIT();
144  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
145 
146 
147  /**********************************************************************/
148  /* MODIFY AS NEEDED */
149  /*
150  TEXT,
151  directed BOOLEAN DEFAULT true,
152  **********************************************************************/
153 
154 
155  PGR_DBG("Calling process");
156  process(
157  text_to_cstring(PG_GETARG_TEXT_P(0)),
158  PG_GETARG_BOOL(1),
159  &result_tuples,
160  &result_count);
161 
162 
163  /* */
164  /**********************************************************************/
165 
166 #if PGSQL_VERSION > 95
167  funcctx->max_calls = result_count;
168 #else
169  funcctx->max_calls = (uint32_t)result_count;
170 #endif
171  funcctx->user_fctx = result_tuples;
172  if (get_call_result_type(fcinfo, NULL, &tuple_desc)
173  != TYPEFUNC_COMPOSITE) {
174  ereport(ERROR,
175  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
176  errmsg("function returning record called in context "
177  "that cannot accept type record")));
178  }
179 
180  funcctx->tuple_desc = tuple_desc;
181  MemoryContextSwitchTo(oldcontext);
182  }
183 
184  funcctx = SRF_PERCALL_SETUP();
185  tuple_desc = funcctx->tuple_desc;
186  result_tuples = (Line_graph_rt*) funcctx->user_fctx;
187 
188  if (funcctx->call_cntr < funcctx->max_calls) {
189  HeapTuple tuple;
190  Datum result;
191  Datum *values;
192  bool* nulls;
193 
194  values = palloc(5 * sizeof(Datum));
195  nulls = palloc(5 * sizeof(bool));
196 
197 
198  size_t i;
199  for (i = 0; i < 5; ++i) {
200  nulls[i] = false;
201  }
202 
203  // postgres starts counting from 1
204  values[0] = Int32GetDatum(funcctx->call_cntr + 1);
205  values[1] = Int64GetDatum(result_tuples[funcctx->call_cntr].source);
206  values[2] = Int64GetDatum(result_tuples[funcctx->call_cntr].target);
207  values[3] = Float8GetDatum(result_tuples[funcctx->call_cntr].cost);
208  values[4] = Float8GetDatum(result_tuples[funcctx->call_cntr].reverse_cost);
209 
210  tuple = heap_form_tuple(tuple_desc, values, nulls);
211  result = HeapTupleGetDatum(tuple);
212  SRF_RETURN_NEXT(funcctx, result);
213  } else {
214  /**********************************************************************/
215  /* MODIFY AS NEEDED */
216 
217  PGR_DBG("Clean up code");
218 
219  /**********************************************************************/
220 
221  SRF_RETURN_DONE(funcctx);
222  }
223 }
static edge_t edges[22573]
int64_t source
Definition: line_graph_rt.h:69
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:129
void pgr_get_edges(char *edges_sql, pgr_edge_t **edges, size_t *total_edges)
basic edge_sql
Definition: edges_input.c:540
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)
if(DOXYGEN_FOUND) configure_file($
Definition: CMakeLists.txt:13
void pgr_SPI_connect(void)
float reverse_cost
Definition: line_graph_rt.h:72
int64_t target
Definition: line_graph_rt.h:70
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)