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