pgRouting  2.2
pgRouting extends the PostGIS / PostgreSQL geospatial database to provide geospatial routing functionality.
 All Classes Functions Variables Pages
floydWarshall.c
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 <unistd.h>
31 
32 #include "postgres.h"
33 #include "funcapi.h"
34 #if PGSQL_VERSION > 92
35 #include "access/htup_details.h"
36 #endif
37 #include "fmgr.h"
38 
39 // #define DEBUG
40 
41 #include "./../../common/src/debug_macro.h"
42 #include "./../../common/src/time_msg.h"
43 #include "./../../common/src/pgr_types.h"
44 #include "./../../common/src/postgres_connection.h"
45 #include "./../../common/src/edges_input.h"
46 #include "./floydWarshall_driver.h"
47 
48 PG_FUNCTION_INFO_V1(floydWarshall);
49 #ifndef _MSC_VER
50 Datum
51 #else // _MSC_VER
52 PGDLLEXPORT Datum
53 #endif
54 floydWarshall(PG_FUNCTION_ARGS);
55 
56 static
57 void
58 process(
59  char* edges_sql,
60  bool directed,
61  Matrix_cell_t **result_tuples,
62  size_t *result_count) {
63  pgr_SPI_connect();
64 
65  PGR_DBG("Load data");
66  pgr_edge_t *edges = NULL;
67  size_t total_tuples = 0;
68  pgr_get_data_4_columns(edges_sql, &edges, &total_tuples);
69 
70  if (total_tuples == 0) {
71  PGR_DBG("No edges found");
72  (*result_count) = 0;
73  (*result_tuples) = NULL;
74  pgr_SPI_finish();
75  return;
76  }
77  PGR_DBG("Total %ld tuples in query:", total_tuples);
78 
79  clock_t start_t = clock();
80  PGR_DBG("Starting processing");
81  char *err_msg = (char *)"";
82  do_pgr_floydWarshall(
83  edges,
84  total_tuples,
85  directed,
86  result_tuples,
87  result_count,
88  &err_msg);
89  time_msg(" processing FloydWarshall", start_t, clock());
90 
91  PGR_DBG("Returning %ld tuples\n", *result_count);
92  PGR_DBG("Returned message = %s\n", err_msg);
93 
94  pfree(edges);
95  pgr_SPI_finish();
96 }
97 
98 
99 #ifndef _MSC_VER
100 Datum
101 #else // _MSC_VER
102 PGDLLEXPORT Datum
103 #endif
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  char* nulls;
168 
169  /*********************************************************************/
170  values = palloc(3 * sizeof(Datum));
171  nulls = palloc(3 * sizeof(char));
172 
173  // postgres starts counting from 1
174  values[0] = Int64GetDatum(result_tuples[call_cntr].from_vid);
175  nulls[0] = ' ';
176  values[1] = Int64GetDatum(result_tuples[call_cntr].to_vid);
177  nulls[1] = ' ';
178  values[2] = Float8GetDatum(result_tuples[call_cntr].cost);
179  nulls[2] = ' ';
180  /*********************************************************************/
181 
182  tuple = heap_formtuple(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