PGROUTING  3.2
sequentialVertexColoring.c File Reference

Connecting code with postgres. More...

#include <stdbool.h>
#include "c_common/postgres_connection.h"
#include "utils/array.h"
#include "c_common/debug_macro.h"
#include "c_common/e_report.h"
#include "c_common/time_msg.h"
#include "c_common/edges_input.h"
#include "c_common/arrays_input.h"
#include "drivers/coloring/sequentialVertexColoring_driver.h"
Include dependency graph for sequentialVertexColoring.c:

Go to the source code of this file.

Functions

PGDLLEXPORT Datum _pgr_sequentialvertexcoloring (PG_FUNCTION_ARGS)
 Helps in converting postgres variables to C variables, and returns the result. More...
 
 PG_FUNCTION_INFO_V1 (_pgr_sequentialvertexcoloring)
 
static void process (char *edges_sql, pgr_vertex_color_rt **result_tuples, size_t *result_count)
 Static function, loads the data from postgres to C types for further processing. More...
 

Detailed Description

Connecting code with postgres.

Definition in file sequentialVertexColoring.c.

Function Documentation

◆ _pgr_sequentialvertexcoloring()

PGDLLEXPORT Datum _pgr_sequentialvertexcoloring ( PG_FUNCTION_ARGS  )

Helps in converting postgres variables to C variables, and returns the result.

Definition at line 118 of file sequentialVertexColoring.c.

118  {
119  FuncCallContext *funcctx;
120  TupleDesc tuple_desc;
121 
122  /**********************************************************************/
123  pgr_vertex_color_rt *result_tuples = NULL;
124  size_t result_count = 0;
125  /**********************************************************************/
126 
127  if (SRF_IS_FIRSTCALL()) {
128  MemoryContext oldcontext;
129  funcctx = SRF_FIRSTCALL_INIT();
130  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
131 
132  /***********************************************************************
133  *
134  * pgr_sequentialVertexColoring(edges_sql TEXT);
135  *
136  **********************************************************************/
137 
138  process(
139  text_to_cstring(PG_GETARG_TEXT_P(0)),
140  &result_tuples,
141  &result_count);
142 
143  /**********************************************************************/
144 
145 
146 #if PGSQL_VERSION > 95
147  funcctx->max_calls = result_count;
148 #else
149  funcctx->max_calls = (uint32_t)result_count;
150 #endif
151  funcctx->user_fctx = result_tuples;
152  if (get_call_result_type(fcinfo, NULL, &tuple_desc)
153  != TYPEFUNC_COMPOSITE) {
154  ereport(ERROR,
155  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
156  errmsg("function returning record called in context "
157  "that cannot accept type record")));
158  }
159 
160  funcctx->tuple_desc = tuple_desc;
161  MemoryContextSwitchTo(oldcontext);
162  }
163 
164  funcctx = SRF_PERCALL_SETUP();
165  tuple_desc = funcctx->tuple_desc;
166  result_tuples = (pgr_vertex_color_rt*) funcctx->user_fctx;
167 
168  if (funcctx->call_cntr < funcctx->max_calls) {
169  HeapTuple tuple;
170  Datum result;
171  Datum *values;
172  bool* nulls;
173 
174  /***********************************************************************
175  *
176  * OUT node BIGINT,
177  * OUT color BIGINT,
178  *
179  **********************************************************************/
180 
181  size_t num = 3;
182  values = palloc(num * sizeof(Datum));
183  nulls = palloc(num * sizeof(bool));
184 
185 
186  size_t i;
187  for (i = 0; i < num; ++i) {
188  nulls[i] = false;
189  }
190 
191  values[0] = Int64GetDatum(result_tuples[funcctx->call_cntr].node);
192  values[1] = Int64GetDatum(result_tuples[funcctx->call_cntr].color);
193 
194  /**********************************************************************/
195 
196  tuple = heap_form_tuple(tuple_desc, values, nulls);
197  result = HeapTupleGetDatum(tuple);
198  SRF_RETURN_NEXT(funcctx, result);
199  } else {
200  SRF_RETURN_DONE(funcctx);
201  }
202 }

References pgr_vertex_color_rt::color, if(), pgr_vertex_color_rt::node, and process().

◆ PG_FUNCTION_INFO_V1()

PG_FUNCTION_INFO_V1 ( _pgr_sequentialvertexcoloring  )

◆ process()

static void process ( char *  edges_sql,
pgr_vertex_color_rt **  result_tuples,
size_t *  result_count 
)
static

Static function, loads the data from postgres to C types for further processing.

It first connects the C function to the SPI manager. Then converts the postgres array to C array and loads the edges belonging to the graph in C types. Then it calls the function do_pgr_sequentialVertexColoring defined in the sequentialVertexColoring_driver.h file for further processing. Finally, it frees the memory and disconnects the C function to the SPI manager.

Parameters
edges_sqlthe edges of the SQL query
result_tuplesthe rows in the result
result_countthe count of rows in the result
Returns
void

Definition at line 66 of file sequentialVertexColoring.c.

70  {
72 
73  (*result_tuples) = NULL;
74  (*result_count) = 0;
75 
76  pgr_edge_t *edges = NULL;
77  size_t total_edges = 0;
78 
79  pgr_get_edges(edges_sql, &edges, &total_edges);
80 
81  clock_t start_t = clock();
82  char *log_msg = NULL;
83  char *notice_msg = NULL;
84  char *err_msg = NULL;
86  edges, total_edges,
87 
88  result_tuples,
89  result_count,
90  &log_msg,
91  &notice_msg,
92  &err_msg);
93 
94  time_msg("processing pgr_sequentialVertexColoring", start_t, clock());
95 
96  if (err_msg && (*result_tuples)) {
97  pfree(*result_tuples);
98  (*result_tuples) = NULL;
99  (*result_count) = 0;
100  }
101 
102  pgr_global_report(log_msg, notice_msg, err_msg);
103 
104  if (log_msg) pfree(log_msg);
105  if (notice_msg) pfree(notice_msg);
106  if (err_msg) pfree(err_msg);
107  if (edges) pfree(edges);
108 
109  pgr_SPI_finish();
110 }

References do_pgr_sequentialVertexColoring(), pgr_get_edges(), pgr_global_report(), pgr_SPI_connect(), pgr_SPI_finish(), and time_msg().

Referenced by _pgr_sequentialvertexcoloring().

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
do_pgr_sequentialVertexColoring
void do_pgr_sequentialVertexColoring(pgr_edge_t *data_edges, size_t total_edges, pgr_vertex_color_rt **return_tuples, size_t *return_count, char **log_msg, char **notice_msg, char **err_msg)
Performs exception handling and converts the results to postgres.
Definition: sequentialVertexColoring_driver.cpp:93
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
if
if(DOXYGEN_FOUND) configure_file($
Definition: doxygen/CMakeLists.txt:13
process
static void process(char *edges_sql, pgr_vertex_color_rt **result_tuples, size_t *result_count)
Static function, loads the data from postgres to C types for further processing.
Definition: sequentialVertexColoring.c:66
time_msg
void time_msg(char *msg, clock_t start_t, clock_t end_t)
Definition: time_msg.c:32
pgr_global_report
void pgr_global_report(char *log, char *notice, char *err)
notice & error
Definition: e_report.c:93
pgr_vertex_color_rt
Definition: pgr_vertex_color_rt.h:36