vrpRouting  0.3
matrix_input.c File Reference
Include dependency graph for matrix_input.c:

Go to the source code of this file.

Functions

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)
 
static void fetch_matrix (HeapTuple *tuple, TupleDesc *tupdesc, Column_info_t *info, Vroom_matrix_t *matrix, bool is_plain)
 
void get_vroom_matrix (char *sql, Vroom_matrix_t **rows, size_t *total_rows, bool is_plain)
 Reads the VROOM matrix. More...
 

Function Documentation

◆ 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 
)
static

Definition at line 77 of file matrix_input.c.

84  {
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 }

References fetch_matrix(), PGR_DBG, pgr_fetch_column_info(), pgr_SPI_cursor_open(), pgr_SPI_prepare(), and time_msg().

Referenced by get_vroom_matrix().

◆ fetch_matrix()

static void fetch_matrix ( HeapTuple *  tuple,
TupleDesc *  tupdesc,
Column_info_t info,
Vroom_matrix_t matrix,
bool  is_plain 
)
static

Definition at line 56 of file matrix_input.c.

61  {
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 }

References Vroom_matrix_t::cost, Vroom_matrix_t::duration, Vroom_matrix_t::end_id, get_Cost(), get_Duration(), get_MatrixIndex(), get_PositiveTInterval(), and Vroom_matrix_t::start_id.

Referenced by db_get_matrix().

◆ get_vroom_matrix()

void get_vroom_matrix ( char *  sql,
Vroom_matrix_t **  rows,
size_t *  total_rows,
bool  is_plain 
)

Reads the VROOM matrix.

Parameters
[in]sqlSQL query that has the following columns: start_id, end_id, duration, cost
[in]is_plainWhether the plain or timestamp function is used
[out]rowsC Container that holds all the matrix rows
[out]total_rowsTotal rows recieved

cost is not mandatory

Definition at line 159 of file matrix_input.c.

163  {
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 }

References ANY_INTEGER, Column_info_t::colNumber, db_get_matrix(), Column_info_t::eType, INTEGER, INTERVAL, Column_info_t::name, Column_info_t::strict, and Column_info_t::type.

Referenced by process().

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
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
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