PGROUTING  3.2
boyerMyrvold.c
Go to the documentation of this file.
1 /*PGR-GNU*****************************************************************
2 File: boyerMyrvold.c
3 
4 Generated with Template by:
5 Copyright (c) 2019 pgRouting developers
7 
8 Function's developer:
9 Copyright (c) 2020 Himanshu Raj
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"
41 
43 PGDLLEXPORT Datum _pgr_boyermyrvold(PG_FUNCTION_ARGS);
45 
46 static void
48  char *edges_sql,
49 
50  pgr_boyer_t **result_tuples,
51  size_t *result_count) {
53 
54  PGR_DBG("Initializing arrays");
55 
56 
57  (*result_tuples) = NULL;
58  (*result_count) = 0;
59 
60  PGR_DBG("Load data");
61  pgr_edge_t *edges = NULL;
62  size_t total_edges = 0;
63 
64  pgr_get_edges(edges_sql, &edges, &total_edges);
65  PGR_DBG("Total %ld edges in query:", total_edges);
66 
67  if (total_edges == 0) {
69  return;
70  }
71 
72  PGR_DBG("Starting processing");
73  clock_t start_t = clock();
74  char *log_msg = NULL;
75  char *notice_msg = NULL;
76  char *err_msg = NULL;
78  edges,
79  total_edges,
80 
81  result_tuples,
82  result_count,
83 
84  &log_msg,
85  &notice_msg,
86  &err_msg);
87 
88  time_msg(" processing pgr_boyerMyrvold", start_t, clock());
89  PGR_DBG("Returning %ld tuples", *result_count);
90 
91  if (err_msg) {
92  if (*result_tuples)
93  pfree(*result_tuples);
94  }
95 
96  pgr_global_report(log_msg, notice_msg, err_msg);
97 
98  if (edges)
99  pfree(edges);
100  if (log_msg)
101  pfree(log_msg);
102  if (notice_msg)
103  pfree(notice_msg);
104  if (err_msg)
105  pfree(err_msg);
106 
107  pgr_SPI_finish();
108 }
109 
110 PGDLLEXPORT Datum _pgr_boyermyrvold(PG_FUNCTION_ARGS) {
111  FuncCallContext *funcctx;
112  TupleDesc tuple_desc;
113 
114  /**************************************************************************/
115  pgr_boyer_t *result_tuples = NULL;
116  size_t result_count = 0;
117  /**************************************************************************/
118 
119  if (SRF_IS_FIRSTCALL()) {
120  MemoryContext oldcontext;
121  funcctx = SRF_FIRSTCALL_INIT();
122  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
123 
124  /**********************************************************************/
125  /*
126  pgr_boyerMyrvold(
127  edge_sql TEXT)
128  */
129  /**********************************************************************/
130 
131  PGR_DBG("Calling process");
132  process(
133  text_to_cstring(PG_GETARG_TEXT_P(0)),
134  &result_tuples,
135  &result_count);
136 
137  /**********************************************************************/
138 
139 #if PGSQL_VERSION > 95
140  funcctx->max_calls = result_count;
141 #else
142  funcctx->max_calls = (uint32_t)result_count;
143 #endif
144  funcctx->user_fctx = result_tuples;
145  if (get_call_result_type(fcinfo, NULL, &tuple_desc) != TYPEFUNC_COMPOSITE) {
146  ereport(ERROR,
147  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
148  errmsg("function returning record called in context "
149  "that cannot accept type record")));
150  }
151 
152  funcctx->tuple_desc = tuple_desc;
153  MemoryContextSwitchTo(oldcontext);
154  }
155 
156  funcctx = SRF_PERCALL_SETUP();
157  tuple_desc = funcctx->tuple_desc;
158  result_tuples = (pgr_boyer_t *)funcctx->user_fctx;
159 
160  if (funcctx->call_cntr < funcctx->max_calls) {
161  HeapTuple tuple;
162  Datum result;
163  Datum *values;
164  bool *nulls;
165 
166  /**********************************************************************/
167  /*
168  OUT source BIGINT,
169  OUT target_vid BIGINT,
170  OUT cost FLOAT,
171  */
172  /**********************************************************************/
173  size_t numb = 4;
174  values = palloc(numb * sizeof(Datum));
175  nulls = palloc(numb * sizeof(bool));
176 
177  size_t i;
178  for (i = 0; i < numb; ++i) {
179  nulls[i] = false;
180  }
181 
182  values[0] = Int32GetDatum(funcctx->call_cntr + 1);
183  values[1] = Int64GetDatum(result_tuples[funcctx->call_cntr].source);
184  values[2] = Int64GetDatum(result_tuples[funcctx->call_cntr].target);
185  values[3] = Float8GetDatum(result_tuples[funcctx->call_cntr].cost);
186 
187  /**********************************************************************/
188 
189  tuple = heap_form_tuple(tuple_desc, values, nulls);
190  result = HeapTupleGetDatum(tuple);
191  SRF_RETURN_NEXT(funcctx, result);
192  } else {
193  /**********************************************************************/
194 
195  PGR_DBG("Clean up code");
196 
197  /**********************************************************************/
198 
199  SRF_RETURN_DONE(funcctx);
200  }
201 }
time_msg.h
postgres_connection.h
pgr_edge_t
Definition: pgr_edge_t.h:37
pgr_SPI_connect
void pgr_SPI_connect(void)
Definition: postgres_connection.c:82
boyerMyrvold_driver.h
pgr_SPI_finish
void pgr_SPI_finish(void)
Definition: postgres_connection.c:71
_pgr_boyermyrvold
PGDLLEXPORT Datum _pgr_boyermyrvold(PG_FUNCTION_ARGS)
Definition: boyerMyrvold.c:110
e_report.h
arrays_input.h
debug_macro.h
process
static void process(char *edges_sql, pgr_boyer_t **result_tuples, size_t *result_count)
Definition: boyerMyrvold.c:47
do_pgr_boyerMyrvold
void do_pgr_boyerMyrvold(pgr_edge_t *data_edges, size_t total_edges, pgr_boyer_t **return_tuples, size_t *return_count, char **log_msg, char **notice_msg, char **err_msg)
Definition: boyerMyrvold_driver.cpp:46
PGR_DBG
#define PGR_DBG(...)
Definition: debug_macro.h:34
pgr_boyer_t
Definition: pgr_boyer_t.h:37
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
PG_FUNCTION_INFO_V1
PG_FUNCTION_INFO_V1(_pgr_boyermyrvold)
time_msg
void time_msg(char *msg, clock_t start_t, clock_t end_t)
Definition: time_msg.c:32
pgr_boyer_t::cost
double cost
Definition: pgr_boyer_t.h:40
edges_input.h
pgr_boyer_t::target
int64_t target
Definition: pgr_boyer_t.h:39
pgr_global_report
void pgr_global_report(char *log, char *notice, char *err)
notice & error
Definition: e_report.c:93
pgr_boyer_t::source
int64_t source
Definition: pgr_boyer_t.h:38