PGROUTING  3.2
vehicles_input.c
Go to the documentation of this file.
1 /*PGR-GNU*****************************************************************
2 File: vehicles_input.c
3 
4 Copyright (c) 2016 Celia Virginia Vergara Castillo
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 
26 
27 #include <limits.h>
28 #include <float.h>
29 
30 
31 #include "c_types/column_info_t.h"
32 
33 #include "c_common/debug_macro.h"
35 #include "c_common/time_msg.h"
36 
37 
38 static
40  HeapTuple *tuple,
41  TupleDesc *tupdesc,
42  Column_info_t info[16],
43  Vehicle_t *vehicle,
44  bool with_id) {
45  vehicle->id = pgr_SPI_getBigInt(tuple, tupdesc, info[0]);
46  vehicle->capacity = pgr_SPI_getFloat8(tuple, tupdesc, info[1]);
47 
48  vehicle->start_x = with_id ?
49  0 :
50  pgr_SPI_getFloat8(tuple, tupdesc, info[2]);
51  vehicle->start_y = with_id ?
52  0 :
53  pgr_SPI_getFloat8(tuple, tupdesc, info[3]);
54 
55  vehicle->speed = column_found(info[13].colNumber) ?
56  pgr_SPI_getFloat8(tuple, tupdesc, info[13]) :
57  1;
58  vehicle->cant_v = column_found(info[4].colNumber) ?
59  pgr_SPI_getBigInt(tuple, tupdesc, info[4]) :
60  1;
61  vehicle->start_open_t = column_found(info[5].colNumber) ?
62  pgr_SPI_getFloat8(tuple, tupdesc, info[5]) :
63  0;
64  vehicle->start_close_t = column_found(info[6].colNumber) ?
65  pgr_SPI_getFloat8(tuple, tupdesc, info[6]) :
66  DBL_MAX;
67  vehicle->start_service_t = column_found(info[7].colNumber) ?
68  pgr_SPI_getFloat8(tuple, tupdesc, info[7]) :
69  0;
70 
71 
72  if (!(column_found(info[8].colNumber))
73  && column_found(info[9].colNumber)) {
74  ereport(ERROR,
75  (errmsg("Column \'%s\' not Found", info[8].name),
76  errhint("%s was found, also column is expected %s ",
77  info[9].name, info[8].name)));
78  }
79  if (column_found(info[8].colNumber)
80  && !(column_found(info[9].colNumber))) {
81  ereport(ERROR,
82  (errmsg("Column \'%s\' not Found", info[9].name),
83  errhint("%s was found, also column is expected %s ",
84  info[8].name, info[9].name)));
85  }
86 
87  vehicle->end_x = column_found(info[8].colNumber) ?
88  pgr_SPI_getFloat8(tuple, tupdesc, info[8]) :
89  vehicle->start_x;
90  vehicle->end_y = column_found(info[9].colNumber) ?
91  pgr_SPI_getFloat8(tuple, tupdesc, info[9]) :
92  vehicle->start_y;
93 
94  if (!(column_found(info[10].colNumber))
95  && column_found(info[11].colNumber)) {
96  ereport(ERROR,
97  (errmsg("Column \'%s\' not Found", info[10].name),
98  errhint("%s was found, also column is expected %s ",
99  info[10].name, info[11].name)));
100  }
101 
102  if (column_found(info[10].colNumber)
103  && !(column_found(info[11].colNumber))) {
104  ereport(ERROR,
105  (errmsg("Column \'%s\' not Found", info[11].name),
106  errhint("%s was found, also column is expected %s ",
107  info[11].name, info[10].name)));
108  }
109  vehicle->end_open_t = column_found(info[10].colNumber) ?
110  pgr_SPI_getFloat8(tuple, tupdesc, info[10]) :
111  vehicle->start_open_t;
112  vehicle->end_close_t = column_found(info[11].colNumber) ?
113  pgr_SPI_getFloat8(tuple, tupdesc, info[11]) :
114  vehicle->start_close_t;
115  vehicle->end_service_t = column_found(info[12].colNumber) ?
116  pgr_SPI_getFloat8(tuple, tupdesc, info[12]) :
117  vehicle->start_service_t;
118 
119  vehicle->speed = column_found(info[13].colNumber) ?
120  pgr_SPI_getFloat8(tuple, tupdesc, info[13]) :
121  1;
122  vehicle->start_node_id = with_id ?
123  pgr_SPI_getBigInt(tuple, tupdesc, info[14]) :
124  0;
125  vehicle->end_node_id = with_id ?
126  (column_found(info[12].colNumber) ?
127  pgr_SPI_getBigInt(tuple, tupdesc, info[15]) :
128  vehicle->start_node_id) :
129  0;
130 }
131 
132 
133 
134 static
136  char *vehicles_sql,
137  Vehicle_t **vehicles,
138  size_t *total_vehicles,
139  bool with_id) {
140  clock_t start_t = clock();
141 
142  const int tuple_limit = 1000000;
143 
144  PGR_DBG("pgr_get_vehicles");
145  PGR_DBG("%s", vehicles_sql);
146 
147  Column_info_t info[16];
148 
149  int i;
150  for (i = 0; i < 16; ++i) {
151  info[i].colNumber = -1;
152  info[i].type = 0;
153  info[i].strict = true;
154  info[i].eType = ANY_NUMERICAL;
155  }
156 
157  info[0].name = "id";
158  info[1].name = "capacity";
159  info[2].name = "start_x";
160  info[3].name = "start_y";
161  info[4].name = "number";
162  info[5].name = "start_open";
163  info[6].name = "start_close";
164  info[7].name = "start_service";
165  info[8].name = "end_x";
166  info[9].name = "end_y";
167  info[10].name = "end_open";
168  info[11].name = "end_close";
169  info[12].name = "end_service";
170  info[13].name = "speed";
171  info[14].name = "start_node_id";
172  info[15].name = "end_node_id";
173 
174  info[0].eType = ANY_INTEGER;
175  info[4].eType = ANY_INTEGER;
176  info[14].eType = ANY_INTEGER;
177  info[15].eType = ANY_INTEGER;
178 
179  for (i = 4; i < 16; ++i) {
180  info[i].strict = false;
181  }
182 
183  if (with_id) {
184  /*
185  * with id, then start_x and start_y are optional
186  * start_node_id is compulsory
187  */
188  info[2].strict = false;
189  info[3].strict = false;
190  info[14].strict = true;
191  }
192 
193  size_t total_tuples;
194 
195  void *SPIplan;
196  SPIplan = pgr_SPI_prepare(vehicles_sql);
197  Portal SPIportal;
198  SPIportal = pgr_SPI_cursor_open(SPIplan);
199 
200  bool moredata = true;
201  (*total_vehicles) = total_tuples = 0;
202 
203  /* on the first tuple get the column numbers */
204 
205  while (moredata == true) {
206  SPI_cursor_fetch(SPIportal, true, tuple_limit);
207  if (total_tuples == 0) {
208  pgr_fetch_column_info(info, 16);
209  }
210  size_t ntuples = SPI_processed;
211  total_tuples += ntuples;
212  PGR_DBG("SPI_processed %ld", ntuples);
213  if (ntuples > 0) {
214  if ((*vehicles) == NULL)
215  (*vehicles) = (Vehicle_t *)palloc0(
216  total_tuples * sizeof(Vehicle_t));
217  else
218  (*vehicles) = (Vehicle_t *)repalloc(
219  (*vehicles),
220  total_tuples * sizeof(Vehicle_t));
221 
222  if ((*vehicles) == NULL) {
223  elog(ERROR, "Out of memory");
224  }
225 
226  size_t t;
227  SPITupleTable *tuptable = SPI_tuptable;
228  TupleDesc tupdesc = SPI_tuptable->tupdesc;
229  PGR_DBG("processing %ld", ntuples);
230  for (t = 0; t < ntuples; t++) {
231  HeapTuple tuple = tuptable->vals[t];
232  fetch_vehicles(&tuple, &tupdesc, info,
233  &(*vehicles)[total_tuples - ntuples + t], with_id);
234  }
235  SPI_freetuptable(tuptable);
236  } else {
237  moredata = false;
238  }
239  }
240 
241  SPI_cursor_close(SPIportal);
242 
243  if (total_tuples == 0) {
244  (*total_vehicles) = 0;
245  PGR_DBG("NO orders");
246  return;
247  }
248 
249  (*total_vehicles) = total_tuples;
250  if (with_id) {
251  PGR_DBG("Finish reading %ld vehicles for matrix", (*total_vehicles));
252  } else {
253  PGR_DBG("Finish reading %ld vehicles for euclidean", (*total_vehicles));
254  }
255  time_msg("reading edges", start_t, clock());
256 }
257 
258 void
260  char *vehicles_sql,
261  Vehicle_t **vehicles,
262  size_t *total_vehicles) {
263  pgr_get_vehicles_general(vehicles_sql, vehicles, total_vehicles, false);
264 }
265 
266 void
268  char *vehicles_sql,
269  Vehicle_t **vehicles,
270  size_t *total_vehicles) {
271  pgr_get_vehicles_general(vehicles_sql, vehicles, total_vehicles, true);
272 }
273 
Column_info_t::colNumber
int colNumber
Definition: column_info_t.h:51
Vehicle_t::capacity
double capacity
Definition: vehicle_t.h:42
Vehicle_t::end_service_t
double end_service_t
Definition: vehicle_t.h:61
time_msg.h
Vehicle_t::start_open_t
double start_open_t
Definition: vehicle_t.h:51
Column_info_t::strict
bool strict
Definition: column_info_t.h:53
pgr_SPI_prepare
SPIPlanPtr pgr_SPI_prepare(char *sql)
Definition: postgres_connection.c:94
fetch_vehicles
static void fetch_vehicles(HeapTuple *tuple, TupleDesc *tupdesc, Column_info_t info[16], Vehicle_t *vehicle, bool with_id)
Definition: vehicles_input.c:39
Vehicle_t::end_close_t
double end_close_t
Definition: vehicle_t.h:60
Vehicle_t::start_y
double start_y
Definition: vehicle_t.h:46
Vehicle_t::end_open_t
double end_open_t
Definition: vehicle_t.h:59
Vehicle_t
Definition: vehicle_t.h:40
Vehicle_t::start_close_t
double start_close_t
Definition: vehicle_t.h:52
vehicles_input.h
pgr_SPI_getBigInt
int64_t pgr_SPI_getBigInt(HeapTuple *tuple, TupleDesc *tupdesc, Column_info_t info)
Function returns the value of specified column in integer type.
Definition: get_check_data.c:216
pgr_fetch_column_info
void pgr_fetch_column_info(Column_info_t info[], int info_size)
Function tells expected type of each column and then check the correspondence type of each column.
Definition: get_check_data.c:78
Vehicle_t::end_x
double end_x
Definition: vehicle_t.h:55
debug_macro.h
pgr_get_vehicles
void pgr_get_vehicles(char *vehicles_sql, Vehicle_t **vehicles, size_t *total_vehicles)
Reads the vehicles orders.
Definition: vehicles_input.c:259
Column_info_t::name
char * name
Definition: column_info_t.h:54
pgr_SPI_cursor_open
Portal pgr_SPI_cursor_open(SPIPlanPtr SPIplan)
Definition: postgres_connection.c:107
Vehicle_t::cant_v
int64_t cant_v
Definition: vehicle_t.h:49
PGR_DBG
#define PGR_DBG(...)
Definition: debug_macro.h:34
Vehicle_t::end_node_id
int64_t end_node_id
Definition: vehicle_t.h:57
Vehicle_t::start_service_t
double start_service_t
Definition: vehicle_t.h:53
Vehicle_t::start_x
double start_x
Definition: vehicle_t.h:45
Vehicle_t::end_y
double end_y
Definition: vehicle_t.h:56
Vehicle_t::speed
double speed
Definition: vehicle_t.h:43
pgr_SPI_getFloat8
double pgr_SPI_getFloat8(HeapTuple *tuple, TupleDesc *tupdesc, Column_info_t info)
Function returns the value of specified column in double type.
Definition: get_check_data.c:246
Column_info_t::eType
expectType eType
Definition: column_info_t.h:55
pgr_get_vehicles_general
static void pgr_get_vehicles_general(char *vehicles_sql, Vehicle_t **vehicles, size_t *total_vehicles, bool with_id)
Definition: vehicles_input.c:135
time_msg
void time_msg(char *msg, clock_t start_t, clock_t end_t)
Definition: time_msg.c:32
ANY_INTEGER
@ ANY_INTEGER
Definition: column_info_t.h:41
column_info_t.h
Vehicle_t::start_node_id
int64_t start_node_id
Definition: vehicle_t.h:47
ANY_NUMERICAL
@ ANY_NUMERICAL
Definition: column_info_t.h:42
column_found
bool column_found(int colNumber)
Function will check whether the colNumber represent any specific column or NULL (SPI_ERROR_NOATTRIBUT...
Definition: get_check_data.c:36
Vehicle_t::id
int64_t id
Definition: vehicle_t.h:41
get_check_data.h
Column_info_t::type
uint64_t type
Definition: column_info_t.h:52
Column_info_t
Definition: column_info_t.h:49
pgr_get_vehicles_with_id
void pgr_get_vehicles_with_id(char *vehicles_sql, Vehicle_t **vehicles, size_t *total_vehicles)
Reads the vehicles orders.
Definition: vehicles_input.c:267