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_one.c
Go to the documentation of this file.
1 /*PGR-GNU*****************************************************************
2 File: edge_disjoint_paths_many_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 
31 #include "postgres.h"
32 #include "executor/spi.h"
33 #include "funcapi.h"
34 #include "utils/array.h"
35 #include "catalog/pg_type.h"
36 #if PGSQL_VERSION > 92
37 #include "access/htup_details.h"
38 #endif
39 
40 /*
41  * Uncomment when needed
42  */
43 
44 // #define DEBUG
45 
46 #include "fmgr.h"
47 #include "./../../common/src/debug_macro.h"
48 #include "./../../common/src/time_msg.h"
49 #include "./../../common/src/pgr_types.h"
50 #include "./../../common/src/postgres_connection.h"
51 #include "./../../common/src/edges_input.h"
52 #include "./../../common/src/arrays_input.h"
54 
55 PGDLLEXPORT Datum
56 edge_disjoint_paths_many_to_one(PG_FUNCTION_ARGS);
57 
58 /******************************************************************************/
59 /* MODIFY AS NEEDED */
60 static
61 void
63  char *edges_sql,
64  int64_t *source_vertices, size_t size_source_verticesArr,
65  int64_t sink_vertex,
66  bool directed,
67  General_path_element_t **result_tuples,
68  size_t *result_count) {
70 
71  PGR_DBG("Load data");
72  pgr_basic_edge_t *edges = NULL;
73 
74  size_t total_tuples = 0;
75 
76  pgr_get_basic_edges(edges_sql, &edges, &total_tuples);
77 
78  if (total_tuples == 0) {
79  PGR_DBG("No edges found");
80  (*result_count) = 0;
81  (*result_tuples) = NULL;
83  return;
84  }
85  PGR_DBG("Total %ld tuples in query:", total_tuples);
86 
87  PGR_DBG("Starting processing");
88  clock_t start_t = clock();
89  char *err_msg = NULL;
91  edges,
92  total_tuples,
93  source_vertices,
94  size_source_verticesArr,
95  sink_vertex,
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 *source_vertices;
138  size_t size_source_verticesArr;
139  source_vertices = (int64_t *)
140  pgr_get_bigIntArray(&size_source_verticesArr,
141  PG_GETARG_ARRAYTYPE_P(1));
142  PGR_DBG("source_verticesArr size %ld ", size_source_verticesArr);
143 
144  PGR_DBG("Calling process");
145  process(
146  pgr_text2char(PG_GETARG_TEXT_P(0)),
147  source_vertices, size_source_verticesArr,
148  PG_GETARG_INT64(2),
149  PG_GETARG_BOOL(3),
150  &result_tuples,
151  &result_count);
152 
153  /* */
154  /**********************************************************************/
155 
156  funcctx->max_calls = (uint32_t) result_count;
157  funcctx->user_fctx = result_tuples;
158  if (get_call_result_type(fcinfo, NULL, &tuple_desc)
159  != TYPEFUNC_COMPOSITE) {
160  ereport(ERROR,
161  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
162  errmsg("function returning record called in context "
163  "that cannot accept type record")));
164  }
165 
166  funcctx->tuple_desc = tuple_desc;
167  MemoryContextSwitchTo(oldcontext);
168  }
169 
170  funcctx = SRF_PERCALL_SETUP();
171  call_cntr = funcctx->call_cntr;
172  max_calls = funcctx->max_calls;
173  tuple_desc = funcctx->tuple_desc;
174  result_tuples = (General_path_element_t *) funcctx->user_fctx;
175 
176  if (call_cntr < max_calls) {
177  HeapTuple tuple;
178  Datum result;
179  Datum *values;
180  bool *nulls;
181 
182  /**********************************************************************/
183  /* MODIFY AS NEEDED */
184 
185  values = palloc(5 * sizeof(Datum));
186  nulls = palloc(5 * sizeof(bool));
187 
188  size_t i;
189  for (i = 0; i < 5; ++i) {
190  nulls[i] = false;
191  }
192 
193  // postgres starts counting from 1
194  values[0] = Int32GetDatum(call_cntr + 1);
195  values[1] = Int32GetDatum(result_tuples[call_cntr].seq);
196  values[2] = Int64GetDatum(result_tuples[call_cntr].start_id);
197  values[3] = Int64GetDatum(result_tuples[call_cntr].node);
198  values[4] = Int64GetDatum(result_tuples[call_cntr].edge);
199  /**********************************************************************/
200 
201  tuple = heap_form_tuple(tuple_desc, values, nulls);
202  result = HeapTupleGetDatum(tuple);
203  SRF_RETURN_NEXT(funcctx, result);
204  } else {
205  // cleanup
206  if (result_tuples) free(result_tuples);
207 
208  SRF_RETURN_DONE(funcctx);
209  }
210 }
211 
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 pgr_SPI_finish(void)
PG_FUNCTION_INFO_V1(edge_disjoint_paths_many_to_one)
edge_astar_t * edges
Definition: BDATester.cpp:46
void pgr_SPI_connect(void)
PGDLLEXPORT Datum edge_disjoint_paths_many_to_one(PG_FUNCTION_ARGS)
char * err_msg
Definition: BDATester.cpp:50
char * pgr_text2char(text *in)
static void process(char *edges_sql, int64_t *source_vertices, size_t size_source_verticesArr, int64_t sink_vertex, bool directed, General_path_element_t **result_tuples, size_t *result_count)
void do_pgr_edge_disjoint_paths_many_to_one(pgr_basic_edge_t *data_edges, size_t total_tuples, int64_t *source_vertices, size_t size_source_verticesArr, int64_t sink_vertex, bool directed, General_path_element_t **return_tuples, size_t *return_count, char **err_msg)