PGROUTING  2.6-dev
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 
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  restriction->cost = pgr_SPI_getFloat8(tuple, tupdesc, info[1]);
49 
50  restriction->via = NULL;
51  restriction->via_size = 0;
52 
53  restriction->via = pgr_SPI_getBigIntArr(
54  tuple, tupdesc, info[2], &restriction->via_size);
55 }
56 
57 
58 void
60  char *restrictions_sql,
61  Restriction_t **restrictions,
62  size_t *total_restrictions) {
63  const int tuple_limit = 1000000;
64  clock_t start_t = clock();
65 
66  PGR_DBG("pgr_get_restrictions");
67  PGR_DBG("%s", restrictions_sql);
68 
69  Column_info_t info[3];
70 
71  int i;
72  for (i = 0; i < 3; ++i) {
73  info[i].colNumber = -1;
74  info[i].type = 0;
75  info[i].strict = true;
76  }
77 
78  /* restriction id */
79  info[0].name = "id";
80  info[1].name = "cost";
81  /* array of edges */
82  info[2].name = "path";
83 
84  info[0].eType = ANY_INTEGER;
85  info[1].eType = ANY_NUMERICAL;
86  info[2].eType = ANY_INTEGER_ARRAY;
87 
88 
89  size_t ntuples;
90  size_t total_tuples;
91 
92  void *SPIplan;
93  SPIplan = pgr_SPI_prepare(restrictions_sql);
94  Portal SPIportal;
95  SPIportal = pgr_SPI_cursor_open(SPIplan);
96 
97  bool moredata = TRUE;
98  (*total_restrictions) = total_tuples = 0;
99 
100 
101  /* on the first tuple get the column numbers */
102 
103  while (moredata == TRUE) {
104  SPI_cursor_fetch(SPIportal, TRUE, tuple_limit);
105  if (total_tuples == 0) {
106  pgr_fetch_column_info(info, 3);
107  }
108  ntuples = SPI_processed;
109  total_tuples += ntuples;
110  PGR_DBG("Restrictions to be processed %ld", ntuples);
111  PGR_DBG("size of structure %ld", sizeof(Restriction_t));
112  if (ntuples > 0) {
113  if ((*restrictions) == NULL) {
114  (*restrictions) = (Restriction_t *)palloc(
115  total_tuples * sizeof(Restriction_t));
116  } else {
117  (*restrictions) = (Restriction_t *)repalloc(
118  (*restrictions),
119  total_tuples * sizeof(Restriction_t));
120  }
121 
122  if ((*restrictions) == NULL) {
123  elog(ERROR, "Out of memory");
124  }
125 
126  size_t t;
127  SPITupleTable *tuptable = SPI_tuptable;
128  TupleDesc tupdesc = SPI_tuptable->tupdesc;
129  PGR_DBG("processing %ld", ntuples);
130  for (t = 0; t < ntuples; t++) {
131  HeapTuple tuple = tuptable->vals[t];
132  fetch_restriction(&tuple, &tupdesc, info,
133  &(*restrictions)[total_tuples - ntuples + t]);
134  }
135  SPI_freetuptable(tuptable);
136  } else {
137  moredata = FALSE;
138  }
139  }
140 
141  SPI_cursor_close(SPIportal);
142 
143  if (total_tuples == 0) {
144  (*total_restrictions) = 0;
145  PGR_DBG("NO restrictions");
146  return;
147  }
148 
149  (*total_restrictions) = total_tuples;
150  PGR_DBG("Finish reading %ld restrictions, %ld",
151  total_tuples,
152  (*total_restrictions));
153  clock_t end_t = clock();
154  time_msg(" reading Restrictions", start_t, end_t);
155 }
int64_t pgr_SPI_getBigInt(HeapTuple *tuple, TupleDesc *tupdesc, Column_info_t info)
uint64_t via_size
Definition: restriction_t.h:63
int64_t * via
Definition: restriction_t.h:62
static void fetch_restriction(HeapTuple *tuple, TupleDesc *tupdesc, Column_info_t info[3], Restriction_t *restriction)
void pgr_get_restrictions(char *restrictions_sql, Restriction_t **restrictions, size_t *total_restrictions)
#define PGR_DBG(...)
Definition: debug_macro.h:34
uint64_t type
Definition: column_info_t.h:73
void time_msg(char *msg, clock_t start_t, clock_t end_t)
Definition: time_msg.c:32
void pgr_fetch_column_info(Column_info_t info[], int info_size)
int64_t * pgr_SPI_getBigIntArr(HeapTuple *tuple, TupleDesc *tupdesc, Column_info_t info, uint64_t *the_size)
Portal pgr_SPI_cursor_open(SPIPlanPtr SPIplan)
double pgr_SPI_getFloat8(HeapTuple *tuple, TupleDesc *tupdesc, Column_info_t info)
SPIPlanPtr pgr_SPI_prepare(char *sql)
expectType eType
Definition: column_info_t.h:76