vrpRouting  0.3
matrixRows_input.c
Go to the documentation of this file.
1 /*PGR-GNU*****************************************************************
2 File: matrixRows_input.c
3 
4 Copyright (c) 2015 pgRouting developers
6 
7 Developer:
8 Copyright (c) 2015 Celia Virginia Vergara Castillo
9 
10 ------
11 
12 This program is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 2 of the License, or
15 (at your option) any later version.
16 
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
21 
22 You should have received a copy of the GNU General Public License
23 along with this program; if not, write to the Free Software
24 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 
26  ********************************************************************PGR-GNU*/
27 
29 
30 #include "c_types/column_info_t.h"
31 #include "c_types/matrix_cell_t.h"
32 
34 
35 #ifdef PROFILE
36 #include "c_common/time_msg.h"
37 #include "c_common/debug_macro.h"
38 #endif
39 
40 /*
41 .. pgr_pickDeliver start
42 
43 A ``SELECT`` statement that returns the following columns:
44 
45 ::
46 
47  start_vid, end_vid, agg_cost
48 
49 ============= =============== ================================================
50 Column Type Description
51 ============= =============== ================================================
52 **start_vid** |ANY-INTEGER| Identifier of a node.
53 **end_vid** |ANY-NUMERICAL| Identifier of a node
54 **agg_cost** |ANY-NUMERICAL| Time to travel from ``start_vid`` to ``end_vid``
55 ============= =============== ================================================
56 
57 .. pgr_pickDeliver end
58 
59 */
60 
61 static
63  HeapTuple *tuple,
64  TupleDesc *tupdesc,
65  Column_info_t info[3],
66  Matrix_cell_t *row) {
67  row->from_vid = get_Id(tuple, tupdesc, info[0], -1);
68  row->to_vid = get_Id(tuple, tupdesc, info[1], -1);
69  row->cost = get_PositiveTInterval_plain(tuple, tupdesc, info[2], 0);
70 }
71 
72 static
74  HeapTuple *tuple,
75  TupleDesc *tupdesc,
76  Column_info_t info[3],
77  Matrix_cell_t *row) {
78  row->from_vid = get_Id(tuple, tupdesc, info[0], -1);
79  row->to_vid = get_Id(tuple, tupdesc, info[1], -1);
80  row->cost = get_PositiveTInterval(tuple, tupdesc, info[2], 0);
81 }
82 
88 static
89 void
91  char *sql,
92  Column_info_t *info,
93  const int kind,
94  Matrix_cell_t **rows,
95  size_t *total_rows) {
96 #ifdef PROFILE
97  clock_t start_t = clock();
98  PGR_DBG("%s", sql);
99 #endif
100 
101  const int tuple_limit = 1000000;
102  size_t total_tuples = 0;
103  const int column_count = 3;
104 
105  void *SPIplan;
106  SPIplan = pgr_SPI_prepare(sql);
107 
108  Portal SPIportal;
109  SPIportal = pgr_SPI_cursor_open(SPIplan);
110 
111 
112  bool moredata = true;
113  (*total_rows) = total_tuples;
114 
115  while (moredata == true) {
116  SPI_cursor_fetch(SPIportal, true, tuple_limit);
117  if (total_tuples == 0)
118  pgr_fetch_column_info(info, column_count);
119 
120  size_t ntuples = SPI_processed;
121  total_tuples += ntuples;
122 
123  if (ntuples > 0) {
124  if ((*rows) == NULL)
125  (*rows) = (Matrix_cell_t *)palloc0(
126  total_tuples * sizeof(Matrix_cell_t));
127  else
128  (*rows) = (Matrix_cell_t *)repalloc(
129  (*rows), total_tuples * sizeof(Matrix_cell_t));
130 
131  if ((*rows) == NULL) {
132  elog(ERROR, "Out of memory");
133  }
134 
135  SPITupleTable *tuptable = SPI_tuptable;
136  TupleDesc tupdesc = SPI_tuptable->tupdesc;
137 
138  size_t t;
139  for (t = 0; t < ntuples; t++) {
140  HeapTuple tuple = tuptable->vals[t];
141  switch (kind) {
142  case 0 : fetch_timestamps(&tuple, &tupdesc, info,
143  &(*rows)[total_tuples - ntuples + t]);
144  break;
145  case 1 : fetch_plain(&tuple, &tupdesc, info,
146  &(*rows)[total_tuples - ntuples + t]);
147  break;
148  }
149  }
150  SPI_freetuptable(tuptable);
151  } else {
152  moredata = false;
153  }
154  }
155 
156  SPI_cursor_close(SPIportal);
157 
158 
159  if (total_tuples == 0) {
160  (*total_rows) = 0;
161  return;
162  }
163 
164  (*total_rows) = total_tuples;
165 #ifdef PROFILE
166  time_msg(" reading time matrix", start_t, clock());
167 #endif
168 }
169 
175 void
177  char *sql,
178  Matrix_cell_t **rows,
179  size_t *total_rows) {
180  Column_info_t info[3];
181 
182  int i;
183  for (i = 0; i < 3; ++i) {
184  info[i].colNumber = -1;
185  info[i].type = 0;
186  info[i].strict = true;
187  info[i].eType = ANY_INTEGER;
188  }
189  info[0].name = "start_vid";
190  info[1].name = "end_vid";
191  info[2].name = "travel_time";
192 
193  info[2].eType = INTERVAL;
194  get_matrixRows_general(sql, info, 0, rows, total_rows);
195 }
196 
202 void
204  char *sql,
205  Matrix_cell_t **rows,
206  size_t *total_rows) {
207  Column_info_t info[3];
208 
209  int i;
210  for (i = 0; i < 3; ++i) {
211  info[i].colNumber = -1;
212  info[i].type = 0;
213  info[i].strict = true;
214  info[i].eType = ANY_INTEGER;
215  }
216  info[0].name = "start_vid";
217  info[1].name = "end_vid";
218  info[2].name = "agg_cost";
219 
220  info[2].eType = ANY_NUMERICAL;
221  get_matrixRows_general(sql, info, 1, rows, total_rows);
222 }
Column_info_t::colNumber
int colNumber
Definition: column_info_t.h:54
time_msg.h
Column_info_t::strict
bool strict
Definition: column_info_t.h:56
get_matrixRows
void get_matrixRows(char *sql, Matrix_cell_t **rows, size_t *total_rows)
Get the travel time matrix.
Definition: matrixRows_input.c:176
pgr_SPI_prepare
SPIPlanPtr pgr_SPI_prepare(char *sql)
Definition: postgres_connection.c:88
get_matrixRows_plain
void get_matrixRows_plain(char *sql, Matrix_cell_t **rows, size_t *total_rows)
Get the travel time matrix with numerical types.
Definition: matrixRows_input.c:203
matrixRows_input.h
get_Id
Id get_Id(HeapTuple *tuple, TupleDesc *tupdesc, Column_info_t info, Id opt_value)
@params [in] tuple @params [in] tupdesc @params [in] info about the column been fetched @params [in] ...
Definition: get_check_data.c:562
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
debug_macro.h
Column_info_t::name
char * name
Definition: column_info_t.h:57
fetch_timestamps
static void fetch_timestamps(HeapTuple *tuple, TupleDesc *tupdesc, Column_info_t info[3], Matrix_cell_t *row)
Definition: matrixRows_input.c:73
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
fetch_plain
static void fetch_plain(HeapTuple *tuple, TupleDesc *tupdesc, Column_info_t info[3], Matrix_cell_t *row)
Definition: matrixRows_input.c:62
Matrix_cell_t
struct Matrix_cell_t Matrix_cell_t
Definition: matrixRows_input.h:31
Column_info_t::eType
expectType eType
Definition: column_info_t.h:58
Matrix_cell_t
traveling costs
Definition: matrix_cell_t.h:41
time_msg
void time_msg(char *msg, clock_t start_t, clock_t end_t)
Definition: time_msg.c:32
Matrix_cell_t::cost
TInterval cost
arrival node's identifier
Definition: matrix_cell_t.h:44
ANY_INTEGER
@ ANY_INTEGER
Definition: column_info_t.h:40
get_matrixRows_general
static void get_matrixRows_general(char *sql, Column_info_t *info, const int kind, Matrix_cell_t **rows, size_t *total_rows)
bigint start_vid, bigint end_vid, float agg_cost,
Definition: matrixRows_input.c:90
get_PositiveTInterval_plain
TInterval get_PositiveTInterval_plain(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:546
column_info_t.h
Matrix_cell_t::from_vid
Id from_vid
Definition: matrix_cell_t.h:42
ANY_NUMERICAL
@ ANY_NUMERICAL
Definition: column_info_t.h:41
get_check_data.h
Column_info_t::type
uint64_t type
Definition: column_info_t.h:55
Column_info_t
Definition: column_info_t.h:52
matrix_cell_t.h
Matrix_cell_t::to_vid
Id to_vid
departure node's identifier
Definition: matrix_cell_t.h:43