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_many_to_many.c
Go to the documentation of this file.
1 /*PGR-GNU*****************************************************************
2 File: edge_disjoint_paths_many_to_many.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 "utils/array.h"
34 #include "catalog/pg_type.h"
35 #if PGSQL_VERSION > 92
36 #include "access/htup_details.h"
37 #endif
38 
39 /*
40  * Uncomment when needed
41  */
42 
43 // #define DEBUG
44 
45 #include "fmgr.h"
46 #include "./../../common/src/debug_macro.h"
47 #include "./../../common/src/time_msg.h"
48 #include "./../../common/src/pgr_types.h"
49 #include "./../../common/src/postgres_connection.h"
50 #include "./../../common/src/edges_input.h"
51 #include "./../../common/src/arrays_input.h"
53 
54 PGDLLEXPORT Datum
55 edge_disjoint_paths_many_to_many(PG_FUNCTION_ARGS);
56 
57 /******************************************************************************/
58 /* MODIFY AS NEEDED */
59 static
60 void
62  char *edges_sql,
63  int64_t *source_vertices, size_t size_source_verticesArr,
64  int64_t *sink_vertices, size_t size_sink_verticesArr,
65  bool directed,
66  General_path_element_t **result_tuples,
67  size_t *result_count) {
69 
70  PGR_DBG("Load data");
71  pgr_basic_edge_t *edges = NULL;
72 
73  size_t total_tuples = 0;
74 
75  pgr_get_basic_edges(edges_sql, &edges, &total_tuples);
76 
77  if (total_tuples == 0) {
78  PGR_DBG("No edges found");
79  (*result_count) = 0;
80  (*result_tuples) = NULL;
82  return;
83  }
84  PGR_DBG("Total %ld tuples in query:", total_tuples);
85 
86  PGR_DBG("Starting processing");
87  clock_t start_t = clock();
88  char *err_msg = NULL;
90  edges,
91  total_tuples,
92  source_vertices,
93  size_source_verticesArr,
94  sink_vertices,
95  size_sink_verticesArr,
96  directed,
97  result_tuples,
98  result_count,
99  &err_msg);
100 
101  time_msg("processing edge disjoint paths", start_t, clock());
102  PGR_DBG("Returning %ld tuples\n", *result_count);
103  PGR_DBG("Returned message = %s\n", err_msg);
104 
105  free(err_msg);
106  pfree(edges);
107  pgr_SPI_finish();
108 }
109 /* */
110 /******************************************************************************/
111 
113 PGDLLEXPORT Datum
115  FuncCallContext *funcctx;
116  uint32_t call_cntr;
117  uint32_t max_calls;
118  TupleDesc tuple_desc;
119 
120  /**************************************************************************/
121  /* MODIFY AS NEEDED */
122  /* */
123  General_path_element_t *result_tuples = 0;
124  size_t result_count = 0;
125  /* */
126  /**************************************************************************/
127 
128  if (SRF_IS_FIRSTCALL()) {
129  MemoryContext oldcontext;
130  funcctx = SRF_FIRSTCALL_INIT();
131  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
132 
133 
134  /**********************************************************************/
135  /* MODIFY AS NEEDED */
136 
137  int64_t *sink_vertices;
138  size_t size_sink_verticesArr;
139  sink_vertices = (int64_t *)
140  pgr_get_bigIntArray(&size_sink_verticesArr,
141  PG_GETARG_ARRAYTYPE_P(2));
142  PGR_DBG("sink_verticesArr size %ld ", size_sink_verticesArr);
143 
144  int64_t *source_vertices;
145  size_t size_source_verticesArr;
146  source_vertices = (int64_t *)
147  pgr_get_bigIntArray(&size_source_verticesArr,
148  PG_GETARG_ARRAYTYPE_P(1));
149  PGR_DBG("source_verticesArr size %ld ", size_source_verticesArr);
150 
151  PGR_DBG("Calling process");
152  process(
153  pgr_text2char(PG_GETARG_TEXT_P(0)),
154  source_vertices, size_source_verticesArr,
155  sink_vertices, size_sink_verticesArr,
156  PG_GETARG_BOOL(3),
157  &result_tuples,
158  &result_count);
159 
160  /* */
161  /**********************************************************************/
162 
163  funcctx->max_calls = (uint32_t) result_count;
164  funcctx->user_fctx = result_tuples;
165  if (get_call_result_type(fcinfo, NULL, &tuple_desc)
166  != TYPEFUNC_COMPOSITE) {
167  ereport(ERROR,
168  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
169  errmsg("function returning record called in context "
170  "that cannot accept type record")));
171  }
172 
173  funcctx->tuple_desc = tuple_desc;
174  MemoryContextSwitchTo(oldcontext);
175  }
176 
177  funcctx = SRF_PERCALL_SETUP();
178  call_cntr = funcctx->call_cntr;
179  max_calls = funcctx->max_calls;
180  tuple_desc = funcctx->tuple_desc;
181  result_tuples = (General_path_element_t *) funcctx->user_fctx;
182 
183  if (call_cntr < max_calls) {
184  HeapTuple tuple;
185  Datum result;
186  Datum *values;
187  bool *nulls;
188 
189  /**********************************************************************/
190  /* MODIFY AS NEEDED */
191 
192  values = palloc(6 * sizeof(Datum));
193  nulls = palloc(6 * sizeof(bool));
194 
195  size_t i;
196  for (i = 0; i < 6; ++i) {
197  nulls[i] = false;
198  }
199 
200  // postgres starts counting from 1
201  values[0] = Int32GetDatum(call_cntr + 1);
202  values[1] = Int32GetDatum(result_tuples[call_cntr].seq);
203  values[2] = Int64GetDatum(result_tuples[call_cntr].start_id);
204  values[3] = Int64GetDatum(result_tuples[call_cntr].end_id);
205  values[4] = Int64GetDatum(result_tuples[call_cntr].node);
206  values[5] = Int64GetDatum(result_tuples[call_cntr].edge);
207  /**********************************************************************/
208 
209  tuple = heap_form_tuple(tuple_desc, values, nulls);
210  result = HeapTupleGetDatum(tuple);
211  SRF_RETURN_NEXT(funcctx, result);
212  } else {
213  // cleanup
214  if (result_tuples) free(result_tuples);
215 
216  SRF_RETURN_DONE(funcctx);
217  }
218 }
219 
PGDLLEXPORT Datum edge_disjoint_paths_many_to_many(PG_FUNCTION_ARGS)
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
int64_t * pgr_get_bigIntArray(size_t *arrlen, ArrayType *input)
Definition: arrays_input.c:38
void time_msg(char *msg, clock_t start_t, clock_t end_t)
Definition: time_msg.c:31
void do_pgr_edge_disjoint_paths_many_to_many(pgr_basic_edge_t *data_edges, size_t total_tuples, 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 **err_msg)
void pgr_SPI_finish(void)
static void process(char *edges_sql, int64_t *source_vertices, size_t size_source_verticesArr, int64_t *sink_vertices, size_t size_sink_verticesArr, bool directed, General_path_element_t **result_tuples, size_t *result_count)
PG_FUNCTION_INFO_V1(edge_disjoint_paths_many_to_many)
edge_astar_t * edges
Definition: BDATester.cpp:46
void pgr_SPI_connect(void)
char * err_msg
Definition: BDATester.cpp:50
char * pgr_text2char(text *in)