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