PGROUTING  2.6-dev
new_trsp.c
Go to the documentation of this file.
1 /*PGR-GNU*****************************************************************
2 
3 File: new_trsp.c
4 
5 Copyright (c) 2017 pgRouting developers
6 Mail: project@pgrouting.org
7 
8 ------
9 
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
14 
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19 
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 
24 ********************************************************************PGR-GNU*/
25 
26 
28 #include "utils/array.h"
29 
30 
32 
33 #include "c_common/debug_macro.h"
34 #include "c_common/e_report.h"
35 #include "c_common/time_msg.h"
36 
37 #include "c_types/pgr_edge_t.h"
38 #include "c_types/restriction_t.h"
40 
41 #include "c_common/edges_input.h"
43 #include "c_common/arrays_input.h"
44 
45 PGDLLEXPORT Datum turn_restriction(PG_FUNCTION_ARGS);
47 
48 
49 static
51  char* edges_sql,
52  char* restrictions_sql,
53 
54  ArrayType *starts,
55  ArrayType *ends,
56 
57  bool directed,
58 
59  General_path_element_t **result_tuples,
60  size_t *result_count) {
62 
63  pgr_edge_t *edges = NULL;
64  size_t total_edges = 0;
65  pgr_get_edges(edges_sql, &edges, &total_edges);
66 
67  Restriction_t * restrictions = NULL;
68  size_t total_restrictions = 0;
69  pgr_get_restrictions(restrictions_sql, &restrictions, &total_restrictions);
70 
71  int64_t* start_vidsArr = NULL;
72  size_t size_start_vidsArr = 0;
73  start_vidsArr = (int64_t*)
74  pgr_get_bigIntArray(&size_start_vidsArr, starts);
75 
76  int64_t* end_vidsArr = NULL;
77  size_t size_end_vidsArr = 0;
78  end_vidsArr = (int64_t*)
79  pgr_get_bigIntArray(&size_end_vidsArr, ends);
80 
81  PGR_DBG("Starting timer");
82  clock_t start_t = clock();
83  char* log_msg = NULL;
84  char* notice_msg = NULL;
85  char* err_msg = NULL;
86 
87  do_trsp(
88  edges,
89  total_edges,
90 
91  restrictions,
92  total_restrictions,
93 
94  start_vidsArr, size_start_vidsArr,
95  end_vidsArr, size_end_vidsArr,
96 
97  directed,
98 
99  result_tuples,
100  result_count,
101  &log_msg,
102  &notice_msg,
103  &err_msg);
104  time_msg("processing _pgr_trsp", start_t, clock());
105 
106  if (err_msg && (*result_tuples)) {
107  pfree(*result_tuples);
108  (*result_tuples) = NULL;
109  (*result_count) = 0;
110  }
111 
112  pgr_global_report(log_msg, notice_msg, err_msg);
113 
114  pgr_SPI_finish();
115 }
116 
117 
118 
119 PGDLLEXPORT Datum
120 turn_restriction(PG_FUNCTION_ARGS) {
121  FuncCallContext *funcctx;
122  TupleDesc tuple_desc;
123 
124  size_t result_count = 0;
125  General_path_element_t *result_tuples = NULL;
126 
127  // stuff done only on the first call of the function
128  if (SRF_IS_FIRSTCALL()) {
129  MemoryContext oldcontext;
130  funcctx = SRF_FIRSTCALL_INIT();
131  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
132 
133  PGR_DBG("Calling compute_trsp");
134  compute_trsp(
135  text_to_cstring(PG_GETARG_TEXT_P(0)),
136  text_to_cstring(PG_GETARG_TEXT_P(1)),
137  PG_GETARG_ARRAYTYPE_P(2),
138  PG_GETARG_ARRAYTYPE_P(3),
139  PG_GETARG_BOOL(4),
140  &result_tuples, &result_count);
141 
142  //-----------------------------------------------
143 
144 #if PGSQL_VERSION > 95
145  funcctx->max_calls = result_count;
146 #else
147  funcctx->max_calls = (uint32_t)result_count;
148 #endif
149 
150  funcctx->user_fctx = result_tuples;
151  if (get_call_result_type(fcinfo, NULL, &tuple_desc)
152  != TYPEFUNC_COMPOSITE) {
153  ereport(ERROR,
154  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
155  errmsg("function returning record called in context "
156  "that cannot accept type record")));
157  }
158 
159  funcctx->tuple_desc = tuple_desc;
160  MemoryContextSwitchTo(oldcontext);
161  }
162 
163  funcctx = SRF_PERCALL_SETUP();
164 
165  tuple_desc = funcctx->tuple_desc;
166  result_tuples = (General_path_element_t *) funcctx->user_fctx;
167 
168  if (funcctx->call_cntr < funcctx->max_calls) {
169  // do when there is more left to send
170  HeapTuple tuple;
171  Datum result;
172  Datum *values;
173  bool* nulls;
174  size_t call_cntr = funcctx->call_cntr;
175 
176 
177  size_t numb = 8;
178  values = palloc(numb * sizeof(Datum));
179  nulls = palloc(numb * sizeof(bool));
180 
181  size_t i;
182  for (i = 0; i < numb; ++i) {
183  nulls[i] = false;
184  }
185 
186  values[0] = Int32GetDatum(call_cntr + 1);
187  values[1] = Int32GetDatum(result_tuples[call_cntr].seq);
188  values[2] = Int64GetDatum(result_tuples[call_cntr].start_id);
189  values[3] = Int64GetDatum(result_tuples[call_cntr].end_id);
190  values[4] = Int64GetDatum(result_tuples[call_cntr].node);
191  values[5] = Int64GetDatum(result_tuples[call_cntr].edge);
192  values[6] = Float8GetDatum(result_tuples[call_cntr].cost);
193  values[7] = Float8GetDatum(result_tuples[call_cntr].agg_cost);
194 
195  tuple = heap_form_tuple(tuple_desc, values, nulls);
196 
197  result = HeapTupleGetDatum(tuple);
198 
199  pfree(values);
200  pfree(nulls);
201 
202  SRF_RETURN_NEXT(funcctx, result);
203  } else {
204  SRF_RETURN_DONE(funcctx);
205  }
206 }
207 
Definition: trsp.h:31
void pgr_get_restrictions(char *restrictions_sql, Restriction_t **restrictions, size_t *total_restrictions)
#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:545
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 do_trsp(pgr_edge_t *edges, size_t total_edges, Restriction_t *restrictions, size_t restrictions_size, int64_t *start_vidsArr, size_t size_start_vidsArr, int64_t *end_vidsArr, size_t size_end_vidsArr, bool directed, General_path_element_t **return_tuples, size_t *return_count, char **log_msg, char **notice_msg, char **err_msg)
Definition: trsp_driver.cpp:41
static void compute_trsp(char *edges_sql, char *restrictions_sql, ArrayType *starts, ArrayType *ends, bool directed, General_path_element_t **result_tuples, size_t *result_count)
Definition: new_trsp.c:50
void pgr_global_report(char *log, char *notice, char *err)
notice & error
Definition: e_report.c:93
void pgr_SPI_finish(void)
PGDLLEXPORT Datum turn_restriction(PG_FUNCTION_ARGS)
Definition: new_trsp.c:120
if(DOXYGEN_FOUND) configure_file($
Definition: CMakeLists.txt:13
void pgr_SPI_connect(void)
PG_FUNCTION_INFO_V1(turn_restriction)