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