PGROUTING  2.4
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
one_to_one_dijkstra.c
Go to the documentation of this file.
1 /*PGR-GNU*****************************************************************
2 File: one_to_one_dijkstra.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/e_report.h"
34 #include "./../../common/src/time_msg.h"
35 #include "./../../common/src/pgr_types.h"
36 #include "./../../common/src/edges_input.h"
38 
40 
41 PGDLLEXPORT Datum one_to_one_dijkstra(PG_FUNCTION_ARGS);
43 
44 static
45 void
47  char* edges_sql,
48  int64_t start_vid,
49  int64_t end_vid,
50  bool directed,
51  bool only_cost,
52  General_path_element_t **result_tuples,
53  size_t *result_count) {
54  if (start_vid == end_vid) {
55  return;
56  }
57 
59 
60  pgr_edge_t *edges = NULL;
61  size_t total_edges = 0;
62  pgr_get_edges(edges_sql, &edges, &total_edges);
63 
64  if (total_edges == 0) {
66  return;
67  }
68 
69  PGR_DBG("Starting timer");
70  clock_t start_t = clock();
71  char* log_msg = NULL;
72  char* notice_msg = NULL;
73  char* err_msg = NULL;
75  edges, total_edges,
76  &start_vid, 1,
77  &end_vid, 1,
78 
79  directed,
80  only_cost,
81  true, // normal
82 
83  result_tuples,
84  result_count,
85 
86  &log_msg,
87  &notice_msg,
88  &err_msg);
89 
90  if (only_cost) {
91  time_msg("processing pgr_dijkstraCost(one to one)", start_t, clock());
92  } else {
93  time_msg("processing pgr_dijkstra(one to one)", start_t, clock());
94  }
95 
96  if (err_msg && (*result_tuples)) {
97  pfree(*result_tuples);
98  (*result_count) = 0;
99  (*result_tuples) = NULL;
100  }
101 
102  pgr_global_report(log_msg, notice_msg, err_msg);
103 
104  if (log_msg) pfree(log_msg);
105  if (notice_msg) pfree(notice_msg);
106  if (err_msg) pfree(err_msg);
107  if (edges) pfree(edges);
108 
109  pgr_SPI_finish();
110 }
111 
112 
113 PGDLLEXPORT Datum
114 one_to_one_dijkstra(PG_FUNCTION_ARGS) {
115  FuncCallContext *funcctx;
116  TupleDesc tuple_desc;
117 
118  /**********************************************************************/
119  General_path_element_t *result_tuples = 0;
120  size_t result_count = 0;
121  /**********************************************************************/
122 
123  if (SRF_IS_FIRSTCALL()) {
124  MemoryContext oldcontext;
125  funcctx = SRF_FIRSTCALL_INIT();
126  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
127 
128 
129  /**********************************************************************/
130  // pgr_dijkstra(
131  // sql TEXT,
132  // start_vids BIGINT,
133  // end_vid BIGINT,
134  // directed BOOLEAN default true,
135 
136  process(
137  text_to_cstring(PG_GETARG_TEXT_P(0)),
138  PG_GETARG_INT64(1),
139  PG_GETARG_INT64(2),
140  PG_GETARG_BOOL(3),
141  PG_GETARG_BOOL(4),
142  &result_tuples,
143  &result_count);
144 
145  /**********************************************************************/
146 
147 #if PGSQL_VERSION > 95
148  funcctx->max_calls = result_count;
149 #else
150  funcctx->max_calls = (uint32_t)result_count;
151 #endif
152 
153  funcctx->user_fctx = result_tuples;
154  if (get_call_result_type(fcinfo, NULL, &tuple_desc)
155  != TYPEFUNC_COMPOSITE) {
156  ereport(ERROR,
157  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
158  errmsg("function returning record called in context "
159  "that cannot accept type record")));
160  }
161 
162  funcctx->tuple_desc = tuple_desc;
163  MemoryContextSwitchTo(oldcontext);
164  }
165 
166  funcctx = SRF_PERCALL_SETUP();
167  tuple_desc = funcctx->tuple_desc;
168  result_tuples = (General_path_element_t*) funcctx->user_fctx;
169 
170  if (funcctx->call_cntr < funcctx->max_calls) {
171  HeapTuple tuple;
172  Datum result;
173  Datum *values;
174  bool* nulls;
175  size_t call_cntr = funcctx->call_cntr;
176 
177  /**********************************************************************/
178  // OUT seq INTEGER,
179  // OUT path_seq INTEGER,
180  // OUT node BIGINT,
181  // OUT edge BIGINT,
182  // OUT cost FLOAT,
183  // OUT agg_cost FLOAT
184 
185  size_t numb = 6;
186  values = palloc(numb * sizeof(Datum));
187  nulls = palloc(numb * sizeof(bool));
188 
189 
190  size_t i;
191  for (i = 0; i < numb; ++i) {
192  nulls[i] = false;
193  }
194 
195  values[0] = Int32GetDatum(call_cntr + 1);
196  values[1] = Int32GetDatum(result_tuples[call_cntr].seq);
197  values[2] = Int64GetDatum(result_tuples[call_cntr].node);
198  values[3] = Int64GetDatum(result_tuples[call_cntr].edge);
199  values[4] = Float8GetDatum(result_tuples[call_cntr].cost);
200  values[5] = Float8GetDatum(result_tuples[call_cntr].agg_cost);
201  /**********************************************************************/
202 
203  tuple = heap_form_tuple(tuple_desc, values, nulls);
204  result = HeapTupleGetDatum(tuple);
205  SRF_RETURN_NEXT(funcctx, result);
206  } else {
207  SRF_RETURN_DONE(funcctx);
208  }
209 }
PG_FUNCTION_INFO_V1(one_to_one_dijkstra)
#define PGR_DBG(...)
Definition: debug_macro.h:34
void pgr_get_edges(char *edges_sql, pgr_edge_t **edges, size_t *total_edges)
basic edge_sql
Definition: edges_input.c:539
PG_MODULE_MAGIC
void time_msg(char *msg, clock_t start_t, clock_t end_t)
Definition: time_msg.c:32
PGDLLEXPORT Datum one_to_one_dijkstra(PG_FUNCTION_ARGS)
void pgr_global_report(char *log, char *notice, char *err)
notice & error
Definition: e_report.c:93
void pgr_SPI_finish(void)
edge_astar_t * edges
Definition: BDATester.cpp:46
static void process(char *edges_sql, int64_t start_vid, int64_t end_vid, bool directed, bool only_cost, General_path_element_t **result_tuples, size_t *result_count)
void pgr_SPI_connect(void)
void do_pgr_many_to_many_dijkstra(pgr_edge_t *data_edges, size_t total_edges, int64_t *start_vidsArr, size_t size_start_vidsArr, int64_t *end_vidsArr, size_t size_end_vidsArr, bool directed, bool only_cost, bool normal, General_path_element_t **return_tuples, size_t *return_count, char **log_msg, char **notice_msg, char **err_msg)
char * err_msg
Definition: BDATester.cpp:50