Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Input the data from the restrict table #834

Merged
merged 14 commits into from
Jun 21, 2017
Merged
16 changes: 12 additions & 4 deletions doc/src/proposed.rst
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,19 @@ As part of the :ref:`dijkstra`
.. rubric:: Families

:ref:`astar`

.. include:: aStar-family.rst
:start-after: index from here
:end-before: index to here

:ref:`bdAstar`

.. include:: bdAstar-family.rst
:start-after: index from here
:end-before: index to here

:ref:`bdDijkstra`

.. include:: bdDijkstra-family.rst
:start-after: index from here
:end-before: index to here
Expand Down Expand Up @@ -154,6 +154,15 @@ Experimental and Proposed functions
VRP-category


.. rubric:: rewrite of pgr_TRSP

- :ref:`pgr_dijkstraTRSP`

.. toctree::
:hidden:

pgr_dijkstraTRSP


..
The template
Expand All @@ -163,4 +172,3 @@ Experimental and Proposed functions
:hidden:
..
../src/funnyDijkstra/doc/pgr_funnyDijkstra.rst

1 change: 1 addition & 0 deletions include/c_common/get_check_data.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ void pgr_check_any_numerical_type(Column_info_t info);
void pgr_check_char_type(Column_info_t info);
void pgr_check_text_type(Column_info_t info);
void pgr_check_boolean_type(Column_info_t info);
void pgr_check_any_integerarray_type(Column_info_t info);


