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