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