PGROUTING  2.4
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
arrays_input.c
Go to the documentation of this file.
1 /*PGR-GNU*****************************************************************
2 File: arrays_input.c
3 
4 Copyright (c) 2015 Celia Virginia Vergara Castillo
5 vicky_vergara@hotmail.com
6 
7 ------
8 
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
13 
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18 
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 
23 ********************************************************************PGR-GNU*/
24 
25 #include "./postgres_connection.h"
26 
27 #include "utils/lsyscache.h"
28 #include "catalog/pg_type.h"
29 #include "utils/array.h"
30 
31 
32 #include "./pgr_types.h"
33 #include "./time_msg.h"
34 #include "./debug_macro.h"
35 #include "./arrays_input.h"
36 #include <assert.h>
37 
38 static
39 int64_t*
40 pgr_get_bigIntArr(ArrayType *v, size_t *arrlen, bool allow_empty) {
41  clock_t start_t = clock();
42  int64_t *c_array = NULL;;
43 
44  Oid element_type = ARR_ELEMTYPE(v);
45  int *dim = ARR_DIMS(v);
46  int ndim = ARR_NDIM(v);
47  int nitems = ArrayGetNItems(ndim, dim);
48  Datum *elements;
49  bool *nulls;
50  int16 typlen;
51  bool typbyval;
52  char typalign;
53 
54  assert((*arrlen) == 0);
55 
56 
57  if (allow_empty && (ndim == 0 || nitems <= 0)) {
58  PGR_DBG("ndim %i nitems % i", ndim, nitems);
59  return (int64_t*) NULL;
60  }
61  /* the array is not empty*/
62 
63  if (ndim != 1) {
64  elog(ERROR, "One dimension expected");
65  return (int64_t*)NULL;
66  }
67 
68  if (nitems <= 0) {
69  elog(ERROR, "No elements found");
70  return (int64_t*)NULL;
71  }
72 
73  get_typlenbyvalalign(element_type,
74  &typlen, &typbyval, &typalign);
75 
76  /* validate input data type */
77  switch (element_type) {
78  case INT2OID:
79  case INT4OID:
80  case INT8OID:
81  break;
82  default:
83  elog(ERROR, "Expected array of ANY-INTEGER");
84  return (int64_t*)NULL;
85  break;
86  }
87 
88  deconstruct_array(v, element_type, typlen, typbyval,
89  typalign, &elements, &nulls,
90  &nitems);
91 
92  c_array = (int64_t *) palloc(sizeof(int64_t) * (size_t)nitems);
93  if (!c_array) {
94  elog(ERROR, "Out of memory!");
95  }
96 
97 
98  int i;
99  for (i = 0; i < nitems; i++) {
100  if (nulls[i]) {
101  pfree(c_array);
102  elog(ERROR, "NULL value found in Array!");
103  } else {
104  switch (element_type) {
105  case INT2OID:
106  c_array[i] = (int64_t) DatumGetInt16(elements[i]);
107  break;
108  case INT4OID:
109  c_array[i] = (int64_t) DatumGetInt32(elements[i]);
110  break;
111  case INT8OID:
112  c_array[i] = DatumGetInt64(elements[i]);
113  break;
114  }
115  }
116  }
117  (*arrlen) = (size_t)nitems;
118 
119  pfree(elements);
120  pfree(nulls);
121  PGR_DBG("Array size %ld", (*arrlen));
122  time_msg("reading Array", start_t, clock());
123  return c_array;
124 }
125 
126 
127 int64_t* pgr_get_bigIntArray(size_t *arrlen, ArrayType *input) {
128  return pgr_get_bigIntArr(input, arrlen, false);
129 }
130 
131 
132 
133 int64_t* pgr_get_bigIntArray_allowEmpty(size_t *arrlen, ArrayType *input) {
134  return pgr_get_bigIntArr(input, arrlen, true);
135 }
static int64_t * pgr_get_bigIntArr(ArrayType *v, size_t *arrlen, bool allow_empty)
Definition: arrays_input.c:40
#define PGR_DBG(...)
Definition: debug_macro.h:34
int64_t * pgr_get_bigIntArray_allowEmpty(size_t *arrlen, ArrayType *input)
Definition: arrays_input.c:133
int64_t * pgr_get_bigIntArray(size_t *arrlen, ArrayType *input)
Definition: arrays_input.c:127
void time_msg(char *msg, clock_t start_t, clock_t end_t)
Definition: time_msg.c:32