PGROUTING  2.4
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
floydWarshall.c
Go to the documentation of this file.
1 /*PGR-GNU*****************************************************************
2 File: floydWarshall.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) 2015 Celia Virginia Vergara Castillo
10 Mail: vicky_vergara@hotmail.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 
32 
33 #include "./../../common/src/debug_macro.h"
34 #include "./../../common/src/time_msg.h"
35 #include "./../../common/src/pgr_types.h"
36 #include "./../../common/src/edges_input.h"
37 
38 #include "./floydWarshall_driver.h"
39 
40 PGDLLEXPORT Datum floydWarshall(PG_FUNCTION_ARGS);
42 
43 static
44 void
46  char* edges_sql,
47  bool directed,
48  Matrix_cell_t **result_tuples,
49  size_t *result_count) {
51 
52  PGR_DBG("Load data");
53  pgr_edge_t *edges = NULL;
54  size_t total_tuples = 0;
55  pgr_get_edges_no_id(edges_sql, &edges, &total_tuples);
56 
57  if (total_tuples == 0) {
58  PGR_DBG("No edges found");
59  (*result_count) = 0;
60  (*result_tuples) = NULL;
62  return;
63  }
64  PGR_DBG("Total %ld tuples in query:", total_tuples);
65 
66  clock_t start_t = clock();
67  PGR_DBG("Starting processing");
68  char *err_msg = NULL;
69  char *log_msg = NULL;
71  edges,
72  total_tuples,
73  directed,
74  result_tuples,
75  result_count,
76  &log_msg,
77  &err_msg);
78  time_msg(" processing FloydWarshall", start_t, clock());
79 
80  PGR_DBG("Returning %ld tuples\n", *result_count);
81 
82  if (log_msg) {
83  PGR_DBG("LOG = %s\n", log_msg);
84  free(log_msg);
85  }
86 
87  if (err_msg) {
88  if (*result_tuples) free(*result_tuples);
89  elog(ERROR, " %s\n", err_msg);
90  }
91 
92  pfree(edges);
94 }
95 
96 
97 PGDLLEXPORT Datum
98 floydWarshall(PG_FUNCTION_ARGS) {
99  FuncCallContext *funcctx;
100  TupleDesc tuple_desc;
101 
102  /**************************************************************************/
103  /* */
104  Matrix_cell_t *result_tuples = NULL;
105  size_t result_count = 0;
106  /* */
107  /**************************************************************************/
108 
109  if (SRF_IS_FIRSTCALL()) {
110  MemoryContext oldcontext;
111  funcctx = SRF_FIRSTCALL_INIT();
112  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
113 
114 
115  /*********************************************************************/
116  /* */
117  // QUERY
118  // CREATE OR REPLACE FUNCTION pgr_floydWarshalll(
119  // edges_sql TEXT,
120  // directed BOOLEAN,
121  // OUT seq INTEGER,
122  // OUT from_vid bigint,
123  // OUT to_vid bigint,
124  // OUT cost float)
125 
126 
127  PGR_DBG("Calling process");
128  process(
129  text_to_cstring(PG_GETARG_TEXT_P(0)),
130  PG_GETARG_BOOL(1),
131  &result_tuples,
132  &result_count);
133 
134  /* */
135  /*********************************************************************/
136 
137 #if PGSQL_VERSION > 95
138  funcctx->max_calls = result_count;
139 #else
140  funcctx->max_calls = (uint32_t)result_count;
141 #endif
142  funcctx->user_fctx = result_tuples;
143  if (get_call_result_type(fcinfo, NULL, &tuple_desc)
144  != TYPEFUNC_COMPOSITE)
145  ereport(ERROR,
146  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
147  errmsg("function returning record called in context "
148  "that cannot accept type record")));
149 
150  funcctx->tuple_desc = tuple_desc;
151  MemoryContextSwitchTo(oldcontext);
152  }
153 
154  funcctx = SRF_PERCALL_SETUP();
155  tuple_desc = funcctx->tuple_desc;
156  result_tuples = (Matrix_cell_t*) funcctx->user_fctx;
157 
158  if (funcctx->call_cntr < funcctx->max_calls) {
159  HeapTuple tuple;
160  Datum result;
161  Datum *values;
162  bool* nulls;
163 
164  /*********************************************************************/
165  values = palloc(3 * sizeof(Datum));
166  nulls = palloc(3 * sizeof(bool));
167 
168  // postgres starts counting from 1
169  values[0] = Int64GetDatum(result_tuples[funcctx->call_cntr].from_vid);
170  nulls[0] = false;
171  values[1] = Int64GetDatum(result_tuples[funcctx->call_cntr].to_vid);
172  nulls[1] = false;
173  values[2] = Float8GetDatum(result_tuples[funcctx->call_cntr].cost);
174  nulls[2] = false;
175  /*********************************************************************/
176 
177  tuple = heap_form_tuple(tuple_desc, values, nulls);
178  result = HeapTupleGetDatum(tuple);
179  SRF_RETURN_NEXT(funcctx, result);
180  } else {
181  SRF_RETURN_DONE(funcctx);
182  }
183 }
184 
PGDLLEXPORT Datum floydWarshall(PG_FUNCTION_ARGS)
Definition: floydWarshall.c:98
void do_pgr_floydWarshall(pgr_edge_t *data_edges, size_t total_tuples, bool directedFlag, Matrix_cell_t **return_tuples, size_t *return_count, char **log_msg, char **err_msg)
#define PGR_DBG(...)
Definition: debug_macro.h:34
static void process(char *edges_sql, bool directed, Matrix_cell_t **result_tuples, size_t *result_count)
Definition: floydWarshall.c:45
void time_msg(char *msg, clock_t start_t, clock_t end_t)
Definition: time_msg.c:32
void pgr_SPI_finish(void)
void pgr_get_edges_no_id(char *edges_sql, pgr_edge_t **edges, size_t *total_edges)
edges_sql without id parameter
Definition: edges_input.c:561
PG_FUNCTION_INFO_V1(floydWarshall)
int64_t from_vid
Definition: pgr_types.h:165
edge_astar_t * edges
Definition: BDATester.cpp:46
void pgr_SPI_connect(void)
double cost
Definition: pgr_types.h:167
int64_t to_vid
Definition: pgr_types.h:166
char * err_msg
Definition: BDATester.cpp:50