PGROUTING  2.6
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 
31 
32 #include "c_common/debug_macro.h"
33 #include "c_common/e_report.h"
34 #include "c_common/time_msg.h"
35 #include "c_common/edges_input.h"
36 
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 *log_msg = NULL;
67  char *notice_msg = NULL;
68  char *err_msg = NULL;
69  clock_t start_t = clock();
71  edges,
72  total_tuples,
73  directed,
74  result_tuples,
75  result_count,
76  &log_msg,
77  &err_msg);
78  time_msg(" processing Johnson", start_t, clock());
79 
80  if (err_msg && (*result_tuples)) {
81  free(*result_tuples);
82  (*result_tuples) = NULL;
83  (*result_count) = 0;
84  }
85 
86  pgr_global_report(log_msg, notice_msg, err_msg);
87 
88 
89  if (log_msg) pfree(log_msg);
90  if (notice_msg) pfree(notice_msg);
91  if (err_msg) pfree(err_msg);
92 
93  pfree(edges);
95 }
96 /* */
97 /******************************************************************************/
98 
100 PGDLLEXPORT Datum
101 johnson(PG_FUNCTION_ARGS) {
102  FuncCallContext *funcctx;
103  TupleDesc tuple_desc;
104 
105  /**************************************************************************/
106  /* MODIFY AS NEEDED */
107  /* */
108  Matrix_cell_t *result_tuples = NULL;
109  size_t result_count = 0;
110  /* */
111  /**************************************************************************/
112 
113  if (SRF_IS_FIRSTCALL()) {
114  MemoryContext oldcontext;
115  funcctx = SRF_FIRSTCALL_INIT();
116  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
117 
118 
119  /*********************************************************************/
120  /* MODIFY AS NEEDED */
121  // CREATE OR REPLACE FUNCTION pgr_johnson(
122  // edges_sql TEXT,
123  // directed BOOLEAN,
124 
125  PGR_DBG("Calling process");
126  process(
127  text_to_cstring(PG_GETARG_TEXT_P(0)),
128  PG_GETARG_BOOL(1),
129  &result_tuples,
130  &result_count);
131 
132  /* */
133  /*********************************************************************/
134 
135 #if PGSQL_VERSION > 95
136  funcctx->max_calls = result_count;
137 #else
138  funcctx->max_calls = (uint32_t)result_count;
139 #endif
140  funcctx->user_fctx = result_tuples;
141  if (get_call_result_type(fcinfo, NULL, &tuple_desc)
142  != TYPEFUNC_COMPOSITE)
143  ereport(ERROR,
144  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
145  errmsg("function returning record called in context "
146  "that cannot accept type record")));
147 
148  funcctx->tuple_desc = tuple_desc;
149  MemoryContextSwitchTo(oldcontext);
150  }
151 
152  funcctx = SRF_PERCALL_SETUP();
153  tuple_desc = funcctx->tuple_desc;
154  result_tuples = (Matrix_cell_t*) funcctx->user_fctx;
155 
156  if (funcctx->call_cntr < funcctx->max_calls) {
157  HeapTuple tuple;
158  Datum result;
159  Datum *values;
160  bool *nulls;
161 
162  /*********************************************************************/
163  /* MODIFY AS NEEDED */
164  // OUT seq BIGINT,
165  // OUT from_vid BIGINT,
166  // OUT to_vid BIGINT,
167  // OUT cost float)
168 
169  values = palloc(3 * sizeof(Datum));
170  nulls = palloc(3 * sizeof(bool));
171 
172  // postgres starts counting from 1
173  values[0] = Int64GetDatum(result_tuples[funcctx->call_cntr].from_vid);
174  nulls[0] = false;
175  values[1] = Int64GetDatum(result_tuples[funcctx->call_cntr].to_vid);
176  nulls[1] = false;
177  values[2] = Float8GetDatum(result_tuples[funcctx->call_cntr].cost);
178  nulls[2] = false;
179 
180  /*********************************************************************/
181 
182  tuple = heap_form_tuple(tuple_desc, values, nulls);
183  result = HeapTupleGetDatum(tuple);
184  SRF_RETURN_NEXT(funcctx, result);
185  } else {
186  SRF_RETURN_DONE(funcctx);
187  }
188 }
189 
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 **log_msg, 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_global_report(char *log, char *notice, char *err)
notice & error
Definition: e_report.c:93
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:567
if(DOXYGEN_FOUND) configure_file($
Definition: CMakeLists.txt:13
int64_t from_vid
Definition: matrix_cell_t.h:59
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
int64_t to_vid
Definition: matrix_cell_t.h:60
PGDLLEXPORT Datum johnson(PG_FUNCTION_ARGS)
Definition: johnson.c:101