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_many.c
Go to the documentation of this file.
1 /*PGR-GNU*****************************************************************
2 File: edge_disjoint_paths_one_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_one_to_many(PG_FUNCTION_ARGS);
56 
57 /******************************************************************************/
58 /* MODIFY AS NEEDED */
59 static
60 void
62  char *edges_sql,
63  int64_t source_vertex,
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_vertex,
93  sink_vertices,
94  size_sink_verticesArr,
95  directed,
96  result_tuples,
97  result_count,
98  &err_msg);
99 
100  time_msg("processing edge disjoint paths", start_t, clock());
101  PGR_DBG("Returning %ld tuples\n", *result_count);
102  PGR_DBG("Returned message = %s\n", err_msg);
103 
104  free(err_msg);
105  pfree(edges);
106  pgr_SPI_finish();
107 }
108 /* */
109 /******************************************************************************/
110 
112 PGDLLEXPORT Datum
114  FuncCallContext *funcctx;
115  uint32_t call_cntr;
116  uint32_t max_calls;
117  TupleDesc tuple_desc;
118 
119  /**************************************************************************/
120  /* MODIFY AS NEEDED */
121  /* */
122  General_path_element_t *result_tuples = 0;
123  size_t result_count = 0;
124  /* */
125  /**************************************************************************/
126 
127  if (SRF_IS_FIRSTCALL()) {
128  MemoryContext oldcontext;
129  funcctx = SRF_FIRSTCALL_INIT();
130  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
131 
132 
133  /**********************************************************************/
134  /* MODIFY AS NEEDED */
135 
136  int64_t *sink_vertices;
137  size_t size_sink_verticesArr;
138  sink_vertices = (int64_t *)
139  pgr_get_bigIntArray(&size_sink_verticesArr,
140  PG_GETARG_ARRAYTYPE_P(2));
141  PGR_DBG("sink_verticesArr size %ld ", size_sink_verticesArr);
142 
143  PGR_DBG("Calling process");
144  process(
145  pgr_text2char(PG_GETARG_TEXT_P(0)),
146  PG_GETARG_INT64(1),
147  sink_vertices, size_sink_verticesArr,
148  PG_GETARG_BOOL(3),
149  &result_tuples,
150  &result_count);
151 
152  /* */
153  /**********************************************************************/
154 
155  funcctx->max_calls = (uint32_t) result_count;
156  funcctx->user_fctx = result_tuples;
157  if (get_call_result_type(fcinfo, NULL, &tuple_desc)
158  != TYPEFUNC_COMPOSITE) {
159  ereport(ERROR,
160  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
161  errmsg("function returning record called in context "
162  "that cannot accept type record")));
163  }
164 
165  funcctx->tuple_desc = tuple_desc;
166  MemoryContextSwitchTo(oldcontext);
167  }
168 
169  funcctx = SRF_PERCALL_SETUP();
170  call_cntr = funcctx->call_cntr;
171  max_calls = funcctx->max_calls;
172  tuple_desc = funcctx->tuple_desc;
173  result_tuples = (General_path_element_t *) funcctx->user_fctx;
174 
175  if (call_cntr < max_calls) {
176  HeapTuple tuple;
177  Datum result;
178  Datum *values;
179  bool *nulls;
180 
181  /**********************************************************************/
182  /* MODIFY AS NEEDED */
183 
184  values = palloc(5 * sizeof(Datum));
185  nulls = palloc(5 * sizeof(bool));
186 
187  size_t i;
188  for (i = 0; i < 5; ++i) {
189  nulls[i] = false;
190  }
191 
192  // postgres starts counting from 1
193  values[0] = Int32GetDatum(call_cntr + 1);
194  values[1] = Int32GetDatum(result_tuples[call_cntr].seq);
195  values[2] = Int64GetDatum(result_tuples[call_cntr].end_id);
196  values[3] = Int64GetDatum(result_tuples[call_cntr].node);
197  values[4] = Int64GetDatum(result_tuples[call_cntr].edge);
198  /**********************************************************************/
199 
200  tuple = heap_form_tuple(tuple_desc, values, nulls);
201  result = HeapTupleGetDatum(tuple);
202  SRF_RETURN_NEXT(funcctx, result);
203  } else {
204  // cleanup
205  if (result_tuples) free(result_tuples);
206 
207  SRF_RETURN_DONE(funcctx);
208  }
209 }
210 
void pgr_get_basic_edges(char *sql, pgr_basic_edge_t **edges, size_t *total_edges)
read basic edges
Definition: edges_input.c:568
void do_pgr_edge_disjoint_paths_one_to_many(pgr_basic_edge_t *data_edges, size_t total_tuples, int64_t source_vertex, int64_t *sink_vertices, size_t size_sink_verticesArr, bool directed, General_path_element_t **return_tuples, size_t *return_count, char **err_msg)
static void process(char *edges_sql, int64_t source_vertex, int64_t *sink_vertices, size_t size_sink_verticesArr, bool directed, General_path_element_t **result_tuples, size_t *result_count)
#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_one_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)
PGDLLEXPORT Datum edge_disjoint_paths_one_to_many(PG_FUNCTION_ARGS)