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
johnson.c
Go to the documentation of this file.
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 PGDLLEXPORT Datum johnson(PG_FUNCTION_ARGS);
49 
50 /******************************************************************************/
51 /* MODIFY AS NEEDED */
52 static
53 void process(
54  char* edges_sql,
55  bool directed,
56  Matrix_cell_t **result_tuples,
57  size_t *result_count) {
59 
60  PGR_DBG("Load data");
61  pgr_edge_t *edges = NULL;
62  size_t total_tuples = 0;
63  pgr_get_edges_no_id(edges_sql, &edges, &total_tuples);
64 
65  if (total_tuples == 0) {
66  PGR_DBG("No edges found");
67  (*result_count) = 0;
68  (*result_tuples) = NULL;
70  return;
71  }
72  PGR_DBG("Total %ld tuples in query:", total_tuples);
73 
74  PGR_DBG("Starting processing");
75  char *err_msg = (char *)"";
76  clock_t start_t = clock();
78  edges,
79  total_tuples,
80  directed,
81  result_tuples,
82  result_count,
83  &err_msg);
84  time_msg(" processing Johnson", start_t, clock());
85  PGR_DBG("Returning %ld tuples\n", *result_count);
86  PGR_DBG("Returned message = %s\n", err_msg);
87 
88  free(err_msg);
89  pfree(edges);
91 }
92 /* */
93 /******************************************************************************/
94 
96 PGDLLEXPORT Datum
97 johnson(PG_FUNCTION_ARGS) {
98  FuncCallContext *funcctx;
99  uint32_t call_cntr;
100  uint32_t max_calls;
101  TupleDesc tuple_desc;
102 
103  /**************************************************************************/
104  /* MODIFY AS NEEDED */
105  /* */
106  Matrix_cell_t *result_tuples = 0;
107  size_t result_count = 0;
108  /* */
109  /**************************************************************************/
110 
111  if (SRF_IS_FIRSTCALL()) {
112  MemoryContext oldcontext;
113  funcctx = SRF_FIRSTCALL_INIT();
114  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
115 
116 
117  /*********************************************************************/
118  /* MODIFY AS NEEDED */
119  // CREATE OR REPLACE FUNCTION pgr_johnson(
120  // edges_sql TEXT,
121  // directed BOOLEAN,
122 
123  PGR_DBG("Calling process");
124  process(
125  pgr_text2char(PG_GETARG_TEXT_P(0)),
126  PG_GETARG_BOOL(1),
127  &result_tuples,
128  &result_count);
129 
130  /* */
131  /*********************************************************************/
132 
133  funcctx->max_calls = (uint32_t)result_count;
134  funcctx->user_fctx = result_tuples;
135  if (get_call_result_type(fcinfo, NULL, &tuple_desc) != TYPEFUNC_COMPOSITE)
136  ereport(ERROR,
137  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
138  errmsg("function returning record called in context "
139  "that cannot accept type record")));
140 
141  funcctx->tuple_desc = tuple_desc;
142  MemoryContextSwitchTo(oldcontext);
143  }
144 
145  funcctx = SRF_PERCALL_SETUP();
146  call_cntr = funcctx->call_cntr;
147  max_calls = funcctx->max_calls;
148  tuple_desc = funcctx->tuple_desc;
149  result_tuples = (Matrix_cell_t*) funcctx->user_fctx;
150 
151  if (call_cntr < max_calls) {
152  HeapTuple tuple;
153  Datum result;
154  Datum *values;
155  bool *nulls;
156 
157  /*********************************************************************/
158  /* MODIFY AS NEEDED */
159  // OUT seq BIGINT,
160  // OUT from_vid BIGINT,
161  // OUT to_vid BIGINT,
162  // OUT cost float)
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[call_cntr].from_vid);
169  nulls[0] = false;
170  values[1] = Int64GetDatum(result_tuples[call_cntr].to_vid);
171  nulls[1] = false;
172  values[2] = Float8GetDatum(result_tuples[call_cntr].cost);
173  nulls[2] = false;
174 
175  /*********************************************************************/
176 
177  tuple = heap_form_tuple(tuple_desc, values, nulls);
178  result = HeapTupleGetDatum(tuple);
179  SRF_RETURN_NEXT(funcctx, result);
180  } else {
181  // cleanup
182  if (result_tuples) free(result_tuples);
183 
184  SRF_RETURN_DONE(funcctx);
185  }
186 }
187 
void do_pgr_johnson(pgr_edge_t *data_edges, size_t total_tuples, bool directed, Matrix_cell_t **return_tuples, size_t *return_count, char **err_msg)
#define PGR_DBG(...)
Definition: debug_macro.h:33
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
edge_astar_t * edges
Definition: BDATester.cpp:46
PG_FUNCTION_INFO_V1(johnson)
void pgr_SPI_connect(void)
static void process(char *edges_sql, bool directed, Matrix_cell_t **result_tuples, size_t *result_count)
Definition: johnson.c:53
PGDLLEXPORT Datum johnson(PG_FUNCTION_ARGS)
Definition: johnson.c:97
char * err_msg
Definition: BDATester.cpp:50
char * pgr_text2char(text *in)