PGROUTING  2.4
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
customers_input.c
Go to the documentation of this file.
1 /*PGR-GNU*****************************************************************
2 File: customers_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 "./../../common/src/postgres_connection.h"
26 
27 #include "./../../common/src/debug_macro.h"
28 #include "./../../common/src/pgr_types.h"
29 #include "./../../common/src/get_check_data.h"
30 #include "./customers_input.h"
31 
32 
33 static
35  HeapTuple *tuple,
36  TupleDesc *tupdesc,
37  Column_info_t info[9],
38  Customer_t *customer) {
39  customer->id = pgr_SPI_getBigInt(tuple, tupdesc, info[0]);
40  customer->x = pgr_SPI_getFloat8(tuple, tupdesc, info[1]);
41  customer->y = pgr_SPI_getFloat8(tuple, tupdesc, info[2]);
42  customer->demand = pgr_SPI_getFloat8(tuple, tupdesc, info[3]);
43  customer->Etime = pgr_SPI_getFloat8(tuple, tupdesc, info[4]);
44  customer->Ltime = pgr_SPI_getFloat8(tuple, tupdesc, info[5]);
45  customer->Stime = pgr_SPI_getFloat8(tuple, tupdesc, info[6]);
46  customer->Pindex = pgr_SPI_getBigInt(tuple, tupdesc, info[7]);
47  customer->Dindex = pgr_SPI_getBigInt(tuple, tupdesc, info[8]);
48  customer->Ddist = 0;
49 }
50 
51 
52 
53 void
55  char *customers_sql,
56  Customer_t **customers,
57  size_t *total_customers) {
58  const int tuple_limit = 1000000;
59 
60  PGR_DBG("pgr_get_customers_data");
61  PGR_DBG("%s", customers_sql);
62 
63  Column_info_t info[9];
64 
65  int i;
66  for (i = 0; i < 9; ++i) {
67  info[i].colNumber = -1;
68  info[i].type = 0;
69  info[i].strict = true;
70  info[i].eType = ANY_NUMERICAL;
71  }
72 
86  info[0].name = strdup("id");
87  info[1].name = strdup("x");
88  info[2].name = strdup("y");
89  info[3].name = strdup("demand");
90  info[4].name = strdup("opentime");
91  info[5].name = strdup("closetime");
92  info[6].name = strdup("servicetime");
93  info[7].name = strdup("pindex");
94  info[8].name = strdup("dindex");
95 
96  info[0].eType = ANY_INTEGER;
97  info[7].eType = ANY_INTEGER;
98  info[8].eType = ANY_INTEGER;
99 
100 
101  size_t ntuples;
102  size_t total_tuples;
103 
104  void *SPIplan;
105  SPIplan = pgr_SPI_prepare(customers_sql);
106  Portal SPIportal;
107  SPIportal = pgr_SPI_cursor_open(SPIplan);
108 
109  bool moredata = TRUE;
110  (*total_customers) = total_tuples = 0;
111 
112  /* on the first tuple get the column numbers */
113 
114  while (moredata == TRUE) {
115  SPI_cursor_fetch(SPIportal, TRUE, tuple_limit);
116  if (total_tuples == 0) {
117  pgr_fetch_column_info(info, 9);
118  }
119  ntuples = SPI_processed;
120  total_tuples += ntuples;
121  PGR_DBG("SPI_processed %ld", ntuples);
122  if (ntuples > 0) {
123  if ((*customers) == NULL)
124  (*customers) = (Customer_t *)palloc0(
125  total_tuples * sizeof(Customer_t));
126  else
127  (*customers) = (Customer_t *)repalloc(
128  (*customers), total_tuples * sizeof(Customer_t));
129 
130  if ((*customers) == NULL) {
131  elog(ERROR, "Out of memory");
132  }
133 
134  size_t t;
135  SPITupleTable *tuptable = SPI_tuptable;
136  TupleDesc tupdesc = SPI_tuptable->tupdesc;
137  PGR_DBG("processing %ld", ntuples);
138  for (t = 0; t < ntuples; t++) {
139  HeapTuple tuple = tuptable->vals[t];
140  fetch_customer(&tuple, &tupdesc, info,
141  &(*customers)[total_tuples - ntuples + t]);
142  }
143  SPI_freetuptable(tuptable);
144  } else {
145  moredata = FALSE;
146  }
147  }
148 
149  SPI_cursor_close(SPIportal);
150 
151  if (total_tuples == 0) {
152  (*total_customers) = 0;
153  PGR_DBG("NO customers");
154  return;
155  }
156 
157  (*total_customers) = total_tuples;
158  PGR_DBG("Finish reading %ld data, %ld", total_tuples, (*total_customers));
159 }
int64_t pgr_SPI_getBigInt(HeapTuple *tuple, TupleDesc *tupdesc, Column_info_t info)
double y
Definition: pgr_types.h:230
double demand
Definition: pgr_types.h:231
void pgr_get_customers_data(char *customers_sql, Customer_t **customers, size_t *total_customers)
#define PGR_DBG(...)
Definition: debug_macro.h:34
uint64_t type
Definition: pgr_types.h:200
static void fetch_customer(HeapTuple *tuple, TupleDesc *tupdesc, Column_info_t info[9], Customer_t *customer)
int64_t id
Definition: pgr_types.h:228
char * name
Definition: pgr_types.h:202
int64_t Pindex
Definition: pgr_types.h:235
void pgr_fetch_column_info(Column_info_t info[], int info_size)
double Stime
Definition: pgr_types.h:234
double Ddist
Definition: pgr_types.h:237
int64_t Dindex
Definition: pgr_types.h:236
double Etime
Definition: pgr_types.h:232
double Ltime
Definition: pgr_types.h:233
double x
Definition: pgr_types.h:229
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: pgr_types.h:203