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

Support z,x,y and record-returning funcs, table rework #380

Merged
merged 13 commits into from
Dec 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ tilejson = "0.3"
[dev-dependencies]
ctor = "0.1"
indoc = "1"
#test-log = "0.2"

[dev-dependencies.criterion]
version = "0.4.0"
Expand Down
35 changes: 26 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -311,17 +311,34 @@ curl localhost:3000/points,lines/0/0/0

## Function Sources

Function Source is a database function which can be used to query [vector tiles](https://github.com/mapbox/vector-tile-spec). When started, martin will look for the functions with a suitable signature. A function that takes `z integer`, `x integer`, `y integer`, and `query_params json` and returns `bytea`, can be used as a Function Source.
Function Source is a database function which can be used to query [vector tiles](https://github.com/mapbox/vector-tile-spec). When started, martin will look for the functions with a suitable signature. A function that takes `z integer` (or `zoom integer`), `x integer`, `y integer`, and an optional `query json` and returns `bytea`, can be used as a Function Source. Alternatively the function could return a record with a single `bytea` field, or a record with two fields of types `bytea` and `text`, where the `text` field is a etag key (i.e. md5 hash).

| Argument | Type | Description |
|--------------|---------|-------------------------|
| z | integer | Tile zoom parameter |
| x | integer | Tile x parameter |
| y | integer | Tile y parameter |
| query_params | json | Query string parameters |
| Argument | Type | Description |
|----------------------------|---------|-------------------------|
| z (or zoom) | integer | Tile zoom parameter |
| x | integer | Tile x parameter |
| y | integer | Tile y parameter |
| query (optional, any name) | json | Query string parameters |

For example, if you have a table `table_source` in WGS84 (`4326` SRID), then you can use this function as a Function Source:

```sql, ignore
CREATE OR REPLACE FUNCTION function_zxy_query(z integer, x integer, y integer) RETURNS bytea AS $$
DECLARE
mvt bytea;
BEGIN
SELECT INTO mvt ST_AsMVT(tile, 'function_zxy_query', 4096, 'geom') FROM (
SELECT
ST_AsMVTGeom(ST_Transform(ST_CurveToLine(geom), 3857), ST_TileEnvelope(z, x, y), 4096, 64, true) AS geom
FROM table_source
WHERE geom && ST_Transform(ST_TileEnvelope(z, x, y), 4326)
) as tile WHERE geom IS NOT NULL;

RETURN mvt;
END
$$ LANGUAGE plpgsql IMMUTABLE STRICT PARALLEL SAFE;
```

```sql, ignore
CREATE OR REPLACE FUNCTION function_zxy_query(z integer, x integer, y integer, query_params json) RETURNS bytea AS $$
DECLARE
Expand Down Expand Up @@ -466,7 +483,7 @@ pool_size: 20
worker_processes: 8

# Associative arrays of table sources
table_sources:
tables:
table_source_id:
# Table schema (required)
schema: public
Expand Down Expand Up @@ -512,7 +529,7 @@ table_sources:
gid: int4

# Associative arrays of function sources
function_sources:
functions:
function_source_id:
# Schema name (required)
schema: public
Expand Down
7 changes: 4 additions & 3 deletions justfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ set shell := ["bash", "-c"]

export DATABASE_URL := "postgres://postgres@localhost/db"
export CARGO_TERM_COLOR := "always"
# export RUST_LOG := "debug"
# export RUST_BACKTRACE := "1"

@_default:
Expand Down Expand Up @@ -49,9 +50,9 @@ bench: start-db
test: test-unit test-int

# Run Rust unit and doc tests (cargo test)
test-unit: start-db
cargo test --all-targets
cargo test --all-targets --all-features
test-unit *ARGS: start-db
cargo test --all-targets {{ARGS}}
cargo test --all-targets --all-features {{ARGS}}
cargo test --doc

# Run integration tests
Expand Down
17 changes: 9 additions & 8 deletions src/bin/main.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use actix_web::dev::Server;
use clap::Parser;
use log::{error, info, warn};
use martin::config::{read_config, ConfigBuilder};
use martin::config::{read_config, Config, ConfigBuilder};
use martin::pg::config::{PgArgs, PgConfigBuilder};
use martin::pg::db::PgConfigurator;
use martin::pg::configurator::resolve_pg_data;
use martin::source::IdResolver;
use martin::srv::config::{SrvArgs, SrvConfigBuilder};
use martin::srv::server;
Expand Down Expand Up @@ -70,13 +70,14 @@ async fn start(args: Args) -> io::Result<Server> {
if let Some(file_cfg) = file_cfg {
builder.merge(file_cfg);
}
let mut config = builder.finalize()?;
let config = builder.finalize()?;

let id_resolver = IdResolver::new(RESERVED_KEYWORDS);
let mut pg_config = PgConfigurator::new(&config.pg, id_resolver).await?;
let sources = pg_config.discover_db_sources().await?;
config.pg.table_sources = pg_config.table_sources;
config.pg.function_sources = pg_config.function_sources;
let (sources, pg_config, _) = resolve_pg_data(config.pg, id_resolver).await?;
let config = Config {
pg: pg_config,
..config
};

if save_config.is_some() {
let yaml = serde_yaml::to_string(&config).expect("Unable to serialize config");
Expand All @@ -94,7 +95,7 @@ async fn start(args: Args) -> io::Result<Server> {
}

let listen_addresses = config.srv.listen_addresses.clone();
let server = server::new(config, sources);
let server = server::new(config.srv, sources);
info!("Martin has been started on {listen_addresses}.");
info!("Use http://{listen_addresses}/catalog to get the list of available sources.");
Ok(server)
Expand Down
26 changes: 12 additions & 14 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ mod tests {
pool_size: 20
worker_processes: 8

table_sources:
tables:
table_source:
schema: public
table: table_source
Expand All @@ -106,7 +106,7 @@ mod tests {
properties:
gid: int4

function_sources:
functions:
function_zxy_query:
schema: public
function: function_zxy_query
Expand All @@ -133,14 +133,13 @@ mod tests {
pool_size: 20,
discover_functions: false,
discover_tables: false,
table_sources: HashMap::from([(
tables: HashMap::from([(
"table_source".to_string(),
TableInfo {
schema: "public".to_string(),
table: "table_source".to_string(),
srid: 4326,
geometry_column: "geom".to_string(),
id_column: None,
minzoom: Some(0),
maxzoom: Some(30),
bounds: Some([-180, -90, 180, 90].into()),
Expand All @@ -149,19 +148,18 @@ mod tests {
clip_geom: Some(true),
geometry_type: Some("GEOMETRY".to_string()),
properties: HashMap::from([("gid".to_string(), "int4".to_string())]),
unrecognized: HashMap::new(),
..Default::default()
},
)]),
function_sources: HashMap::from([(
functions: HashMap::from([(
"function_zxy_query".to_string(),
FunctionInfo {
schema: "public".to_string(),
function: "function_zxy_query".to_string(),
minzoom: Some(0),
maxzoom: Some(30),
bounds: Some(Bounds::MAX),
unrecognized: HashMap::new(),
},
FunctionInfo::new_extended(
"public".to_string(),
"function_zxy_query".to_string(),
0,
30,
Bounds::MAX,
),
)]),
},
};
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ pub mod config;
pub mod pg;
pub mod source;
pub mod srv;
pub mod utils;

// Ensure README.md contains valid code
#[cfg(doctest)]
Expand Down
Loading