PGROUTING  3.2
edwardMoore.c
Go to the documentation of this file.
1 /*PGR-GNU*****************************************************************
2 File: edwardMoore.c
3 
4 Generated with Template by:
5 Copyright (c) 2019 pgRouting developers
7 
8 Function's developer:
9 Copyright (c) 2019 Gudesa Venkata Sai Akhil
11 
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 "utils/array.h"
34 
35 #include "c_common/debug_macro.h"
36 #include "c_common/e_report.h"
37 #include "c_common/time_msg.h"
38 
39 #include "c_common/edges_input.h"
40 #include "c_common/arrays_input.h"
42 
44 
45 PGDLLEXPORT Datum _pgr_edwardmoore(PG_FUNCTION_ARGS);
47 
48 static void
50  char *edges_sql,
51  char *combinations_sql,
52  ArrayType *starts,
53  ArrayType *ends,
54  bool directed,
55 
56  General_path_element_t **result_tuples,
57  size_t *result_count) {
59 
60  PGR_DBG("Initializing arrays");
61 
62  size_t size_start_vidsArr = 0;
63  int64_t *start_vidsArr = NULL;
64 
65  size_t size_end_vidsArr = 0;
66  int64_t *end_vidsArr = NULL;
67 
68  size_t total_combinations = 0;
69  pgr_combination_t *combinations = NULL;
70 
71  if (starts && ends) {
72  start_vidsArr = (int64_t*)
73  pgr_get_bigIntArray(&size_start_vidsArr, starts);
74  end_vidsArr = (int64_t*)
75  pgr_get_bigIntArray(&size_end_vidsArr, ends);
76  } else if (combinations_sql) {
77  pgr_get_combinations(combinations_sql, &combinations, &total_combinations);
78  if (total_combinations == 0) {
79  if (combinations)
80  pfree(combinations);
82  return;
83  }
84  }
85 
86  (*result_tuples) = NULL;
87  (*result_count) = 0;
88 
89  PGR_DBG("Load data");
90  pgr_edge_t *edges = NULL;
91  size_t total_edges = 0;
92 
93  pgr_get_edges(edges_sql, &edges, &total_edges);
94  PGR_DBG("Total %ld edges in query:", total_edges);
95 
96  if (total_edges == 0) {
97  if (start_vidsArr)
98  pfree(start_vidsArr);
99  if (end_vidsArr)
100  pfree(end_vidsArr);
101  pgr_SPI_finish();
102  return;
103  }
104 
105  PGR_DBG("Starting processing");
106  clock_t start_t = clock();
107  char *log_msg = NULL;
108  char *notice_msg = NULL;
109  char *err_msg = NULL;
111  edges,
112  total_edges,
113  combinations,
114  total_combinations,
115  start_vidsArr, size_start_vidsArr,
116  end_vidsArr, size_end_vidsArr,
117 
118  directed,
119 
120  result_tuples,
121  result_count,
122 
123  &log_msg,
124  &notice_msg,
125  &err_msg);
126 
127  time_msg(" processing pgr_edwardMoore", start_t, clock());
128  PGR_DBG("Returning %ld tuples", *result_count);
129 
130  if (err_msg) {
131  if (*result_tuples)
132  pfree(*result_tuples);
133  }
134 
135  pgr_global_report(log_msg, notice_msg, err_msg);
136 
137  if (edges)
138  pfree(edges);
139  if (log_msg)
140  pfree(log_msg);
141  if (notice_msg)
142  pfree(notice_msg);
143  if (err_msg)
144  pfree(err_msg);
145 
146  if (start_vidsArr)
147  pfree(start_vidsArr);
148  if (end_vidsArr)
149  pfree(end_vidsArr);
150  pgr_SPI_finish();
151 }
152 
153 PGDLLEXPORT Datum _pgr_edwardmoore(PG_FUNCTION_ARGS) {
154  FuncCallContext *funcctx;
155  TupleDesc tuple_desc;
156 
157  /**************************************************************************/
158  General_path_element_t *result_tuples = NULL;
159  size_t result_count = 0;
160  /**************************************************************************/
161 
162  if (SRF_IS_FIRSTCALL()) {
163  MemoryContext oldcontext;
164  funcctx = SRF_FIRSTCALL_INIT();
165  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
166 
167  PGR_DBG("Calling process");
168  if (PG_NARGS() == 4) {
169  /*
170  * many to many
171  */
172  process(
173  text_to_cstring(PG_GETARG_TEXT_P(0)),
174  NULL,
175  PG_GETARG_ARRAYTYPE_P(1),
176  PG_GETARG_ARRAYTYPE_P(2),
177  PG_GETARG_BOOL(3),
178  &result_tuples,
179  &result_count);
180 
181  } else if (PG_NARGS() == 3) {
182  /*
183  * combinations
184  */
185  process(
186  text_to_cstring(PG_GETARG_TEXT_P(0)),
187  text_to_cstring(PG_GETARG_TEXT_P(1)),
188  NULL,
189  NULL,
190  PG_GETARG_BOOL(2),
191  &result_tuples,
192  &result_count);
193  }
194 
195 
196  /**********************************************************************/
197 
198 #if PGSQL_VERSION > 95
199  funcctx->max_calls = result_count;
200 #else
201  funcctx->max_calls = (uint32_t)result_count;
202 #endif
203  funcctx->user_fctx = result_tuples;
204  if (get_call_result_type(fcinfo, NULL, &tuple_desc) != TYPEFUNC_COMPOSITE) {
205  ereport(ERROR,
206  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
207  errmsg("function returning record called in context "
208  "that cannot accept type record")));
209  }
210 
211  funcctx->tuple_desc = tuple_desc;
212  MemoryContextSwitchTo(oldcontext);
213  }
214 
215  funcctx = SRF_PERCALL_SETUP();
216  tuple_desc = funcctx->tuple_desc;
217  result_tuples = (General_path_element_t *)funcctx->user_fctx;
218 
219  if (funcctx->call_cntr < funcctx->max_calls) {
220  HeapTuple tuple;
221  Datum result;
222  Datum *values;
223  bool *nulls;
224 
225  /**********************************************************************/
226  /*
227  OUT seq BIGINT,
228  OUT path_seq BIGINT,
229  OUT start_vid BIGINT,
230  OUT end_vid BIGINT,
231  OUT node BIGINT,
232  OUT edge BIGINT,
233  OUT cost FLOAT,
234  OUT agg_cost FLOAT
235  */
236  /**********************************************************************/
237  size_t numb = 8;
238  values = palloc(numb * sizeof(Datum));
239  nulls = palloc(numb * sizeof(bool));
240 
241  size_t i;
242  for (i = 0; i < numb; ++i) {
243  nulls[i] = false;
244  }
245 
246  values[0] = Int32GetDatum(funcctx->call_cntr + 1);
247  values[1] = Int32GetDatum(result_tuples[funcctx->call_cntr].seq);
248  values[2] = Int64GetDatum(result_tuples[funcctx->call_cntr].start_id);
249  values[3] = Int64GetDatum(result_tuples[funcctx->call_cntr].end_id);
250  values[4] = Int64GetDatum(result_tuples[funcctx->call_cntr].node);
251  values[5] = Int64GetDatum(result_tuples[funcctx->call_cntr].edge);
252  values[6] = Float8GetDatum(result_tuples[funcctx->call_cntr].cost);
253  values[7] = Float8GetDatum(result_tuples[funcctx->call_cntr].agg_cost);
254 
255  /**********************************************************************/
256 
257  tuple = heap_form_tuple(tuple_desc, values, nulls);
258  result = HeapTupleGetDatum(tuple);
259  SRF_RETURN_NEXT(funcctx, result);
260  } else {
261  /**********************************************************************/
262 
263  PGR_DBG("Clean up code");
264 
265  /**********************************************************************/
266 
267  SRF_RETURN_DONE(funcctx);
268  }
269 }
combinations_input.h
General_path_element_t::edge
int64_t edge
Definition: general_path_element_t.h:42
time_msg.h
postgres_connection.h
pgr_edge_t
Definition: pgr_edge_t.h:37
_pgr_edwardmoore
PGDLLEXPORT Datum _pgr_edwardmoore(PG_FUNCTION_ARGS)
Definition: edwardMoore.c:153
pgr_SPI_connect
void pgr_SPI_connect(void)
Definition: postgres_connection.c:82
General_path_element_t::seq
int seq
Definition: general_path_element_t.h:38
pgr_SPI_finish
void pgr_SPI_finish(void)
Definition: postgres_connection.c:71
e_report.h
arrays_input.h
General_path_element_t::start_id
int64_t start_id
Definition: general_path_element_t.h:39
edwardMoore_driver.h
process
static void process(char *edges_sql, char *combinations_sql, ArrayType *starts, ArrayType *ends, bool directed, General_path_element_t **result_tuples, size_t *result_count)
Definition: edwardMoore.c:49
debug_macro.h
pgr_get_bigIntArray
int64_t * pgr_get_bigIntArray(size_t *arrlen, ArrayType *input)
Enforces the input array to be NOT empty.
Definition: arrays_input.c:146
PGR_DBG
#define PGR_DBG(...)
Definition: debug_macro.h:34
pgr_combination_t
Definition: pgr_combination_t.h:43
General_path_element_t::node
int64_t node
Definition: general_path_element_t.h:41
pgr_get_combinations
void pgr_get_combinations(char *combinations_sql, pgr_combination_t **combinations, size_t *total_combinations)
combinations_sql
Definition: combinations_input.c:147
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
General_path_element_t
Definition: general_path_element_t.h:37
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
General_path_element_t::end_id
int64_t end_id
Definition: general_path_element_t.h:40
edges_input.h
do_pgr_edwardMoore
void do_pgr_edwardMoore(pgr_edge_t *data_edges, size_t total_edges, pgr_combination_t *combinations, size_t total_combinations, int64_t *start_vidsArr, size_t size_start_vidsArr, int64_t *end_vidsArr, size_t size_end_vidsArr, bool directed, General_path_element_t **return_tuples, size_t *return_count, char **log_msg, char **notice_msg, char **err_msg)
Definition: edwardMoore_driver.cpp:82
General_path_element_t::cost
double cost
Definition: general_path_element_t.h:43
pgr_global_report
void pgr_global_report(char *log, char *notice, char *err)
notice & error
Definition: e_report.c:93
PG_FUNCTION_INFO_V1
PG_FUNCTION_INFO_V1(_pgr_edwardmoore)
General_path_element_t::agg_cost
double agg_cost
Definition: general_path_element_t.h:44