PGROUTING  3.2
arrays_input.c File Reference
#include "c_common/arrays_input.h"
#include <assert.h>
#include <utils/lsyscache.h>
#include <catalog/pg_type.h>
#include "c_common/time_msg.h"
#include "c_common/debug_macro.h"
Include dependency graph for arrays_input.c:

Go to the source code of this file.

Functions

static int64_t * pgr_get_bigIntArr (ArrayType *v, size_t *arrlen, bool allow_empty)
 Function for array input. More...
 
int64_t * pgr_get_bigIntArray (size_t *arrlen, ArrayType *input)
 Enforces the input array to be NOT empty. More...
 
int64_t * pgr_get_bigIntArray_allowEmpty (size_t *arrlen, ArrayType *input)
 Allows the input array to be empty. More...
 

Function Documentation

◆ pgr_get_bigIntArr()

static int64_t* pgr_get_bigIntArr ( ArrayType *  v,
size_t *  arrlen,
bool  allow_empty 
)
static

Function for array input.

This function generates the array inputs according to their type received through ArrayType *v parameter and store them in c_array. It can be empty also if received allow_empty true. The cases of failure are:-

  1. When ndim is not equal to one dimension.
  2. When no element is found i.e. nitems is zero or negative.
  3. If the element type doesn't lie in switch cases, give the error of expected array of any integer type
  4. When size of c_array is out of range or memory.
  5. When null value is found in the array.

All these failures are represented as error through elog.

Parameters
[in]vThe type of element to be processed.
[out]arrlenThe length of the array (To be determined in this function).
[in]allow_emptyBool type parameter that tells us whether to consider empty array or not.
Precondition
The initial value of *arrlen should be zero.
Returns
The resultant array i.e. c_array.

Definition at line 54 of file arrays_input.c.

54  {
55  clock_t start_t = clock();
56  int64_t *c_array = NULL;
57 
58  Oid element_type = ARR_ELEMTYPE(v);
59  int *dim = ARR_DIMS(v);
60  int ndim = ARR_NDIM(v);
61  int nitems = ArrayGetNItems(ndim, dim);
62  Datum *elements;
63  bool *nulls;
64  int16 typlen;
65  bool typbyval;
66  char typalign;
67 
68  assert((*arrlen) == 0);
69 
70 
71  if (allow_empty && (ndim == 0 || nitems <= 0)) {
72  PGR_DBG("ndim %i nitems % i", ndim, nitems);
73  return (int64_t*)NULL;
74  }
75  /* the array is not empty*/
76 
77  if (ndim != 1) {
78  elog(ERROR, "One dimension expected");
79  return (int64_t*)NULL;
80  }
81 
82  if (nitems <= 0) {
83  elog(ERROR, "No elements found");
84  return (int64_t*)NULL;
85  }
86 
87  get_typlenbyvalalign(element_type,
88  &typlen, &typbyval, &typalign);
89 
90  /* validate input data type */
91  switch (element_type) {
92  case INT2OID:
93  case INT4OID:
94  case INT8OID:
95  break;
96  default:
97  elog(ERROR, "Expected array of ANY-INTEGER");
98  return (int64_t*)NULL;
99  break;
100  }
101 
102  deconstruct_array(v, element_type, typlen, typbyval,
103  typalign, &elements, &nulls,
104  &nitems);
105 
106  c_array = (int64_t *) palloc(sizeof(int64_t) * (size_t)nitems);
107  if (!c_array) {
108  elog(ERROR, "Out of memory!");
109  }
110 
111 
112  int i;
113  for (i = 0; i < nitems; i++) {
114  if (nulls[i]) {
115  pfree(c_array);
116  elog(ERROR, "NULL value found in Array!");
117  } else {
118  switch (element_type) {
119  case INT2OID:
120  c_array[i] = (int64_t) DatumGetInt16(elements[i]);
121  break;
122  case INT4OID:
123  c_array[i] = (int64_t) DatumGetInt32(elements[i]);
124  break;
125  case INT8OID:
126  c_array[i] = DatumGetInt64(elements[i]);
127  break;
128  }
129  }
130  }
131  (*arrlen) = (size_t)nitems;
132 
133  pfree(elements);
134  pfree(nulls);
135  PGR_DBG("Array size %ld", (*arrlen));
136  time_msg("reading Array", start_t, clock());
137  return c_array;
138 }

References PGR_DBG, and time_msg().

Referenced by pgr_get_bigIntArray(), and pgr_get_bigIntArray_allowEmpty().

◆ pgr_get_bigIntArray()

int64_t* pgr_get_bigIntArray ( size_t *  arrlen,
ArrayType *  input 
)

Enforces the input array to be NOT empty.

Parameters
[out]arrlenLength of the array
[in]inputInput type of the array
Returns
Returns the output of pgr_get_bitIntArray when allow_empty is set to false.

Definition at line 146 of file arrays_input.c.

146  {
147  return pgr_get_bigIntArr(input, arrlen, false);
148 }

References pgr_get_bigIntArr().

Referenced by compute_trsp(), pgr_SPI_getBigIntArr(), and process().

◆ pgr_get_bigIntArray_allowEmpty()

int64_t* pgr_get_bigIntArray_allowEmpty ( size_t *  arrlen,
ArrayType *  input 
)

Allows the input array to be empty.

Parameters
[out]arrlenLength of the array
[in]inputInput type of the array
Returns
Returns the output of pgr_get_bitIntArray when allow_empty is set to true.

Definition at line 156 of file arrays_input.c.

156  {
157  return pgr_get_bigIntArr(input, arrlen, true);
158 }

References pgr_get_bigIntArr().

Referenced by process().

pgr_get_bigIntArr
static int64_t * pgr_get_bigIntArr(ArrayType *v, size_t *arrlen, bool allow_empty)
Function for array input.
Definition: arrays_input.c:54
PGR_DBG
#define PGR_DBG(...)
Definition: debug_macro.h:34
time_msg
void time_msg(char *msg, clock_t start_t, clock_t end_t)
Definition: time_msg.c:32