PGROUTING  2.4
 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 "./../../common/src/postgres_connection.h"
31 #include "utils/array.h"
32 
33 #include "./../../common/src/debug_macro.h"
34 #include "./../../common/src/e_report.h"
35 #include "./../../common/src/time_msg.h"
36 #include "./../../common/src/pgr_types.h"
37 #include "./../../common/src/edges_input.h"
38 #include "./../../common/src/arrays_input.h"
40 
41 PGDLLEXPORT Datum
42 edge_disjoint_paths_one_to_many(PG_FUNCTION_ARGS);
43 
44 /******************************************************************************/
45 /* MODIFY AS NEEDED */
46 static
47 void
49  char *edges_sql,
50  int64_t source_vertex,
51  ArrayType *ends,
52  bool directed,
53  General_path_element_t **result_tuples,
54  size_t *result_count) {
56 
57  size_t size_sink_verticesArr = 0;
58  int64_t* sink_vertices =
59  pgr_get_bigIntArray(&size_sink_verticesArr, ends);
60 
61 
62  size_t total_tuples = 0;
63  pgr_basic_edge_t *edges = NULL;
64  pgr_get_basic_edges(edges_sql, &edges, &total_tuples);
65 
66  if (total_tuples == 0) {
67  if (sink_vertices) pfree(sink_vertices);
69  return;
70  }
71 
72  PGR_DBG("Starting timer");
73  clock_t start_t = clock();
74  char* log_msg = NULL;
75  char* notice_msg = NULL;
76  char *err_msg = NULL;
78 
79  edges, total_tuples,
80  &source_vertex, 1,
81  sink_vertices, size_sink_verticesArr,
82  directed,
83 
84  result_tuples, result_count,
85 
86  &log_msg,
87  &notice_msg,
88  &err_msg);
89 
90  time_msg("pgr_edgeDisjointPaths(one to many)", start_t, clock());
91 
92 
93  if (edges) pfree(edges);
94  if (sink_vertices) pfree(sink_vertices);
95 
96  if (err_msg && (*result_tuples)) {
97  pfree(*result_tuples);
98  (*result_tuples) = NULL;
99  (*result_count) = 0;
100  }
101 
102  pgr_global_report(log_msg, notice_msg, err_msg);
103 
104  if (log_msg) pfree(log_msg);
105  if (notice_msg) pfree(notice_msg);
106  if (err_msg) pfree(err_msg);
107  pgr_SPI_finish();
108 }
109 /* */
110 /******************************************************************************/
111 
113 PGDLLEXPORT Datum
115  FuncCallContext *funcctx;
116  TupleDesc tuple_desc;
117 
118  /**************************************************************************/
119  /* MODIFY AS NEEDED */
120  /* */
121  General_path_element_t *result_tuples = NULL;
122  size_t result_count = 0;
123  /* */
124  /**************************************************************************/
125 
126  if (SRF_IS_FIRSTCALL()) {
127  MemoryContext oldcontext;
128  funcctx = SRF_FIRSTCALL_INIT();
129  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
130 
131 
132  /**********************************************************************/
133  /* MODIFY AS NEEDED */
134 
135  process(
136  text_to_cstring(PG_GETARG_TEXT_P(0)),
137  PG_GETARG_INT64(1),
138  PG_GETARG_ARRAYTYPE_P(2),
139  PG_GETARG_BOOL(3),
140  &result_tuples,
141  &result_count);
142 
143  /* */
144  /**********************************************************************/
145 
146 #if PGSQL_VERSION > 95
147  funcctx->max_calls = result_count;
148 #else
149  funcctx->max_calls = (uint32_t)result_count;
150 #endif
151  funcctx->user_fctx = result_tuples;
152  if (get_call_result_type(fcinfo, NULL, &tuple_desc)
153  != TYPEFUNC_COMPOSITE) {
154  ereport(ERROR,
155  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
156  errmsg("function returning record called in context "
157  "that cannot accept type record")));
158  }
159 
160  funcctx->tuple_desc = tuple_desc;
161  MemoryContextSwitchTo(oldcontext);
162  }
163 
164  funcctx = SRF_PERCALL_SETUP();
165  tuple_desc = funcctx->tuple_desc;
166  result_tuples = (General_path_element_t *) funcctx->user_fctx;
167 
168  if (funcctx->call_cntr < funcctx->max_calls) {
169  HeapTuple tuple;
170  Datum result;
171  Datum *values;
172  bool *nulls;
173 
174  /**********************************************************************/
175  /* MODIFY AS NEEDED */
176 
177  values = palloc(5 * sizeof(Datum));
178  nulls = palloc(5 * sizeof(bool));
179 
180  size_t i;
181  for (i = 0; i < 5; ++i) {
182  nulls[i] = false;
183  }
184 
185  // postgres starts counting from 1
186  values[0] = Int32GetDatum(funcctx->call_cntr + 1);
187  values[1] = Int32GetDatum(result_tuples[funcctx->call_cntr].seq);
188  values[2] = Int64GetDatum(result_tuples[funcctx->call_cntr].end_id);
189  values[3] = Int64GetDatum(result_tuples[funcctx->call_cntr].node);
190  values[4] = Int64GetDatum(result_tuples[funcctx->call_cntr].edge);
191  /**********************************************************************/
192 
193  tuple = heap_form_tuple(tuple_desc, values, nulls);
194  result = HeapTupleGetDatum(tuple);
195  SRF_RETURN_NEXT(funcctx, result);
196  } else {
197  SRF_RETURN_DONE(funcctx);
198  }
199 }
200 
static void process(char *edges_sql, int64_t source_vertex, ArrayType *ends, bool directed, General_path_element_t **result_tuples, size_t *result_count)
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)
int64_t * pgr_get_bigIntArray(size_t *arrlen, ArrayType *input)
Definition: arrays_input.c:127
void time_msg(char *msg, clock_t start_t, clock_t end_t)
Definition: time_msg.c:32
void pgr_global_report(char *log, char *notice, char *err)
notice & error
Definition: e_report.c:93
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
PGDLLEXPORT Datum edge_disjoint_paths_one_to_many(PG_FUNCTION_ARGS)