PGROUTING  2.4
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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
5 vicky_vergara@hotmail.com
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 
25 
26 #include "./postgres_connection.h"
27 #include "./debug_macro.h"
28 #include "./pgr_types.h"
29 #include "./time_msg.h"
30 #include "./get_check_data.h"
31 #include "./restrictions_input.h"
32 
33 
34 static
36  HeapTuple *tuple,
37  TupleDesc *tupdesc,
38  Column_info_t info[4],
39  Restrict_t *restriction) {
40  restriction->target_id = pgr_SPI_getBigInt(tuple, tupdesc, info[0]);
41  restriction->to_cost = pgr_SPI_getFloat8(tuple, tupdesc, info[1]);
42  char *str = DatumGetCString(
43  SPI_getvalue(*tuple, *tupdesc, info[2].colNumber));
44 
45 // TODO(someone) because its text, no guarantee the text read is correct
46 // move this code to c++ to tokenize the integers.
47 
48  int i = 0;
49  for (i = 0; i < MAX_RULE_LENGTH; ++i) restriction->via[i] = -1;
50 
51  if (str != NULL) {
52  char *token = NULL;
53  int i = 0;
54 
55  token = (char *)strtok(str, " ,");
56 
57  while (token != NULL && i < MAX_RULE_LENGTH) {
58  restriction->via[i] = atoi(token);
59  i++;
60  token = (char *)strtok(NULL, " ,");
61  }
62  }
63 }
64 
65 
66 void
68  char *restrictions_sql,
69  Restrict_t **restrictions,
70  size_t *total_restrictions) {
71  const int tuple_limit = 1000000;
72  clock_t start_t = clock();
73 
74  PGR_DBG("pgr_get_restriction_data");
75  PGR_DBG("%s", restrictions_sql);
76 
77  Column_info_t info[3];
78 
79  int i;
80  for (i = 0; i < 3; ++i) {
81  info[i].colNumber = -1;
82  info[i].type = 0;
83  info[i].strict = true;
84  info[i].eType = ANY_INTEGER;
85  }
86  info[0].name = strdup("target_id");
87  info[1].name = strdup("to_cost");
88  info[2].name = strdup("via_path");
89 
90  info[1].eType = ANY_NUMERICAL;
91  info[2].eType = TEXT;
92 
93 
94  size_t ntuples;
95  size_t total_tuples;
96 
97  void *SPIplan;
98  SPIplan = pgr_SPI_prepare(restrictions_sql);
99  Portal SPIportal;
100  SPIportal = pgr_SPI_cursor_open(SPIplan);
101 
102  bool moredata = TRUE;
103  (*total_restrictions) = total_tuples = 0;
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  ntuples = SPI_processed;
113  total_tuples += ntuples;
114  PGR_DBG("SPI_processed %ld", ntuples);
115  if (ntuples > 0) {
116  if ((*restrictions) == NULL)
117  (*restrictions) = (Restrict_t *)palloc0(
118  total_tuples * sizeof(Restrict_t));
119  else
120  (*restrictions) = (Restrict_t *)repalloc(
121  (*restrictions),
122  total_tuples * sizeof(Restrict_t));
123 
124  if ((*restrictions) == NULL) {
125  elog(ERROR, "Out of memory");
126  }
127 
128  size_t t;
129  SPITupleTable *tuptable = SPI_tuptable;
130  TupleDesc tupdesc = SPI_tuptable->tupdesc;
131  PGR_DBG("processing %ld", ntuples);
132  for (t = 0; t < ntuples; t++) {
133  HeapTuple tuple = tuptable->vals[t];
134  fetch_restriction(&tuple, &tupdesc, info,
135  &(*restrictions)[total_tuples - ntuples + t]);
136  }
137  SPI_freetuptable(tuptable);
138  } else {
139  moredata = FALSE;
140  }
141  }
142 
143  SPI_cursor_close(SPIportal);
144 
145  if (total_tuples == 0) {
146  (*total_restrictions) = 0;
147  PGR_DBG("NO restrictions");
148  return;
149  }
150 
151  (*total_restrictions) = total_tuples;
152  PGR_DBG("Finish reading %ld data, %ld",
153  total_tuples,
154  (*total_restrictions));
155  clock_t end_t = clock();
156  time_msg(" reading Restrictions", start_t, end_t);
157 }
int64_t pgr_SPI_getBigInt(HeapTuple *tuple, TupleDesc *tupdesc, Column_info_t info)
#define PGR_DBG(...)
Definition: debug_macro.h:34
uint64_t type
Definition: pgr_types.h:200
void time_msg(char *msg, clock_t start_t, clock_t end_t)
Definition: time_msg.c:32
char * name
Definition: pgr_types.h:202
void pgr_fetch_column_info(Column_info_t info[], int info_size)
double to_cost
Definition: pgr_types.h:175
int64_t target_id
Definition: pgr_types.h:174
void pgr_get_restriction_data(char *restrictions_sql, Restrict_t **restrictions, size_t *total_restrictions)
int64_t via[5]
Definition: pgr_types.h:176
Portal pgr_SPI_cursor_open(SPIPlanPtr SPIplan)
double pgr_SPI_getFloat8(HeapTuple *tuple, TupleDesc *tupdesc, Column_info_t info)
#define MAX_RULE_LENGTH
Definition: pgr_types.h:172
SPIPlanPtr pgr_SPI_prepare(char *sql)
expectType eType
Definition: pgr_types.h:203
static void fetch_restriction(HeapTuple *tuple, TupleDesc *tupdesc, Column_info_t info[4], Restrict_t *restriction)