pgRouting
pgRouting extends the PostGIS / PostgreSQL geospatial database to provide geospatial routing functionality.
 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 "postgres.h"
31 #include "executor/spi.h"
32 #include "funcapi.h"
33 #include "catalog/pg_type.h"
34 #if PGSQL_VERSION > 92
35 #include "access/htup_details.h"
36 #endif
37 
38 /*
39  * Uncomment when needed
40  */
41 
42 // #define DEBUG
43 
44 #include "fmgr.h"
45 #include "./../../common/src/debug_macro.h"
46 #include "./../../common/src/time_msg.h"
47 #include "./../../common/src/pgr_types.h"
48 #include "./../../common/src/postgres_connection.h"
49 #include "./../../common/src/edges_input.h"
51 
52 PGDLLEXPORT Datum
53 edge_disjoint_paths_one_to_one(PG_FUNCTION_ARGS);
54 
55 /******************************************************************************/
56 /* MODIFY AS NEEDED */
57 static
58 void
60  char *edges_sql,
61  int64_t source_vertex,
62  int64_t sink_vertex,
63  bool directed,
64  General_path_element_t **result_tuples,
65  size_t *result_count) {
67 
68  PGR_DBG("Load data");
69  pgr_basic_edge_t *edges = NULL;
70 
71  if (source_vertex == sink_vertex) {
72  (*result_count) = 0;
73  (*result_tuples) = NULL;
75  return;
76  }
77 
78  size_t total_tuples = 0;
79 
80  pgr_get_basic_edges(edges_sql, &edges, &total_tuples);
81 
82  if (total_tuples == 0) {
83  PGR_DBG("No edges found");
84  (*result_count) = 0;
85  (*result_tuples) = NULL;
87  return;
88  }
89  PGR_DBG("Total %ld tuples in query:", total_tuples);
90 
91  PGR_DBG("Starting processing");
92  clock_t start_t = clock();
93  char *err_msg = NULL;
95  edges,
96  total_tuples,
97  source_vertex,
98  sink_vertex,
99  directed,
100  result_tuples,
101  result_count,
102  &err_msg);
103 
104  time_msg("processing edge disjoint paths", start_t, clock());
105  PGR_DBG("Returning %ld tuples\n", *result_count);
106  PGR_DBG("Returned message = %s\n", err_msg);
107 
108  free(err_msg);
109  pfree(edges);
110  pgr_SPI_finish();
111 }
112 /* */
113 /******************************************************************************/
114 
116 PGDLLEXPORT Datum
118  FuncCallContext *funcctx;
119  uint32_t call_cntr;
120  uint32_t max_calls;
121  TupleDesc tuple_desc;
122 
123  /**************************************************************************/
124  /* MODIFY AS NEEDED */
125  /* */
126  General_path_element_t *result_tuples = 0;
127  size_t result_count = 0;
128  /* */
129  /**************************************************************************/
130 
131  if (SRF_IS_FIRSTCALL()) {
132  MemoryContext oldcontext;
133  funcctx = SRF_FIRSTCALL_INIT();
134  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
135 
136 
137  /**********************************************************************/
138  /* MODIFY AS NEEDED */
139 
140  PGR_DBG("Calling process");
141  process(
142  pgr_text2char(PG_GETARG_TEXT_P(0)),
143  PG_GETARG_INT64(1),
144  PG_GETARG_INT64(2),
145  PG_GETARG_BOOL(3),
146  &result_tuples,
147  &result_count);
148 
149  /* */
150  /**********************************************************************/
151 
152  funcctx->max_calls = (uint32_t) result_count;
153  funcctx->user_fctx = result_tuples;
154  if (get_call_result_type(fcinfo, NULL, &tuple_desc)
155  != TYPEFUNC_COMPOSITE) {
156  ereport(ERROR,
157  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
158  errmsg("function returning record called in context "
159  "that cannot accept type record")));
160  }
161 
162  funcctx->tuple_desc = tuple_desc;
163  MemoryContextSwitchTo(oldcontext);
164  }
165 
166  funcctx = SRF_PERCALL_SETUP();
167  call_cntr = funcctx->call_cntr;
168  max_calls = funcctx->max_calls;
169  tuple_desc = funcctx->tuple_desc;
170  result_tuples = (General_path_element_t *) funcctx->user_fctx;
171 
172  if (call_cntr < max_calls) {
173  HeapTuple tuple;
174  Datum result;
175  Datum *values;
176  bool *nulls;
177 
178  /**********************************************************************/
179  /* MODIFY AS NEEDED */
180 
181  values = palloc(4 * sizeof(Datum));
182  nulls = palloc(4 * sizeof(bool));
183 
184  size_t i;
185  for (i = 0; i < 4; ++i) {
186  nulls[i] = false;
187  }
188 
189  // postgres starts counting from 1
190  values[0] = Int32GetDatum(call_cntr + 1);
191  values[1] = Int32GetDatum(result_tuples[call_cntr].seq);
192  values[2] = Int64GetDatum(result_tuples[call_cntr].node);
193  values[3] = Int64GetDatum(result_tuples[call_cntr].edge);
194  /**********************************************************************/
195 
196  tuple = heap_form_tuple(tuple_desc, values, nulls);
197  result = HeapTupleGetDatum(tuple);
198  SRF_RETURN_NEXT(funcctx, result);
199  } else {
200  // cleanup
201  if (result_tuples) free(result_tuples);
202 
203  SRF_RETURN_DONE(funcctx);
204  }
205 }
206 
void pgr_get_basic_edges(char *sql, pgr_basic_edge_t **edges, size_t *total_edges)
read basic edges
Definition: edges_input.c:568
#define PGR_DBG(...)
Definition: debug_macro.h:33
void time_msg(char *msg, clock_t start_t, clock_t end_t)
Definition: time_msg.c:31
PG_FUNCTION_INFO_V1(edge_disjoint_paths_one_to_one)
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 do_pgr_edge_disjoint_paths_one_to_one(pgr_basic_edge_t *data_edges, size_t total_tuples, int64_t source_vertex, int64_t sink_vertex, bool directed, General_path_element_t **return_tuples, size_t *return_count, char **err_msg)
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)
char * pgr_text2char(text *in)