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
one_to_one_dijkstra.c
Go to the documentation of this file.
1 /*PGR-GNU*****************************************************************
2 File: one_to_one_dijkstra.c
3 
4 Generated with Template by:
5 Copyright (c) 2015 pgRouting developers
6 Mail: project@pgrouting.org
7 
8 Function's developer:
9 Copyright (c) 2015 Celia Virginia Vergara Castillo
10 Mail: vicky_vergara@hotmail.com
11 
12 ------
13 
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation; either version 2 of the License, or
17 (at your option) any later version.
18 
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
23 
24 You should have received a copy of the GNU General Public License
25 along with this program; if not, write to the Free Software
26 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 
28 ********************************************************************PGR-GNU*/
29 
30 #include "postgres.h"
31 #include "executor/spi.h"
32 #include "funcapi.h"
33 #include "utils/array.h"
34 #include "catalog/pg_type.h"
35 #if PGSQL_VERSION > 92
36 #include "access/htup_details.h"
37 #endif
38 
39 
40 #include "fmgr.h"
41 #include "./../../common/src/debug_macro.h"
42 #include "./../../common/src/time_msg.h"
43 #include "./../../common/src/pgr_types.h"
44 #include "./../../common/src/postgres_connection.h"
45 #include "./../../common/src/edges_input.h"
47 
49 
50 PGDLLEXPORT Datum one_to_one_dijkstra(PG_FUNCTION_ARGS);
51 
52 /******************************************************************************/
53 /* MODIFY AS NEEDED */
54 static
55 void
57  char* edges_sql,
58  int64_t start_vid,
59  int64_t end_vid,
60  bool directed,
61  bool only_cost,
62  General_path_element_t **result_tuples,
63  size_t *result_count) {
65 
66  PGR_DBG("Load data");
67  pgr_edge_t *edges = NULL;
68 
69  if (start_vid == end_vid) {
70  (*result_count) = 0;
71  (*result_tuples) = NULL;
73  return;
74  }
75 
76  size_t total_tuples = 0;
77  pgr_get_edges(edges_sql, &edges, &total_tuples);
78 
79  if (total_tuples == 0) {
80  PGR_DBG("No edges found");
81  (*result_count) = 0;
82  (*result_tuples) = NULL;
84  return;
85  }
86  PGR_DBG("Total %ld tuples in query:", total_tuples);
87 
88  PGR_DBG("Starting processing");
89  clock_t start_t = clock();
90  char *err_msg = NULL;
92  edges,
93  total_tuples,
94  start_vid,
95  end_vid,
96  directed,
97  only_cost,
98  result_tuples,
99  result_count,
100  &err_msg);
101 
102  time_msg(" processing Dijkstra one to one", start_t, clock());
103  PGR_DBG("Returning %ld tuples\n", *result_count);
104  PGR_DBG("Returned message = %s\n", err_msg);
105 
106  free(err_msg);
107  pfree(edges);
108  pgr_SPI_finish();
109 }
110 /* */
111 /******************************************************************************/
112 
114 PGDLLEXPORT Datum
115 one_to_one_dijkstra(PG_FUNCTION_ARGS) {
116  FuncCallContext *funcctx;
117  uint32_t call_cntr;
118  uint32_t max_calls;
119  TupleDesc tuple_desc;
120 
121  /**************************************************************************/
122  /* MODIFY AS NEEDED */
123  /* */
124  General_path_element_t *result_tuples = 0;
125  size_t result_count = 0;
126  /* */
127  /**************************************************************************/
128 
129  if (SRF_IS_FIRSTCALL()) {
130  MemoryContext oldcontext;
131  funcctx = SRF_FIRSTCALL_INIT();
132  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
133 
134 
135  /**********************************************************************/
136  /* MODIFY AS NEEDED */
137  // CREATE OR REPLACE FUNCTION pgr_dijkstra(
138  // sql text, start_vids BIGINT,
139  // end_vid BIGINT,
140  // directed BOOLEAN default true,
141 
142  PGR_DBG("Calling process");
143  process(
144  pgr_text2char(PG_GETARG_TEXT_P(0)),
145  PG_GETARG_INT64(1),
146  PG_GETARG_INT64(2),
147  PG_GETARG_BOOL(3),
148  PG_GETARG_BOOL(4),
149  &result_tuples,
150  &result_count);
151 
152  /* */
153  /**********************************************************************/
154 
155  funcctx->max_calls = (uint32_t)result_count;
156  funcctx->user_fctx = result_tuples;
157  if (get_call_result_type(fcinfo, NULL, &tuple_desc)
158  != TYPEFUNC_COMPOSITE) {
159  ereport(ERROR,
160  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
161  errmsg("function returning record called in context "
162  "that cannot accept type record")));
163  }
164 
165  funcctx->tuple_desc = tuple_desc;
166  MemoryContextSwitchTo(oldcontext);
167  }
168 
169  funcctx = SRF_PERCALL_SETUP();
170  call_cntr = funcctx->call_cntr;
171  max_calls = funcctx->max_calls;
172  tuple_desc = funcctx->tuple_desc;
173  result_tuples = (General_path_element_t*) funcctx->user_fctx;
174 
175  if (call_cntr < max_calls) {
176  HeapTuple tuple;
177  Datum result;
178  Datum *values;
179  bool* nulls;
180 
181  /**********************************************************************/
182  /* MODIFY AS NEEDED */
183  // OUT seq INTEGER,
184  // OUT path_seq INTEGER,
185  // OUT node BIGINT,
186  // OUT edge BIGINT,
187  // OUT cost FLOAT,
188  // OUT agg_cost FLOAT
189 
190  values = palloc(6 * sizeof(Datum));
191  nulls = palloc(6 * sizeof(bool));
192 
193 
194  size_t i;
195  for (i = 0; i < 6; ++i) {
196  nulls[i] = false;
197  }
198 
199  // postgres starts counting from 1
200  values[0] = Int32GetDatum(call_cntr + 1);
201  values[1] = Int32GetDatum(result_tuples[call_cntr].seq);
202  values[2] = Int64GetDatum(result_tuples[call_cntr].node);
203  values[3] = Int64GetDatum(result_tuples[call_cntr].edge);
204  values[4] = Float8GetDatum(result_tuples[call_cntr].cost);
205  values[5] = Float8GetDatum(result_tuples[call_cntr].agg_cost);
206  /**********************************************************************/
207 
208  tuple = heap_form_tuple(tuple_desc, values, nulls);
209  result = HeapTupleGetDatum(tuple);
210  SRF_RETURN_NEXT(funcctx, result);
211  } else {
212  // cleanup
213  if (result_tuples) free(result_tuples);
214 
215  SRF_RETURN_DONE(funcctx);
216  }
217 }
218 
void do_pgr_one_to_one_dijkstra(pgr_edge_t *data_edges, size_t total_tuples, int64_t start_vid, int64_t end_vid, bool directed, bool only_cost, General_path_element_t **return_tuples, size_t *return_count, char **err_msg)
PG_FUNCTION_INFO_V1(one_to_one_dijkstra)
#define PGR_DBG(...)
Definition: debug_macro.h:33
void pgr_get_edges(char *edges_sql, pgr_edge_t **edges, size_t *total_edges)
basic edge_sql
Definition: edges_input.c:535
PG_MODULE_MAGIC
void time_msg(char *msg, clock_t start_t, clock_t end_t)
Definition: time_msg.c:31
PGDLLEXPORT Datum one_to_one_dijkstra(PG_FUNCTION_ARGS)
void pgr_SPI_finish(void)
edge_astar_t * edges
Definition: BDATester.cpp:46
static void process(char *edges_sql, int64_t start_vid, int64_t end_vid, bool directed, bool only_cost, General_path_element_t **result_tuples, size_t *result_count)
void pgr_SPI_connect(void)
char * err_msg
Definition: BDATester.cpp:50
char * pgr_text2char(text *in)