PGROUTING  2.5
 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 
32 #include "utils/array.h"
33 
34 
35 #include "c_common/debug_macro.h"
36 #include "c_common/e_report.h"
37 #include "c_common/time_msg.h"
38 
39 #include "c_common/edges_input.h"
40 #include "c_common/arrays_input.h"
41 
43 
44 PGDLLEXPORT Datum bdDijkstra(PG_FUNCTION_ARGS);
46 
47 
48 /******************************************************************************/
49 /* MODIFY AS NEEDED */
50 static
51 void
53  char* edges_sql,
54  ArrayType *starts,
55  ArrayType *ends,
56  bool directed,
57  bool only_cost,
58  General_path_element_t **result_tuples,
59  size_t *result_count) {
61 
62  int64_t* start_vidsArr = NULL;
63  size_t size_start_vidsArr = 0;
64  start_vidsArr = (int64_t*)
65  pgr_get_bigIntArray(&size_start_vidsArr, starts);
66 
67  int64_t* end_vidsArr = NULL;
68  size_t size_end_vidsArr = 0;
69  end_vidsArr = (int64_t*)
70  pgr_get_bigIntArray(&size_end_vidsArr, ends);
71 
72  pgr_edge_t *edges = NULL;
73  size_t total_edges = 0;
74 
75  pgr_get_edges(edges_sql, &edges, &total_edges);
76  PGR_DBG("Total %ld edges in query:", total_edges);
77 
78  if (total_edges == 0) {
79  PGR_DBG("No edges found");
81  return;
82  }
83 
84  PGR_DBG("Starting processing");
85  clock_t start_t = clock();
86  char *log_msg = NULL;
87  char *notice_msg = NULL;
88  char *err_msg = NULL;
90  edges, total_edges,
91  start_vidsArr, size_start_vidsArr,
92  end_vidsArr, size_end_vidsArr,
93 
94  directed,
95  only_cost,
96 
97  result_tuples,
98  result_count,
99  &log_msg,
100  &notice_msg,
101  &err_msg);
102 
103  time_msg(" processing pgr_bdDijkstra", start_t, clock());
104  PGR_DBG("Returning %ld tuples", *result_count);
105 
106  if (err_msg) {
107  if (*result_tuples) free(*result_tuples);
108  }
109  pgr_global_report(log_msg, notice_msg, err_msg);
110 
111  pfree(edges);
112  pgr_SPI_finish();
113 }
114 /* */
115 /******************************************************************************/
116 
117 PGDLLEXPORT Datum bdDijkstra(PG_FUNCTION_ARGS) {
118  FuncCallContext *funcctx;
119  TupleDesc tuple_desc;
120 
121  /**************************************************************************/
122  /* MODIFY AS NEEDED */
123  /* */
124  General_path_element_t *result_tuples = NULL;
125  size_t result_count = 0;
126  /* */
127  /**************************************************************************/
128 
129  if (SRF_IS_FIRSTCALL()) {
130  MemoryContext oldcontext;
131  funcctx = SRF_FIRSTCALL_INIT();
132  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
133 
134 
135  /**********************************************************************/
136  /* MODIFY AS NEEDED */
137  /*
138  edges_sql TEXT,
139  start_vid BIGINT,
140  end_vid BIGINT,
141  directed BOOLEAN DEFAULT true,
142  only_cost BOOLEAN DEFAULT false,
143  **********************************************************************/
144 
145 
146  process(
147  text_to_cstring(PG_GETARG_TEXT_P(0)),
148  PG_GETARG_ARRAYTYPE_P(1),
149  PG_GETARG_ARRAYTYPE_P(2),
150  PG_GETARG_BOOL(3),
151  PG_GETARG_BOOL(4),
152  &result_tuples,
153  &result_count);
154 
155  /* */
156  /**********************************************************************/
157 
158 #if PGSQL_VERSION > 95
159  funcctx->max_calls = result_count;
160 #else
161  funcctx->max_calls = (uint32_t)result_count;
162 #endif
163  funcctx->user_fctx = result_tuples;
164  if (get_call_result_type(fcinfo, NULL, &tuple_desc)
165  != TYPEFUNC_COMPOSITE) {
166  ereport(ERROR,
167  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
168  errmsg("function returning record called in context "
169  "that cannot accept type record")));
170  }
171 
172  funcctx->tuple_desc = tuple_desc;
173  MemoryContextSwitchTo(oldcontext);
174  }
175 
176  funcctx = SRF_PERCALL_SETUP();
177  tuple_desc = funcctx->tuple_desc;
178 
179  result_tuples = (General_path_element_t*) funcctx->user_fctx;
180 
181  if (funcctx->call_cntr < funcctx->max_calls) {
182  HeapTuple tuple;
183  Datum result;
184  Datum *values;
185  bool* nulls;
186  size_t call_cntr = funcctx->call_cntr;
187 
188 
189  /**********************************************************************/
190  /* MODIFY AS NEEDED */
191  /*
192  OUT seq INTEGER,
193  OUT path_seq INTEGER,
194  OUT node BIGINT,
195  OUT edge BIGINT,
196  OUT cost FLOAT,
197  OUT agg_cost FLOAT
198  ***********************************************************************/
199 
200  size_t numb = 8;
201  values = palloc(numb * sizeof(Datum));
202  nulls = palloc(numb * sizeof(bool));
203 
204 
205  size_t i;
206  for (i = 0; i < numb; ++i) {
207  nulls[i] = false;
208  }
209 
210  values[0] = Int32GetDatum(call_cntr + 1);
211  values[1] = Int32GetDatum(result_tuples[call_cntr].seq);
212  values[2] = Int64GetDatum(result_tuples[call_cntr].start_id);
213  values[3] = Int64GetDatum(result_tuples[call_cntr].end_id);
214  values[4] = Int64GetDatum(result_tuples[call_cntr].node);
215  values[5] = Int64GetDatum(result_tuples[call_cntr].edge);
216  values[6] = Float8GetDatum(result_tuples[call_cntr].cost);
217  values[7] = Float8GetDatum(result_tuples[call_cntr].agg_cost);
218 
219  /**********************************************************************/
220 
221  tuple = heap_form_tuple(tuple_desc, values, nulls);
222  result = HeapTupleGetDatum(tuple);
223  SRF_RETURN_NEXT(funcctx, result);
224  } else {
225  SRF_RETURN_DONE(funcctx);
226  }
227 }
static edge_t edges[22573]
Definition: trsp.h:31
#define PGR_DBG(...)
Definition: debug_macro.h:34
PGDLLEXPORT Datum bdDijkstra(PG_FUNCTION_ARGS)
Definition: bdDijkstra.c:117
void pgr_get_edges(char *edges_sql, pgr_edge_t **edges, size_t *total_edges)
basic edge_sql
Definition: edges_input.c:540
int64_t * pgr_get_bigIntArray(size_t *arrlen, ArrayType *input)
enforces the input array to be NOT empty
Definition: arrays_input.c:124
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, ArrayType *starts, ArrayType *ends, bool directed, bool only_cost, General_path_element_t **result_tuples, size_t *result_count)
Definition: bdDijkstra.c:52
PG_FUNCTION_INFO_V1(bdDijkstra)
void pgr_SPI_connect(void)
void do_pgr_bdDijkstra(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, General_path_element_t **return_tuples, size_t *return_count, char **log_msg, char **notice_msg, char **err_msg)