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