PGROUTING  2.6-dev
orders_input.c
Go to the documentation of this file.
1 /*PGR-GNU*****************************************************************
2 File: pd_orders_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 
25 #include "c_common/orders_input.h"
26 
27 #include "c_types/column_info_t.h"
28 
29 #include "c_common/debug_macro.h"
31 #include "c_common/time_msg.h"
32 
33 
34 static
36  HeapTuple *tuple,
37  TupleDesc *tupdesc,
38  Column_info_t info[14],
39  bool matrix_version,
40  PickDeliveryOrders_t *pd_order) {
41  pd_order->id = pgr_SPI_getBigInt(tuple, tupdesc, info[0]);
42  pd_order->demand = pgr_SPI_getFloat8(tuple, tupdesc, info[1]);
43 
44  /*
45  * the pickups
46  */
47  pd_order->pick_x = matrix_version ?
48  0 : pgr_SPI_getFloat8(tuple, tupdesc, info[2]);
49  pd_order->pick_y = matrix_version ?
50  0 : pgr_SPI_getFloat8(tuple, tupdesc, info[3]);
51  pd_order->pick_open_t = pgr_SPI_getFloat8(tuple, tupdesc, info[4]);
52  pd_order->pick_close_t = pgr_SPI_getFloat8(tuple, tupdesc, info[5]);
53  pd_order->pick_service_t = column_found(info[6].colNumber) ?
54  pgr_SPI_getFloat8(tuple, tupdesc, info[6]) : 0;
55 
56  /*
57  * the deliveries
58  */
59  pd_order->deliver_x = matrix_version ?
60  0 : pgr_SPI_getFloat8(tuple, tupdesc, info[7]);
61  pd_order->deliver_y = matrix_version ?
62  0 : pgr_SPI_getFloat8(tuple, tupdesc, info[8]);
63  pd_order->deliver_open_t = pgr_SPI_getFloat8(tuple, tupdesc, info[9]);
64  pd_order->deliver_close_t = pgr_SPI_getFloat8(tuple, tupdesc, info[10]);
65  pd_order->deliver_service_t = column_found(info[11].colNumber) ?
66  pgr_SPI_getFloat8(tuple, tupdesc, info[11]) : 0;
67 
68  pd_order->pick_node_id = matrix_version ?
69  pgr_SPI_getBigInt(tuple, tupdesc, info[12]) : 0;
70  pd_order->deliver_node_id = matrix_version ?
71  pgr_SPI_getBigInt(tuple, tupdesc, info[13]) : 0;
72 }
73 
74 
75 
76 static
77 void
79  char *pd_orders_sql,
80  PickDeliveryOrders_t **pd_orders,
81  size_t *total_pd_orders,
82  bool with_id) {
83  clock_t start_t = clock();
84 
85  const int tuple_limit = 1000000;
86 
87  PGR_DBG("pgr_get_pd_orders_data");
88  PGR_DBG("%s", pd_orders_sql);
89 
90  Column_info_t info[14];
91 
92  int i;
93  for (i = 0; i < 14; ++i) {
94  info[i].colNumber = -1;
95  info[i].type = 0;
96  info[i].strict = true;
97  info[i].eType = ANY_NUMERICAL;
98  }
99 
100  info[0].name = "id";
101  info[1].name = "demand";
102  info[2].name = "p_x";
103  info[3].name = "p_y";
104  info[4].name = "p_open";
105  info[5].name = "p_close";
106  info[6].name = "p_service";
107  info[7].name = "d_x";
108  info[8].name = "d_y";
109  info[9].name = "d_open";
110  info[10].name = "d_close";
111  info[11].name = "d_service";
112  info[12].name = "p_node_id";
113  info[13].name = "d_node_id";
114 
115  info[0].eType = ANY_INTEGER;
116  info[12].eType = ANY_INTEGER;
117  info[13].eType = ANY_INTEGER;
118 
119  /* service is optional*/
120  info[6].strict = false;
121  info[11].strict = false;
122  /* nodes are going to be ignored*/
123  info[12].strict = false;
124  info[13].strict = false;
125 
126  if (with_id) {
127  /* (x,y) values are ignored*/
128  info[2].strict = false;
129  info[3].strict = false;
130  info[7].strict = false;
131  info[8].strict = false;
132  /* nodes are compulsory*/
133  info[12].strict = true;
134  info[13].strict = true;
135  }
136 
137 
138 
139  size_t ntuples;
140  size_t total_tuples;
141 
142  void *SPIplan;
143  SPIplan = pgr_SPI_prepare(pd_orders_sql);
144  Portal SPIportal;
145  SPIportal = pgr_SPI_cursor_open(SPIplan);
146 
147  bool moredata = TRUE;
148  (*total_pd_orders) = total_tuples = 0;
149 
150  /* on the first tuple get the column numbers */
151 
152  while (moredata == TRUE) {
153  SPI_cursor_fetch(SPIportal, TRUE, tuple_limit);
154  if (total_tuples == 0) {
155  pgr_fetch_column_info(info, 14);
156  }
157  ntuples = SPI_processed;
158  total_tuples += ntuples;
159  PGR_DBG("SPI_processed %ld", ntuples);
160  if (ntuples > 0) {
161  if ((*pd_orders) == NULL)
162  (*pd_orders) = (PickDeliveryOrders_t *)palloc0(
163  total_tuples * sizeof(PickDeliveryOrders_t));
164  else
165  (*pd_orders) = (PickDeliveryOrders_t *)repalloc(
166  (*pd_orders),
167  total_tuples * sizeof(PickDeliveryOrders_t));
168 
169  if ((*pd_orders) == NULL) {
170  elog(ERROR, "Out of memory");
171  }
172 
173  size_t t;
174  SPITupleTable *tuptable = SPI_tuptable;
175  TupleDesc tupdesc = SPI_tuptable->tupdesc;
176  PGR_DBG("processing %ld", ntuples);
177  for (t = 0; t < ntuples; t++) {
178  HeapTuple tuple = tuptable->vals[t];
179  fetch_pd_orders(&tuple, &tupdesc, info, with_id,
180  &(*pd_orders)[total_tuples - ntuples + t]);
181  }
182  SPI_freetuptable(tuptable);
183  } else {
184  moredata = FALSE;
185  }
186  }
187 
188  SPI_cursor_close(SPIportal);
189 
190  if (total_tuples == 0) {
191  (*total_pd_orders) = 0;
192  PGR_DBG("NO orders");
193  return;
194  }
195 
196  (*total_pd_orders) = total_tuples;
197  if (with_id) {
198  PGR_DBG("Finish reading %ld orders for matrix", (*total_pd_orders));
199  } else {
200  PGR_DBG("Finish reading %ld orders for eucledian", (*total_pd_orders));
201  }
202  time_msg("reading edges", start_t, clock());
203 }
204 
205 void
207  char *pd_orders_sql,
208  PickDeliveryOrders_t **pd_orders,
209  size_t *total_pd_orders) {
210  pgr_get_pd_orders_general(pd_orders_sql, pd_orders, total_pd_orders, false);
211 }
212 
213 void
215  char *pd_orders_sql,
216  PickDeliveryOrders_t **pd_orders,
217  size_t *total_pd_orders) {
218  pgr_get_pd_orders_general(pd_orders_sql, pd_orders, total_pd_orders, true);
219 }
int64_t pgr_SPI_getBigInt(HeapTuple *tuple, TupleDesc *tupdesc, Column_info_t info)
void pgr_get_pd_orders(char *pd_orders_sql, PickDeliveryOrders_t **pd_orders, size_t *total_pd_orders)
Reads the pick-Deliver orders.
Definition: orders_input.c:206
void pgr_get_pd_orders_with_id(char *pd_orders_sql, PickDeliveryOrders_t **pd_orders, size_t *total_pd_orders)
Reads the pick-Deliver orders.
Definition: orders_input.c:214
#define PGR_DBG(...)
Definition: debug_macro.h:34
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
static void pgr_get_pd_orders_general(char *pd_orders_sql, PickDeliveryOrders_t **pd_orders, size_t *total_pd_orders, bool with_id)
Definition: orders_input.c:78
static void fetch_pd_orders(HeapTuple *tuple, TupleDesc *tupdesc, Column_info_t info[14], bool matrix_version, PickDeliveryOrders_t *pd_order)
Definition: orders_input.c:35
void pgr_fetch_column_info(Column_info_t info[], int info_size)
bool column_found(int colNumber)
Portal pgr_SPI_cursor_open(SPIPlanPtr SPIplan)
double pgr_SPI_getFloat8(HeapTuple *tuple, TupleDesc *tupdesc, Column_info_t info)
SPIPlanPtr pgr_SPI_prepare(char *sql)
expectType eType
Definition: column_info_t.h:76