PGROUTING  2.4
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
many_to_dist_driving_distance.c
Go to the documentation of this file.
1 /*PGR-GNU*****************************************************************
2 File: many_to_dist_driving_distance.c
3 
4 Copyright (c) 2015 Celia Virginia Vergara Castillo
5 Mail: vicky_Vergara@hotmail.com
6 
7 ------
8 
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
13 
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18 
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 
23  ********************************************************************PGR-GNU*/
24 
25 #include "./../../common/src/postgres_connection.h"
26 #include "utils/array.h"
27 
28 #include "./../../common/src/debug_macro.h"
29 #include "./../../common/src/e_report.h"
30 #include "./../../common/src/time_msg.h"
31 #include "./../../common/src/pgr_types.h"
32 #include "./../../common/src/edges_input.h"
33 #include "./../../common/src/arrays_input.h"
34 #include "./drivedist_driver.h"
35 
36 
37 PGDLLEXPORT Datum driving_many_to_dist(PG_FUNCTION_ARGS);
39 
40 
41 static
42 void process(
43  char* sql,
44  ArrayType *starts,
45  float8 distance,
46  bool directed,
47  bool equicost,
48  General_path_element_t **result_tuples,
49  size_t *result_count) {
51 
52  size_t size_start_vidsArr = 0;
53  int64_t* start_vidsArr = pgr_get_bigIntArray(&size_start_vidsArr, starts);
54 
55  pgr_edge_t *edges = NULL;
56  size_t total_tuples = 0;
57  pgr_get_edges(sql, &edges, &total_tuples);
58 
59  if (total_tuples == 0) {
60  return;
61  }
62 
63  PGR_DBG("Starting timer");
64  clock_t start_t = clock();
65  char* log_msg = NULL;
66  char* notice_msg = NULL;
67  char* err_msg = NULL;
69  edges, total_tuples,
70  start_vidsArr, size_start_vidsArr,
71  distance,
72  directed,
73  equicost,
74  result_tuples, result_count,
75  &log_msg,
76  &notice_msg,
77  &err_msg);
78 
79  time_msg("processing pgr_drivingDistance()",
80  start_t, clock());
81 
82  if (err_msg && (*result_tuples)) {
83  pfree(*result_tuples);
84  (*result_tuples) = NULL;
85  (*result_count) = 0;
86  }
87 
88  pgr_global_report(log_msg, notice_msg, err_msg);
89 
90  if (log_msg) pfree(log_msg);
91  if (notice_msg) pfree(notice_msg);
92  if (err_msg) pfree(err_msg);
93  if (edges) pfree(edges);
94  if (start_vidsArr) pfree(start_vidsArr);
95 
97 }
98 
99 
100 PGDLLEXPORT Datum
101 driving_many_to_dist(PG_FUNCTION_ARGS) {
102  FuncCallContext *funcctx;
103  TupleDesc tuple_desc;
104 
105  /**********************************************************************/
106  General_path_element_t* result_tuples = 0;
107  size_t result_count = 0;
108  /**********************************************************************/
109 
110  if (SRF_IS_FIRSTCALL()) {
111  MemoryContext oldcontext;
112 
113  funcctx = SRF_FIRSTCALL_INIT();
114  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
115 
116  /**********************************************************************/
117 
118  PGR_DBG("Calling driving_many_to_dist_driver");
119  process(
120  text_to_cstring(PG_GETARG_TEXT_P(0)),
121  PG_GETARG_ARRAYTYPE_P(1),
122  PG_GETARG_FLOAT8(2),
123  PG_GETARG_BOOL(3),
124  PG_GETARG_BOOL(4),
125  &result_tuples, &result_count);
126 
127  /**********************************************************************/
128 
129 
130 #if PGSQL_VERSION > 95
131  funcctx->max_calls = result_count;
132 #else
133  funcctx->max_calls = (uint32_t)result_count;
134 #endif
135  funcctx->user_fctx = result_tuples;
136  if (get_call_result_type(fcinfo, NULL, &tuple_desc)
137  != TYPEFUNC_COMPOSITE)
138  ereport(ERROR,
139  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
140  errmsg("function returning record called in context "
141  "that cannot accept type record")));
142 
143  funcctx->tuple_desc = tuple_desc;
144 
145  MemoryContextSwitchTo(oldcontext);
146  }
147 
148  funcctx = SRF_PERCALL_SETUP();
149 
150  tuple_desc = funcctx->tuple_desc;
151  result_tuples = (General_path_element_t*) funcctx->user_fctx;
152 
153  if (funcctx->call_cntr < funcctx->max_calls) {
154  HeapTuple tuple;
155  Datum result;
156  Datum *values;
157  bool* nulls;
158 
159  /**********************************************************************/
160  size_t numb = 6;
161  values = palloc(numb * sizeof(Datum));
162  nulls = palloc(numb * sizeof(bool));
163 
164  size_t i;
165  for (i = 0; i < numb; ++i) {
166  nulls[i] = false;
167  }
168  values[0] = Int32GetDatum(funcctx->call_cntr + 1);
169  values[1] = Int64GetDatum(result_tuples[funcctx->call_cntr].start_id);
170  values[2] = Int64GetDatum(result_tuples[funcctx->call_cntr].node);
171  values[3] = Int64GetDatum(result_tuples[funcctx->call_cntr].edge);
172  values[4] = Float8GetDatum(result_tuples[funcctx->call_cntr].cost);
173  values[5] = Float8GetDatum(result_tuples[funcctx->call_cntr].agg_cost);
174 
175  /**********************************************************************/
176  tuple = heap_form_tuple(tuple_desc, values, nulls);
177  result = HeapTupleGetDatum(tuple);
178 
179  pfree(values);
180  pfree(nulls);
181 
182  SRF_RETURN_NEXT(funcctx, result);
183  } else {
184  SRF_RETURN_DONE(funcctx);
185  }
186 }
187 
#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
void do_pgr_driving_many_to_dist(pgr_edge_t *data_edges, size_t total_edges, int64_t *start_vertex, size_t s_len, double distance, bool directedFlag, bool equiCostFlag, General_path_element_t **return_tuples, size_t *return_count, char **log_msg, char **notice_msg, char **err_msg)
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)
double float8
Definition: postgres.h:23
PG_FUNCTION_INFO_V1(driving_many_to_dist)
edge_astar_t * edges
Definition: BDATester.cpp:46
void pgr_SPI_connect(void)
PGDLLEXPORT Datum driving_many_to_dist(PG_FUNCTION_ARGS)
char * err_msg
Definition: BDATester.cpp:50
static void process(char *sql, ArrayType *starts, float8 distance, bool directed, bool equicost, General_path_element_t **result_tuples, size_t *result_count)