PGROUTING  2.4
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
bdDijkstra.c
Go to the documentation of this file.
1 /*PGR-GNU*****************************************************************
2 File: bdDijkstra.c
3 
4 Generated with Template by:
5 Copyright (c) 2016 pgRouting developers
6 Mail: project@pgrouting.org
7 
8 Function's developer:
9 Copyright (c) 2016 Celia Virginia Vergara Castillo
10 Mail: vicky_vergara@hotmail.com
11 
12 
13 ------
14 
15 This program is free software; you can redistribute it and/or modify
16 it under the terms of the GNU General Public License as published by
17 the Free Software Foundation; either version 2 of the License, or
18 (at your option) any later version.
19 
20 This program is distributed in the hope that it will be useful,
21 but WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 GNU General Public License for more details.
24 
25 You should have received a copy of the GNU General Public License
26 along with this program; if not, write to the Free Software
27 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
28 
29 ********************************************************************PGR-GNU*/
30 
31 #include "./../../common/src/postgres_connection.h"
32 
33 #include "./../../common/src/debug_macro.h"
34 #include "./../../common/src/e_report.h"
35 #include "./../../common/src/time_msg.h"
36 #include "./../../common/src/pgr_types.h"
37 #include "./../../common/src/edges_input.h"
38 
39 #include "./bdDijkstra_driver.h"
40 
41 PGDLLEXPORT Datum bdDijkstra(PG_FUNCTION_ARGS);
43 
44 
45 /******************************************************************************/
46 /* MODIFY AS NEEDED */
47 static
48 void
50  char* edges_sql,
51  int64_t start_vid,
52  int64_t end_vid,
53  bool directed,
54  bool only_cost,
55  General_path_element_t **result_tuples,
56  size_t *result_count) {
58 
59  PGR_DBG("Load data");
60  pgr_edge_t *edges = NULL;
61  size_t total_edges = 0;
62 
63  if (start_vid == end_vid) {
65  return;
66  }
67 
68  pgr_get_edges(edges_sql, &edges, &total_edges);
69  PGR_DBG("Total %ld edges in query:", total_edges);
70 
71  if (total_edges == 0) {
72  PGR_DBG("No edges found");
74  return;
75  }
76 
77  PGR_DBG("Starting processing");
78  clock_t start_t = clock();
79  char *log_msg = NULL;
80  char *notice_msg = NULL;
81  char *err_msg = NULL;
83  edges,
84  total_edges,
85  start_vid,
86  end_vid,
87  directed,
88  only_cost,
89  result_tuples,
90  result_count,
91  &log_msg,
92  &notice_msg,
93  &err_msg);
94 
95  time_msg(" processing pgr_bdDijkstra", start_t, clock());
96  PGR_DBG("Returning %ld tuples", *result_count);
97 
98  if (err_msg) {
99  if (*result_tuples) free(*result_tuples);
100  }
101  pgr_global_report(log_msg, notice_msg, err_msg);
102 
103  pfree(edges);
104  pgr_SPI_finish();
105 }
106 /* */
107 /******************************************************************************/
108 
109 PGDLLEXPORT Datum bdDijkstra(PG_FUNCTION_ARGS) {
110  FuncCallContext *funcctx;
111  TupleDesc tuple_desc;
112 
113  /**************************************************************************/
114  /* MODIFY AS NEEDED */
115  /* */
116  General_path_element_t *result_tuples = NULL;
117  size_t result_count = 0;
118  /* */
119  /**************************************************************************/
120 
121  if (SRF_IS_FIRSTCALL()) {
122  MemoryContext oldcontext;
123  funcctx = SRF_FIRSTCALL_INIT();
124  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
125 
126 
127  /**********************************************************************/
128  /* MODIFY AS NEEDED */
129  /*
130  edges_sql TEXT,
131  start_vid BIGINT,
132  end_vid BIGINT,
133  directed BOOLEAN DEFAULT true,
134  only_cost BOOLEAN DEFAULT false,
135  **********************************************************************/
136 
137 
138  process(
139  text_to_cstring(PG_GETARG_TEXT_P(0)),
140  PG_GETARG_INT64(1),
141  PG_GETARG_INT64(2),
142  PG_GETARG_BOOL(3),
143  PG_GETARG_BOOL(4),
144  &result_tuples,
145  &result_count);
146 
147  /* */
148  /**********************************************************************/
149 
150 #if PGSQL_VERSION > 95
151  funcctx->max_calls = result_count;
152 #else
153  funcctx->max_calls = (uint32_t)result_count;
154 #endif
155  funcctx->user_fctx = result_tuples;
156  if (get_call_result_type(fcinfo, NULL, &tuple_desc)
157  != TYPEFUNC_COMPOSITE) {
158  ereport(ERROR,
159  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
160  errmsg("function returning record called in context "
161  "that cannot accept type record")));
162  }
163 
164  funcctx->tuple_desc = tuple_desc;
165  MemoryContextSwitchTo(oldcontext);
166  }
167 
168  funcctx = SRF_PERCALL_SETUP();
169  tuple_desc = funcctx->tuple_desc;
170 
171  result_tuples = (General_path_element_t*) funcctx->user_fctx;
172 
173  if (funcctx->call_cntr < funcctx->max_calls) {
174  HeapTuple tuple;
175  Datum result;
176  Datum *values;
177  bool* nulls;
178 
179  /**********************************************************************/
180  /* MODIFY AS NEEDED */
181  /*
182  OUT seq INTEGER,
183  OUT path_seq INTEGER,
184  OUT node BIGINT,
185  OUT edge BIGINT,
186  OUT cost FLOAT,
187  OUT agg_cost FLOAT
188  ***********************************************************************/
189 
190  values = palloc(6 * sizeof(Datum));
191  nulls = palloc(6 * sizeof(bool));
192 
193 
194  size_t i;
195  for (i = 0; i < 6; ++i) {
196  nulls[i] = false;
197  }
198 
199  // postgres starts counting from 1
200  values[0] = Int32GetDatum(funcctx->call_cntr + 1);
201  values[1] = Int32GetDatum(result_tuples[funcctx->call_cntr].seq);
202  values[2] = Int64GetDatum(result_tuples[funcctx->call_cntr].node);
203  values[3] = Int64GetDatum(result_tuples[funcctx->call_cntr].edge);
204  values[4] = Float8GetDatum(result_tuples[funcctx->call_cntr].cost);
205  values[5] = Float8GetDatum(result_tuples[funcctx->call_cntr].agg_cost);
206  /**********************************************************************/
207 
208  tuple = heap_form_tuple(tuple_desc, values, nulls);
209  result = HeapTupleGetDatum(tuple);
210  SRF_RETURN_NEXT(funcctx, result);
211  } else {
212  SRF_RETURN_DONE(funcctx);
213  }
214 }
void do_pgr_bdDijkstra(pgr_edge_t *data_edges, size_t total_edges, int64_t start_vid, int64_t end_vid, bool directed, bool only_cost, General_path_element_t **return_tuples, size_t *return_count, char **log_msg, char **notice_msg, char **err_msg)
#define PGR_DBG(...)
Definition: debug_macro.h:34
PGDLLEXPORT Datum bdDijkstra(PG_FUNCTION_ARGS)
Definition: bdDijkstra.c:109
void pgr_get_edges(char *edges_sql, pgr_edge_t **edges, size_t *total_edges)
basic edge_sql
Definition: edges_input.c:539
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)
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)
Definition: bdDijkstra.c:49
edge_astar_t * edges
Definition: BDATester.cpp:46
PG_FUNCTION_INFO_V1(bdDijkstra)
void pgr_SPI_connect(void)
char * err_msg
Definition: BDATester.cpp:50