PGROUTING  3.2
combinations_input.c
Go to the documentation of this file.
1 /*PGR-GNU*****************************************************************
2 File: combinations_input.c
3 
4 Generated with Template by:
5 Copyright (c) 2015 pgRouting developers
7 
8 Function's developer:
9 Copyright (c) 2020 Mahmoud SAKR and Esteban ZIMANYI
11 
12 ------
13 
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation; either version 2 of the License, or
17 (at your option) any later version.
18 
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
23 
24 You should have received a copy of the GNU General Public License
25 along with this program; if not, write to the Free Software
26 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 
28  ********************************************************************PGR-GNU*/
29 
31 
32 #include <math.h>
33 #include <float.h>
34 #include <limits.h>
35 /* for size-t */
36 #ifdef __cplusplus
37 # include <cstddef>
38 #else
39 # include <stddef.h>
40 #endif
41 
42 #include "c_types/column_info_t.h"
44 #include "c_common/debug_macro.h"
46 #include "c_common/time_msg.h"
47 
48 
49 static
51  HeapTuple *tuple,
52  TupleDesc *tupdesc,
53  Column_info_t info[2],
54  pgr_combination_t *combination,
55  size_t *valid_combinations) {
56  combination->source = pgr_SPI_getBigInt(tuple, tupdesc, info[0]);
57  combination->target = pgr_SPI_getBigInt(tuple, tupdesc, info[1]);
58 
59  *valid_combinations = *valid_combinations + 1;
60 }
61 
62 
63 
64 static
65 void
67  char *sql,
68  pgr_combination_t **combinations,
69  size_t *totalTuples) {
70  clock_t start_t = clock();
71 
72  const int tuple_limit = 1000000;
73 
74  size_t total_tuples;
75  size_t valid_combinations;
76 
77  Column_info_t info[2];
78 
79  int i;
80  for (i = 0; i < 2; ++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 = "source";
87  info[1].name = "target";
88 
89  void *SPIplan;
90  SPIplan = pgr_SPI_prepare(sql);
91 
92  Portal SPIportal;
93  SPIportal = pgr_SPI_cursor_open(SPIplan);
94 
95 
96  bool moredata = true;
97  (*totalTuples) = total_tuples = valid_combinations = 0;
98 
99  while (moredata == true) {
100  SPI_cursor_fetch(SPIportal, true, tuple_limit);
101  if (total_tuples == 0)
102  pgr_fetch_column_info(info, 2);
103 
104  size_t ntuples = SPI_processed;
105  total_tuples += ntuples;
106 
107  if (ntuples > 0) {
108  if ((*combinations) == NULL)
109  (*combinations) = (pgr_combination_t *)
110  palloc0(total_tuples * sizeof(pgr_combination_t));
111  else
112  (*combinations) = (pgr_combination_t *)
113  repalloc((*combinations), total_tuples * sizeof(pgr_combination_t));
114 
115  if ((*combinations) == NULL) {
116  elog(ERROR, "Out of memory");
117  }
118 
119  size_t t;
120  SPITupleTable *tuptable = SPI_tuptable;
121  TupleDesc tupdesc = SPI_tuptable->tupdesc;
122  for (t = 0; t < ntuples; t++) {
123  HeapTuple tuple = tuptable->vals[t];
124  fetch_combination(&tuple, &tupdesc, info,
125  &(*combinations)[total_tuples - ntuples + t],
126  &valid_combinations);
127  }
128  SPI_freetuptable(tuptable);
129  } else {
130  moredata = false;
131  }
132  }
133 
134  SPI_cursor_close(SPIportal);
135 
136  if (total_tuples == 0 || valid_combinations == 0) {
137  PGR_DBG("No combinations found");
138  }
139 
140  (*totalTuples) = total_tuples;
141  PGR_DBG("Reading %ld combinations", total_tuples);
142  time_msg("reading combinations", start_t, clock());
143 }
144 
145 /* select source, target */
146 void
148  char *combinations_sql,
149  pgr_combination_t **combinations,
150  size_t *total_combinations) {
151  get_combinations_2_columns(combinations_sql, combinations, total_combinations);
152 }
Column_info_t::colNumber
int colNumber
Definition: column_info_t.h:51
combinations_input.h
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_combination_t.h
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
get_combinations_2_columns
static void get_combinations_2_columns(char *sql, pgr_combination_t **combinations, size_t *totalTuples)
Definition: combinations_input.c:66
pgr_combination_t::target
int64_t target
Definition: pgr_combination_t.h:45
pgr_combination_t
Definition: pgr_combination_t.h:43
pgr_get_combinations
void pgr_get_combinations(char *combinations_sql, pgr_combination_t **combinations, size_t *total_combinations)
combinations_sql
Definition: combinations_input.c:147
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
column_info_t.h
pgr_combination_t::source
int64_t source
Definition: pgr_combination_t.h:44
get_check_data.h
fetch_combination
static void fetch_combination(HeapTuple *tuple, TupleDesc *tupdesc, Column_info_t info[2], pgr_combination_t *combination, size_t *valid_combinations)
Definition: combinations_input.c:50
Column_info_t::type
uint64_t type
Definition: column_info_t.h:52
Column_info_t
Definition: column_info_t.h:49