PGROUTING  3.2
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
7 
8 Function's developer:
9 Copyright (c) 2015 Celia Virginia Vergara Castillo
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 <stdbool.h>
32 
33 #include "c_common/debug_macro.h"
34 #include "c_common/e_report.h"
35 #include "c_common/time_msg.h"
36 
37 #include "c_common/edges_input.h"
38 
40 
41 PGDLLEXPORT Datum _pgr_floydwarshall(PG_FUNCTION_ARGS);
43 
44 static
45 void
47  char* edges_sql,
48  bool directed,
49  Matrix_cell_t **result_tuples,
50  size_t *result_count) {
52 
53  PGR_DBG("Load data");
54  pgr_edge_t *edges = NULL;
55  size_t total_tuples = 0;
56  pgr_get_edges_no_id(edges_sql, &edges, &total_tuples);
57 
58  if (total_tuples == 0) {
59  PGR_DBG("No edges found");
60  (*result_count) = 0;
61  (*result_tuples) = NULL;
63  return;
64  }
65  PGR_DBG("Total %ld tuples in query:", total_tuples);
66 
67  clock_t start_t = clock();
68  PGR_DBG("Starting processing");
69  char *err_msg = NULL;
70  char *notice_msg = NULL;
71  char *log_msg = NULL;
73  edges,
74  total_tuples,
75  directed,
76  result_tuples,
77  result_count,
78  &log_msg,
79  &err_msg);
80  time_msg(" processing FloydWarshall", start_t, clock());
81 
82  if (err_msg && (*result_tuples)) {
83  free(*result_tuples);
84  (*result_tuples) = NULL;
85  (*result_count) = 0;
86  }
87 
88  pgr_global_report(log_msg, notice_msg, err_msg);
89 
90 
91  if (log_msg) pfree(log_msg);
92  if (notice_msg) pfree(notice_msg);
93  if (err_msg) pfree(err_msg);
94 
95  pfree(edges);
97 }
98 
99 
100 PGDLLEXPORT Datum
101 _pgr_floydwarshall(PG_FUNCTION_ARGS) {
102  FuncCallContext *funcctx;
103  TupleDesc tuple_desc;
104 
105  /**************************************************************************/
106  /* */
107  Matrix_cell_t *result_tuples = NULL;
108  size_t result_count = 0;
109  /* */
110  /**************************************************************************/
111 
112  if (SRF_IS_FIRSTCALL()) {
113  MemoryContext oldcontext;
114  funcctx = SRF_FIRSTCALL_INIT();
115  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
116 
117 
118  /*********************************************************************/
119  /* */
120  // QUERY
121  // CREATE OR REPLACE FUNCTION pgr_floydWarshalll(
122  // edges_sql TEXT,
123  // directed BOOLEAN,
124  // OUT seq INTEGER,
125  // OUT from_vid bigint,
126  // OUT to_vid bigint,
127  // OUT cost float)
128 
129 
130  PGR_DBG("Calling process");
131  process(
132  text_to_cstring(PG_GETARG_TEXT_P(0)),
133  PG_GETARG_BOOL(1),
134  &result_tuples,
135  &result_count);
136 
137  /* */
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  funcctx->tuple_desc = tuple_desc;
154  MemoryContextSwitchTo(oldcontext);
155  }
156 
157  funcctx = SRF_PERCALL_SETUP();
158  tuple_desc = funcctx->tuple_desc;
159  result_tuples = (Matrix_cell_t*) funcctx->user_fctx;
160 
161  if (funcctx->call_cntr < funcctx->max_calls) {
162  HeapTuple tuple;
163  Datum result;
164  Datum *values;
165  bool* nulls;
166 
167  /*********************************************************************/
168  values = palloc(3 * sizeof(Datum));
169  nulls = palloc(3 * sizeof(bool));
170 
171  // postgres starts counting from 1
172  values[0] = Int64GetDatum(result_tuples[funcctx->call_cntr].from_vid);
173  nulls[0] = false;
174  values[1] = Int64GetDatum(result_tuples[funcctx->call_cntr].to_vid);
175  nulls[1] = false;
176  values[2] = Float8GetDatum(result_tuples[funcctx->call_cntr].cost);
177  nulls[2] = false;
178  /*********************************************************************/
179 
180  tuple = heap_form_tuple(tuple_desc, values, nulls);
181  result = HeapTupleGetDatum(tuple);
182  SRF_RETURN_NEXT(funcctx, result);
183  } else {
184  SRF_RETURN_DONE(funcctx);
185  }
186 }
187 
time_msg.h
_pgr_floydwarshall
PGDLLEXPORT Datum _pgr_floydwarshall(PG_FUNCTION_ARGS)
Definition: floydWarshall.c:101
postgres_connection.h
pgr_edge_t
Definition: pgr_edge_t.h:37
floydWarshall_driver.h
pgr_SPI_connect
void pgr_SPI_connect(void)
Definition: postgres_connection.c:82
pgr_get_edges_no_id
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:733
matrix_cell::to_vid
int64_t to_vid
Definition: matrix_cell_t.h:39
pgr_SPI_finish
void pgr_SPI_finish(void)
Definition: postgres_connection.c:71
e_report.h
matrix_cell::cost
double cost
Definition: matrix_cell_t.h:40
debug_macro.h
process
static void process(char *edges_sql, bool directed, Matrix_cell_t **result_tuples, size_t *result_count)
Definition: floydWarshall.c:46
PGR_DBG
#define PGR_DBG(...)
Definition: debug_macro.h:34
if
if(DOXYGEN_FOUND) configure_file($
Definition: doxygen/CMakeLists.txt:13
time_msg
void time_msg(char *msg, clock_t start_t, clock_t end_t)
Definition: time_msg.c:32
matrix_cell::from_vid
int64_t from_vid
Definition: matrix_cell_t.h:38
edges_input.h
do_pgr_floydWarshall
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)
Definition: floydWarshall_driver.cpp:42
matrix_cell
Definition: matrix_cell_t.h:37
pgr_global_report
void pgr_global_report(char *log, char *notice, char *err)
notice & error
Definition: e_report.c:93
PG_FUNCTION_INFO_V1
PG_FUNCTION_INFO_V1(_pgr_floydwarshall)