29 #include "utils/array.h"
52 int initial_solution_id,
54 size_t *result_count) {
57 (errcode(ERRCODE_INTERNAL_ERROR),
58 errmsg(
"Illegal value in parameter: factor"),
59 errhint(
"Value found: %f <= 0", factor)));
61 (*result_tuples) = NULL;
67 (errcode(ERRCODE_INTERNAL_ERROR),
68 errmsg(
"Illegal value in parameter: max_cycles"),
69 errhint(
"Negative value found: max_cycles: %d ", max_cycles)));
71 (*result_tuples) = NULL;
75 if (initial_solution_id <= 0 || initial_solution_id > 6) {
76 elog(ERROR,
"Illegal value in parameter: initial_sol");
78 (*result_tuples) = NULL;
86 size_t total_pd_orders = 0;
88 &pd_orders_arr, &total_pd_orders);
92 size_t total_vehicles = 0;
94 &vehicles_arr, &total_vehicles,
96 PGR_DBG(
"total vehicles %ld", total_vehicles);
99 for (
size_t i = 0; i < total_pd_orders; i++) {
100 PGR_DBG(
"%ld %f pick %f %f %ld - "
101 "%ld %ld %ld deliver %f %f %ld - %ld %ld %ld ",
124 for (
size_t i = 0; i < total_vehicles; i++) {
125 PGR_DBG(
"%ld %f %f , start %f %f %ld %ld %ld "
126 "end %f %f %ld %ld %ld number %ld ",
128 vehicles_arr[i].capacity,
129 vehicles_arr[i].speed,
131 vehicles_arr[i].start_x,
132 vehicles_arr[i].start_y,
133 vehicles_arr[i].start_open_t,
134 vehicles_arr[i].start_close_t,
135 vehicles_arr[i].start_service_t,
137 vehicles_arr[i].end_x,
138 vehicles_arr[i].end_y,
139 vehicles_arr[i].end_open_t,
140 vehicles_arr[i].end_close_t,
141 vehicles_arr[i].end_service_t,
143 vehicles_arr[i].cant_v);
147 if (total_pd_orders == 0 || total_vehicles == 0) {
149 (*result_tuples) = NULL;
153 PGR_DBG(
"Total %ld orders in query:", total_pd_orders);
155 PGR_DBG(
"Starting processing");
156 clock_t start_t = clock();
157 char *log_msg = NULL;
158 char *notice_msg = NULL;
159 char *err_msg = NULL;
161 pd_orders_arr, total_pd_orders,
162 vehicles_arr, total_vehicles,
174 time_msg(
"_pgr_pickDeliverEuclidean", start_t, clock());
176 if (err_msg && (*result_tuples)) {
177 pfree(*result_tuples);
179 (*result_tuples) = NULL;
184 if (log_msg) pfree(log_msg);
185 if (notice_msg) pfree(notice_msg);
186 if (err_msg) pfree(err_msg);
187 if (pd_orders_arr) pfree(pd_orders_arr);
188 if (vehicles_arr) pfree(vehicles_arr);
197 FuncCallContext *funcctx;
198 TupleDesc tuple_desc;
204 size_t result_count = 0;
208 if (SRF_IS_FIRSTCALL()) {
209 MemoryContext oldcontext;
210 funcctx = SRF_FIRSTCALL_INIT();
211 oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
224 text_to_cstring(PG_GETARG_TEXT_P(0)),
225 text_to_cstring(PG_GETARG_TEXT_P(1)),
235 funcctx->max_calls = result_count;
236 funcctx->user_fctx = result_tuples;
237 if (get_call_result_type(fcinfo, NULL, &tuple_desc)
238 != TYPEFUNC_COMPOSITE) {
240 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
241 errmsg(
"function returning record called in context "
242 "that cannot accept type record")));
245 funcctx->tuple_desc = tuple_desc;
246 MemoryContextSwitchTo(oldcontext);
249 funcctx = SRF_PERCALL_SETUP();
250 tuple_desc = funcctx->tuple_desc;
253 if (funcctx->call_cntr < funcctx->max_calls) {
258 size_t call_cntr = funcctx->call_cntr;
274 values = palloc(numb *
sizeof(Datum));
275 nulls = palloc(numb *
sizeof(
bool));
278 for (i = 0; i < numb; ++i) {
284 values[0] = Int32GetDatum(funcctx->call_cntr + 1);
285 values[1] = Int32GetDatum(result_tuples[call_cntr].vehicle_seq);
286 values[2] = Int64GetDatum(result_tuples[call_cntr].vehicle_id);
287 values[3] = Int32GetDatum(result_tuples[call_cntr].stop_seq);
288 values[4] = Int32GetDatum(result_tuples[call_cntr].stop_type + 1);
289 values[5] = Int64GetDatum(result_tuples[call_cntr].order_id);
290 values[6] = Int64GetDatum(result_tuples[call_cntr].cargo);
291 values[7] = Int64GetDatum(result_tuples[call_cntr].travelTime);
292 values[8] = Int64GetDatum(result_tuples[call_cntr].arrivalTime);
293 values[9] = Int64GetDatum(result_tuples[call_cntr].waitDuration);
294 values[10] = Int64GetDatum(result_tuples[call_cntr].serviceDuration);
295 values[11] = Int64GetDatum(result_tuples[call_cntr].departureTime);
299 tuple = heap_form_tuple(tuple_desc, values, nulls);
300 result = HeapTupleGetDatum(tuple);
301 SRF_RETURN_NEXT(funcctx, result);
303 SRF_RETURN_DONE(funcctx);