vrpRouting  0.3
jobs_input.c
Go to the documentation of this file.
1 /*PGR-GNU*****************************************************************
2 File: jobs_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, location_id
39  [, setup, service, delivery, pickup, skills, priority, data]
40 
41 
42 ==================== ========================= =========== ================================================
43 Column Type Default Description
44 ==================== ========================= =========== ================================================
45 **id** ``ANY-INTEGER`` Positive unique identifier of the job.
46 
47 **location_id** ``ANY-INTEGER`` Positive identifier of the job location.
48 
49 **setup** |interval| |interval0| Job setup duration.
50 
51 **service** |interval| |interval0| Job service duration.
52 
53 **delivery** ``ARRAY[ANY-INTEGER]`` Empty Array Array of non-negative integers describing
54  multidimensional quantities for delivery such
55  as number of items, weight, volume etc.
56 
57  - All jobs must have the same value of
58  :code:`array_length(delivery, 1)`
59 
60 **pickup** ``ARRAY[ANY-INTEGER]`` Empty Array Array of non-negative integers describing
61  multidimensional quantities for pickup such as
62  number of items, weight, volume etc.
63 
64  - All jobs must have the same value of
65  :code:`array_length(pickup, 1)`
66 
67 **skills** ``ARRAY[INTEGER]`` Empty Array Array of non-negative integers defining
68  mandatory skills.
69 
70 **priority** ``INTEGER`` 0 Priority level of the job
71 
72  - Ranges from ``[0, 100]``
73 
74 **data** ``JSONB`` '{}'::JSONB Any metadata information of the job.
75 ==================== ========================= =========== ================================================
76 
77 Where:
78 
79 :ANY-INTEGER: SMALLINT, INTEGER, BIGINT
80 
81 .. vrp_vroom end
82 */
83 
84 static
86  HeapTuple *tuple,
87  TupleDesc *tupdesc,
88  Column_info_t *info,
89  Vroom_job_t *job,
90  bool is_plain) {
91  job->id = get_Idx(tuple, tupdesc, info[0], 0);
92  job->location_id = get_MatrixIndex(tuple, tupdesc, info[1], 0);
93 
94  if (is_plain) {
95  job->setup = get_Duration(tuple, tupdesc, info[2], 0);
96  job->service = get_Duration(tuple, tupdesc, info[3], 0);
97  } else {
98  job->setup = (Duration)get_PositiveTInterval(tuple, tupdesc, info[2], 0);
99  job->service = (Duration)get_PositiveTInterval(tuple, tupdesc, info[3], 0);
100  }
101 
102  /*
103  * The deliveries
104  */
105  job->delivery_size = 0;
106  job->delivery = column_found(info[4].colNumber) ?
107  spi_getPositiveBigIntArr_allowEmpty(tuple, tupdesc, info[4], &job->delivery_size)
108  : NULL;
109 
110  /*
111  * The pickups
112  */
113  job->pickup_size = 0;
114  job->pickup = column_found(info[5].colNumber) ?
115  spi_getPositiveBigIntArr_allowEmpty(tuple, tupdesc, info[5], &job->pickup_size)
116  : NULL;
117 
118  job->skills_size = 0;
119  job->skills = column_found(info[6].colNumber) ?
120  spi_getPositiveIntArr_allowEmpty(tuple, tupdesc, info[6], &job->skills_size)
121  : NULL;
122 
123  job->priority = get_Priority(tuple, tupdesc, info[7], 0);
124 
125  job->data = column_found(info[8].colNumber)
126  ? spi_getText(tuple, tupdesc, info[8])
127  : strdup("{}");
128 }
129 
130 
131 static
133  char *jobs_sql,
134  Vroom_job_t **jobs,
135  size_t *total_jobs,
136 
137  Column_info_t *info,
138  const int column_count,
139  bool is_plain) {
140 #ifdef PROFILE
141  clock_t start_t = clock();
142  PGR_DBG("%s", jobs_sql);
143 #endif
144 
145  const int tuple_limit = 1000000;
146 
147  size_t total_tuples;
148 
149  void *SPIplan;
150  SPIplan = pgr_SPI_prepare(jobs_sql);
151  Portal SPIportal;
152  SPIportal = pgr_SPI_cursor_open(SPIplan);
153 
154  bool moredata = true;
155  (*total_jobs) = total_tuples = 0;
156 
157  /* on the first tuple get the column numbers */
158 
159  while (moredata == true) {
160  SPI_cursor_fetch(SPIportal, true, tuple_limit);
161  if (total_tuples == 0) {
162  pgr_fetch_column_info(info, column_count);
163  }
164  size_t ntuples = SPI_processed;
165  total_tuples += ntuples;
166  if (ntuples > 0) {
167  if ((*jobs) == NULL)
168  (*jobs) = (Vroom_job_t *)palloc0(
169  total_tuples * sizeof(Vroom_job_t));
170  else
171  (*jobs) = (Vroom_job_t *)repalloc(
172  (*jobs),
173  total_tuples * sizeof(Vroom_job_t));
174 
175  if ((*jobs) == NULL) {
176  elog(ERROR, "Out of memory");
177  }
178 
179  size_t t;
180  SPITupleTable *tuptable = SPI_tuptable;
181  TupleDesc tupdesc = SPI_tuptable->tupdesc;
182  for (t = 0; t < ntuples; t++) {
183  HeapTuple tuple = tuptable->vals[t];
184  fetch_jobs(&tuple, &tupdesc, info,
185  &(*jobs)[total_tuples - ntuples + t], is_plain);
186  }
187  SPI_freetuptable(tuptable);
188  } else {
189  moredata = false;
190  }
191  }
192 
193  SPI_cursor_close(SPIportal);
194 
195  if (total_tuples == 0) {
196  (*total_jobs) = 0;
197  return;
198  }
199 
200  (*total_jobs) = total_tuples;
201 #ifdef PROFILE
202  time_msg("reading jobs", start_t, clock());
203 #endif
204 }
205 
206 
212 void
214  char *sql,
215  Vroom_job_t **rows,
216  size_t *total_rows,
217  bool is_plain) {
218  int kColumnCount = 9;
219  Column_info_t info[kColumnCount];
220 
221  for (int i = 0; i < kColumnCount; ++i) {
222  info[i].colNumber = -1;
223  info[i].type = 0;
224  info[i].strict = false;
225  info[i].eType = ANY_INTEGER;
226  }
227 
228  info[0].name = "id";
229  info[1].name = "location_id";
230  info[2].name = "setup";
231  info[3].name = "service";
232  info[4].name = "delivery";
233  info[5].name = "pickup";
234  info[6].name = "skills";
235  info[7].name = "priority";
236  info[8].name = "data";
237 
238  info[2].eType = INTEGER; // setup
239  info[3].eType = INTEGER; // service
240  info[4].eType = ANY_INTEGER_ARRAY; // delivery
241  info[5].eType = ANY_INTEGER_ARRAY; // pickup
242  info[6].eType = INTEGER_ARRAY; // skills
243  info[7].eType = INTEGER; // priority
244  info[8].eType = JSONB; // data
245 
246  if (!is_plain) {
247  info[2].eType = INTERVAL; // setup
248  info[3].eType = INTERVAL; // service
249  }
250 
251  /* Only id and location_id are mandatory */
252  info[0].strict = true;
253  info[1].strict = true;
254 
255  db_get_jobs(sql, rows, total_rows, info, kColumnCount, is_plain);
256 }
Vroom_job_t
struct Vroom_job_t Vroom_job_t
Definition: typedefs.h:93
Column_info_t::colNumber
int colNumber
Definition: column_info_t.h:54
Vroom_job_t::pickup_size
size_t pickup_size
Quantities for pickup.
Definition: vroom_job_t.h:65
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
Vroom_job_t::delivery
Amount * delivery
Job service duration.
Definition: vroom_job_t.h:61
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
Vroom_job_t::priority
Priority priority
Number of mandatory skills.
Definition: vroom_job_t.h:70
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
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
PGR_DBG
#define PGR_DBG(...)
Definition: debug_macro.h:34
INTERVAL
@ INTERVAL
Definition: column_info_t.h:48
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_job_t::service
Duration service
Job setup duration.
Definition: vroom_job_t.h:59
Vroom_job_t::skills
Skill * skills
Number of pickup quantities.
Definition: vroom_job_t.h:67
Vroom_job_t::location_id
MatrixIndex location_id
The job's identifier.
Definition: vroom_job_t.h:56
ANY_INTEGER_ARRAY
@ ANY_INTEGER_ARRAY
Definition: column_info_t.h:46
INTEGER_ARRAY
@ INTEGER_ARRAY
Definition: column_info_t.h:45
Vroom_job_t::pickup
Amount * pickup
Number of delivery quantities.
Definition: vroom_job_t.h:64
Vroom_job_t::setup
Duration setup
Location index of job in matrix.
Definition: vroom_job_t.h:58
Column_info_t::eType
expectType eType
Definition: column_info_t.h:58
Vroom_job_t
Job's attributes.
Definition: vroom_job_t.h:54
Vroom_job_t::data
char * data
Priority level of job.
Definition: vroom_job_t.h:72
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
get_vroom_jobs
void get_vroom_jobs(char *sql, Vroom_job_t **rows, size_t *total_rows, bool is_plain)
Reads the VROOM jobs.
Definition: jobs_input.c:213
Duration
uint32_t Duration
Definition: typedefs.h:81
jobs_input.h
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
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
fetch_jobs
static void fetch_jobs(HeapTuple *tuple, TupleDesc *tupdesc, Column_info_t *info, Vroom_job_t *job, bool is_plain)
Definition: jobs_input.c:85
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
Vroom_job_t::delivery_size
size_t delivery_size
Quantities for delivery.
Definition: vroom_job_t.h:62
INTEGER
@ INTEGER
Definition: column_info_t.h:39
db_get_jobs
static void db_get_jobs(char *jobs_sql, Vroom_job_t **jobs, size_t *total_jobs, Column_info_t *info, const int column_count, bool is_plain)
Definition: jobs_input.c:132
Vroom_job_t::skills_size
size_t skills_size
Mandatory skills.
Definition: vroom_job_t.h:68
Vroom_job_t::id
Idx id
Definition: vroom_job_t.h:55