-
-
Notifications
You must be signed in to change notification settings - Fork 372
/
coordinates_input.c
162 lines (123 loc) · 4.44 KB
/
coordinates_input.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/*PGR-GNU*****************************************************************
File: matrixRows_input.c
Copyright (c) 2015 Celia Virginia Vergara Castillo
------
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
********************************************************************PGR-GNU*/
#include "c_common/coordinates_input.h"
#include "c_types/column_info_t.h"
#include "c_types/coordinate_t.h"
#include "c_common/debug_macro.h"
#include "c_common/get_check_data.h"
#include "c_common/time_msg.h"
/*
.. Coordinates SQL definition start
========= ================= =================================================
Column Type Description
========= ================= =================================================
``id`` ``ANY-INTEGER`` Identifier of the starting vertex.
``x`` ``ANY-NUMERICAL`` X value of the coordinate.
``y`` ``ANY-NUMERICAL`` Y value of the coordinate.
========= ================= =================================================
.. Coordinates SQL definition end
*/
static
void pgr_fetch_row(
HeapTuple *tuple,
TupleDesc *tupdesc,
Column_info_t info[3],
int64_t *default_id,
Coordinate_t *distance) {
if (column_found(info[0].colNumber)) {
distance->id = pgr_SPI_getBigInt(tuple, tupdesc, info[0]);
} else {
distance->id = *default_id;
++(*default_id);
}
distance->x = pgr_SPI_getFloat8(tuple, tupdesc, info[1]);
distance->y = pgr_SPI_getFloat8(tuple, tupdesc, info[2]);
}
/*!
* bigint id,
* float x,
* float y,
*/
void pgr_get_coordinates(
char *sql,
Coordinate_t **coordinates,
size_t *total_coordinates) {
clock_t start_t = clock();
const int tuple_limit = 1000000;
size_t total_tuples = 0;
Column_info_t info[3];
int i;
for (i = 0; i < 3; ++i) {
info[i].colNumber = -1;
info[i].type = 0;
info[i].strict = true;
info[i].eType = ANY_NUMERICAL;
}
info[0].name = "id";
info[1].name = "x";
info[2].name = "y";
info[0].eType = ANY_INTEGER;
info[0].strict = false;
void *SPIplan;
SPIplan = pgr_SPI_prepare(sql);
Portal SPIportal;
SPIportal = pgr_SPI_cursor_open(SPIplan);
bool moredata = true;
(*total_coordinates) = total_tuples;
int64_t default_id = 1;
while (moredata == true) {
SPI_cursor_fetch(SPIportal, true, tuple_limit);
if (total_tuples == 0)
pgr_fetch_column_info(info, 3);
size_t ntuples = SPI_processed;
total_tuples += ntuples;
if (ntuples > 0) {
if ((*coordinates) == NULL)
(*coordinates) = (Coordinate_t *)
palloc0(total_tuples * sizeof(Coordinate_t));
else
(*coordinates) = (Coordinate_t *)
repalloc((*coordinates),
total_tuples * sizeof(Coordinate_t));
if ((*coordinates) == NULL) {
elog(ERROR, "Out of memory");
}
SPITupleTable *tuptable = SPI_tuptable;
TupleDesc tupdesc = SPI_tuptable->tupdesc;
PGR_DBG("Processing %ld coordinates tupĺes", ntuples);
size_t t;
for (t = 0; t < ntuples; t++) {
HeapTuple tuple = tuptable->vals[t];
pgr_fetch_row(&tuple, &tupdesc, info,
&default_id,
&(*coordinates)[total_tuples - ntuples + t]);
}
SPI_freetuptable(tuptable);
} else {
moredata = false;
}
}
SPI_cursor_close(SPIportal);
if (total_tuples == 0) {
(*total_coordinates) = 0;
PGR_DBG("NO coordinates");
return;
}
(*total_coordinates) = total_tuples;
time_msg(" reading coordinates:", start_t, clock());
}