PGROUTING  3.2
matrixRows_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 /* for bool */
28 # include <stdbool.h>
29 /* for size_t */
30 # include <stddef.h>
31 
32 #include "c_types/column_info_t.h"
33 
34 #include "c_common/debug_macro.h"
36 #include "c_common/time_msg.h"
37 
38 
39 
40 static
42  HeapTuple *tuple,
43  TupleDesc *tupdesc,
44  Column_info_t info[3],
45  Matrix_cell_t *distance) {
46  distance->from_vid = pgr_SPI_getBigInt(tuple, tupdesc, info[0]);
47  distance->to_vid = pgr_SPI_getBigInt(tuple, tupdesc, info[1]);
48  distance->cost = pgr_SPI_getFloat8(tuple, tupdesc, info[2]);
49 }
50 
57  char *sql,
58  Matrix_cell_t **rows,
59  size_t *total_rows) {
60  clock_t start_t = clock();
61 
62  const int tuple_limit = 1000000;
63 
64  size_t total_tuples = 0;
65 
66  Column_info_t info[3];
67 
68  int i;
69  for (i = 0; i < 3; ++i) {
70  info[i].colNumber = -1;
71  info[i].type = 0;
72  info[i].strict = true;
73  info[i].eType = ANY_INTEGER;
74  }
75  info[0].name = "start_vid";
76  info[1].name = "end_vid";
77  info[2].name = "agg_cost";
78 
79  info[2].eType = ANY_NUMERICAL;
80 
81 
82  void *SPIplan;
83  SPIplan = pgr_SPI_prepare(sql);
84 
85  Portal SPIportal;
86  SPIportal = pgr_SPI_cursor_open(SPIplan);
87 
88 
89  bool moredata = true;
90  (*total_rows) = total_tuples;
91 
92  while (moredata == true) {
93  SPI_cursor_fetch(SPIportal, true, tuple_limit);
94  if (total_tuples == 0)
95  pgr_fetch_column_info(info, 3);
96 
97  size_t ntuples = SPI_processed;
98  total_tuples += ntuples;
99 
100  if (ntuples > 0) {
101  if ((*rows) == NULL)
102  (*rows) = (Matrix_cell_t *)palloc0(
103  total_tuples * sizeof(Matrix_cell_t));
104  else
105  (*rows) = (Matrix_cell_t *)repalloc(
106  (*rows), total_tuples * sizeof(Matrix_cell_t));
107 
108  if ((*rows) == NULL) {
109  elog(ERROR, "Out of memory");
110  }
111 
112  SPITupleTable *tuptable = SPI_tuptable;
113  TupleDesc tupdesc = SPI_tuptable->tupdesc;
114  PGR_DBG("processing %ld matrix cell tupĺes", ntuples);
115 
116  size_t t;
117  for (t = 0; t < ntuples; t++) {
118  HeapTuple tuple = tuptable->vals[t];
119  pgr_fetch_row(&tuple, &tupdesc, info,
120  &(*rows)[total_tuples - ntuples + t]);
121  }
122  SPI_freetuptable(tuptable);
123  } else {
124  moredata = false;
125  }
126  }
127 
128  SPI_cursor_close(SPIportal);
129 
130 
131  if (total_tuples == 0) {
132  (*total_rows) = 0;
133  PGR_DBG("NO rows");
134  return;
135  }
136 
137  (*total_rows) = total_tuples;
138  time_msg(" reading matrix cells", start_t, clock());
139 }
Column_info_t::colNumber
int colNumber
Definition: column_info_t.h:51
pgr_get_matrixRows
void pgr_get_matrixRows(char *sql, Matrix_cell_t **rows, size_t *total_rows)
bigint start_vid, bigint end_vid, float agg_cost,
Definition: matrixRows_input.c:56
pgr_fetch_row
static void pgr_fetch_row(HeapTuple *tuple, TupleDesc *tupdesc, Column_info_t info[3], Matrix_cell_t *distance)
Definition: matrixRows_input.c:41
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
matrix_cell::to_vid
int64_t to_vid
Definition: matrix_cell_t.h:39
matrixRows_input.h
matrix_cell::cost
double cost
Definition: matrix_cell_t.h:40
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
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
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
Matrix_cell_t
struct matrix_cell Matrix_cell_t
column_info_t.h
matrix_cell::from_vid
int64_t from_vid
Definition: matrix_cell_t.h:38
ANY_NUMERICAL
@ ANY_NUMERICAL
Definition: column_info_t.h:42
get_check_data.h
matrix_cell
Definition: matrix_cell_t.h:37
Column_info_t::type
uint64_t type
Definition: column_info_t.h:52
Column_info_t
Definition: column_info_t.h:49