vrpRouting  0.3
shipments_input.c
Go to the documentation of this file.
1 /*PGR-GNU*****************************************************************
2 File: shipments_input.c
3 
4 Copyright (c) 2021 pgRouting developers
6 
7 Function's developer:
8 Copyright (c) 2021 Ashish Kumar
10 
11 ------
12 
13 This program is free software; you can redistribute it and/or modify
14 it under the terms of the GNU General Public License as published by
15 the Free Software Foundation; either version 2 of the License, or
16 (at your option) any later version.
17 
18 This program is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
22 
23 You should have received a copy of the GNU General Public License
24 along with this program; if not, write to the Free Software
25 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 
27  ********************************************************************PGR-GNU*/
28 
30 
31 /*
32 .. vrp_vroom start
33 
34 A ``SELECT`` statement that returns the following columns:
35 
36 ::
37 
38  id, p_location_id [, p_setup, p_service], d_location_id [, d_setup, d_service]
39  [, amount, skills, priority, p_data, d_data]
40 
41 
42 ====================== ========================= =========== ================================================
43 Column Type Default Description
44 ====================== ========================= =========== ================================================
45 **id** ``ANY-INTEGER`` Positive unique identifier of the shipment.
46 
47 **p_location_id** ``ANY-INTEGER`` Positive identifier of the pickup location.
48 
49 **p_setup** |interval| |interval0| Pickup setup duration.
50 
51 **p_service** |interval| |interval0| Pickup service duration.
52 
53 **d_location_id** ``ANY-INTEGER`` Positive identifier of the delivery location.
54 
55 **d_setup** |interval| |interval0| Delivery setup duration.
56 
57 **d_service** |interval| |interval0| Delivery service duration.
58 
59 **amount** ``ARRAY[ANY-INTEGER]`` Empty Array Array of non-negative integers describing
60  multidimensional quantities such as number
61  of items, weight, volume etc.
62 
63  - All shipments must have the same value of
64  :code:`array_length(amount, 1)`
65 
66 **skills** ``ARRAY[INTEGER]`` Empty Array Array of non-negative integers defining
67  mandatory skills.
68 
69 **priority** ``INTEGER`` 0 Priority level of the shipment.
70 
71  - Ranges from ``[0, 100]``
72 
73 **p_data** ``JSONB`` '{}'::JSONB Any metadata information of the pickup shipment.
74 
75 **d_data** ``JSONB`` '{}'::JSONB Any metadata information of the delivery shipment.
76 ====================== ========================= =========== ================================================
77 
78 Where:
79 
80 :ANY-INTEGER: SMALLINT, INTEGER, BIGINT
81 
82 .. vrp_vroom end
83 */
84 
85 static
87  HeapTuple *tuple,
88  TupleDesc *tupdesc,
89  Column_info_t *info,
90  Vroom_shipment_t *shipment,
91  bool is_plain) {
92  shipment->id = get_Idx(tuple, tupdesc, info[0], 0);
93 
94  shipment->p_location_id = get_MatrixIndex(tuple, tupdesc, info[1], 0);
95  shipment->d_location_id = get_MatrixIndex(tuple, tupdesc, info[4], 0);
96 
97  if (is_plain) {
98  shipment->p_setup = get_Duration(tuple, tupdesc, info[2], 0);
99  shipment->p_service = get_Duration(tuple, tupdesc, info[3], 0);
100  shipment->d_setup = get_Duration(tuple, tupdesc, info[5], 0);
101  shipment->d_service = get_Duration(tuple, tupdesc, info[6], 0);
102  } else {
103  shipment->p_setup =
104  (Duration)get_PositiveTInterval(tuple, tupdesc, info[2], 0);
105  shipment->p_service =
106  (Duration)get_PositiveTInterval(tuple, tupdesc, info[3], 0);
107  shipment->d_setup =
108  (Duration)get_PositiveTInterval(tuple, tupdesc, info[5], 0);
109  shipment->d_service =
110  (Duration)get_PositiveTInterval(tuple, tupdesc, info[6], 0);
111  }
112 
113  shipment->amount_size = 0;
114  shipment->amount = column_found(info[7].colNumber) ?
115  spi_getPositiveBigIntArr_allowEmpty(tuple, tupdesc, info[7], &shipment->amount_size)
116  : NULL;
117 
118  shipment->skills_size = 0;
119  shipment->skills = column_found(info[8].colNumber) ?
120  spi_getPositiveIntArr_allowEmpty(tuple, tupdesc, info[8], &shipment->skills_size)
121  : NULL;
122 
123  shipment->priority = get_Priority(tuple, tupdesc, info[9], 0);
124 
125  shipment->p_data = column_found(info[10].colNumber)
126  ? spi_getText(tuple, tupdesc, info[10])
127  : strdup("{}");
128  shipment->d_data = column_found(info[11].colNumber)
129  ? spi_getText(tuple, tupdesc, info[11])
130  : strdup("{}");
131 }
132 
133 
134 static
136  char *shipments_sql,
137  Vroom_shipment_t **shipments,
138  size_t *total_shipments,
139 
140  Column_info_t *info,
141  const int column_count,
142  bool is_plain) {
143 #ifdef PROFILE
144  clock_t start_t = clock();
145  PGR_DBG("%s", shipments_sql);
146 #endif
147 
148  const int tuple_limit = 1000000;
149 
150  size_t total_tuples;
151 
152  void *SPIplan;
153  SPIplan = pgr_SPI_prepare(shipments_sql);
154  Portal SPIportal;
155  SPIportal = pgr_SPI_cursor_open(SPIplan);
156 
157  bool moredata = true;
158  (*total_shipments) = total_tuples = 0;
159 
160  /* on the first tuple get the column numbers */
161 
162  while (moredata == true) {
163  SPI_cursor_fetch(SPIportal, true, tuple_limit);
164  if (total_tuples == 0) {
165  pgr_fetch_column_info(info, column_count);
166  }
167  size_t ntuples = SPI_processed;
168  total_tuples += ntuples;
169  if (ntuples > 0) {
170  if ((*shipments) == NULL)
171  (*shipments) = (Vroom_shipment_t *)palloc0(
172  total_tuples * sizeof(Vroom_shipment_t));
173  else
174  (*shipments) = (Vroom_shipment_t *)repalloc(
175  (*shipments),
176  total_tuples * sizeof(Vroom_shipment_t));
177 
178  if ((*shipments) == NULL) {
179  elog(ERROR, "Out of memory");
180  }
181 
182  size_t t;
183  SPITupleTable *tuptable = SPI_tuptable;
184  TupleDesc tupdesc = SPI_tuptable->tupdesc;
185  for (t = 0; t < ntuples; t++) {
186  HeapTuple tuple = tuptable->vals[t];
187  fetch_shipments(&tuple, &tupdesc, info,
188  &(*shipments)[total_tuples - ntuples + t], is_plain);
189  }
190  SPI_freetuptable(tuptable);
191  } else {
192  moredata = false;
193  }
194  }
195 
196  SPI_cursor_close(SPIportal);
197 
198  if (total_tuples == 0) {
199  (*total_shipments) = 0;
200  return;
201  }
202 
203  (*total_shipments) = total_tuples;
204 #ifdef PROFILE
205  time_msg("reading shipments", start_t, clock());
206 #endif
207 }
208 
209 
215 void
217  char *sql,
218  Vroom_shipment_t **rows,
219  size_t *total_rows,
220  bool is_plain) {
221  int kColumnCount = 12;
222  Column_info_t info[kColumnCount];
223 
224  for (int i = 0; i < kColumnCount; ++i) {
225  info[i].colNumber = -1;
226  info[i].type = 0;
227  info[i].strict = false;
228  info[i].eType = ANY_INTEGER;
229  }
230 
231  info[0].name = "id";
232 
233  /* pickup shipments */
234  info[1].name = "p_location_id";
235  info[2].name = "p_setup";
236  info[3].name = "p_service";
237 
238  /* delivery shipments */
239  info[4].name = "d_location_id";
240  info[5].name = "d_setup";
241  info[6].name = "d_service";
242 
243  info[7].name = "amount";
244  info[8].name = "skills";
245  info[9].name = "priority";
246  info[10].name = "p_data";
247  info[11].name = "d_data";
248 
249  info[2].eType = INTEGER; // p_setup
250  info[3].eType = INTEGER; // p_service
251  info[5].eType = INTEGER; // d_setup
252  info[6].eType = INTEGER; // d_service
253  info[7].eType = ANY_INTEGER_ARRAY; // amount
254  info[8].eType = INTEGER_ARRAY; // skills
255  info[9].eType = INTEGER; // priority
256  info[10].eType = JSONB; // p_data
257  info[11].eType = JSONB; // d_data
258 
259  if (!is_plain) {
260  info[2].eType = INTERVAL; // p_setup
261  info[3].eType = INTERVAL; // p_service
262  info[5].eType = INTERVAL; // d_setup
263  info[6].eType = INTERVAL; // d_service
264  }
265 
266  /* id and location_id of pickup and delivery are mandatory */
267  info[0].strict = true;
268  info[1].strict = true;
269  info[4].strict = true;
270 
271  db_get_shipments(sql, rows, total_rows, info, kColumnCount, is_plain);
272 }
fetch_shipments
static void fetch_shipments(HeapTuple *tuple, TupleDesc *tupdesc, Column_info_t *info, Vroom_shipment_t *shipment, bool is_plain)
Definition: shipments_input.c:86
Column_info_t::colNumber
int colNumber
Definition: column_info_t.h:54
Vroom_shipment_t::priority
Priority priority
Number of skills.
Definition: vroom_shipment_t.h:75
Vroom_shipment_t::p_service
Duration p_service
Pickup setup time.
Definition: vroom_shipment_t.h:62
Vroom_shipment_t::d_setup
Duration d_setup
Delivery location index in matrix.
Definition: vroom_shipment_t.h:66
Vroom_shipment_t::amount_size
size_t amount_size
Quantities for shipment.
Definition: vroom_shipment_t.h:70
Column_info_t::strict
bool strict
Definition: column_info_t.h:56
pgr_SPI_prepare
SPIPlanPtr pgr_SPI_prepare(char *sql)
Definition: postgres_connection.c:88
spi_getText
char * spi_getText(HeapTuple *tuple, TupleDesc *tupdesc, Column_info_t info)
under development
Definition: get_check_data.c:832
get_MatrixIndex
MatrixIndex get_MatrixIndex(HeapTuple *tuple, TupleDesc *tupdesc, Column_info_t info, MatrixIndex opt_value)
@params [in] tuple @params [in] tupdesc @params [in] info about the column been fetched @params [in] ...
Definition: get_check_data.c:658
get_Priority
Priority get_Priority(HeapTuple *tuple, TupleDesc *tupdesc, Column_info_t info, Priority opt_value)
@params [in] tuple @params [in] tupdesc @params [in] info about the column been fetched @params [in] ...
Definition: get_check_data.c:747
db_get_shipments
static void db_get_shipments(char *shipments_sql, Vroom_shipment_t **shipments, size_t *total_shipments, Column_info_t *info, const int column_count, bool is_plain)
Definition: shipments_input.c:135
Vroom_shipment_t::d_data
char * d_data
Metadata information of pickup shipment.
Definition: vroom_shipment_t.h:78
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:905
Vroom_shipment_t::p_location_id
MatrixIndex p_location_id
Shipment identifier.
Definition: vroom_shipment_t.h:60
get_vroom_shipments
void get_vroom_shipments(char *sql, Vroom_shipment_t **rows, size_t *total_rows, bool is_plain)
Reads the VROOM shipments.
Definition: shipments_input.c:216
Column_info_t::name
char * name
Definition: column_info_t.h:57
pgr_SPI_cursor_open
Portal pgr_SPI_cursor_open(SPIPlanPtr SPIplan)
Definition: postgres_connection.c:98
Vroom_shipment_t::skills
Skill * skills
Number of quantities.
Definition: vroom_shipment_t.h:72
PGR_DBG
#define PGR_DBG(...)
Definition: debug_macro.h:34
INTERVAL
@ INTERVAL
Definition: column_info_t.h:48
Vroom_shipment_t
Vehicles's attributes.
Definition: vroom_shipment_t.h:56
JSONB
@ JSONB
Definition: column_info_t.h:43
get_PositiveTInterval
TInterval get_PositiveTInterval(HeapTuple *tuple, TupleDesc *tupdesc, Column_info_t info, TInterval opt_value)
@params [in] tuple @params [in] tupdesc @params [in] info about the column been fetched @params [in] ...
Definition: get_check_data.c:511
get_Duration
Duration get_Duration(HeapTuple *tuple, TupleDesc *tupdesc, Column_info_t info, Duration opt_value)
@params [in] tuple @params [in] tupdesc @params [in] info about the column been fetched @params [in] ...
Definition: get_check_data.c:681
Vroom_shipment_t::d_service
Duration d_service
Delivery setup time.
Definition: vroom_shipment_t.h:67
Vroom_shipment_t::p_setup
Duration p_setup
Pickup location index in matrix.
Definition: vroom_shipment_t.h:61
shipments_input.h
ANY_INTEGER_ARRAY
@ ANY_INTEGER_ARRAY
Definition: column_info_t.h:46
INTEGER_ARRAY
@ INTEGER_ARRAY
Definition: column_info_t.h:45
Vroom_shipment_t::p_data
char * p_data
Priority level of shipment.
Definition: vroom_shipment_t.h:77
Column_info_t::eType
expectType eType
Definition: column_info_t.h:58
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:40
Vroom_shipment_t
struct Vroom_shipment_t Vroom_shipment_t
Definition: typedefs.h:94
Vroom_shipment_t::amount
Amount * amount
Delivery service time.
Definition: vroom_shipment_t.h:69
Duration
uint32_t Duration
Definition: typedefs.h:81
Vroom_shipment_t::skills_size
size_t skills_size
Mandatory skills.
Definition: vroom_shipment_t.h:73
spi_getPositiveBigIntArr_allowEmpty
int64_t * spi_getPositiveBigIntArr_allowEmpty(HeapTuple *tuple, TupleDesc *tupdesc, Column_info_t info, size_t *the_size)
Function returns the values of specified columns in array.
Definition: get_check_data.c:377
Vroom_shipment_t::d_location_id
MatrixIndex d_location_id
Pickup service time.
Definition: vroom_shipment_t.h:65
spi_getPositiveIntArr_allowEmpty
uint32_t * spi_getPositiveIntArr_allowEmpty(HeapTuple *tuple, TupleDesc *tupdesc, Column_info_t info, size_t *the_size)
Function returns the values of specified columns in array.
Definition: get_check_data.c:393
Vroom_shipment_t::id
Idx id
Definition: vroom_shipment_t.h:57
get_Idx
Idx get_Idx(HeapTuple *tuple, TupleDesc *tupdesc, Column_info_t info, Idx opt_value)
@params [in] tuple @params [in] tupdesc @params [in] info about the column been fetched @params [in] ...
Definition: get_check_data.c:581
column_found
bool column_found(int colNumber)
Check whether the colNumber represent any specific column or NULL (SPI_ERROR_NOATTRIBUTE).
Definition: get_check_data.c:900
Column_info_t::type
uint64_t type
Definition: column_info_t.h:55
Column_info_t
Definition: column_info_t.h:52
INTEGER
@ INTEGER
Definition: column_info_t.h:39