PGROUTING  3.2
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
7 
8 Function's developer:
9 Copyright (c) 2015 Celia Virginia Vergara Castillo
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 <stdbool.h>
32 
33 #include "c_common/debug_macro.h"
34 #include "c_common/e_report.h"
35 #include "c_common/time_msg.h"
36 #include "c_common/edges_input.h"
37 
39 
40 PGDLLEXPORT Datum _pgr_johnson(PG_FUNCTION_ARGS);
42 
43 /******************************************************************************/
44 /* MODIFY AS NEEDED */
45 static
46 void process(
47  char* edges_sql,
48  bool directed,
49  Matrix_cell_t **result_tuples,
50  size_t *result_count) {
52 
53  PGR_DBG("Load data");
54  pgr_edge_t *edges = NULL;
55  size_t total_tuples = 0;
56  pgr_get_edges_no_id(edges_sql, &edges, &total_tuples);
57 
58  if (total_tuples == 0) {
59  PGR_DBG("No edges found");
60  (*result_count) = 0;
61  (*result_tuples) = NULL;
63  return;
64  }
65  PGR_DBG("Total %ld tuples in query:", total_tuples);
66 
67  PGR_DBG("Starting processing");
68  char *log_msg = NULL;
69  char *notice_msg = NULL;
70  char *err_msg = NULL;
71  clock_t start_t = clock();
73  edges,
74  total_tuples,
75  directed,
76  result_tuples,
77  result_count,
78  &log_msg,
79  &err_msg);
80  time_msg(" processing Johnson", start_t, clock());
81 
82  if (err_msg && (*result_tuples)) {
83  free(*result_tuples);
84  (*result_tuples) = NULL;
85  (*result_count) = 0;
86  }
87 
88  pgr_global_report(log_msg, notice_msg, err_msg);
89 
90 
91  if (log_msg) pfree(log_msg);
92  if (notice_msg) pfree(notice_msg);
93  if (err_msg) pfree(err_msg);
94 
95  pfree(edges);
97 }
98 /* */
99 /******************************************************************************/
100 
101 PGDLLEXPORT Datum
102 _pgr_johnson(PG_FUNCTION_ARGS) {
103  FuncCallContext *funcctx;
104  TupleDesc tuple_desc;
105 
106  /**************************************************************************/
107  /* MODIFY AS NEEDED */
108  /* */
109  Matrix_cell_t *result_tuples = NULL;
110  size_t result_count = 0;
111  /* */
112  /**************************************************************************/
113 
114  if (SRF_IS_FIRSTCALL()) {
115  MemoryContext oldcontext;
116  funcctx = SRF_FIRSTCALL_INIT();
117  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
118 
119 
120  /*********************************************************************/
121  /* MODIFY AS NEEDED */
122  // CREATE OR REPLACE FUNCTION pgr_johnson(
123  // edges_sql TEXT,
124  // directed BOOLEAN,
125 
126  PGR_DBG("Calling process");
127  process(
128  text_to_cstring(PG_GETARG_TEXT_P(0)),
129  PG_GETARG_BOOL(1),
130  &result_tuples,
131  &result_count);
132 
133  /* */
134  /*********************************************************************/
135 
136 #if PGSQL_VERSION > 95
137  funcctx->max_calls = result_count;
138 #else
139  funcctx->max_calls = (uint32_t)result_count;
140 #endif
141  funcctx->user_fctx = result_tuples;
142  if (get_call_result_type(fcinfo, NULL, &tuple_desc)
143  != TYPEFUNC_COMPOSITE)
144  ereport(ERROR,
145  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
146  errmsg("function returning record called in context "
147  "that cannot accept type record")));
148 
149  funcctx->tuple_desc = tuple_desc;
150  MemoryContextSwitchTo(oldcontext);
151  }
152 
153  funcctx = SRF_PERCALL_SETUP();
154  tuple_desc = funcctx->tuple_desc;
155  result_tuples = (Matrix_cell_t*) funcctx->user_fctx;
156 
157  if (funcctx->call_cntr < funcctx->max_calls) {
158  HeapTuple tuple;
159  Datum result;
160  Datum *values;
161  bool *nulls;
162 
163  /*********************************************************************/
164  /* MODIFY AS NEEDED */
165  // OUT seq BIGINT,
166  // OUT from_vid BIGINT,
167  // OUT to_vid BIGINT,
168  // OUT cost float)
169 
170  values = palloc(3 * sizeof(Datum));
171  nulls = palloc(3 * sizeof(bool));
172 
173  // postgres starts counting from 1
174  values[0] = Int64GetDatum(result_tuples[funcctx->call_cntr].from_vid);
175  nulls[0] = false;
176  values[1] = Int64GetDatum(result_tuples[funcctx->call_cntr].to_vid);
177  nulls[1] = false;
178  values[2] = Float8GetDatum(result_tuples[funcctx->call_cntr].cost);
179  nulls[2] = false;
180 
181  /*********************************************************************/
182 
183  tuple = heap_form_tuple(tuple_desc, values, nulls);
184  result = HeapTupleGetDatum(tuple);
185  SRF_RETURN_NEXT(funcctx, result);
186  } else {
187  SRF_RETURN_DONE(funcctx);
188  }
189 }
190 
johnson_driver.h
time_msg.h
postgres_connection.h
pgr_edge_t
Definition: pgr_edge_t.h:37
pgr_SPI_connect
void pgr_SPI_connect(void)
Definition: postgres_connection.c:82
pgr_get_edges_no_id
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:733
matrix_cell::to_vid
int64_t to_vid
Definition: matrix_cell_t.h:39
pgr_SPI_finish
void pgr_SPI_finish(void)
Definition: postgres_connection.c:71
e_report.h
do_pgr_johnson
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)
Definition: johnson_driver.cpp:42
_pgr_johnson
PGDLLEXPORT Datum _pgr_johnson(PG_FUNCTION_ARGS)
Definition: johnson.c:102
matrix_cell::cost
double cost
Definition: matrix_cell_t.h:40
debug_macro.h
PG_FUNCTION_INFO_V1
PG_FUNCTION_INFO_V1(_pgr_johnson)
PGR_DBG
#define PGR_DBG(...)
Definition: debug_macro.h:34
process
static void process(char *edges_sql, bool directed, Matrix_cell_t **result_tuples, size_t *result_count)
Definition: johnson.c:46
if
if(DOXYGEN_FOUND) configure_file($
Definition: doxygen/CMakeLists.txt:13
time_msg
void time_msg(char *msg, clock_t start_t, clock_t end_t)
Definition: time_msg.c:32
matrix_cell::from_vid
int64_t from_vid
Definition: matrix_cell_t.h:38
edges_input.h
matrix_cell
Definition: matrix_cell_t.h:37
pgr_global_report
void pgr_global_report(char *log, char *notice, char *err)
notice & error
Definition: e_report.c:93