PGROUTING  3.2
restrictions_input.c
Go to the documentation of this file.
1 /*PGR-GNU*****************************************************************
2 File: restrictions_input.c
3 
4 Copyright (c) 2015 Celia Virginia Vergara Castillo
6 
7 ------
8 
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
13 
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18 
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 
23  ********************************************************************PGR-GNU*/
24 
26 
27 #include "c_types/column_info_t.h"
28 
29 #include "c_common/debug_macro.h"
30 #include "c_common/time_msg.h"
32 
33 
34 static
36  HeapTuple *tuple,
37  TupleDesc *tupdesc,
38  Column_info_t info[3],
39  Restriction_t *restriction) {
40  /*
41  * reading the restriction id
42  */
43  restriction->id = pgr_SPI_getBigInt(tuple, tupdesc, info[0]);
44 
45  /*
46  * reading the cost
47  */
48  if (column_found(info[1].colNumber)) {
49  restriction->cost = pgr_SPI_getFloat8(tuple, tupdesc, info[1]);
50  } else {
51  restriction->cost = -1;
52  }
53 
54  restriction->via = NULL;
55  restriction->via_size = 0;
56 
57  restriction->via = pgr_SPI_getBigIntArr(
58  tuple, tupdesc, info[2], &restriction->via_size);
59 }
60 
61 
62 void
64  char *restrictions_sql,
65  Restriction_t **restrictions,
66  size_t *total_restrictions) {
67  const int tuple_limit = 1000000;
68  clock_t start_t = clock();
69 
70  PGR_DBG("pgr_get_restrictions");
71  PGR_DBG("%s", restrictions_sql);
72 
73  Column_info_t info[3];
74 
75  int i;
76  for (i = 0; i < 3; ++i) {
77  info[i].colNumber = -1;
78  info[i].type = 0;
79  info[i].strict = true;
80  }
81 
82  /* restriction id */
83  info[0].name = "id";
84  info[1].name = "cost";
85  /* array of edges */
86  info[2].name = "path";
87 
88  info[0].eType = ANY_INTEGER;
89  info[1].eType = ANY_NUMERICAL;
90  info[2].eType = ANY_INTEGER_ARRAY;
91 
92  info[1].strict = false;
93 
94  size_t total_tuples;
95 
96  void *SPIplan;
97  SPIplan = pgr_SPI_prepare(restrictions_sql);
98  Portal SPIportal;
99  SPIportal = pgr_SPI_cursor_open(SPIplan);
100 
101  bool moredata = true;
102  (*total_restrictions) = total_tuples = 0;
103 
104 
105  /* on the first tuple get the column numbers */
106 
107  while (moredata == true) {
108  SPI_cursor_fetch(SPIportal, true, tuple_limit);
109  if (total_tuples == 0) {
110  pgr_fetch_column_info(info, 3);
111  }
112  size_t ntuples = SPI_processed;
113  total_tuples += ntuples;
114  PGR_DBG("Restrictions to be processed %ld", ntuples);
115  PGR_DBG("size of structure %ld", sizeof(Restriction_t));
116  if (ntuples > 0) {
117  if ((*restrictions) == NULL) {
118  (*restrictions) = (Restriction_t *)palloc(
119  total_tuples * sizeof(Restriction_t));
120  } else {
121  (*restrictions) = (Restriction_t *)repalloc(
122  (*restrictions),
123  total_tuples * sizeof(Restriction_t));
124  }
125 
126  if ((*restrictions) == NULL) {
127  elog(ERROR, "Out of memory");
128  }
129 
130  size_t t;
131  SPITupleTable *tuptable = SPI_tuptable;
132  TupleDesc tupdesc = SPI_tuptable->tupdesc;
133  PGR_DBG("processing %ld", ntuples);
134  for (t = 0; t < ntuples; t++) {
135  HeapTuple tuple = tuptable->vals[t];
136  fetch_restriction(&tuple, &tupdesc, info,
137  &(*restrictions)[total_tuples - ntuples + t]);
138  }
139  SPI_freetuptable(tuptable);
140  } else {
141  moredata = false;
142  }
143  }
144 
145  SPI_cursor_close(SPIportal);
146 
147  if (total_tuples == 0) {
148  (*total_restrictions) = 0;
149  PGR_DBG("NO restrictions");
150  return;
151  }
152 
153  (*total_restrictions) = total_tuples;
154  PGR_DBG("Finish reading %ld restrictions, %ld",
155  total_tuples,
156  (*total_restrictions));
157  clock_t end_t = clock();
158  time_msg(" reading Restrictions", start_t, end_t);
159 }
Column_info_t::colNumber
int colNumber
Definition: column_info_t.h:51
Restriction_t::cost
double cost
Definition: restriction_t.h:39
time_msg.h
fetch_restriction
static void fetch_restriction(HeapTuple *tuple, TupleDesc *tupdesc, Column_info_t info[3], Restriction_t *restriction)
Definition: restrictions_input.c:35
pgr_SPI_getBigIntArr
int64_t * pgr_SPI_getBigIntArr(HeapTuple *tuple, TupleDesc *tupdesc, Column_info_t info, uint64_t *the_size)
Function returns the values of specified columns in array.
Definition: get_check_data.c:196
Column_info_t::strict
bool strict
Definition: column_info_t.h:53
pgr_SPI_prepare
SPIPlanPtr pgr_SPI_prepare(char *sql)
Definition: postgres_connection.c:94
pgr_get_restrictions
void pgr_get_restrictions(char *restrictions_sql, Restriction_t **restrictions, size_t *total_restrictions)
Definition: restrictions_input.c:63
pgr_SPI_getBigInt
int64_t pgr_SPI_getBigInt(HeapTuple *tuple, TupleDesc *tupdesc, Column_info_t info)
Function returns the value of specified column in integer type.
Definition: get_check_data.c:216
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:78
debug_macro.h
Restriction_t::via
int64_t * via
Definition: restriction_t.h:40
Column_info_t::name
char * name
Definition: column_info_t.h:54
pgr_SPI_cursor_open
Portal pgr_SPI_cursor_open(SPIPlanPtr SPIplan)
Definition: postgres_connection.c:107
PGR_DBG
#define PGR_DBG(...)
Definition: debug_macro.h:34
Restriction_t::id
int64_t id
Definition: restriction_t.h:38
restrictions_input.h
ANY_INTEGER_ARRAY
@ ANY_INTEGER_ARRAY
Definition: column_info_t.h:45
pgr_SPI_getFloat8
double pgr_SPI_getFloat8(HeapTuple *tuple, TupleDesc *tupdesc, Column_info_t info)
Function returns the value of specified column in double type.
Definition: get_check_data.c:246
Column_info_t::eType
expectType eType
Definition: column_info_t.h:55
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:41
column_info_t.h
Restriction_t
Definition: restriction_t.h:37
ANY_NUMERICAL
@ ANY_NUMERICAL
Definition: column_info_t.h:42
column_found
bool column_found(int colNumber)
Function will check whether the colNumber represent any specific column or NULL (SPI_ERROR_NOATTRIBUT...
Definition: get_check_data.c:36
get_check_data.h
Column_info_t::type
uint64_t type
Definition: column_info_t.h:52
Column_info_t
Definition: column_info_t.h:49
Restriction_t::via_size
uint64_t via_size
Definition: restriction_t.h:41