PGROUTING  3.2
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
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 #include <stdbool.h>
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 _trsp(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  size_t size_start_vidsArr = 0;
72  int64_t* start_vidsArr = (int64_t*)
73  pgr_get_bigIntArray(&size_start_vidsArr, starts);
74 
75  size_t size_end_vidsArr = 0;
76  int64_t* end_vidsArr = (int64_t*)
77  pgr_get_bigIntArray(&size_end_vidsArr, ends);
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;
84 
85  do_trsp(
86  edges,
87  total_edges,
88 
89  restrictions,
90  total_restrictions,
91 
92  start_vidsArr, size_start_vidsArr,
93  end_vidsArr, size_end_vidsArr,
94 
95  directed,
96 
97  result_tuples,
98  result_count,
99  &log_msg,
100  &notice_msg,
101  &err_msg);
102  time_msg("processing _pgr_trsp", start_t, clock());
103 
104  if (err_msg && (*result_tuples)) {
105  pfree(*result_tuples);
106  (*result_tuples) = NULL;
107  (*result_count) = 0;
108  }
109 
110  pgr_global_report(log_msg, notice_msg, err_msg);
111 
112  pgr_SPI_finish();
113 }
114 
115 
116 
117 PGDLLEXPORT Datum
118 _trsp(PG_FUNCTION_ARGS) {
119  FuncCallContext *funcctx;
120  TupleDesc tuple_desc;
121 
122  size_t result_count = 0;
123  General_path_element_t *result_tuples = NULL;
124 
125  // stuff done only on the first call of the function
126  if (SRF_IS_FIRSTCALL()) {
127  MemoryContext oldcontext;
128  funcctx = SRF_FIRSTCALL_INIT();
129  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
130 
131  PGR_DBG("Calling compute_trsp");
132  compute_trsp(
133  text_to_cstring(PG_GETARG_TEXT_P(0)),
134  text_to_cstring(PG_GETARG_TEXT_P(1)),
135  PG_GETARG_ARRAYTYPE_P(2),
136  PG_GETARG_ARRAYTYPE_P(3),
137  PG_GETARG_BOOL(4),
138  &result_tuples, &result_count);
139 
140  //-----------------------------------------------
141 
142 #if PGSQL_VERSION > 95
143  funcctx->max_calls = result_count;
144 #else
145  funcctx->max_calls = (uint32_t)result_count;
146 #endif
147 
148  funcctx->user_fctx = result_tuples;
149  if (get_call_result_type(fcinfo, NULL, &tuple_desc)
150  != TYPEFUNC_COMPOSITE) {
151  ereport(ERROR,
152  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
153  errmsg("function returning record called in context "
154  "that cannot accept type record")));
155  }
156 
157  funcctx->tuple_desc = tuple_desc;
158  MemoryContextSwitchTo(oldcontext);
159  }
160 
161  funcctx = SRF_PERCALL_SETUP();
162 
163  tuple_desc = funcctx->tuple_desc;
164  result_tuples = (General_path_element_t *) funcctx->user_fctx;
165 
166  if (funcctx->call_cntr < funcctx->max_calls) {
167  // do when there is more left to send
168  HeapTuple tuple;
169  Datum result;
170  Datum *values;
171  bool* nulls;
172  size_t call_cntr = funcctx->call_cntr;
173 
174 
175  size_t numb = 8;
176  values = palloc(numb * sizeof(Datum));
177  nulls = palloc(numb * sizeof(bool));
178 
179  size_t i;
180  for (i = 0; i < numb; ++i) {
181  nulls[i] = false;
182  }
183 
184  values[0] = Int32GetDatum(call_cntr + 1);
185  values[1] = Int32GetDatum(result_tuples[call_cntr].seq);
186  values[2] = Int64GetDatum(result_tuples[call_cntr].start_id);
187  values[3] = Int64GetDatum(result_tuples[call_cntr].end_id);
188  values[4] = Int64GetDatum(result_tuples[call_cntr].node);
189  values[5] = Int64GetDatum(result_tuples[call_cntr].edge);
190  values[6] = Float8GetDatum(result_tuples[call_cntr].cost);
191  values[7] = Float8GetDatum(result_tuples[call_cntr].agg_cost);
192 
193  tuple = heap_form_tuple(tuple_desc, values, nulls);
194 
195  result = HeapTupleGetDatum(tuple);
196 
197  pfree(values);
198  pfree(nulls);
199 
200  SRF_RETURN_NEXT(funcctx, result);
201  } else {
202  SRF_RETURN_DONE(funcctx);
203  }
204 }
205 
time_msg.h
PG_FUNCTION_INFO_V1
PG_FUNCTION_INFO_V1(_trsp)
postgres_connection.h
pgr_edge_t
Definition: pgr_edge_t.h:37
trsp_driver.h
pgr_SPI_connect
void pgr_SPI_connect(void)
Definition: postgres_connection.c:82
pgr_get_restrictions
void pgr_get_restrictions(char *restrictions_sql, Restriction_t **restrictions, size_t *total_restrictions)
Definition: restrictions_input.c:63
pgr_SPI_finish
void pgr_SPI_finish(void)
Definition: postgres_connection.c:71
e_report.h
arrays_input.h
edge
Definition: trsp.h:41
debug_macro.h
pgr_get_bigIntArray
int64_t * pgr_get_bigIntArray(size_t *arrlen, ArrayType *input)
Enforces the input array to be NOT empty.
Definition: arrays_input.c:146
do_trsp
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
PGR_DBG
#define PGR_DBG(...)
Definition: debug_macro.h:34
compute_trsp
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
restrictions_input.h
pgr_get_edges
void pgr_get_edges(char *edges_sql, pgr_edge_t **edges, size_t *total_edges)
basic edge_sql
Definition: edges_input.c:711
_trsp
PGDLLEXPORT Datum _trsp(PG_FUNCTION_ARGS)
Definition: new_trsp.c:118
restriction_t.h
General_path_element_t
Definition: general_path_element_t.h:37
if
if(DOXYGEN_FOUND) configure_file($
Definition: doxygen/CMakeLists.txt:13
time_msg
void time_msg(char *msg, clock_t start_t, clock_t end_t)
Definition: time_msg.c:32
Restriction_t
Definition: restriction_t.h:37
general_path_element_t.h
edges_input.h
pgr_edge_t.h
pgr_global_report
void pgr_global_report(char *log, char *notice, char *err)
notice & error
Definition: e_report.c:93