PGROUTING  2.4
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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
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 #include "./postgres_connection.h"
26 
27 #include "./debug_macro.h"
28 #include "./pgr_types.h"
29 #include "./get_check_data.h"
30 #include "./time_msg.h"
31 #include "./matrixRows_input.h"
32 
33 
34 
35 static
37  HeapTuple *tuple,
38  TupleDesc *tupdesc,
39  Column_info_t info[3],
40  Matrix_cell_t *distance) {
41  distance->from_vid = pgr_SPI_getBigInt(tuple, tupdesc, info[0]);
42  distance->to_vid = pgr_SPI_getBigInt(tuple, tupdesc, info[1]);
43  distance->cost = pgr_SPI_getFloat8(tuple, tupdesc, info[2]);
44 }
45 
52  char *sql,
53  Matrix_cell_t **rows,
54  size_t *total_rows) {
55  clock_t start_t = clock();
56 
57  const int tuple_limit = 1000000;
58 
59  size_t ntuples;
60  size_t total_tuples = 0;
61 
62  Column_info_t info[3];
63 
64  int i;
65  for (i = 0; i < 3; ++i) {
66  info[i].colNumber = -1;
67  info[i].type = 0;
68  info[i].strict = true;
69  info[i].eType = ANY_INTEGER;
70  }
71  info[0].name = strdup("start_vid");
72  info[1].name = strdup("end_vid");
73  info[2].name = strdup("agg_cost");
74 
75  info[2].eType = ANY_NUMERICAL;
76 
77 
78  void *SPIplan;
79  SPIplan = pgr_SPI_prepare(sql);
80 
81  Portal SPIportal;
82  SPIportal = pgr_SPI_cursor_open(SPIplan);
83 
84 
85  bool moredata = TRUE;
86  (*total_rows) = total_tuples;
87 
88  while (moredata == TRUE) {
89  SPI_cursor_fetch(SPIportal, TRUE, tuple_limit);
90  if (total_tuples == 0)
91  pgr_fetch_column_info(info, 3);
92 
93  ntuples = SPI_processed;
94  total_tuples += ntuples;
95 
96  if (ntuples > 0) {
97  if ((*rows) == NULL)
98  (*rows) = (Matrix_cell_t *)palloc0(
99  total_tuples * sizeof(Matrix_cell_t));
100  else
101  (*rows) = (Matrix_cell_t *)repalloc(
102  (*rows), total_tuples * sizeof(Matrix_cell_t));
103 
104  if ((*rows) == NULL) {
105  elog(ERROR, "Out of memory");
106  }
107 
108  SPITupleTable *tuptable = SPI_tuptable;
109  TupleDesc tupdesc = SPI_tuptable->tupdesc;
110  PGR_DBG("processing %ld edge tupĺes", ntuples);
111 
112  size_t t;
113  for (t = 0; t < ntuples; t++) {
114  HeapTuple tuple = tuptable->vals[t];
115  pgr_fetch_row(&tuple, &tupdesc, info,
116  &(*rows)[total_tuples - ntuples + t]);
117  }
118  SPI_freetuptable(tuptable);
119  } else {
120  moredata = FALSE;
121  }
122  }
123 
124  SPI_cursor_close(SPIportal);
125 
126 
127  if (total_tuples == 0) {
128  (*total_rows) = 0;
129  PGR_DBG("NO rows");
130  return;
131  }
132 
133  (*total_rows) = total_tuples;
134  time_msg(" reading Edges", start_t, clock());
135 }
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)
struct matrix_cell Matrix_cell_t
int64_t from_vid
Definition: pgr_types.h:165
double cost
Definition: pgr_types.h:167
void pgr_get_matrixRows(char *sql, Matrix_cell_t **rows, size_t *total_rows)
bigint start_vid, bigint end_vid, float agg_cost,
Portal pgr_SPI_cursor_open(SPIPlanPtr SPIplan)
int64_t to_vid
Definition: pgr_types.h:166
double pgr_SPI_getFloat8(HeapTuple *tuple, TupleDesc *tupdesc, Column_info_t info)
SPIPlanPtr pgr_SPI_prepare(char *sql)
static void pgr_fetch_row(HeapTuple *tuple, TupleDesc *tupdesc, Column_info_t info[3], Matrix_cell_t *distance)
expectType eType
Definition: pgr_types.h:203