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

Go to the source code of this file.

Functions

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)
 
static void fetch_breaks (HeapTuple *tuple, TupleDesc *tupdesc, Column_info_t *info, Vroom_break_t *vroom_break, bool is_plain)
 
void get_vroom_breaks (char *sql, Vroom_break_t **rows, size_t *total_rows, bool is_plain)
 Reads the VROOM breaks. More...
 

Function Documentation

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

Definition at line 78 of file breaks_input.c.

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

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

Referenced by get_vroom_breaks().

◆ fetch_breaks()

static void fetch_breaks ( HeapTuple *  tuple,
TupleDesc *  tupdesc,
Column_info_t info,
Vroom_break_t vroom_break,
bool  is_plain 
)
static

Definition at line 57 of file breaks_input.c.

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

References column_found(), Vroom_break_t::data, get_Duration(), get_Idx(), get_PositiveTInterval(), Vroom_break_t::id, Vroom_break_t::service, spi_getText(), and Vroom_break_t::vehicle_id.

Referenced by db_get_breaks().

◆ get_vroom_breaks()

void get_vroom_breaks ( char *  sql,
Vroom_break_t **  rows,
size_t *  total_rows,
bool  is_plain 
)

Reads the VROOM breaks.

Parameters
[in]sqlSQL query to execute
[out]rowsC Container that holds the data
[out]total_rowsTotal rows recieved

Definition at line 160 of file breaks_input.c.

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

References ANY_INTEGER, Column_info_t::colNumber, db_get_breaks(), Column_info_t::eType, INTEGER, INTERVAL, JSONB, 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
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
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