Skip to content

Commit

Permalink
Merge pull request #3 from adrianprelipcean/feat/improvements
Browse files Browse the repository at this point in the history
Feat/improvements
  • Loading branch information
adrianprelipcean authored May 18, 2024
2 parents eeed36c + ecfbdb0 commit a18781c
Show file tree
Hide file tree
Showing 17 changed files with 92 additions and 62 deletions.
1 change: 0 additions & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ jobs:
strategy:
matrix:
python-version: ["3.10"]
container: pgxn/pgxn-tools
permissions:
contents: write
id-token: write
Expand Down
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
# SPDX-License-Identifier: CC-BY-NC-SA-4.0

EXTENSION = pgtfs
DATA = pgtfs--0.0.1.sql
DATA = sql/pgtfs--0.0.1.sql sql/pgtfs--0.0.2.sql
MODULE_big = pgtfs
OBJS = pgtfs.o src/models/network.o src/csa/csa.o
REGRESS= pgtfs_test network_edge_cases_test
REGRESS_OPTS= --user=postgres
REGRESS_OPTS= --inputdir=tests --outputdir=tests --user=postgres
PG_CONFIG = pg_config
PGXS := $(shell $(PG_CONFIG) --pgxs)

Expand Down
6 changes: 6 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to
[Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [0.0.2](https://github.com/adrianprelipcean/pgtfs/releases/tag/v0.0.2)
### Changed
- use `double` instead of `float8` and `time_t` to maintain data type consistency between PostgreSQL and PGTFS
- `csa.cpp` correctly imports `postgres.h` for calling `elog` type functionality
- folder structure change: tests and relevant data is in the `tests/` folder and the (mostly versioning) extension specific sql files are in the `sql/` folder

## [0.0.1](https://github.com/adrianprelipcean/pgtfs/releases/tag/v0.0.1)
### Added
Expand Down
2 changes: 1 addition & 1 deletion docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
project = 'PGTFS'
copyright = '2024, Adrian C. Prelipcean'
author = 'Adrian C. Prelipcean'
version = '0.0.1'
version = '0.0.2'

# -- General configuration ---------------------------------------------------
# https://www.sphinx-doc.org/en/master/usage/configuration.html#general-configuration
Expand Down
2 changes: 1 addition & 1 deletion pgtfs.control
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
comment = 'Contains public transportation routing functionality using the GTFS format'
default_version = '0.0.1'
default_version = '0.0.2'
relocatable = true
20 changes: 10 additions & 10 deletions pgtfs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ extern "C"
extern "C"
{
PG_MODULE_MAGIC;
static const char *EXTENSION_VERSION = "0.0.1";
static const char *EXTENSION_VERSION = "0.0.2";

PG_FUNCTION_INFO_V1(pgtfs_csa);
/**
* @brief Implements the pgtfs_csa PostgreSQL extension function.
*
*
* This function performs the Connection Scan Algorithm (CSA) to find connections
* between the specified origin and destination at the given departure time.
*
*
* @param fcinfo Function call information.
* @return A set of rows representing the solutions found.
*/
Expand Down Expand Up @@ -112,14 +112,14 @@ extern "C"

values[0] = CStringGetTextDatum(stop.stop_id.c_str());
values[1] = Int32GetDatum(stop.stop_sequence);
values[2] = Float8GetDatum((float)stop.arrival_time);
values[2] = Float8GetDatum(stop.arrival_time);
values[3] = CStringGetTextDatum(stop.trip_id.c_str());

bool nulls[4];
nulls[0] = stop.stop_id.empty();
nulls[1] = false;
nulls[2] = false;
nulls[3] = stop.trip_id.empty();
nulls[0] = stop.stop_id.empty();
nulls[1] = false;
nulls[2] = false;
nulls[3] = stop.trip_id.empty();

HeapTuple tuple = heap_form_tuple(funcctx->tuple_desc, values, nulls);
SRF_RETURN_NEXT(funcctx, HeapTupleGetDatum(tuple));
Expand All @@ -133,10 +133,10 @@ extern "C"
PG_FUNCTION_INFO_V1(pgtfs_version);
/**
* @brief Returns version information about the extension.
*
*
* This function returns a textual representation of version information
* including the extension version, PostgreSQL version, and compiler version.
*
*
* @param fcinfo Function call information.
* @return A text representation of the version information.
*/
Expand Down
File renamed without changes.
23 changes: 23 additions & 0 deletions sql/pgtfs--0.0.2.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
-- SPDX-FileCopyrightText: © 2024 Adrian C. Prelipcean
--
-- SPDX-License-Identifier: EUPL-1.2

CREATE OR REPLACE FUNCTION pgtfs_csa(
origin TEXT,
destination TEXT,
departure_time DOUBLE PRECISION,
network TEXT
)
RETURNS TABLE (
stop_id TEXT,
stop_sequence INT,
arrival_time DOUBLE PRECISION,
trip_id TEXT
)
AS '$libdir/pgtfs'
LANGUAGE c STRICT;

CREATE OR REPLACE FUNCTION pgtfs_version()
RETURNS TEXT
AS '$libdir/pgtfs'
LANGUAGE c STRICT;
16 changes: 8 additions & 8 deletions src/csa/csa.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,17 @@

std::vector<SolutionCSA> perform_CSA(const char *origin, const char *destination, float8 departure_time, NetworkRow *network, int64_t network_size)
{
time_t departure_epoch = (time_t)departure_time;
double departure_epoch = departure_time;

int num_rows = network_size;
std::vector<SolutionCSA> stops;

std::priority_queue<std::tuple<time_t, std::string, std::vector<std::tuple<std::string, std::string, time_t>>>,
std::vector<std::tuple<time_t, std::string, std::vector<std::tuple<std::string, std::string, time_t>>>>,
std::greater<std::tuple<time_t, std::string, std::vector<std::tuple<std::string, std::string, time_t>>>>>
std::priority_queue<std::tuple<double, std::string, std::vector<std::tuple<std::string, std::string, double>>>,
std::vector<std::tuple<double, std::string, std::vector<std::tuple<std::string, std::string, double>>>>,
std::greater<std::tuple<double, std::string, std::vector<std::tuple<std::string, std::string, double>>>>>
pq;

std::unordered_map<std::string, time_t> shortest_times;
std::unordered_map<std::string, double> shortest_times;

shortest_times[std::string(origin)] = departure_epoch;
pq.push({departure_epoch, std::string(origin), {{std::string(origin), "", departure_epoch}}});
Expand Down Expand Up @@ -55,14 +55,14 @@ std::vector<SolutionCSA> perform_CSA(const char *origin, const char *destination
{
if (current_node == network[i].from_stop_id)
{
if (current_time <= (time_t)network[i].departure_time)
if (current_time <= network[i].departure_time)
{
time_t neighbor_arrival_time = (time_t)network[i].departure_time + (time_t)network[i].travel_time;
double neighbor_arrival_time = network[i].departure_time + network[i].travel_time;

if (shortest_times.find(network[i].to_stop_id) == shortest_times.end() || neighbor_arrival_time < shortest_times[network[i].to_stop_id])
{
shortest_times[network[i].to_stop_id] = neighbor_arrival_time;
std::vector<std::tuple<std::string, std::string, time_t>> new_route = route;
std::vector<std::tuple<std::string, std::string, double>> new_route = route;
new_route.push_back(std::make_tuple(network[i].to_stop_id, network[i].trip_id, neighbor_arrival_time));
pq.push({neighbor_arrival_time, network[i].to_stop_id, new_route});
}
Expand Down
4 changes: 3 additions & 1 deletion src/csa/csa.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@
#ifndef CSA_H
#define CSA_H

extern "C" {
#include "postgres.h"
}
#include <vector>
#include <ctime>
#include <string>
#include "postgres.h"
#include "src/models/network.h"

/**
Expand Down
6 changes: 3 additions & 3 deletions src/models/network.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ typedef struct {
char trip_id[MAX_STRING_LENGTH]; /**< Trip ID */
char from_stop_id[MAX_STRING_LENGTH]; /**< From stop ID */
char to_stop_id[MAX_STRING_LENGTH]; /**< To stop ID */
float8 arrival_time; /**< Arrival time as epoch*/
float8 departure_time; /**< Departure time as epoch*/
float8 travel_time; /**< Travel time */
double arrival_time; /**< Arrival time as epoch*/
double departure_time; /**< Departure time as epoch*/
double travel_time; /**< Travel time */
bool nulls[6]; /**< Array of boolean flags indicating null values */
} NetworkRow;

Expand Down
File renamed without changes.
70 changes: 35 additions & 35 deletions expected/pgtfs_test.out → tests/expected/pgtfs_test.out
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ $$
stop_id | stop_sequence | arrival_time | trip_id
---------+---------------+--------------+---------
1 | 0 | 1712707200 |
2 | 1 | 1712759936 | 1753
2 | 1 | 1712759880 | 1753
(2 rows)

-- simple connectivity test where sequence has a gap (3 is missing)
Expand All @@ -49,9 +49,9 @@ $$
stop_id | stop_sequence | arrival_time | trip_id
---------+---------------+--------------+---------
1 | 0 | 1712707200 |
2 | 1 | 1712759936 | 1753
3 | 2 | 1712761984 | 1753
4 | 3 | 1712762112 | 1753
2 | 1 | 1712759880 | 1753
3 | 2 | 1712762040 | 1753
4 | 3 | 1712762100 | 1753
(4 rows)

-- connected and route on single trip
Expand Down Expand Up @@ -137,19 +137,19 @@ $$
stop_id | stop_sequence | arrival_time | trip_id
---------+---------------+--------------+---------
10017 | 0 | 1712707200 |
50017 | 1 | 1712759936 | 1753
50213 | 2 | 1712761984 | 1753
50342 | 3 | 1712763904 | 1753
50433 | 4 | 1712765824 | 1753
50603 | 5 | 1712768128 | 1753
50691 | 6 | 1712769408 | 1753
50811 | 7 | 1712770816 | 1753
51059 | 8 | 1712773120 | 1753
51190 | 9 | 1712775296 | 1753
51358 | 10 | 1712777088 | 1753
51451 | 11 | 1712777984 | 1753
51530 | 12 | 1712779136 | 1753
51607 | 13 | 1712779776 | 1753
50017 | 1 | 1712759880 | 1753
50213 | 2 | 1712762040 | 1753
50342 | 3 | 1712763960 | 1753
50433 | 4 | 1712765820 | 1753
50603 | 5 | 1712768160 | 1753
50691 | 6 | 1712769360 | 1753
50811 | 7 | 1712770860 | 1753
51059 | 8 | 1712773080 | 1753
51190 | 9 | 1712775300 | 1753
51358 | 10 | 1712777040 | 1753
51451 | 11 | 1712778000 | 1753
51530 | 12 | 1712779080 | 1753
51607 | 13 | 1712779800 | 1753
(14 rows)

-- connected and route on multiple trips
Expand Down Expand Up @@ -216,24 +216,24 @@ $$
stop_id | stop_sequence | arrival_time | trip_id
---------+---------------+--------------+---------
10017 | 0 | 1712707200 |
50017 | 1 | 1712752640 | 1663
50213 | 2 | 1712754816 | 1663
50342 | 3 | 1712756864 | 1663
50433 | 4 | 1712758656 | 1663
50603 | 5 | 1712761088 | 1663
50653 | 6 | 1712763648 | 5105
50691 | 7 | 1712764288 | 5105
50732 | 8 | 1712764928 | 5105
50744 | 9 | 1712765184 | 5105
50768 | 10 | 1712765568 | 5105
50770 | 11 | 1712765824 | 5105
50811 | 12 | 1712766464 | 5105
51059 | 13 | 1712773120 | 1753
51190 | 14 | 1712775296 | 1753
51358 | 15 | 1712777088 | 1753
51451 | 16 | 1712777984 | 1753
51530 | 17 | 1712779136 | 1753
51607 | 18 | 1712779776 | 1753
50017 | 1 | 1712752620 | 1663
50213 | 2 | 1712754840 | 1663
50342 | 3 | 1712756820 | 1663
50433 | 4 | 1712758680 | 1663
50603 | 5 | 1712761080 | 1663
50653 | 6 | 1712763600 | 5105
50691 | 7 | 1712764320 | 5105
50732 | 8 | 1712764920 | 5105
50744 | 9 | 1712765190 | 5105
50768 | 10 | 1712765580 | 5105
50770 | 11 | 1712765850 | 5105
50811 | 12 | 1712766420 | 5105
51059 | 13 | 1712773080 | 1753
51190 | 14 | 1712775300 | 1753
51358 | 15 | 1712777040 | 1753
51451 | 16 | 1712778000 | 1753
51530 | 17 | 1712779080 | 1753
51607 | 18 | 1712779800 | 1753
(19 rows)

-- disconnected (origin and destination exist)
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit a18781c

Please sign in to comment.