PGROUTING  3.2
floydWarshall.c File Reference
Include dependency graph for floydWarshall.c:

Go to the source code of this file.

Functions

PGDLLEXPORT Datum _pgr_floydwarshall (PG_FUNCTION_ARGS)
 
 PG_FUNCTION_INFO_V1 (_pgr_floydwarshall)
 
static void process (char *edges_sql, bool directed, Matrix_cell_t **result_tuples, size_t *result_count)
 

Function Documentation

◆ _pgr_floydwarshall()

PGDLLEXPORT Datum _pgr_floydwarshall ( PG_FUNCTION_ARGS  )

Definition at line 101 of file floydWarshall.c.

101  {
102  FuncCallContext *funcctx;
103  TupleDesc tuple_desc;
104 
105  /**************************************************************************/
106  /* */
107  Matrix_cell_t *result_tuples = NULL;
108  size_t result_count = 0;
109  /* */
110  /**************************************************************************/
111 
112  if (SRF_IS_FIRSTCALL()) {
113  MemoryContext oldcontext;
114  funcctx = SRF_FIRSTCALL_INIT();
115  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
116 
117 
118  /*********************************************************************/
119  /* */
120  // QUERY
121  // CREATE OR REPLACE FUNCTION pgr_floydWarshalll(
122  // edges_sql TEXT,
123  // directed BOOLEAN,
124  // OUT seq INTEGER,
125  // OUT from_vid bigint,
126  // OUT to_vid bigint,
127  // OUT cost float)
128 
129 
130  PGR_DBG("Calling process");
131  process(
132  text_to_cstring(PG_GETARG_TEXT_P(0)),
133  PG_GETARG_BOOL(1),
134  &result_tuples,
135  &result_count);
136 
137  /* */
138  /*********************************************************************/
139 
140 #if PGSQL_VERSION > 95
141  funcctx->max_calls = result_count;
142 #else
143  funcctx->max_calls = (uint32_t)result_count;
144 #endif
145  funcctx->user_fctx = result_tuples;
146  if (get_call_result_type(fcinfo, NULL, &tuple_desc)
147  != TYPEFUNC_COMPOSITE)
148  ereport(ERROR,
149  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
150  errmsg("function returning record called in context "
151  "that cannot accept type record")));
152 
153  funcctx->tuple_desc = tuple_desc;
154  MemoryContextSwitchTo(oldcontext);
155  }
156 
157  funcctx = SRF_PERCALL_SETUP();
158  tuple_desc = funcctx->tuple_desc;
159  result_tuples = (Matrix_cell_t*) funcctx->user_fctx;
160 
161  if (funcctx->call_cntr < funcctx->max_calls) {
162  HeapTuple tuple;
163  Datum result;
164  Datum *values;
165  bool* nulls;
166 
167  /*********************************************************************/
168  values = palloc(3 * sizeof(Datum));
169  nulls = palloc(3 * sizeof(bool));
170 
171  // postgres starts counting from 1
172  values[0] = Int64GetDatum(result_tuples[funcctx->call_cntr].from_vid);
173  nulls[0] = false;
174  values[1] = Int64GetDatum(result_tuples[funcctx->call_cntr].to_vid);
175  nulls[1] = false;
176  values[2] = Float8GetDatum(result_tuples[funcctx->call_cntr].cost);
177  nulls[2] = false;
178  /*********************************************************************/
179 
180  tuple = heap_form_tuple(tuple_desc, values, nulls);
181  result = HeapTupleGetDatum(tuple);
182  SRF_RETURN_NEXT(funcctx, result);
183  } else {
184  SRF_RETURN_DONE(funcctx);
185  }
186 }

References matrix_cell::cost, matrix_cell::from_vid, if(), PGR_DBG, process(), and matrix_cell::to_vid.

◆ PG_FUNCTION_INFO_V1()

PG_FUNCTION_INFO_V1 ( _pgr_floydwarshall  )

◆ process()

static void process ( char *  edges_sql,
bool  directed,
Matrix_cell_t **  result_tuples,
size_t *  result_count 
)
static

Definition at line 46 of file floydWarshall.c.

50  {
52 
53  PGR_DBG("Load data");
54  pgr_edge_t *edges = NULL;
55  size_t total_tuples = 0;
56  pgr_get_edges_no_id(edges_sql, &edges, &total_tuples);
57 
58  if (total_tuples == 0) {
59  PGR_DBG("No edges found");
60  (*result_count) = 0;
61  (*result_tuples) = NULL;
63  return;
64  }
65  PGR_DBG("Total %ld tuples in query:", total_tuples);
66 
67  clock_t start_t = clock();
68  PGR_DBG("Starting processing");
69  char *err_msg = NULL;
70  char *notice_msg = NULL;
71  char *log_msg = NULL;
73  edges,
74  total_tuples,
75  directed,
76  result_tuples,
77  result_count,
78  &log_msg,
79  &err_msg);
80  time_msg(" processing FloydWarshall", start_t, clock());
81 
82  if (err_msg && (*result_tuples)) {
83  free(*result_tuples);
84  (*result_tuples) = NULL;
85  (*result_count) = 0;
86  }
87 
88  pgr_global_report(log_msg, notice_msg, err_msg);
89 
90 
91  if (log_msg) pfree(log_msg);
92  if (notice_msg) pfree(notice_msg);
93  if (err_msg) pfree(err_msg);
94 
95  pfree(edges);
97 }

References do_pgr_floydWarshall(), PGR_DBG, pgr_get_edges_no_id(), pgr_global_report(), pgr_SPI_connect(), pgr_SPI_finish(), and time_msg().

Referenced by _pgr_floydwarshall().

pgr_edge_t
Definition: pgr_edge_t.h:37
pgr_SPI_connect
void pgr_SPI_connect(void)
Definition: postgres_connection.c:82
pgr_get_edges_no_id
void pgr_get_edges_no_id(char *edges_sql, pgr_edge_t **edges, size_t *total_edges)
edges_sql without id parameter
Definition: edges_input.c:733
pgr_SPI_finish
void pgr_SPI_finish(void)
Definition: postgres_connection.c:71
process
static void process(char *edges_sql, bool directed, Matrix_cell_t **result_tuples, size_t *result_count)
Definition: floydWarshall.c:46
PGR_DBG
#define PGR_DBG(...)
Definition: debug_macro.h:34
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
do_pgr_floydWarshall
void do_pgr_floydWarshall(pgr_edge_t *data_edges, size_t total_tuples, bool directedFlag, Matrix_cell_t **return_tuples, size_t *return_count, char **log_msg, char **err_msg)
Definition: floydWarshall_driver.cpp:42
matrix_cell
Definition: matrix_cell_t.h:37
pgr_global_report
void pgr_global_report(char *log, char *notice, char *err)
notice & error
Definition: e_report.c:93