PGROUTING  2.4
 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 "./../../common/src/postgres_connection.h"
31 
32 #include "./../../common/src/debug_macro.h"
33 #include "./../../common/src/time_msg.h"
34 #include "./../../common/src/pgr_types.h"
35 #include "./../../common/src/edges_input.h"
36 
37 #include "./johnson_driver.h"
38 
39 PGDLLEXPORT Datum johnson(PG_FUNCTION_ARGS);
40 
41 /******************************************************************************/
42 /* MODIFY AS NEEDED */
43 static
44 void process(
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  PGR_DBG("Starting processing");
66  char *err_msg = (char *)"";
67  clock_t start_t = clock();
69  edges,
70  total_tuples,
71  directed,
72  result_tuples,
73  result_count,
74  &err_msg);
75  time_msg(" processing Johnson", start_t, clock());
76  PGR_DBG("Returning %ld tuples\n", *result_count);
77  PGR_DBG("Returned message = %s\n", err_msg);
78 
79  free(err_msg);
80  pfree(edges);
82 }
83 /* */
84 /******************************************************************************/
85 
87 PGDLLEXPORT Datum
88 johnson(PG_FUNCTION_ARGS) {
89  FuncCallContext *funcctx;
90  TupleDesc tuple_desc;
91 
92  /**************************************************************************/
93  /* MODIFY AS NEEDED */
94  /* */
95  Matrix_cell_t *result_tuples = NULL;
96  size_t result_count = 0;
97  /* */
98  /**************************************************************************/
99 
100  if (SRF_IS_FIRSTCALL()) {
101  MemoryContext oldcontext;
102  funcctx = SRF_FIRSTCALL_INIT();
103  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
104 
105 
106  /*********************************************************************/
107  /* MODIFY AS NEEDED */
108  // CREATE OR REPLACE FUNCTION pgr_johnson(
109  // edges_sql TEXT,
110  // directed BOOLEAN,
111 
112  PGR_DBG("Calling process");
113  process(
114  text_to_cstring(PG_GETARG_TEXT_P(0)),
115  PG_GETARG_BOOL(1),
116  &result_tuples,
117  &result_count);
118 
119  /* */
120  /*********************************************************************/
121 
122 #if PGSQL_VERSION > 95
123  funcctx->max_calls = result_count;
124 #else
125  funcctx->max_calls = (uint32_t)result_count;
126 #endif
127  funcctx->user_fctx = result_tuples;
128  if (get_call_result_type(fcinfo, NULL, &tuple_desc)
129  != TYPEFUNC_COMPOSITE)
130  ereport(ERROR,
131  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
132  errmsg("function returning record called in context "
133  "that cannot accept type record")));
134 
135  funcctx->tuple_desc = tuple_desc;
136  MemoryContextSwitchTo(oldcontext);
137  }
138 
139  funcctx = SRF_PERCALL_SETUP();
140  tuple_desc = funcctx->tuple_desc;
141  result_tuples = (Matrix_cell_t*) funcctx->user_fctx;
142 
143  if (funcctx->call_cntr < funcctx->max_calls) {
144  HeapTuple tuple;
145  Datum result;
146  Datum *values;
147  bool *nulls;
148 
149  /*********************************************************************/
150  /* MODIFY AS NEEDED */
151  // OUT seq BIGINT,
152  // OUT from_vid BIGINT,
153  // OUT to_vid BIGINT,
154  // OUT cost float)
155 
156  values = palloc(3 * sizeof(Datum));
157  nulls = palloc(3 * sizeof(bool));
158 
159  // postgres starts counting from 1
160  values[0] = Int64GetDatum(result_tuples[funcctx->call_cntr].from_vid);
161  nulls[0] = false;
162  values[1] = Int64GetDatum(result_tuples[funcctx->call_cntr].to_vid);
163  nulls[1] = false;
164  values[2] = Float8GetDatum(result_tuples[funcctx->call_cntr].cost);
165  nulls[2] = false;
166 
167  /*********************************************************************/
168 
169  tuple = heap_form_tuple(tuple_desc, values, nulls);
170  result = HeapTupleGetDatum(tuple);
171  SRF_RETURN_NEXT(funcctx, result);
172  } else {
173  SRF_RETURN_DONE(funcctx);
174  }
175 }
176 
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:34
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:561
int64_t from_vid
Definition: pgr_types.h:165
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:44
double cost
Definition: pgr_types.h:167
int64_t to_vid
Definition: pgr_types.h:166
PGDLLEXPORT Datum johnson(PG_FUNCTION_ARGS)
Definition: johnson.c:88
char * err_msg
Definition: BDATester.cpp:50