pgRouting
pgRouting extends the PostGIS / PostgreSQL geospatial database to provide geospatial routing functionality.
 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 "postgres.h"
26 #include "executor/spi.h"
27 
28 #include "./../../common/src/debug_macro.h"
29 #include "./../../common/src/pgr_types.h"
30 #include "./../../common/src/postgres_connection.h"
31 #include "./../../common/src/get_check_data.h"
32 #include "./customers_input.h"
33 
34 
35 static
37  HeapTuple *tuple,
38  TupleDesc *tupdesc,
39  Column_info_t info[9],
40  Customer_t *customer) {
41  customer->id = pgr_SPI_getBigInt(tuple, tupdesc, info[0]);
42  customer->x = pgr_SPI_getFloat8(tuple, tupdesc, info[1]);
43  customer->y = pgr_SPI_getFloat8(tuple, tupdesc, info[2]);
44  customer->demand = pgr_SPI_getFloat8(tuple, tupdesc, info[3]);
45  customer->Etime = pgr_SPI_getFloat8(tuple, tupdesc, info[4]);
46  customer->Ltime = pgr_SPI_getFloat8(tuple, tupdesc, info[5]);
47  customer->Stime = pgr_SPI_getFloat8(tuple, tupdesc, info[6]);
48  customer->Pindex = pgr_SPI_getBigInt(tuple, tupdesc, info[7]);
49  customer->Dindex = pgr_SPI_getBigInt(tuple, tupdesc, info[8]);
50  customer->Ddist = 0;
51 }
52 
53 
54 
55 void
57  char *customers_sql,
58  Customer_t **customers,
59  size_t *total_customers) {
60  const int tuple_limit = 1000000;
61 
62  PGR_DBG("pgr_get_customers_data");
63  PGR_DBG("%s", customers_sql);
64 
65  Column_info_t info[9];
66 
67  int i;
68  for (i = 0; i < 9; ++i) {
69  info[i].colNumber = -1;
70  info[i].type = 0;
71  info[i].strict = true;
72  info[i].eType = ANY_NUMERICAL;
73  }
74 
88  info[0].name = strdup("id");
89  info[1].name = strdup("x");
90  info[2].name = strdup("y");
91  info[3].name = strdup("demand");
92  info[4].name = strdup("opentime");
93  info[5].name = strdup("closetime");
94  info[6].name = strdup("servicetime");
95  info[7].name = strdup("pindex");
96  info[8].name = strdup("dindex");
97 
98  info[0].eType = ANY_INTEGER;
99  info[7].eType = ANY_INTEGER;
100  info[8].eType = ANY_INTEGER;
101 
102 
103  size_t ntuples;
104  size_t total_tuples;
105 
106  void *SPIplan;
107  SPIplan = pgr_SPI_prepare(customers_sql);
108  Portal SPIportal;
109  SPIportal = pgr_SPI_cursor_open(SPIplan);
110 
111  bool moredata = TRUE;
112  (*total_customers) = total_tuples = 0;
113 
114  /* on the first tuple get the column numbers */
115 
116  while (moredata == TRUE) {
117  SPI_cursor_fetch(SPIportal, TRUE, tuple_limit);
118  if (total_tuples == 0) {
119  pgr_fetch_column_info(info, 9);
120  }
121  ntuples = SPI_processed;
122  total_tuples += ntuples;
123  PGR_DBG("SPI_processed %ld", ntuples);
124  if (ntuples > 0) {
125  if ((*customers) == NULL)
126  (*customers) = (Customer_t *)palloc0(
127  total_tuples * sizeof(Customer_t));
128  else
129  (*customers) = (Customer_t *)repalloc(
130  (*customers), total_tuples * sizeof(Customer_t));
131 
132  if ((*customers) == NULL) {
133  elog(ERROR, "Out of memory");
134  }
135 
136  size_t t;
137  SPITupleTable *tuptable = SPI_tuptable;
138  TupleDesc tupdesc = SPI_tuptable->tupdesc;
139  PGR_DBG("processing %ld", ntuples);
140  for (t = 0; t < ntuples; t++) {
141  HeapTuple tuple = tuptable->vals[t];
142  fetch_customer(&tuple, &tupdesc, info,
143  &(*customers)[total_tuples - ntuples + t]);
144  }
145  SPI_freetuptable(tuptable);
146  } else {
147  moredata = FALSE;
148  }
149  }
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:200
double demand
Definition: pgr_types.h:201
void pgr_get_customers_data(char *customers_sql, Customer_t **customers, size_t *total_customers)
#define PGR_DBG(...)
Definition: debug_macro.h:33
uint64_t type
Definition: pgr_types.h:186
static void fetch_customer(HeapTuple *tuple, TupleDesc *tupdesc, Column_info_t info[9], Customer_t *customer)
int64_t id
Definition: pgr_types.h:198
char * name
Definition: pgr_types.h:188
int64_t Pindex
Definition: pgr_types.h:205
void pgr_fetch_column_info(Column_info_t info[], int info_size)
double Stime
Definition: pgr_types.h:204
double Ddist
Definition: pgr_types.h:207
int64_t Dindex
Definition: pgr_types.h:206
double Etime
Definition: pgr_types.h:202
double Ltime
Definition: pgr_types.h:203
double x
Definition: pgr_types.h:199
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:189