PGROUTING  3.2
chinesePostman.c
Go to the documentation of this file.
1 /*PGR-GNU*****************************************************************
2 File: chinesePostman.c
3 
4 Generated with Template by:
5 Copyright (c) 2015 pgRouting developers
7 
8 Function's developer:
9 Copyright (c) 2018 Maoguang Wang
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 
32 #include <stdbool.h>
34 
35 
36 #include "c_common/debug_macro.h"
37 #include "c_common/e_report.h"
38 #include "c_common/time_msg.h"
40 #include "c_common/edges_input.h"
41 
43 
44 PGDLLEXPORT Datum _pgr_chinesepostman(PG_FUNCTION_ARGS);
46 
47 
48 /******************************************************************************/
49 static
50 void
52  char* edges_sql,
53  bool only_cost,
54  General_path_element_t **result_tuples,
55  size_t *result_count) {
57 
58  pgr_edge_t *edges = NULL;
59  size_t total_edges = 0;
60 
61  pgr_get_edges(edges_sql, &edges, &total_edges);
62 
63  if (total_edges == 0) {
65  return;
66  }
67 
68  clock_t start_t = clock();
69  char *log_msg = NULL;
70  char *notice_msg = NULL;
71  char *err_msg = NULL;
72 
74  edges, total_edges,
75  only_cost,
76 
77  result_tuples, result_count,
78 
79  &log_msg,
80  &notice_msg,
81  &err_msg);
82 
83  if (only_cost) {
84  time_msg(" processing pgr_chinesePostmanCost", start_t, clock());
85  } else {
86  time_msg(" processing pgr_chinesePostman", start_t, clock());
87  }
88 
89  if (edges) pfree(edges);
90 
91  if (err_msg && (*result_tuples)) {
92  pfree(*result_tuples);
93  (*result_tuples) = NULL;
94  (*result_count) = 0;
95  }
96 
97  pgr_global_report(log_msg, notice_msg, err_msg);
98 
99  if (log_msg) pfree(log_msg);
100  if (notice_msg) pfree(notice_msg);
101  if (err_msg) pfree(err_msg);
102 
103  pgr_SPI_finish();
104 }
105 
106 
107 PGDLLEXPORT Datum _pgr_chinesepostman(PG_FUNCTION_ARGS) {
108  FuncCallContext *funcctx;
109  TupleDesc tuple_desc;
110 
111  /**************************************************************************/
112  General_path_element_t *result_tuples = NULL;
113  size_t result_count = 0;
114  /**************************************************************************/
115 
116  if (SRF_IS_FIRSTCALL()) {
117  MemoryContext oldcontext;
118  funcctx = SRF_FIRSTCALL_INIT();
119  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
120 
121 
122  /**********************************************************************/
123 
124  process(
125  text_to_cstring(PG_GETARG_TEXT_P(0)),
126  PG_GETARG_BOOL(1),
127  &result_tuples,
128  &result_count);
129 
130  /**********************************************************************/
131 
132 
133 #if PGSQL_VERSION > 95
134  funcctx->max_calls = result_count;
135 #else
136  funcctx->max_calls = (uint32_t)result_count;
137 #endif
138  funcctx->user_fctx = result_tuples;
139  if (get_call_result_type(fcinfo, NULL, &tuple_desc)
140  != TYPEFUNC_COMPOSITE) {
141  ereport(ERROR,
142  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
143  errmsg("function returning record called in context "
144  "that cannot accept type record")));
145  }
146 
147  funcctx->tuple_desc = tuple_desc;
148  MemoryContextSwitchTo(oldcontext);
149  }
150 
151  funcctx = SRF_PERCALL_SETUP();
152  tuple_desc = funcctx->tuple_desc;
153  result_tuples = (General_path_element_t*) funcctx->user_fctx;
154 
155  if (funcctx->call_cntr < funcctx->max_calls) {
156  HeapTuple tuple;
157  Datum result;
158  Datum *values;
159  bool* nulls;
160  size_t call_cntr = funcctx->call_cntr;
161 
162  /**********************************************************************/
163  /* MODIFY AS NEEDED */
164  /*
165  ***********************************************************************/
166 
167  size_t numb = 5;
168  values =(Datum *)palloc(numb * sizeof(Datum));
169  nulls = palloc(numb * sizeof(bool));
170 
171 
172  size_t i;
173  for (i = 0; i < numb; ++i) {
174  nulls[i] = false;
175  }
176  values[0] = Int32GetDatum(funcctx->call_cntr + 1);
177  values[1] = Int64GetDatum(result_tuples[call_cntr].node);
178  values[2] = Int64GetDatum(result_tuples[call_cntr].edge);
179  values[3] = Float8GetDatum(result_tuples[call_cntr].cost);
180  values[4] = Float8GetDatum(result_tuples[call_cntr].agg_cost);
181  /**********************************************************************/
182 
183  tuple = heap_form_tuple(tuple_desc, values, nulls);
184  result = HeapTupleGetDatum(tuple);
185  SRF_RETURN_NEXT(funcctx, result);
186  } else {
187  SRF_RETURN_DONE(funcctx);
188  }
189 }
190 
time_msg.h
postgres_connection.h
pgr_edge_t
Definition: pgr_edge_t.h:37
pgr_SPI_connect
void pgr_SPI_connect(void)
Definition: postgres_connection.c:82
pgr_SPI_finish
void pgr_SPI_finish(void)
Definition: postgres_connection.c:71
e_report.h
chinesePostman_driver.h
edge
Definition: trsp.h:41
debug_macro.h
PG_FUNCTION_INFO_V1
PG_FUNCTION_INFO_V1(_pgr_chinesepostman)
process
static void process(char *edges_sql, bool only_cost, General_path_element_t **result_tuples, size_t *result_count)
Definition: chinesePostman.c:51
pgr_get_edges
void pgr_get_edges(char *edges_sql, pgr_edge_t **edges, size_t *total_edges)
basic edge_sql
Definition: edges_input.c:711
General_path_element_t
Definition: general_path_element_t.h:37
if
if(DOXYGEN_FOUND) configure_file($
Definition: doxygen/CMakeLists.txt:13
time_msg
void time_msg(char *msg, clock_t start_t, clock_t end_t)
Definition: time_msg.c:32
general_path_element_t.h
edges_input.h
_pgr_chinesepostman
PGDLLEXPORT Datum _pgr_chinesepostman(PG_FUNCTION_ARGS)
Definition: chinesePostman.c:107
pgr_global_report
void pgr_global_report(char *log, char *notice, char *err)
notice & error
Definition: e_report.c:93
do_pgr_directedChPP
void do_pgr_directedChPP(pgr_edge_t *data_edges, size_t total_edges, bool only_cost, General_path_element_t **return_tuples, size_t *return_count, char **log_msg, char **notice_msg, char **err_msg)
Definition: chinesePostman_driver.cpp:44