PGROUTING  2.5
restrictions_input.c File Reference
Include dependency graph for restrictions_input.c:

Go to the source code of this file.

Functions

static void fetch_restriction (HeapTuple *tuple, TupleDesc *tupdesc, Column_info_t info[4], Restrict_t *restriction)
 
void pgr_get_restriction_data (char *restrictions_sql, Restrict_t **restrictions, size_t *total_restrictions)
 

Function Documentation

static void fetch_restriction ( HeapTuple *  tuple,
TupleDesc *  tupdesc,
Column_info_t  info[4],
Restrict_t restriction 
)
static

Definition at line 35 of file restrictions_input.c.

References if(), MAX_RULE_LENGTH, pgr_SPI_getBigInt(), and pgr_SPI_getFloat8().

Referenced by pgr_get_restriction_data().

39  {
40  restriction->target_id = pgr_SPI_getBigInt(tuple, tupdesc, info[0]);
41  restriction->to_cost = pgr_SPI_getFloat8(tuple, tupdesc, info[1]);
42  char *str = DatumGetCString(
43  SPI_getvalue(*tuple, *tupdesc, info[2].colNumber));
44 
45 // TODO(someone) because its text, no guarantee the text read is correct
46 // move this code to c++ to tokenize the integers.
47 
48  int i = 0;
49  for (i = 0; i < MAX_RULE_LENGTH; ++i) restriction->via[i] = -1;
50 
51  if (str != NULL) {
52  char *token = NULL;
53  int i = 0;
54 
55  token = (char *)strtok(str, " ,");
56 
57  while (token != NULL && i < MAX_RULE_LENGTH) {
58  restriction->via[i] = atoi(token);
59  i++;
60  token = (char *)strtok(NULL, " ,");
61  }
62  }
63 }
int64_t pgr_SPI_getBigInt(HeapTuple *tuple, TupleDesc *tupdesc, Column_info_t info)
if(DOXYGEN_FOUND) configure_file($
Definition: CMakeLists.txt:13
#define MAX_RULE_LENGTH
Definition: trsp.h:25
double pgr_SPI_getFloat8(HeapTuple *tuple, TupleDesc *tupdesc, Column_info_t info)

Here is the call graph for this function:

Here is the caller graph for this function:

void pgr_get_restriction_data ( char *  restrictions_sql,
Restrict_t **  restrictions,
size_t *  total_restrictions 
)

Definition at line 67 of file restrictions_input.c.

References ANY_INTEGER, ANY_INTEGER_ARRAY, ANY_NUMERICAL, Column_info_t::colNumber, Column_info_t::eType, fetch_restriction(), Column_info_t::name, PGR_DBG, pgr_fetch_column_info(), pgr_SPI_cursor_open(), pgr_SPI_prepare(), Column_info_t::strict, TEXT, time_msg(), and Column_info_t::type.

70  {
71  const int tuple_limit = 1000000;
72  clock_t start_t = clock();
73 
74  PGR_DBG("pgr_get_restriction_data");
75  PGR_DBG("%s", restrictions_sql);
76 
77  Column_info_t info[3];
78 
79  int i;
80  for (i = 0; i < 3; ++i) {
81  info[i].colNumber = -1;
82  info[i].type = 0;
83  info[i].strict = true;
84  info[i].eType = ANY_INTEGER;
85  }
86  info[0].name = strdup("target_id");
87  info[1].name = strdup("to_cost");
88  info[2].name = strdup("via_path");
89 
90  info[1].eType = ANY_NUMERICAL;
91  info[2].eType = TEXT;
92 
93 
94  size_t ntuples;
95  size_t total_tuples;
96 
97  void *SPIplan;
98  SPIplan = pgr_SPI_prepare(restrictions_sql);
99  Portal SPIportal;
100  SPIportal = pgr_SPI_cursor_open(SPIplan);
101 
102  bool moredata = true;
103  (*total_restrictions) = total_tuples = 0;
104 
105  /* on the first tuple get the column numbers */
106 
107  while (moredata == true) {
108  SPI_cursor_fetch(SPIportal, true, tuple_limit);
109  if (total_tuples == 0) {
110  pgr_fetch_column_info(info, 3);
111  }
112  ntuples = SPI_processed;
113  total_tuples += ntuples;
114  PGR_DBG("SPI_processed %ld", ntuples);
115  if (ntuples > 0) {
116  if ((*restrictions) == NULL)
117  (*restrictions) = (Restrict_t *)palloc0(
118  total_tuples * sizeof(Restrict_t));
119  else
120  (*restrictions) = (Restrict_t *)repalloc(
121  (*restrictions),
122  total_tuples * sizeof(Restrict_t));
123 
124  if ((*restrictions) == NULL) {
125  elog(ERROR, "Out of memory");
126  }
127 
128  size_t t;
129  SPITupleTable *tuptable = SPI_tuptable;
130  TupleDesc tupdesc = SPI_tuptable->tupdesc;
131  PGR_DBG("processing %ld", ntuples);
132  for (t = 0; t < ntuples; t++) {
133  HeapTuple tuple = tuptable->vals[t];
134  fetch_restriction(&tuple, &tupdesc, info,
135  &(*restrictions)[total_tuples - ntuples + t]);
136  }
137  SPI_freetuptable(tuptable);
138  } else {
139  moredata = false;
140  }
141  }
142 
143  SPI_cursor_close(SPIportal);
144 
145  if (total_tuples == 0) {
146  (*total_restrictions) = 0;
147  PGR_DBG("NO restrictions");
148  return;
149  }
150 
151  (*total_restrictions) = total_tuples;
152  PGR_DBG("Finish reading %ld data, %ld",
153  total_tuples,
154  (*total_restrictions));
155  clock_t end_t = clock();
156  time_msg(" reading Restrictions", start_t, end_t);
157 }
#define PGR_DBG(...)
Definition: debug_macro.h:34
uint64_t type
Definition: column_info_t.h:77
void time_msg(char *msg, clock_t start_t, clock_t end_t)
Definition: time_msg.c:32
void pgr_fetch_column_info(Column_info_t info[], int info_size)
Portal pgr_SPI_cursor_open(SPIPlanPtr SPIplan)
SPIPlanPtr pgr_SPI_prepare(char *sql)
expectType eType
Definition: column_info_t.h:80
static void fetch_restriction(HeapTuple *tuple, TupleDesc *tupdesc, Column_info_t info[4], Restrict_t *restriction)

Here is the call graph for this function: