forked from pgRouting/pgrouting
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
To cpp arrays input (pgRouting#2497)
* 🚚 moving arrays_input for cpp compilation * 🔨 arrays_input is compiled as cpp and linked as C * 🔧 adjusting code to the one function for getting integer arrays from postgres * 📚 🐛 fixing doxygen documentation
- Loading branch information
Showing
35 changed files
with
105 additions
and
124 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,9 @@ | ||
/*PGR-GNU***************************************************************** | ||
File: arrays_input.h | ||
Copyright (c) 2023 Celia Virginia Vergara Castillo | ||
Copyright (c) 2015 Celia Virginia Vergara Castillo | ||
[email protected] | ||
mail: vicky at erosion.dev | ||
------ | ||
|
@@ -26,15 +27,21 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |
#define INCLUDE_C_COMMON_ARRAYS_INPUT_H_ | ||
#pragma once | ||
|
||
|
||
#include <stddef.h> | ||
#include <stdint.h> | ||
|
||
#ifdef __cplusplus | ||
extern "C" { | ||
#endif | ||
|
||
#include <postgres.h> | ||
#include <utils/array.h> | ||
|
||
/** @brief Enforces the input array to be @b NOT empty */ | ||
int64_t* pgr_get_bigIntArray(size_t *arrlen, ArrayType *input); | ||
int64_t* pgr_get_bigIntArray(size_t*, ArrayType*, bool); | ||
|
||
/** @brief Allows the input array to be empty */ | ||
int64_t* pgr_get_bigIntArray_allowEmpty(size_t *arrlen, ArrayType *input); | ||
#ifdef __cplusplus | ||
} | ||
#endif | ||
|
||
#endif // INCLUDE_C_COMMON_ARRAYS_INPUT_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,4 +5,5 @@ ADD_LIBRARY(cpp_common OBJECT | |
bpoint.cpp | ||
pgr_messages.cpp | ||
combinations.cpp | ||
arrays_input.cpp | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,9 @@ | ||
/*PGR-GNU***************************************************************** | ||
File: arrays_input.c | ||
File: arrays_input.cpp | ||
Copyright (c) 2023 Celia Virginia Vergara Castillo | ||
Copyright (c) 2015 Celia Virginia Vergara Castillo | ||
[email protected] | ||
mail: vicky at erosion.dev | ||
------ | ||
|
@@ -24,14 +25,17 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |
|
||
#include "c_common/arrays_input.h" | ||
|
||
#include <assert.h> | ||
extern "C" { | ||
#include <utils/lsyscache.h> | ||
#include <catalog/pg_type.h> | ||
} | ||
|
||
#include "c_common/time_msg.h" | ||
#include "c_common/debug_macro.h" | ||
/** | ||
* Function for array input | ||
#include "cpp_common/pgr_alloc.hpp" | ||
|
||
namespace { | ||
|
||
/** @brief get the array contents from postgres | ||
* | ||
* @details This function generates the array inputs according to their type | ||
* received through @a ArrayType *v parameter and store them in @a c_array. It | ||
* can be empty also if received @a allow_empty true. The cases of failure are:- | ||
|
@@ -42,46 +46,42 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |
* 5. When null value is found in the array. | ||
* | ||
* All these failures are represented as error through @a elog. | ||
* @param[in] v The type of element to be processed. | ||
* @param[out] arrlen The length of the array (To be determined in this function). | ||
* @param[in] allow_empty Bool type parameter that tells us whether to consider empty | ||
* array or not. | ||
* @pre The initial value of @a *arrlen should be zero. | ||
* @returns The resultant array i.e. @a c_array. | ||
* @param[in] v Pointer to the postgres C array | ||
* @param[out] arrlen size of the C array | ||
* @param[in] allow_empty flag to allow empty arrays | ||
* | ||
* @pre the array has to be one dimension | ||
* @pre Must have elements (when allow_empty is false) | ||
* | ||
* @returns The resultant array | ||
*/ | ||
static | ||
int64_t* | ||
pgr_get_bigIntArr(ArrayType *v, size_t *arrlen, bool allow_empty) { | ||
clock_t start_t = clock(); | ||
int64_t *c_array = NULL; | ||
|
||
Oid element_type = ARR_ELEMTYPE(v); | ||
int *dim = ARR_DIMS(v); | ||
int ndim = ARR_NDIM(v); | ||
int nitems = ArrayGetNItems(ndim, dim); | ||
Datum *elements; | ||
bool *nulls; | ||
get_bigIntArr(ArrayType *v, size_t *arrlen, bool allow_empty) { | ||
int64_t *c_array = nullptr; | ||
|
||
auto element_type = ARR_ELEMTYPE(v); | ||
auto dim = ARR_DIMS(v); | ||
auto ndim = ARR_NDIM(v); | ||
auto nitems = ArrayGetNItems(ndim, dim); | ||
Datum *elements = nullptr; | ||
bool *nulls = nullptr; | ||
int16 typlen; | ||
bool typbyval; | ||
char typalign; | ||
|
||
assert((*arrlen) == 0); | ||
|
||
|
||
if (allow_empty && (ndim == 0 || nitems <= 0)) { | ||
PGR_DBG("ndim %d nitems %d", ndim, nitems); | ||
return (int64_t*)NULL; | ||
return nullptr; | ||
} | ||
/* the array is not empty*/ | ||
|
||
if (ndim != 1) { | ||
elog(ERROR, "One dimension expected"); | ||
return (int64_t*)NULL; | ||
return nullptr; | ||
} | ||
|
||
if (nitems <= 0) { | ||
elog(ERROR, "No elements found"); | ||
return (int64_t*)NULL; | ||
return nullptr; | ||
} | ||
|
||
get_typlenbyvalalign(element_type, | ||
|
@@ -95,64 +95,54 @@ pgr_get_bigIntArr(ArrayType *v, size_t *arrlen, bool allow_empty) { | |
break; | ||
default: | ||
elog(ERROR, "Expected array of ANY-INTEGER"); | ||
return (int64_t*)NULL; | ||
return nullptr; | ||
break; | ||
} | ||
|
||
deconstruct_array(v, element_type, typlen, typbyval, | ||
typalign, &elements, &nulls, | ||
&nitems); | ||
|
||
c_array = (int64_t *) palloc(sizeof(int64_t) * (size_t)nitems); | ||
c_array = pgr_alloc(static_cast<size_t>(nitems), (c_array)); | ||
if (!c_array) { | ||
elog(ERROR, "Out of memory!"); | ||
} | ||
|
||
|
||
int i; | ||
for (i = 0; i < nitems; i++) { | ||
for (int i = 0; i < nitems; i++) { | ||
if (nulls[i]) { | ||
pfree(c_array); | ||
elog(ERROR, "NULL value found in Array!"); | ||
} else { | ||
switch (element_type) { | ||
case INT2OID: | ||
c_array[i] = (int64_t) DatumGetInt16(elements[i]); | ||
c_array[i] = static_cast<int64_t>(DatumGetInt16(elements[i])); | ||
break; | ||
case INT4OID: | ||
c_array[i] = (int64_t) DatumGetInt32(elements[i]); | ||
c_array[i] = static_cast<int64_t>(DatumGetInt32(elements[i])); | ||
break; | ||
case INT8OID: | ||
c_array[i] = DatumGetInt64(elements[i]); | ||
break; | ||
} | ||
} | ||
} | ||
(*arrlen) = (size_t)nitems; | ||
(*arrlen) = static_cast<size_t>(nitems); | ||
|
||
pfree(elements); | ||
pfree(nulls); | ||
PGR_DBG("Array size %ld", (*arrlen)); | ||
time_msg("reading Array", start_t, clock()); | ||
return c_array; | ||
} | ||
|
||
/** | ||
* @param[out] arrlen Length of the array | ||
* @param[in] input Input type of the array | ||
* @returns Returns the output of @a pgr_get_bitIntArray when @a allow_empty is set to false. | ||
*/ | ||
|
||
int64_t* pgr_get_bigIntArray(size_t *arrlen, ArrayType *input) { | ||
return pgr_get_bigIntArr(input, arrlen, false); | ||
} | ||
} // namespace | ||
|
||
/** | ||
* @param[out] arrlen Length of the array | ||
* @param[in] input Input type of the array | ||
* @returns Returns the output of @a pgr_get_bitIntArray when @a allow_empty is set to true. | ||
* @param[in] input the postgres array | ||
* @param[in] allow_empty when true, empty arrays are accepted. | ||
* @returns Returns a C array of integers | ||
*/ | ||
|
||
int64_t* pgr_get_bigIntArray_allowEmpty(size_t *arrlen, ArrayType *input) { | ||
return pgr_get_bigIntArr(input, arrlen, true); | ||
int64_t* pgr_get_bigIntArray(size_t *arrlen, ArrayType *input, bool allow_empty) { | ||
return get_bigIntArr(input, arrlen, allow_empty); | ||
} |
Oops, something went wrong.