PGROUTING  2.5
contractGraph.c File Reference
#include "c_common/postgres_connection.h"
#include "utils/array.h"
#include "catalog/pg_type.h"
#include "utils/lsyscache.h"
#include "c_common/debug_macro.h"
#include "c_common/e_report.h"
#include "c_common/time_msg.h"
#include "c_types/contracted_rt.h"
#include "c_common/edges_input.h"
#include "c_common/arrays_input.h"
#include "drivers/contraction/contractGraph_driver.h"
Include dependency graph for contractGraph.c:

Go to the source code of this file.

Macros

#define INT8ARRAYOID   1016
 

Functions

PGDLLEXPORT Datum contractGraph (PG_FUNCTION_ARGS)
 
 PG_FUNCTION_INFO_V1 (contractGraph)
 
static void process (char *edges_sql, ArrayType *order, int num_cycles, ArrayType *forbidden, bool directed, contracted_rt **result_tuples, size_t *result_count)
 

Macro Definition Documentation

#define INT8ARRAYOID   1016

Definition at line 36 of file contractGraph.c.

Referenced by contractGraph().

Function Documentation

PGDLLEXPORT Datum contractGraph ( PG_FUNCTION_ARGS  )

Definition at line 130 of file contractGraph.c.

References contracted_rt::contracted_vertices, if(), INT8ARRAYOID, PGR_DBG, and process().

