vrpRouting  0.3
matrix_input.c
Go to the documentation of this file.
1 /*PGR-GNU*****************************************************************
2 File: matrix_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  start_id, end_id, duration [, cost]
39 
40 =============== ================= ============== ==================================================
41 Column Type Default Description
42 =============== ================= ============== ==================================================
43 **start_id** ``ANY-INTEGER`` Identifier of the start node.
44 
45 **end_id** ``ANY-INTEGER`` Identifier of the end node.
46 
47 **duration** |interval| Time to travel from ``start_id`` to ``end_id``
48 
49 **cost** ``INTEGER`` *duration* Cost to travel from ``start_id`` to ``end_id``
50 =============== ================= ============== ==================================================
51 
52 .. vrp_vroom end
53 */
54 
55 static
57  HeapTuple *tuple,
58  TupleDesc *tupdesc,
59  Column_info_t *info,
60  Vroom_matrix_t *matrix,
61  bool is_plain) {
62  matrix->start_id = get_MatrixIndex(tuple, tupdesc, info[0], -1);
63  matrix->end_id = get_MatrixIndex(tuple, tupdesc, info[1], -1);
64 
65  if (is_plain) {
66  matrix->duration = get_Duration(tuple, tupdesc, info[2], 0);
67  } else {
68  matrix->duration = (Duration)get_PositiveTInterval(tuple, tupdesc, info[2], 0);
69  }
70 
71  // If unspecified, cost is same as the duration
72  matrix->cost = get_Cost(tuple, tupdesc, info[3], matrix->duration);
73 }
74 
75 
76 static
78  char *matrix_sql,
79  Vroom_matrix_t **matrix,
80  size_t *total_matrix_rows,
81 
82  Column_info_t *info,
83  const int column_count,
84  bool is_plain) {
85 #ifdef PROFILE
86  clock_t start_t = clock();
87  PGR_DBG("%s", matrix_sql);
88 #endif
89 
90  const int tuple_limit = 1000000;
91 
92  size_t total_tuples;
93 
94  void *SPIplan;
95  SPIplan = pgr_SPI_prepare(matrix_sql);
96  Portal SPIportal;
97  SPIportal = pgr_SPI_cursor_open(SPIplan);
98 
99  bool moredata = true;
100  (*total_matrix_rows) = total_tuples = 0;
101 
102  /* on the first tuple get the column numbers */
103 
104  while (moredata == true) {
105  SPI_cursor_fetch(SPIportal, true, tuple_limit);
106  if (total_tuples == 0) {
107  pgr_fetch_column_info(info, column_count);
108  }
109  size_t ntuples = SPI_processed;
110  total_tuples += ntuples;
111  if (ntuples > 0) {
112  if ((*matrix) == NULL)
113  (*matrix) = (Vroom_matrix_t *)palloc0(
114  total_tuples * sizeof(Vroom_matrix_t));
115  else
116  (*matrix) = (Vroom_matrix_t *)repalloc(
117  (*matrix),
118  total_tuples * sizeof(Vroom_matrix_t));
119 
120  if ((*matrix) == NULL) {
121  elog(ERROR, "Out of memory");
122  }
123 
124  size_t t;
125  SPITupleTable *tuptable = SPI_tuptable;
126  TupleDesc tupdesc = SPI_tuptable->tupdesc;
127  for (t = 0; t < ntuples; t++) {
128  HeapTuple tuple = tuptable->vals[t];
129  fetch_matrix(&tuple, &tupdesc, info,
130  &(*matrix)[total_tuples - ntuples + t], is_plain);
131  }
132  SPI_freetuptable(tuptable);
133  } else {
134  moredata = false;
135  }
136  }
137 
138  SPI_cursor_close(SPIportal);
139 
140  if (total_tuples == 0) {
141  (*total_matrix_rows) = 0;
142  return;
143  }
144 
145  (*total_matrix_rows) = total_tuples;
146 #ifdef PROFILE
147  time_msg("reading matrix", start_t, clock());
148 #endif
149 }
150 
151 
158 void
160  char *sql,
161  Vroom_matrix_t **rows,
162  size_t *total_rows,
163  bool is_plain) {
164  int kColumnCount = 4;
165  Column_info_t info[kColumnCount];
166 
167  for (int i = 0; i < kColumnCount; ++i) {
168  info[i].colNumber = -1;
169  info[i].type = 0;
170  info[i].strict = true;
171  info[i].eType = ANY_INTEGER;
172  }
173  info[0].name = "start_id";
174  info[1].name = "end_id";
175  info[2].name = "duration";
176  info[3].name = "cost";
177 
178  info[2].eType = INTEGER; // duration
179  info[3].eType = INTEGER; // cost
180 
181  if (!is_plain) {
182  info[2].eType = INTERVAL; // duration
183  }
184 
188  info[3].strict = false;
189 
190  db_get_matrix(sql, rows, total_rows, info, kColumnCount, is_plain);
191 }
Column_info_t::colNumber
int colNumber
Definition: column_info_t.h:54
matrix_input.h
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
db_get_matrix
static void db_get_matrix(char *matrix_sql, Vroom_matrix_t **matrix, size_t *total_matrix_rows, Column_info_t *info, const int column_count, bool is_plain)
Definition: matrix_input.c:77
Vroom_matrix_t::start_id
MatrixIndex start_id
Definition: vroom_matrix_t.h:47
Vroom_matrix_t
struct Vroom_matrix_t Vroom_matrix_t
Definition: typedefs.h:96
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_vroom_matrix
void get_vroom_matrix(char *sql, Vroom_matrix_t **rows, size_t *total_rows, bool is_plain)
Reads the VROOM matrix.
Definition: matrix_input.c:159
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_matrix_t::end_id
MatrixIndex end_id
Start node identifier.
Definition: vroom_matrix_t.h:48
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
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_matrix_t
Matrix's attributes.
Definition: vroom_matrix_t.h:46
Column_info_t::eType
expectType eType
Definition: column_info_t.h:58
Vroom_matrix_t::cost
TravelCost cost
Duration to travel from start to end.
Definition: vroom_matrix_t.h:51
fetch_matrix
static void fetch_matrix(HeapTuple *tuple, TupleDesc *tupdesc, Column_info_t *info, Vroom_matrix_t *matrix, bool is_plain)
Definition: matrix_input.c:56
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
Duration
uint32_t Duration
Definition: typedefs.h:81
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
Vroom_matrix_t::duration
Duration duration
End node identifier.
Definition: vroom_matrix_t.h:50
get_Cost
TravelCost get_Cost(HeapTuple *tuple, TupleDesc *tupdesc, Column_info_t info, TravelCost opt_value)
@params [in] tuple @params [in] tupdesc @params [in] info about the column been fetched @params [in] ...
Definition: get_check_data.c:704