vrpRouting  0.3
time_multipliers_input.c
Go to the documentation of this file.
1 /*PGR-GNU*****************************************************************
2 File: time_multipliers_input.c
3 
4 Copyright (c) 2021 pgRouting developers
6 
7 Developer:
8 Copyright (c) 2021 Copyright (c) 2021 Joseph Emile Honour Percival
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"
33 
34 #ifdef PROFILE
35 #include "c_common/time_msg.h"
36 #include "c_common/debug_macro.h"
37 #endif
38 
39 
40 static
41 void fetch_raw(
42  HeapTuple *tuple,
43  TupleDesc *tupdesc,
44  Column_info_t info[2],
45  Time_multipliers_t *row) {
46  row->start_time = get_TTimestamp_plain(tuple, tupdesc, info[0], 0);
47  row->multiplier = spi_getFloat8(tuple, tupdesc, info[1]);
48 }
49 
50 static
52  HeapTuple *tuple,
53  TupleDesc *tupdesc,
54  Column_info_t info[2],
55  Time_multipliers_t *row) {
56  row->start_time = get_TTimestamp(tuple, tupdesc, info[0], 0);
57  row->multiplier = spi_getFloat8(tuple, tupdesc, info[1]);
58 }
59 
65 static
67  char *sql,
68  Column_info_t *info,
69  const int kind,
70  Time_multipliers_t **rows,
71  size_t *total_rows) {
72 #ifdef PROFILE
73  clock_t start_t = clock();
74  PGR_DBG("%s", sql);
75 #endif
76  const int tuple_limit = 1000000;
77  size_t total_tuples = 0;
78  const int column_count = 2;
79 
80  void *SPIplan;
81  SPIplan = pgr_SPI_prepare(sql);
82 
83  Portal SPIportal;
84  SPIportal = pgr_SPI_cursor_open(SPIplan);
85 
86 
87  bool moredata = true;
88  (*total_rows) = total_tuples;
89 
90  while (moredata == true) {
91  SPI_cursor_fetch(SPIportal, true, tuple_limit);
92  if (total_tuples == 0)
93  pgr_fetch_column_info(info, column_count);
94 
95  size_t ntuples = SPI_processed;
96  total_tuples += ntuples;
97 
98  if (ntuples > 0) {
99  if ((*rows) == NULL)
100  (*rows) = (Time_multipliers_t *)palloc0(
101  total_tuples * sizeof(Time_multipliers_t));
102  else
103  (*rows) = (Time_multipliers_t *)repalloc(
104  (*rows), total_tuples * sizeof(Time_multipliers_t));
105 
106  if ((*rows) == NULL) {
107  elog(ERROR, "Out of memory");
108  }
109 
110  SPITupleTable *tuptable = SPI_tuptable;
111  TupleDesc tupdesc = SPI_tuptable->tupdesc;
112 
113  for (size_t t = 0; t < ntuples; t++) {
114  HeapTuple tuple = tuptable->vals[t];
115  switch (kind) {
116  case 0 : fetch_timestamps(&tuple, &tupdesc, info,
117  &(*rows)[total_tuples - ntuples + t]);
118  break;
119  case 1 : fetch_raw(&tuple, &tupdesc, info,
120  &(*rows)[total_tuples - ntuples + t]);
121  break;
122  }
123  }
124  SPI_freetuptable(tuptable);
125  } else {
126  moredata = false;
127  }
128  }
129 
130  SPI_cursor_close(SPIportal);
131 
132 
133  if (total_tuples == 0) {
134  (*total_rows) = 0;
135  return;
136  }
137 
138  (*total_rows) = total_tuples;
139 #ifdef PROFILE
140  time_msg("reading time dependant multipliers", start_t, clock());
141 #endif
142 }
143 
150  char *sql,
151  Time_multipliers_t **rows,
152  size_t *total_rows) {
153  Column_info_t info[2];
154 
155  int i;
156  for (i = 0; i < 2; ++i) {
157  info[i].colNumber = -1;
158  info[i].type = 0;
159  info[i].strict = true;
160  info[i].eType = ANY_NUMERICAL;
161  }
162  info[0].name = "start_time";
163  info[1].name = "multiplier";
164 
165  info[0].eType = TIMESTAMP;
166 
167  get_timeMultipliersGeneral(sql, info, 0, rows, total_rows);
168 }
169 
176  char *sql,
177  Time_multipliers_t **rows,
178  size_t *total_rows) {
179  Column_info_t info[2];
180 
181  int i;
182  for (i = 0; i < 2; ++i) {
183  info[i].colNumber = -1;
184  info[i].type = 0;
185  info[i].strict = true;
186  info[i].eType = ANY_NUMERICAL;
187  }
188  info[0].name = "start_value";
189  info[1].name = "multiplier";
190 
191  info[0].eType = ANY_INTEGER;
192 
193  get_timeMultipliersGeneral(sql, info, 1, rows, total_rows);
194 }
Column_info_t::colNumber
int colNumber
Definition: column_info_t.h:54
time_multipliers_t.h
fetch_timestamps
static void fetch_timestamps(HeapTuple *tuple, TupleDesc *tupdesc, Column_info_t info[2], Time_multipliers_t *row)
Definition: time_multipliers_input.c:51
time_msg.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
Time_multipliers_t
Time Dependant Multipliers.
Definition: time_multipliers_t.h:46
get_TTimestamp
TTimestamp get_TTimestamp(HeapTuple *tuple, TupleDesc *tupdesc, Column_info_t info, TTimestamp opt_value)
@params [in] tuple @params [in] tupdesc @params [in] info about the column been fetched @params [in] ...
Definition: get_check_data.c:457
get_timeMultipliers
void get_timeMultipliers(char *sql, Time_multipliers_t **rows, size_t *total_rows)
Get the time multipliers using interval.
Definition: time_multipliers_input.c:149
Time_multipliers_t
struct Time_multipliers_t Time_multipliers_t
Definition: time_multipliers_input.h:35
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
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
Time_multipliers_t::start_time
TTimestamp start_time
Time of day where the multiplier starts to be valid.
Definition: time_multipliers_t.h:48
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
ANY_INTEGER
@ ANY_INTEGER
Definition: column_info_t.h:40
time_multipliers_input.h
column_info_t.h
get_timeMultipliersGeneral
static void get_timeMultipliersGeneral(char *sql, Column_info_t *info, const int kind, Time_multipliers_t **rows, size_t *total_rows)
param [in] sql multipliers SQL param [in,out] rows catptured information param [in,...
Definition: time_multipliers_input.c:66
ANY_NUMERICAL
@ ANY_NUMERICAL
Definition: column_info_t.h:41
fetch_raw
static void fetch_raw(HeapTuple *tuple, TupleDesc *tupdesc, Column_info_t info[2], Time_multipliers_t *row)
Definition: time_multipliers_input.c:41
get_timeMultipliers_raw
void get_timeMultipliers_raw(char *sql, Time_multipliers_t **rows, size_t *total_rows)
Get the time multipliers using bigint.
Definition: time_multipliers_input.c:175
get_check_data.h
Column_info_t::type
uint64_t type
Definition: column_info_t.h:55
TIMESTAMP
@ TIMESTAMP
Definition: column_info_t.h:47
Column_info_t
Definition: column_info_t.h:52
get_TTimestamp_plain
TTimestamp get_TTimestamp_plain(HeapTuple *tuple, TupleDesc *tupdesc, Column_info_t info, TTimestamp opt_value)
@params [in] tuple @params [in] tupdesc @params [in] info about the column been fetched @params [in] ...
Definition: get_check_data.c:422
spi_getFloat8
double spi_getFloat8(HeapTuple *tuple, TupleDesc *tupdesc, Column_info_t info)
gets value of specified column in double type.
Definition: get_check_data.c:782
Time_multipliers_t::multiplier
Multiplier multiplier
multiplier at hour
Definition: time_multipliers_t.h:50