PGROUTING  2.6
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
5 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 
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 ntuples;
194  size_t total_tuples;
195 
196  void *SPIplan;
197  SPIplan = pgr_SPI_prepare(vehicles_sql);
198  Portal SPIportal;
199  SPIportal = pgr_SPI_cursor_open(SPIplan);
200 
201  bool moredata = true;
202  (*total_vehicles) = total_tuples = 0;
203 
204  /* on the first tuple get the column numbers */
205 
206  while (moredata == true) {
207  SPI_cursor_fetch(SPIportal, true, tuple_limit);
208  if (total_tuples == 0) {
209  pgr_fetch_column_info(info, 16);
210  }
211  ntuples = SPI_processed;
212  total_tuples += ntuples;
213  PGR_DBG("SPI_processed %ld", ntuples);
214  if (ntuples > 0) {
215  if ((*vehicles) == NULL)
216  (*vehicles) = (Vehicle_t *)palloc0(
217  total_tuples * sizeof(Vehicle_t));
218  else
219  (*vehicles) = (Vehicle_t *)repalloc(
220  (*vehicles),
221  total_tuples * sizeof(Vehicle_t));
222 
223  if ((*vehicles) == NULL) {
224  elog(ERROR, "Out of memory");
225  }
226 
227  size_t t;
228  SPITupleTable *tuptable = SPI_tuptable;
229  TupleDesc tupdesc = SPI_tuptable->tupdesc;
230  PGR_DBG("processing %ld", ntuples);
231  for (t = 0; t < ntuples; t++) {
232  HeapTuple tuple = tuptable->vals[t];
233  fetch_vehicles(&tuple, &tupdesc, info,
234  &(*vehicles)[total_tuples - ntuples + t], with_id);
235  }
236  SPI_freetuptable(tuptable);
237  } else {
238  moredata = false;
239  }
240  }
241 
242  SPI_cursor_close(SPIportal);
243 
244  if (total_tuples == 0) {
245  (*total_vehicles) = 0;
246  PGR_DBG("NO orders");
247  return;
248  }
249 
250  (*total_vehicles) = total_tuples;
251  if (with_id) {
252  PGR_DBG("Finish reading %ld vehicles for matrix", (*total_vehicles));
253  } else {
254  PGR_DBG("Finish reading %ld vehicles for eucledian", (*total_vehicles));
255  }
256  time_msg("reading edges", start_t, clock());
257 }
258 
259 void
261  char *vehicles_sql,
262  Vehicle_t **vehicles,
263  size_t *total_vehicles) {
264  pgr_get_vehicles_general(vehicles_sql, vehicles, total_vehicles, false);
265 }
266 
267 void
269  char *vehicles_sql,
270  Vehicle_t **vehicles,
271  size_t *total_vehicles) {
272  pgr_get_vehicles_general(vehicles_sql, vehicles, total_vehicles, true);
273 }
274 
int64_t pgr_SPI_getBigInt(HeapTuple *tuple, TupleDesc *tupdesc, Column_info_t info)
Function returns the value of specified column in integer type.
void pgr_get_vehicles_with_id(char *vehicles_sql, Vehicle_t **vehicles, size_t *total_vehicles)
Reads the vehicles orders.
static void fetch_vehicles(HeapTuple *tuple, TupleDesc *tupdesc, Column_info_t info[16], Vehicle_t *vehicle, bool with_id)
double capacity
Definition: vehicle_t.h:62
double end_service_t
Definition: vehicle_t.h:81
static void pgr_get_vehicles_general(char *vehicles_sql, Vehicle_t **vehicles, size_t *total_vehicles, bool with_id)
double start_open_t
Definition: vehicle_t.h:71
#define PGR_DBG(...)
Definition: debug_macro.h:34
double end_close_t
Definition: vehicle_t.h:80
double end_open_t
Definition: vehicle_t.h:79
uint64_t type
Definition: column_info_t.h:73
void time_msg(char *msg, clock_t start_t, clock_t end_t)
Definition: time_msg.c:32
double start_y
Definition: vehicle_t.h:66
double start_close_t
Definition: vehicle_t.h:72
int64_t cant_v
Definition: vehicle_t.h:69
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...
double start_service_t
Definition: vehicle_t.h:73
double end_x
Definition: vehicle_t.h:75
double end_y
Definition: vehicle_t.h:76
int64_t end_node_id
Definition: vehicle_t.h:77
double start_x
Definition: vehicle_t.h:65
double speed
Definition: vehicle_t.h:63
int64_t start_node_id
Definition: vehicle_t.h:67
bool column_found(int colNumber)
Function will check whether the colNumber represent any specific column or NULL (SPI_ERROR_NOATTRIBUT...
Portal pgr_SPI_cursor_open(SPIPlanPtr SPIplan)
double pgr_SPI_getFloat8(HeapTuple *tuple, TupleDesc *tupdesc, Column_info_t info)
Function returns the value of specified column in double type.
int64_t id
Definition: vehicle_t.h:61
void pgr_get_vehicles(char *vehicles_sql, Vehicle_t **vehicles, size_t *total_vehicles)
Reads the vehicles orders.
SPIPlanPtr pgr_SPI_prepare(char *sql)
expectType eType
Definition: column_info_t.h:76