pgRouting  2.2
pgRouting extends the PostGIS / PostgreSQL geospatial database to provide geospatial routing functionality.
 All Classes Functions Variables Pages
johnson.c
1 /*PGR-GNU*****************************************************************
2 File: johnson.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 "./johnson_driver.h"
47 
48 PG_FUNCTION_INFO_V1(johnson);
49 #ifndef _MSC_VER
50 Datum
51 #else // _MSC_VER
52 PGDLLEXPORT Datum
53 #endif
54 johnson(PG_FUNCTION_ARGS);
55 
56 /******************************************************************************/
57 /* MODIFY AS NEEDED */
58 static
59 void process(
60  char* edges_sql,
61  bool directed,
62  Matrix_cell_t **result_tuples,
63  size_t *result_count) {
64  pgr_SPI_connect();
65 
66  PGR_DBG("Load data");
67  pgr_edge_t *edges = NULL;
68  size_t total_tuples = 0;
69  pgr_get_data_4_columns(edges_sql, &edges, &total_tuples);
70 
71  if (total_tuples == 0) {
72  PGR_DBG("No edges found");
73  (*result_count) = 0;
74  (*result_tuples) = NULL;
75  pgr_SPI_finish();
76  return;
77  }
78  PGR_DBG("Total %ld tuples in query:", total_tuples);
79 
80  PGR_DBG("Starting processing");
81  char *err_msg = (char *)"";
82  clock_t start_t = clock();
83  do_pgr_johnson(
84  edges,
85  total_tuples,
86  directed,
87  result_tuples,
88  result_count,
89  &err_msg);
90  time_msg(" processing Johnson", start_t, clock());
91  PGR_DBG("Returning %ld tuples\n", *result_count);
92  PGR_DBG("Returned message = %s\n", err_msg);
93 
94  free(err_msg);
95  pfree(edges);
96  pgr_SPI_finish();
97 }
98 /* */
99 /******************************************************************************/
100 
101 #ifndef _MSC_VER
102 Datum
103 #else // _MSC_VER
104 PGDLLEXPORT Datum
105 #endif
106 johnson(PG_FUNCTION_ARGS) {
107  FuncCallContext *funcctx;
108  uint32_t call_cntr;
109  uint32_t max_calls;
110  TupleDesc tuple_desc;
111 
112  /**************************************************************************/
113  /* MODIFY AS NEEDED */
114  /* */
115  Matrix_cell_t *result_tuples = 0;
116  size_t result_count = 0;
117  /* */
118  /**************************************************************************/
119 
120  if (SRF_IS_FIRSTCALL()) {
121  MemoryContext oldcontext;
122  funcctx = SRF_FIRSTCALL_INIT();
123  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
124 
125 
126  /*********************************************************************/
127  /* MODIFY AS NEEDED */
128  // CREATE OR REPLACE FUNCTION pgr_johnson(
129  // edges_sql TEXT,
130  // directed BOOLEAN,
131 
132  PGR_DBG("Calling process");
133  process(
134  pgr_text2char(PG_GETARG_TEXT_P(0)),
135  PG_GETARG_BOOL(1),
136  &result_tuples,
137  &result_count);
138 
139  /* */
140  /*********************************************************************/
141 
142  funcctx->max_calls = (uint32_t)result_count;
143  funcctx->user_fctx = result_tuples;
144  if (get_call_result_type(fcinfo, NULL, &tuple_desc) != 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  call_cntr = funcctx->call_cntr;
156  max_calls = funcctx->max_calls;
157  tuple_desc = funcctx->tuple_desc;
158  result_tuples = (Matrix_cell_t*) funcctx->user_fctx;
159 
160  if (call_cntr < max_calls) {
161  HeapTuple tuple;
162  Datum result;
163  Datum *values;
164  char* nulls;
165 
166  /*********************************************************************/
167  /* MODIFY AS NEEDED */
168  // OUT seq BIGINT,
169  // OUT from_vid BIGINT,
170  // OUT to_vid BIGINT,
171  // OUT cost float)
172 
173  values = palloc(3 * sizeof(Datum));
174  nulls = palloc(3 * sizeof(char));
175 
176  // postgres starts counting from 1
177  values[0] = Int64GetDatum(result_tuples[call_cntr].from_vid);
178  nulls[0] = ' ';
179  values[1] = Int64GetDatum(result_tuples[call_cntr].to_vid);
180  nulls[1] = ' ';
181  values[2] = Float8GetDatum(result_tuples[call_cntr].cost);
182  nulls[2] = ' ';
183 
184  /*********************************************************************/
185 
186  tuple = heap_formtuple(tuple_desc, values, nulls);
187  result = HeapTupleGetDatum(tuple);
188  SRF_RETURN_NEXT(funcctx, result);
189  } else {
190  // cleanup
191  if (result_tuples) free(result_tuples);
192 
193  SRF_RETURN_DONE(funcctx);
194  }
195 }
196