PGROUTING  2.4
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
edge_disjoint_paths_one_to_one.c
Go to the documentation of this file.
1 /*PGR-GNU*****************************************************************
2 File: edge_disjoint_paths_one_to_one.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) 2016 Andrea Nardelli
10 Mail: nrd.nardelli@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 
30 #include "./../../common/src/postgres_connection.h"
31 
32 #include "./../../common/src/debug_macro.h"
33 #include "./../../common/src/e_report.h"
34 #include "./../../common/src/time_msg.h"
35 #include "./../../common/src/pgr_types.h"
36 #include "./../../common/src/edges_input.h"
38 
39 PGDLLEXPORT Datum
40 edge_disjoint_paths_one_to_one(PG_FUNCTION_ARGS);
41 
42 /******************************************************************************/
43 /* MODIFY AS NEEDED */
44 static
45 void
47  char *edges_sql,
48  int64_t source_vertex,
49  int64_t sink_vertex,
50  bool directed,
51  General_path_element_t **result_tuples,
52  size_t *result_count) {
54 
55 
56  if (source_vertex == sink_vertex) {
58  return;
59  }
60 
61  size_t total_tuples = 0;
62  pgr_basic_edge_t *edges = NULL;
63  pgr_get_basic_edges(edges_sql, &edges, &total_tuples);
64 
65  if (total_tuples == 0) {
67  return;
68  }
69 
70  PGR_DBG("Starting timer");
71  clock_t start_t = clock();
72  char* log_msg = NULL;
73  char* notice_msg = NULL;
74  char *err_msg = NULL;
75 
77  edges, total_tuples,
78  &source_vertex, 1,
79  &sink_vertex, 1,
80  directed,
81  result_tuples,
82  result_count,
83 
84  &log_msg,
85  &notice_msg,
86  &err_msg);
87 
88  time_msg("pgr_edgeDisjointPaths(one to one)", start_t, clock());
89 
90  if (edges) pfree(edges);
91 
92  if (err_msg && (*result_tuples)) {
93  pfree(*result_tuples);
94  (*result_count) = 0;
95  (*result_tuples) = NULL;
96  }
97 
98  pgr_global_report(log_msg, notice_msg, err_msg);
99 
100  if (log_msg) pfree(log_msg);
101  if (notice_msg) pfree(notice_msg);
102  if (err_msg) pfree(err_msg);
103  pgr_SPI_finish();
104 }
105 /* */
106 /******************************************************************************/
107 
109 PGDLLEXPORT Datum
111  FuncCallContext *funcctx;
112  TupleDesc tuple_desc;
113 
114  /**************************************************************************/
115  /* MODIFY AS NEEDED */
116  /* */
117  General_path_element_t *result_tuples = NULL;
118  size_t result_count = 0;
119  /* */
120  /**************************************************************************/
121 
122  if (SRF_IS_FIRSTCALL()) {
123  MemoryContext oldcontext;
124  funcctx = SRF_FIRSTCALL_INIT();
125  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
126 
127 
128  /**********************************************************************/
129  /* MODIFY AS NEEDED */
130 
131  PGR_DBG("Calling process");
132  process(
133  text_to_cstring(PG_GETARG_TEXT_P(0)),
134  PG_GETARG_INT64(1),
135  PG_GETARG_INT64(2),
136  PG_GETARG_BOOL(3),
137  &result_tuples,
138  &result_count);
139 
140  /* */
141  /**********************************************************************/
142 
143 #if PGSQL_VERSION > 95
144  funcctx->max_calls = result_count;
145 #else
146  funcctx->max_calls = (uint32_t)result_count;
147 #endif
148  funcctx->user_fctx = result_tuples;
149  if (get_call_result_type(fcinfo, NULL, &tuple_desc)
150  != TYPEFUNC_COMPOSITE) {
151  ereport(ERROR,
152  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
153  errmsg("function returning record called in context "
154  "that cannot accept type record")));
155  }
156 
157  funcctx->tuple_desc = tuple_desc;
158  MemoryContextSwitchTo(oldcontext);
159  }
160 
161  funcctx = SRF_PERCALL_SETUP();
162  tuple_desc = funcctx->tuple_desc;
163  result_tuples = (General_path_element_t *) funcctx->user_fctx;
164 
165  if (funcctx->call_cntr < funcctx->max_calls) {
166  HeapTuple tuple;
167  Datum result;
168  Datum *values;
169  bool *nulls;
170 
171  /**********************************************************************/
172  /* MODIFY AS NEEDED */
173 
174  values = palloc(4 * sizeof(Datum));
175  nulls = palloc(4 * sizeof(bool));
176 
177  size_t i;
178  for (i = 0; i < 4; ++i) {
179  nulls[i] = false;
180  }
181 
182  // postgres starts counting from 1
183  values[0] = Int32GetDatum(funcctx->call_cntr + 1);
184  values[1] = Int32GetDatum(result_tuples[funcctx->call_cntr].seq);
185  values[2] = Int64GetDatum(result_tuples[funcctx->call_cntr].node);
186  values[3] = Int64GetDatum(result_tuples[funcctx->call_cntr].edge);
187  /**********************************************************************/
188 
189  tuple = heap_form_tuple(tuple_desc, values, nulls);
190  result = HeapTupleGetDatum(tuple);
191  SRF_RETURN_NEXT(funcctx, result);
192  } else {
193  SRF_RETURN_DONE(funcctx);
194  }
195 }
196 
void pgr_get_basic_edges(char *sql, pgr_basic_edge_t **edges, size_t *total_edges)
read basic edges
Definition: edges_input.c:594
#define PGR_DBG(...)
Definition: debug_macro.h:34
void do_pgr_edge_disjoint_paths(pgr_basic_edge_t *data_edges, size_t total_edges, int64_t *source_vertices, size_t size_source_verticesArr, int64_t *sink_vertices, size_t size_sink_verticesArr, bool directed, General_path_element_t **return_tuples, size_t *return_count, char **log_msg, char **notice_msg, char **err_msg)
void time_msg(char *msg, clock_t start_t, clock_t end_t)
Definition: time_msg.c:32
PG_FUNCTION_INFO_V1(edge_disjoint_paths_one_to_one)
void pgr_global_report(char *log, char *notice, char *err)
notice & error
Definition: e_report.c:93
void pgr_SPI_finish(void)
PGDLLEXPORT Datum edge_disjoint_paths_one_to_one(PG_FUNCTION_ARGS)
edge_astar_t * edges
Definition: BDATester.cpp:46
void pgr_SPI_connect(void)
char * err_msg
Definition: BDATester.cpp:50
static void process(char *edges_sql, int64_t source_vertex, int64_t sink_vertex, bool directed, General_path_element_t **result_tuples, size_t *result_count)