PGROUTING  2.4
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
many_to_one_dijkstra.c
Go to the documentation of this file.
1 /*PGR-GNU*****************************************************************
2 
3 File: many_to_one_dijkstra.c
4 
5 Generated with Template by:
6 Copyright (c) 2015 pgRouting developers
7 Mail: project@pgrouting.org
8 
9 Function's developer:
10 Copyright (c) 2015 Celia Virginia Vergara Castillo
11 Mail: vicky_vergara@hotmail.com
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 #include "utils/array.h"
33 
34 #include "./../../common/src/debug_macro.h"
35 #include "./../../common/src/e_report.h"
36 #include "./../../common/src/time_msg.h"
37 #include "./../../common/src/pgr_types.h"
38 #include "./../../common/src/edges_input.h"
39 #include "./../../common/src/arrays_input.h"
41 
42 PGDLLEXPORT Datum many_to_one_dijkstra(PG_FUNCTION_ARGS);
44 
45 static
46 void
48  char* edges_sql,
49  ArrayType *starts,
50  int64_t end_vid,
51  bool directed,
52  bool only_cost,
53  General_path_element_t **result_tuples,
54  size_t *result_count) {
56 
57  int64_t* start_vidsArr = NULL;
58  size_t size_start_vidsArr = 0;
59  start_vidsArr = (int64_t*)
60  pgr_get_bigIntArray(&size_start_vidsArr, starts);
61 
62  pgr_edge_t *edges = NULL;
63  size_t total_edges = 0;
64  pgr_get_edges_reversed(edges_sql, &edges, &total_edges);
65 
66  if (total_edges == 0) {
67  if (start_vidsArr) pfree(start_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  &end_vid, 1,
80  start_vidsArr, size_start_vidsArr,
81 
82  directed,
83  only_cost,
84  false, // data is reversed
85  result_tuples,
86  result_count,
87 
88  &log_msg,
89  &notice_msg,
90  &err_msg);
91 
92  if (only_cost) {
93  time_msg("processing pgr_dijkstraCost(many to one)", start_t, clock());
94  } else {
95  time_msg("processing pgr_dijkstra(many to one)", start_t, clock());
96  }
97 
98  if (err_msg && (*result_tuples)) {
99  pfree(*result_tuples);
100  (*result_count) = 0;
101  (*result_tuples) = NULL;
102  }
103 
104  pgr_global_report(log_msg, notice_msg, err_msg);
105 
106  if (log_msg) pfree(log_msg);
107  if (notice_msg) pfree(notice_msg);
108  if (err_msg) pfree(err_msg);
109  if (edges) pfree(edges);
110  if (start_vidsArr) pfree(start_vidsArr);
111 
112  pgr_SPI_finish();
113 }
114 
115 PGDLLEXPORT Datum
116 many_to_one_dijkstra(PG_FUNCTION_ARGS) {
117  FuncCallContext *funcctx;
118  TupleDesc tuple_desc;
119 
120  /**********************************************************************/
121  General_path_element_t *result_tuples = NULL;
122  size_t result_count = 0;
123  /**********************************************************************/
124 
125  if (SRF_IS_FIRSTCALL()) {
126  MemoryContext oldcontext;
127  funcctx = SRF_FIRSTCALL_INIT();
128  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
129 
130 
131  /**********************************************************************/
132  // pgr_dijkstra(
133  // sql TEXT,
134  // start_vids ANYARRAY,
135  // end_vid BIGINT,
136  // directed BOOLEAN default true,
137  // only_cost BOOLEAN default false
138 
139  process(
140  text_to_cstring(PG_GETARG_TEXT_P(0)),
141  PG_GETARG_ARRAYTYPE_P(1),
142  PG_GETARG_INT64(2),
143  PG_GETARG_BOOL(3),
144  PG_GETARG_BOOL(4),
145  &result_tuples,
146  &result_count);
147 
148 
149 #if PGSQL_VERSION > 95
150  funcctx->max_calls = result_count;
151 #else
152  funcctx->max_calls = (uint32_t)result_count;
153 #endif
154  funcctx->user_fctx = result_tuples;
155  if (get_call_result_type(fcinfo, NULL, &tuple_desc)
156  != TYPEFUNC_COMPOSITE) {
157  ereport(ERROR,
158  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
159  errmsg("function returning record called in context "
160  "that cannot accept type record")));
161  }
162 
163  funcctx->tuple_desc = tuple_desc;
164  MemoryContextSwitchTo(oldcontext);
165  }
166 
167  funcctx = SRF_PERCALL_SETUP();
168  tuple_desc = funcctx->tuple_desc;
169  result_tuples = (General_path_element_t*) funcctx->user_fctx;
170 
171  if (funcctx->call_cntr < funcctx->max_calls) {
172  HeapTuple tuple;
173  Datum result;
174  Datum *values;
175  bool* nulls;
176  size_t call_cntr = funcctx->call_cntr;
177 
178  /**********************************************************************/
179  // OUT seq INTEGER,
180  // OUT path_seq INTEGER,
181  // OUT start_vid BIGINT,
182  // OUT node BIGINT,
183  // OUT edge BIGINT,
184  // OUT cost FLOAT,
185  // OUT agg_cost FLOAT)
186 
187  size_t numb = 7;
188  values = palloc(numb * sizeof(Datum));
189  nulls = palloc(numb * sizeof(bool));
190 
191  size_t i;
192  for (i = 0; i < numb; ++i) {
193  nulls[i] = false;
194  }
195 
196  values[0] = Int32GetDatum(call_cntr + 1);
197  values[1] = Int32GetDatum(result_tuples[call_cntr].seq);
198  values[2] = Int64GetDatum(result_tuples[call_cntr].start_id);
199  values[3] = Int64GetDatum(result_tuples[call_cntr].node);
200  values[4] = Int64GetDatum(result_tuples[call_cntr].edge);
201  values[5] = Float8GetDatum(result_tuples[call_cntr].cost);
202  values[6] = Float8GetDatum(result_tuples[call_cntr].agg_cost);
203  /**********************************************************************/
204 
205  tuple = heap_form_tuple(tuple_desc, values, nulls);
206  result = HeapTupleGetDatum(tuple);
207  SRF_RETURN_NEXT(funcctx, result);
208  } else {
209  SRF_RETURN_DONE(funcctx);
210  }
211 }
212 
#define PGR_DBG(...)
Definition: debug_macro.h:34
PG_FUNCTION_INFO_V1(many_to_one_dijkstra)
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
PGDLLEXPORT Datum many_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)
static void process(char *edges_sql, ArrayType *starts, int64_t end_vid, bool directed, bool only_cost, General_path_element_t **result_tuples, size_t *result_count)
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)
char * err_msg
Definition: BDATester.cpp:50
void pgr_get_edges_reversed(char *edges_sql, pgr_edge_t **edges, size_t *total_edges)
Definition: edges_input.c:550