vrpRouting  0.3
breaks_input.c
Go to the documentation of this file.
1 /*PGR-GNU*****************************************************************
2 File: breaks_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, vehicle_id [, service, data]
39 
40 ==================== ========================= =========== ================================================
41 Column Type Default Description
42 ==================== ========================= =========== ================================================
43 **id** ``ANY-INTEGER`` Positive unique identifier of the break.
44  (unique for the same vehicle).
45 
46 **vehicle_id** ``ANY-INTEGER`` Positive unique identifier of the vehicle.
47 
48 **service** |interval| |interval0| The break duration.
49 
50 **data** ``JSONB`` '{}'::JSONB Any metadata information of the break.
51 ==================== ========================= =========== ================================================
52 
53 .. vrp_vroom end
54 */
55 
56 static
58  HeapTuple *tuple,
59  TupleDesc *tupdesc,
60  Column_info_t *info,
61  Vroom_break_t *vroom_break,
62  bool is_plain) {
63  vroom_break->id = get_Idx(tuple, tupdesc, info[0], 0);
64  vroom_break->vehicle_id = get_Idx(tuple, tupdesc, info[1], 0);
65  if (is_plain) {
66  vroom_break->service = get_Duration(tuple, tupdesc, info[2], 0);
67  } else {
68  vroom_break->service =
69  (Duration)get_PositiveTInterval(tuple, tupdesc, info[2], 0);
70  }
71  vroom_break->data = column_found(info[3].colNumber)
72  ? spi_getText(tuple, tupdesc, info[3])
73  : strdup("{}");
74 }
75 
76 
77 static
79  char *breaks_sql,
80  Vroom_break_t **breaks,
81  size_t *total_breaks,
82 
83  Column_info_t *info,
84  const int column_count,
85  bool is_plain) {
86 #ifdef PROFILE
87  clock_t start_t = clock();
88  PGR_DBG("%s", breaks_sql);
89 #endif
90 
91  const int tuple_limit = 1000000;
92 
93  size_t total_tuples;
94 
95  void *SPIplan;
96  SPIplan = pgr_SPI_prepare(breaks_sql);
97  Portal SPIportal;
98  SPIportal = pgr_SPI_cursor_open(SPIplan);
99 
100  bool moredata = true;
101  (*total_breaks) = total_tuples = 0;
102 
103  /* on the first tuple get the column numbers */
104 
105  while (moredata == true) {
106  SPI_cursor_fetch(SPIportal, true, tuple_limit);
107  if (total_tuples == 0) {
108  pgr_fetch_column_info(info, column_count);
109  }
110  size_t ntuples = SPI_processed;
111  total_tuples += ntuples;
112  if (ntuples > 0) {
113  if ((*breaks) == NULL)
114  (*breaks) = (Vroom_break_t *)palloc0(
115  total_tuples * sizeof(Vroom_break_t));
116  else
117  (*breaks) = (Vroom_break_t *)repalloc(
118  (*breaks),
119  total_tuples * sizeof(Vroom_break_t));
120 
121  if ((*breaks) == NULL) {
122  elog(ERROR, "Out of memory");
123  }
124 
125  size_t t;
126  SPITupleTable *tuptable = SPI_tuptable;
127  TupleDesc tupdesc = SPI_tuptable->tupdesc;
128  for (t = 0; t < ntuples; t++) {
129  HeapTuple tuple = tuptable->vals[t];
130  fetch_breaks(&tuple, &tupdesc, info,
131  &(*breaks)[total_tuples - ntuples + t], is_plain);
132  }
133  SPI_freetuptable(tuptable);
134  } else {
135  moredata = false;
136  }
137  }
138 
139  SPI_cursor_close(SPIportal);
140 
141  if (total_tuples == 0) {
142  (*total_breaks) = 0;
143  return;
144  }
145 
146  (*total_breaks) = total_tuples;
147 #ifdef PROFILE
148  time_msg("reading breaks", start_t, clock());
149 #endif
150 }
151 
152 
153 
159 void
161  char *sql,
162  Vroom_break_t **rows,
163  size_t *total_rows,
164  bool is_plain) {
165  int kColumnCount = 4;
166  Column_info_t info[kColumnCount];
167 
168  for (int i = 0; i < kColumnCount; ++i) {
169  info[i].colNumber = -1;
170  info[i].type = 0;
171  info[i].strict = true;
172  info[i].eType = ANY_INTEGER;
173  }
174 
175  info[0].name = "id";
176  info[1].name = "vehicle_id";
177  info[2].name = "service";
178  info[3].name = "data";
179 
180  info[2].eType = INTEGER; // service
181  info[3].eType = JSONB; // data
182 
183  if (!is_plain) {
184  info[2].eType = INTERVAL; // service
185  }
186 
187  /* service and data are not mandatory */
188  info[2].strict = false;
189  info[3].strict = false;
190 
191  db_get_breaks(sql, rows, total_rows, info, kColumnCount, is_plain);
192 }
Column_info_t::colNumber
int colNumber
Definition: column_info_t.h:54
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
Vroom_break_t::id
Idx id
Definition: vroom_break_t.h:47
get_vroom_breaks
void get_vroom_breaks(char *sql, Vroom_break_t **rows, size_t *total_rows, bool is_plain)
Reads the VROOM breaks.
Definition: breaks_input.c:160
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
Vroom_break_t::service
Duration service
Identifier of vehicle.
Definition: vroom_break_t.h:49
pgr_SPI_cursor_open
Portal pgr_SPI_cursor_open(SPIPlanPtr SPIplan)
Definition: postgres_connection.c:98
Vroom_break_t
struct Vroom_break_t Vroom_break_t
Definition: typedefs.h:97
fetch_breaks
static void fetch_breaks(HeapTuple *tuple, TupleDesc *tupdesc, Column_info_t *info, Vroom_break_t *vroom_break, bool is_plain)
Definition: breaks_input.c:57
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
Vroom_break_t::data
char * data
Duration of break.
Definition: vroom_break_t.h:50
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_break_t
Vehicle's break attributes.
Definition: vroom_break_t.h:46
db_get_breaks
static void db_get_breaks(char *breaks_sql, Vroom_break_t **breaks, size_t *total_breaks, Column_info_t *info, const int column_count, bool is_plain)
Definition: breaks_input.c:78
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
Vroom_break_t::vehicle_id
Idx vehicle_id
Identifier of break.
Definition: vroom_break_t.h:48
ANY_INTEGER
@ ANY_INTEGER
Definition: column_info_t.h:40
Duration
uint32_t Duration
Definition: typedefs.h:81
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
breaks_input.h