-
-
Notifications
You must be signed in to change notification settings - Fork 395
Expand file tree
/
Copy pathbinaryBreadthFirstSearch.c
More file actions
177 lines (141 loc) · 5.37 KB
/
binaryBreadthFirstSearch.c
File metadata and controls
177 lines (141 loc) · 5.37 KB
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/*PGR-GNU*****************************************************************
File: binaryBreadthFirstSearch.c
Generated with Template by:
Copyright (c) 2015-2026 pgRouting developers
Mail: project@pgrouting.org
Function's developer:
Copyright (c) 2019 Gudesa Venkata Sai Akhil
Mail: gvs.akhil1997@gmail.com
------
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 <stdbool.h>
#include "c_common/postgres_connection.h"
#include "c_types/path_rt.h"
#include "c_common/debug_macro.h"
#include "c_common/e_report.h"
#include "c_common/time_msg.h"
#include "drivers/breadthFirstSearch/binaryBreadthFirstSearch_driver.h"
PGDLLEXPORT Datum _pgr_binarybreadthfirstsearch(PG_FUNCTION_ARGS);
PG_FUNCTION_INFO_V1(_pgr_binarybreadthfirstsearch);
static void
process(
char *edges_sql,
char *combinations_sql,
ArrayType *starts,
ArrayType *ends,
bool directed,
Path_rt **result_tuples,
size_t *result_count) {
pgr_SPI_connect();
char* log_msg = NULL;
char* notice_msg = NULL;
char* err_msg = NULL;
(*result_tuples) = NULL;
(*result_count) = 0;
clock_t start_t = clock();
pgr_do_binaryBreadthFirstSearch(
edges_sql,
combinations_sql,
starts, ends,
directed,
result_tuples,
result_count,
&log_msg,
¬ice_msg,
&err_msg);
time_msg(" processing pgr_binaryBreadthFirstSearch", start_t, clock());
if (err_msg && (*result_tuples)) {
pfree(*result_tuples);
(*result_tuples) = NULL;
(*result_count) = 0;
}
pgr_global_report(&log_msg, ¬ice_msg, &err_msg);
pgr_SPI_finish();
}
PGDLLEXPORT Datum _pgr_binarybreadthfirstsearch(PG_FUNCTION_ARGS) {
FuncCallContext *funcctx = NULL;
TupleDesc tuple_desc = NULL;
Path_rt *result_tuples = NULL;
size_t result_count = 0;
if (SRF_IS_FIRSTCALL()) {
MemoryContext oldcontext = NULL;
funcctx = SRF_FIRSTCALL_INIT();
oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
if (PG_NARGS() == 4) {
/*
* many to many
*/
process(
text_to_cstring(PG_GETARG_TEXT_P(0)),
NULL,
PG_GETARG_ARRAYTYPE_P(1),
PG_GETARG_ARRAYTYPE_P(2),
PG_GETARG_BOOL(3),
&result_tuples,
&result_count);
} else if (PG_NARGS() == 3) {
/*
* combinations
*/
process(
text_to_cstring(PG_GETARG_TEXT_P(0)),
text_to_cstring(PG_GETARG_TEXT_P(1)),
NULL,
NULL,
PG_GETARG_BOOL(2),
&result_tuples,
&result_count);
}
funcctx->max_calls = result_count;
funcctx->user_fctx = result_tuples;
if (get_call_result_type(fcinfo, NULL, &tuple_desc) != TYPEFUNC_COMPOSITE) {
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("function returning record called in context "
"that cannot accept type record")));
}
funcctx->tuple_desc = tuple_desc;
MemoryContextSwitchTo(oldcontext);
}
funcctx = SRF_PERCALL_SETUP();
tuple_desc = funcctx->tuple_desc;
result_tuples = (Path_rt *)funcctx->user_fctx;
if (funcctx->call_cntr < funcctx->max_calls) {
HeapTuple tuple = NULL;
Datum result = 0;
Datum *values = NULL;
bool *nulls = NULL;
size_t numb = 8;
values = palloc(numb * sizeof(Datum));
nulls = palloc(numb * sizeof(bool));
for (size_t i = 0; i < numb; ++i) {
nulls[i] = false;
}
int64_t seq = funcctx->call_cntr == 0? 1 : result_tuples[funcctx->call_cntr - 1].start_id;
values[0] = Int32GetDatum((int32_t)funcctx->call_cntr + 1);
values[1] = Int32GetDatum((int32_t)seq);
values[2] = Int64GetDatum(result_tuples[funcctx->call_cntr].start_id);
values[3] = Int64GetDatum(result_tuples[funcctx->call_cntr].end_id);
values[4] = Int64GetDatum(result_tuples[funcctx->call_cntr].node);
values[5] = Int64GetDatum(result_tuples[funcctx->call_cntr].edge);
values[6] = Float8GetDatum(result_tuples[funcctx->call_cntr].cost);
values[7] = Float8GetDatum(result_tuples[funcctx->call_cntr].agg_cost);
result_tuples[funcctx->call_cntr].start_id = result_tuples[funcctx->call_cntr].edge < 0? 1 : seq + 1;
tuple = heap_form_tuple(tuple_desc, values, nulls);
result = HeapTupleGetDatum(tuple);
SRF_RETURN_NEXT(funcctx, result);
} else {
SRF_RETURN_DONE(funcctx);
}
}