130  {
131  FuncCallContext *funcctx;
132  TupleDesc tuple_desc;
133 
134  /**********************************************************************/
135  contracted_rt *result_tuples = NULL;
136  size_t result_count = 0;
137  /**********************************************************************/
138 
139  if (SRF_IS_FIRSTCALL()) {
140  MemoryContext oldcontext;
141  funcctx = SRF_FIRSTCALL_INIT();
142  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
143  /**********************************************************************/
144  /*
145  edges_sql TEXT,
146  contraction_order BIGINT[],
147  max_cycles integer DEFAULT 1,
148  forbidden_vertices BIGINT[] DEFAULT ARRAY[]::BIGINT[],
149  directed BOOLEAN DEFAULT true,
150 
151  **********************************************************************/
152 
153  process(
154  text_to_cstring(PG_GETARG_TEXT_P(0)),
155  PG_GETARG_ARRAYTYPE_P(1),
156  PG_GETARG_INT32(2),
157  PG_GETARG_ARRAYTYPE_P(3),
158  PG_GETARG_BOOL(4),
159  &result_tuples,
160  &result_count);
161 
162 
163  /**********************************************************************/
164 #if PGSQL_VERSION > 95
165  funcctx->max_calls = result_count;
166 #else
167  funcctx->max_calls = (uint32_t)result_count;
168 #endif
169  funcctx->user_fctx = result_tuples;
170  if (get_call_result_type(fcinfo, NULL, &tuple_desc)
171  != TYPEFUNC_COMPOSITE)
172  ereport(ERROR,
173  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
174  errmsg("function returning record called in context "
175  "that cannot accept type record")));
176  funcctx->tuple_desc = tuple_desc;
177  MemoryContextSwitchTo(oldcontext);
178  }
179 
180  funcctx = SRF_PERCALL_SETUP();
181  tuple_desc = funcctx->tuple_desc;
182  result_tuples = (contracted_rt*) funcctx->user_fctx;
183 
184  if (funcctx->call_cntr < funcctx->max_calls) {
185  HeapTuple tuple;
186  Datum result;
187  Datum *values;
188  bool *nulls;
189  int16 typlen;
190  size_t call_cntr = funcctx->call_cntr;
191 
192  /**********************************************************************/
193  size_t numb = 7;
194  values =(Datum *)palloc(numb * sizeof(Datum));
195  nulls = palloc(numb * sizeof(bool));
196  size_t i;
197  for (i = 0; i < numb; ++i) {
198  nulls[i] = false;
199  }
200 
201  size_t contracted_vertices_size =
202  (size_t)result_tuples[call_cntr].contracted_vertices_size;
203 
204  Datum* contracted_vertices_array;
205  contracted_vertices_array = (Datum*) palloc(sizeof(Datum) *
206  (size_t)contracted_vertices_size);
207 
208  for (i = 0; i < contracted_vertices_size; ++i) {
209  PGR_DBG("Storing contracted vertex %ld",
210  result_tuples[call_cntr].contracted_vertices[i]);
211  contracted_vertices_array[i] =
212  Int64GetDatum(result_tuples[call_cntr].contracted_vertices[i]);
213  }
214 
215  bool typbyval;
216  char typalign;
217  get_typlenbyvalalign(INT8OID, &typlen, &typbyval, &typalign);
218  ArrayType* arrayType;
219  /*
220  * https://doxygen.postgresql.org/arrayfuncs_8c.html
221 
222  ArrayType* construct_array(
223  Datum* elems,
224  int nelems,
225  Oid elmtype, int elmlen, bool elmbyval, char elmalign
226  )
227  */
228  arrayType = construct_array(
229  contracted_vertices_array,
230  (int)contracted_vertices_size,
231  INT8OID, typlen, typbyval, typalign);
232  /*
233  void TupleDescInitEntry(
234  TupleDesc desc,
235  AttrNumber attributeNumber,
236  const char * attributeName,
237  Oid oidtypeid,
238  int32 typmod,
239  int attdim
240  )
241  */
242  TupleDescInitEntry(tuple_desc, (AttrNumber) 4, "contracted_vertices",
243  INT8ARRAYOID, -1, 0);
244 
245  values[0] = Int32GetDatum(call_cntr + 1);
246  values[1] = CStringGetTextDatum(result_tuples[call_cntr].type);
247  values[2] = Int64GetDatum(result_tuples[call_cntr].id);
248  values[3] = PointerGetDatum(arrayType);
249  values[4] = Int64GetDatum(result_tuples[call_cntr].source);
250  values[5] = Int64GetDatum(result_tuples[call_cntr].target);
251  values[6] = Float8GetDatum(result_tuples[call_cntr].cost);
252 
253  /*********************************************************************/
254  tuple = heap_form_tuple(tuple_desc, values, nulls);
255  result = HeapTupleGetDatum(tuple);
256 
257  /*
258  * cleaning up the contracted vertices array
259  */
260  if (result_tuples[funcctx->call_cntr].contracted_vertices) {
261  pfree(result_tuples[funcctx->call_cntr].contracted_vertices);
262  }
263  SRF_RETURN_NEXT(funcctx, result);
264  } else {
265  SRF_RETURN_DONE(funcctx);
266  }
267 }
#define PGR_DBG(...)
Definition: debug_macro.h:34
static void process(char *edges_sql, ArrayType *order, int num_cycles, ArrayType *forbidden, bool directed, contracted_rt **result_tuples, size_t *result_count)
Definition: contractGraph.c:53
int64_t * contracted_vertices
Definition: contracted_rt.h:71
if(DOXYGEN_FOUND) configure_file($
Definition: CMakeLists.txt:13
#define INT8ARRAYOID
Definition: contractGraph.c:36

Here is the call graph for this function:

PG_FUNCTION_INFO_V1 ( contractGraph  )
static void process ( char *  edges_sql,
ArrayType *  order,
int  num_cycles,
ArrayType *  forbidden,
bool  directed,
contracted_rt **  result_tuples,
size_t *  result_count 
)
static

Definition at line 53 of file contractGraph.c.

References do_pgr_contractGraph(), edges, PGR_DBG, pgr_get_bigIntArray(), pgr_get_bigIntArray_allowEmpty(), pgr_get_edges(), pgr_global_report(), pgr_SPI_connect(), pgr_SPI_finish(), and time_msg().

Referenced by contractGraph().

60  {
61  /*
62  * nothing to do
63  */
64  if (num_cycles < 1) return;
65 
67 
68  size_t size_forbidden_vertices = 0;
69  int64_t* forbidden_vertices =
71  &size_forbidden_vertices,
72  forbidden);
73  PGR_DBG("size_forbidden_vertices %ld", size_forbidden_vertices);
74 
75  size_t size_contraction_order = 0;
76  int64_t* contraction_order =
78  &size_contraction_order,
79  order);
80  PGR_DBG("size_contraction_order %ld ", size_contraction_order);
81 
82 
83  size_t total_edges = 0;
84  pgr_edge_t* edges = NULL;
85  pgr_get_edges(edges_sql, &edges, &total_edges);
86  if (total_edges == 0) {
87  if (forbidden_vertices) pfree(forbidden_vertices);
88  if (contraction_order) pfree(contraction_order);
90  return;
91  }
92 
93  PGR_DBG("Starting timer");
94  clock_t start_t = clock();
95  char* log_msg = NULL;
96  char* notice_msg = NULL;
97  char* err_msg = NULL;
99  edges, total_edges,
100  forbidden_vertices, size_forbidden_vertices,
101  contraction_order, size_contraction_order,
102  num_cycles,
103  directed,
104  result_tuples, result_count,
105  &log_msg,
106  &notice_msg,
107  &err_msg);
108 
109  time_msg("processing pgr_contraction()", start_t, clock());
110 
111 
112  if (err_msg && (*result_tuples)) {
113  pfree(*result_tuples);
114  (*result_tuples) = NULL;
115  (*result_count) = 0;
116  }
117 
118  pgr_global_report(log_msg, notice_msg, err_msg);
119 
120  if (log_msg) pfree(log_msg);
121  if (notice_msg) pfree(notice_msg);
122  if (err_msg) pfree(err_msg);
123  if (edges) pfree(edges);
124  if (forbidden_vertices) pfree(forbidden_vertices);
125  if (contraction_order) pfree(contraction_order);
126  pgr_SPI_finish();
127 }
static edge_t edges[22573]
#define PGR_DBG(...)
Definition: debug_macro.h:34
int64_t * pgr_get_bigIntArray_allowEmpty(size_t *arrlen, ArrayType *input)
Allows the input array to be empty.
Definition: arrays_input.c:130
void pgr_get_edges(char *edges_sql, pgr_edge_t **edges, size_t *total_edges)
basic edge_sql
Definition: edges_input.c:540
int64_t * pgr_get_bigIntArray(size_t *arrlen, ArrayType *input)
enforces the input array to be NOT empty
Definition: arrays_input.c:124
void time_msg(char *msg, clock_t start_t, clock_t end_t)
Definition: time_msg.c:32
void pgr_global_report(char *log, char *notice, char *err)
notice & error
Definition: e_report.c:93
void pgr_SPI_finish(void)
void pgr_SPI_connect(void)
void do_pgr_contractGraph(pgr_edge_t *data_edges, size_t total_edges, int64_t *forbidden_vertices, size_t size_forbidden_vertices, int64_t *contraction_order, size_t size_contraction_order, int64_t max_cycles, bool directed, contracted_rt **return_tuples, size_t *return_count, char **log_msg, char **notice_msg, char **err_msg)

Here is the call graph for this function:

Here is the caller graph for this function: