PGROUTING  3.2
pgr_point_input.c
Go to the documentation of this file.
1 /*PGR-GNU*****************************************************************
2 File: matrixRows_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_common/debug_macro.h"
29 #include "c_common/time_msg.h"
30 
31 
32 
33 static
35  HeapTuple *tuple,
36  TupleDesc *tupdesc,
37  Column_info_t info[2],
38  Pgr_point_t *point) {
39  point->x = pgr_SPI_getFloat8(tuple, tupdesc, info[0]);
40  point->y = pgr_SPI_getFloat8(tuple, tupdesc, info[1]);
41 }
42 
49  char *points_sql,
50  Pgr_point_t **points,
51  size_t *pointsTotal) {
52  clock_t start_t = clock();
53 
54  const int tuple_limit = 1000000;
55 
56  size_t total_tuples = 0;
57 
58  Column_info_t info[3];
59 
60  int i;
61  for (i = 0; i < 2; ++i) {
62  info[i].colNumber = -1;
63  info[i].type = 0;
64  info[i].strict = true;
65  info[i].eType = ANY_NUMERICAL;
66  }
67  info[0].name = "x";
68  info[1].name = "y";
69 
70 
71  void *SPIplan;
72  SPIplan = pgr_SPI_prepare(points_sql);
73 
74  Portal SPIportal;
75  SPIportal = pgr_SPI_cursor_open(SPIplan);
76 
77 
78  bool moredata = true;
79  (*pointsTotal) = total_tuples;
80 
81  while (moredata == true) {
82  SPI_cursor_fetch(SPIportal, true, tuple_limit);
83  if (total_tuples == 0)
84  pgr_fetch_column_info(info, 2);
85 
86  size_t ntuples = SPI_processed;
87  total_tuples += ntuples;
88 
89  if (ntuples > 0) {
90  if ((*points) == NULL)
91  (*points) = (Pgr_point_t *)
92  palloc0(total_tuples * sizeof(Pgr_point_t));
93  else
94  (*points) = (Pgr_point_t *)
95  repalloc((*points),
96  total_tuples * sizeof(Pgr_point_t));
97 
98  if ((*points) == NULL) {
99  elog(ERROR, "Out of memory");
100  }
101 
102  SPITupleTable *tuptable = SPI_tuptable;
103  TupleDesc tupdesc = SPI_tuptable->tupdesc;
104 
105  size_t t;
106  for (t = 0; t < ntuples; t++) {
107  HeapTuple tuple = tuptable->vals[t];
108  pgr_fetch_row(&tuple, &tupdesc, info,
109  &(*points)[total_tuples - ntuples + t]);
110  }
111  SPI_freetuptable(tuptable);
112  } else {
113  moredata = false;
114  }
115  }
116 
117  SPI_cursor_close(SPIportal);
118 
119 
120  if (total_tuples == 0) {
121  (*pointsTotal) = 0;
122  return;
123  }
124 
125  (*pointsTotal) = total_tuples;
126  time_msg(" reading points:", start_t, clock());
127 }
Column_info_t::colNumber
int colNumber
Definition: column_info_t.h:51
pgr_fetch_row
static void pgr_fetch_row(HeapTuple *tuple, TupleDesc *tupdesc, Column_info_t info[2], Pgr_point_t *point)
Definition: pgr_point_input.c:34
time_msg.h
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_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
Column_info_t::name
char * name
Definition: column_info_t.h:54
Pgr_point_t::x
double x
Definition: pgr_point_t.h:31
pgr_SPI_cursor_open
Portal pgr_SPI_cursor_open(SPIPlanPtr SPIplan)
Definition: postgres_connection.c:107
pgr_point_input.h
pgr_point_input
void pgr_point_input(char *points_sql, Pgr_point_t **points, size_t *pointsTotal)
bigint id, float x, float y,
Definition: pgr_point_input.c:48
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
Pgr_point_t::y
double y
Definition: pgr_point_t.h:32
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_NUMERICAL
@ ANY_NUMERICAL
Definition: column_info_t.h:42
Pgr_point_t
Definition: pgr_point_t.h:30
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