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
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 "postgres.h"
31 #include "fmgr.h"
32 #include "funcapi.h"
33 #if PGSQL_VERSION > 92
34 #include "access/htup_details.h"
35 #endif
36 
37 // #define DEBUG
38 
39 #include "./../../common/src/debug_macro.h"
40 #include "./../../common/src/time_msg.h"
41 #include "./../../common/src/pgr_types.h"
42 #include "./../../common/src/postgres_connection.h"
43 #include "./../../common/src/edges_input.h"
44 #include "./floydWarshall_driver.h"
45 
46 PGDLLEXPORT Datum floydWarshall(PG_FUNCTION_ARGS);
47 
48 static
49 void
51  char* edges_sql,
52  bool directed,
53  Matrix_cell_t **result_tuples,
54  size_t *result_count) {
56 
57  PGR_DBG("Load data");
58  pgr_edge_t *edges = NULL;
59  size_t total_tuples = 0;
60  pgr_get_edges_no_id(edges_sql, &edges, &total_tuples);
61 
62  if (total_tuples == 0) {
63  PGR_DBG("No edges found");
64  (*result_count) = 0;
65  (*result_tuples) = NULL;
67  return;
68  }
69  PGR_DBG("Total %ld tuples in query:", total_tuples);
70 
71  clock_t start_t = clock();
72  PGR_DBG("Starting processing");
73  char *err_msg = NULL;
74  char *log_msg = NULL;
76  edges,
77  total_tuples,
78  directed,
79  result_tuples,
80  result_count,
81  &log_msg,
82  &err_msg);
83  time_msg(" processing FloydWarshall", start_t, clock());
84 
85  PGR_DBG("Returning %ld tuples\n", *result_count);
86 
87  if (log_msg) {
88  PGR_DBG("LOG = %s\n", log_msg);
89  free(log_msg);
90  }
91 
92  if (err_msg) {
93  if (*result_tuples) free(*result_tuples);
94  elog(ERROR, " %s\n", err_msg);
95  }
96 
97  pfree(edges);
99 }
100 
101 
103 PGDLLEXPORT Datum
104 floydWarshall(PG_FUNCTION_ARGS) {
105  FuncCallContext *funcctx;
106  uint32_t call_cntr;
107  uint32_t max_calls;
108  TupleDesc tuple_desc;
109 
110  /**************************************************************************/
111  /* */
112  Matrix_cell_t *result_tuples = 0;
113  size_t result_count = 0;
114  /* */
115  /**************************************************************************/
116 
117  if (SRF_IS_FIRSTCALL()) {
118  MemoryContext oldcontext;
119  funcctx = SRF_FIRSTCALL_INIT();
120  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
121 
122 
123  /*********************************************************************/
124  /* */
125  // QUERY
126  // CREATE OR REPLACE FUNCTION pgr_floydWarshalll(
127  // edges_sql TEXT,
128  // directed BOOLEAN,
129  // OUT seq INTEGER,
130  // OUT from_vid bigint,
131  // OUT to_vid bigint,
132  // OUT cost float)
133 
134 
135  PGR_DBG("Calling process");
136  process(
137  pgr_text2char(PG_GETARG_TEXT_P(0)),
138  PG_GETARG_BOOL(1),
139  &result_tuples,
140  &result_count);
141 
142  /* */
143  /*********************************************************************/
144 
145  funcctx->max_calls = (uint32_t)result_count;
146  funcctx->user_fctx = result_tuples;
147  if (get_call_result_type(fcinfo, NULL, &tuple_desc) != 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  funcctx->tuple_desc = tuple_desc;
154  MemoryContextSwitchTo(oldcontext);
155  }
156 
157  funcctx = SRF_PERCALL_SETUP();
158  call_cntr = funcctx->call_cntr;
159  max_calls = funcctx->max_calls;
160  tuple_desc = funcctx->tuple_desc;
161  result_tuples = (Matrix_cell_t*) funcctx->user_fctx;
162 
163  if (call_cntr < max_calls) {
164  HeapTuple tuple;
165  Datum result;
166  Datum *values;
167  bool* nulls;
168 
169  /*********************************************************************/
170  values = palloc(3 * sizeof(Datum));
171  nulls = palloc(3 * sizeof(bool));
172 
173  // postgres starts counting from 1
174  values[0] = Int64GetDatum(result_tuples[call_cntr].from_vid);
175  nulls[0] = false;
176  values[1] = Int64GetDatum(result_tuples[call_cntr].to_vid);
177  nulls[1] = false;
178  values[2] = Float8GetDatum(result_tuples[call_cntr].cost);
179  nulls[2] = false;
180  /*********************************************************************/
181 
182  tuple = heap_form_tuple(tuple_desc, values, nulls);
183  result = HeapTupleGetDatum(tuple);
184  SRF_RETURN_NEXT(funcctx, result);
185  } else {
186  // cleanup
187  if (result_tuples) free(result_tuples);
188 
189  SRF_RETURN_DONE(funcctx);
190  }
191 }
192 
PGDLLEXPORT Datum floydWarshall(PG_FUNCTION_ARGS)
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:33
static void process(char *edges_sql, bool directed, Matrix_cell_t **result_tuples, size_t *result_count)
Definition: floydWarshall.c:50
void time_msg(char *msg, clock_t start_t, clock_t end_t)
Definition: time_msg.c:31
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:544
PG_FUNCTION_INFO_V1(floydWarshall)
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)