char pgr_SPI_getChar(
Expand Down
38 changes: 38 additions & 0 deletions include/c_common/restrict_input.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*PGR-GNU*****************************************************************
File: restrict_input.h

Copyright (c) 2017 Celia Virginia Vergara Castillo
Mail: vicky_vergara@hotmail.com

------

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

********************************************************************PGR-GNU*/
/*! @file */


#ifndef INCLUDE_C_COMMON_RESTRICT_INPUT_H_
#define INCLUDE_C_COMMON_RESTRICT_INPUT_H_
#pragma once

#include "c_types/restrict_t.h"

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

#endif // INCLUDE_C_COMMON_RESTRICT_INPUT_H_
4 changes: 2 additions & 2 deletions include/c_types/column_info_t.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ enum {
ANY_INTEGER,
ANY_NUMERICAL,
TEXT,
CHAR1
CHAR1,
ANY_INTEGER_ARRAY
} expectType;


Expand All @@ -77,4 +78,3 @@ struct {


#endif // INCLUDE_C_TYPES_COLUMN_INFO_T_H_

6 changes: 3 additions & 3 deletions include/c_types/restrict_t.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#define MAX_RULE_LENGTH 5

typedef struct {
int64_t target_id;
double to_cost;
int64_t via[MAX_RULE_LENGTH];
int64_t id;
double cost;
int64_t restricted_edges[MAX_RULE_LENGTH];
}
Restrict_t;

Expand Down
6 changes: 3 additions & 3 deletions src/common/src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
ADD_LIBRARY(common OBJECT
ADD_LIBRARY(common OBJECT
postgres_connection.c
e_report.c
#restrictions_input.c
restrict_input.c
basePath_SSEC.cpp

points_input.c
Expand All @@ -11,7 +11,7 @@ ADD_LIBRARY(common OBJECT
orders_input.c
orders_input.c
vehicles_input.c

coordinates_input.c
arrays_input.c

Expand Down
14 changes: 14 additions & 0 deletions src/common/src/get_check_data.c
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ void pgr_fetch_column_info(
case CHAR1:
pgr_check_char_type(info[i]);
break;
case ANY_INTEGER_ARRAY:
pgr_check_any_integerarray_type(info[i]);
break;
default:
elog(ERROR, "Unknown type of column %s", info[i].name);
}
Expand Down Expand Up @@ -119,6 +122,17 @@ pgr_check_any_integer_type(Column_info_t info) {
}
}

void
pgr_check_any_integerarray_type(Column_info_t info) {
if (!(info.type == INT2ARRAYOID
|| info.type == INT4ARRAYOID
|| info.type == 1016)) {
elog(ERROR,
"Unexpected Column '%s' type. Expected ANY-INTEGER-ARRAY",
info.name);
}
}

void pgr_check_any_numerical_type(Column_info_t info) {
if (!(info.type == INT2OID
|| info.type == INT4OID
Expand Down
173 changes: 173 additions & 0 deletions src/common/src/restrict_input.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
/*PGR-GNU*****************************************************************
File: restrict_input.c

Copyright (c) 2017 Celia Virginia Vergara Castillo
vicky_vergara@hotmail.com

------

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

********************************************************************PGR-GNU*/

#include "c_common/restrict_input.h"

#include "c_types/column_info_t.h"

#include "c_common/debug_macro.h"
#include "c_common/time_msg.h"
#include "c_common/get_check_data.h"

#if 1

static
void fetch_restriction(
HeapTuple *tuple,
TupleDesc *tupdesc,
Column_info_t info[4],
Restrict_t *restriction) {
restriction->id = pgr_SPI_getBigInt(tuple, tupdesc, info[0]);
restriction->cost = pgr_SPI_getFloat8(tuple, tupdesc, info[1]);
char *str = DatumGetCString(
SPI_getvalue(*tuple, *tupdesc, info[2].colNumber));

// TODO(someone) because its text, no guarantee the text read is correct
// move this code to c++ to tokenize the integers.
int i = 0;
for (i = 0; i < MAX_RULE_LENGTH; ++i) restriction->restricted_edges[i] = -1;
str[0] = ',';
if (str != NULL) {
char *token = NULL;
int i = 0;

token = (char *)strtok(str, " ,");

while (token != NULL && i < MAX_RULE_LENGTH) {
restriction->restricted_edges[i] = atoi(token);
i++;
token = (char *)strtok(NULL, " ,");
}
}
}

#endif


void
pgr_get_restriction_data(
char *restrictions_sql,
Restrict_t **restrictions,
size_t *total_restrictions) {
const int tuple_limit = 1000000;
clock_t start_t = clock();

PGR_DBG("pgr_get_restriction_data");
PGR_DBG("%s", restrictions_sql);
Column_info_t info[3];

int i;
for (i = 0; i < 3; ++i) {
info[i].colNumber = -1;
info[i].type = 0;
info[i].strict = true;
info[i].eType = ANY_INTEGER;
}
info[0].name = strdup("id");
info[1].name = strdup("cost");
info[2].name = strdup("restricted_edges");

info[1].eType = ANY_NUMERICAL;
info[2].eType = ANY_INTEGER_ARRAY;

#if 0
// experiment starts

size_t total_tuples = (*total_restrictions) ;

(*restrictions) = (Restrict_t *)palloc0(sizeof(Restrict_t));

(*restrictions)[0].id = 1;
(*restrictions)[0].cost = -1;
(*restrictions)[0].restricted_edges[0] = 4;
(*restrictions)[0].restricted_edges[1] = 7;

// experiment ends
#endif

#if 1
size_t ntuples;
size_t total_tuples;

void *SPIplan;
SPIplan = pgr_SPI_prepare(restrictions_sql);
Portal SPIportal;
SPIportal = pgr_SPI_cursor_open(SPIplan);

bool moredata = TRUE;
(*total_restrictions) = total_tuples = 0;


while (moredata == TRUE) {
SPI_cursor_fetch(SPIportal, TRUE, tuple_limit);
if (total_tuples == 0) {
pgr_fetch_column_info(info, 3);
}
ntuples = SPI_processed;
total_tuples += ntuples;
PGR_DBG("SPI_processed %ld", ntuples);
if (ntuples > 0) {
if ((*restrictions) == NULL)
(*restrictions) = (Restrict_t *)palloc0(
total_tuples * sizeof(Restrict_t));
else
(*restrictions) = (Restrict_t *)repalloc(
(*restrictions),
total_tuples * sizeof(Restrict_t));

if ((*restrictions) == NULL) {
elog(ERROR, "Out of memory");
}

size_t t;
SPITupleTable *tuptable = SPI_tuptable;
TupleDesc tupdesc = SPI_tuptable->tupdesc;
PGR_DBG("processing %ld", ntuples);
for (t = 0; t < ntuples; t++) {
HeapTuple tuple = tuptable->vals[t];
fetch_restriction(&tuple, &tupdesc, info,
&(*restrictions)[total_tuples - ntuples + t]);
}
SPI_freetuptable(tuptable);
} else {
moredata = FALSE;
}
}

SPI_cursor_close(SPIportal);

if (total_tuples == 0) {
(*total_restrictions) = 0;
PGR_DBG("NO restrictions");
return;
}

(*total_restrictions) = total_tuples;
#endif
PGR_DBG("Finish reading %ld data, %ld",
total_tuples,
(*total_restrictions));
clock_t end_t = clock();
time_msg(" reading Restrictions", start_t, end_t);
}
23 changes: 23 additions & 0 deletions src/dijkstraTRSP/src/dijkstraTRSP.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#include "c_common/time_msg.h"
/* for functions to get edges informtion */
#include "c_common/edges_input.h"
#include "c_common/restrict_input.h"

#include "drivers/dijkstraTRSP/dijkstraTRSP_driver.h" // the link to the C++ code of the function

Expand Down Expand Up @@ -125,6 +126,27 @@ process(
pgr_get_edges(edges_sql, &edges, &total_edges);
PGR_DBG("Total %ld edges in query:", total_edges);

PGR_DBG("Load restrictions");
Restrict_t *restrictions = NULL;
size_t total_restrictions = 0;

pgr_get_restriction_data(restrictions_sql, &restrictions,
&total_restrictions);

#if 0
size_t i = 0;
while(i < total_restrictions) {
PGR_DBG("id: %ld cost: %lf", restrictions[i].id, restrictions[i].cost);
int j = 0;
while(restrictions[i].restricted_edges[j] != -1) {
PGR_DBG("%ld ", restrictions[i].restricted_edges[j]);
j++;
}
PGR_DBG("\n");
i++;
}
#endif

if (total_edges == 0) {
PGR_DBG("No edges found");
pgr_SPI_finish();
Expand Down Expand Up @@ -162,6 +184,7 @@ process(
if (log_msg) pfree(log_msg);
if (notice_msg) pfree(notice_msg);
if (err_msg) pfree(err_msg);
if (restrictions) pfree(restrictions);
#if 0
/*
* handling arrays example
Expand Down
4 changes: 2 additions & 2 deletions tools/testers/sampledata.sql
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,11 @@ UPDATE pointsOfInterest
--RESTRICTIONS CREATE
CREATE TABLE restrict (
id BIGSERIAL,
restricted BIGINT[] ,
restricted_edges BIGINT[] ,
cost FLOAT
);

INSERT INTO restrict(restricted, cost) VALUES
INSERT INTO restrict(restricted_edges, cost) VALUES
('{4, 7}', -1);

CREATE TABLE restrictions (
Expand Down