Skip to content

Commit 9fae5d6

Browse files
committed
Add option to filter results by namespace
We now have at least two data-sets in the featureService: Who's on First and Divipola. As such, we may want the ability to restrict results to just features from one of the two data-sets (e.g. in a Fortis deployment in Colombia we may want to only consider shapes from Divipola). This change makes the assumption that we'll continue the current trend of prefixing all ids with the data source from where they came, so if an item has an id 1234 and came from the Who's on First dataset, we'll give it an id like `wof-1234`. This is a reasonable pattern so it's fine to make this assumption. Adding a computed index on the id column lets us query the namespace with a reasonable efficiency as the below query plan shows: ``` features=# explain analyze select id from features where lower(split_part(id,'-',1))='divipola'; QUERY PLAN ---------------------------------------------------------------------------------------------------------------------------------------- Bitmap Heap Scan on features (cost=47.06..8406.97 rows=2405 width=13) (actual time=0.977..7.781 rows=5879 loops=1) Recheck Cond: (lower(split_part((id)::text, '-'::text, 1)) = 'divipola'::text) Heap Blocks: exact=1236 -> Bitmap Index Scan on features_namespace_index (cost=0.00..46.46 rows=2405 width=0) (actual time=0.849..0.849 rows=5879 loops=1) Index Cond: (lower(split_part((id)::text, '-'::text, 1)) = 'divipola'::text) Planning time: 0.124 ms Execution time: 8.653 ms ```
1 parent 503d4b5 commit 9fae5d6

File tree

3 files changed

+9
-0
lines changed

3 files changed

+9
-0
lines changed

controllers/features.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ exports.upsert = function(req, res) {
2323
exports.getById = function(req, res) {
2424
let query = {
2525
id: req.params.id.split(','),
26+
filter_namespace: req.query.filter_namespace,
2627
include: req.query.include
2728
};
2829

@@ -37,6 +38,7 @@ exports.getByPoint = function(req, res) {
3738
let query = {
3839
latitude: parseFloat(req.params.latitude),
3940
longitude: parseFloat(req.params.longitude),
41+
filter_namespace: req.query.filter_namespace,
4042
include: req.query.include
4143
};
4244

@@ -54,6 +56,7 @@ exports.getByPoint = function(req, res) {
5456
exports.getByName = function(req, res) {
5557
let query = {
5658
name: req.params.name.split(/,(?=[^ ])/),
59+
filter_namespace: req.query.filter_namespace,
5760
include: req.query.include
5861
};
5962

@@ -75,6 +78,7 @@ exports.getByBoundingBox = function(req, res) {
7578
south: parseFloat(req.params.south),
7679
east: parseFloat(req.params.east),
7780
filter_name: req.query.filter_name,
81+
filter_namespace: req.query.filter_namespace,
7882
include: req.query.include
7983
};
8084

schema.sql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ CREATE TABLE features
2525

2626
CREATE INDEX features_hull_index ON features USING gist (hull);
2727
CREATE INDEX features_name_lower_index ON features(lower(name));
28+
CREATE INDEX features_namespace_index ON features(lower(split_part(id, '-', 1)));
2829

2930
GRANT SELECT, UPDATE, INSERT, DELETE ON features TO frontend;
3031

services/features.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,10 @@ function addQueryPredicates(sql, query) {
131131
sql += ` AND strpos(lower(name), lower(${escapeSql(query.filter_name)})) > 0`;
132132
}
133133

134+
if (query.filter_namespace) {
135+
sql += ` AND lower(split_part(id, '-', 1)) = lower(${escapeSql(query.filter_namespace)})`;
136+
}
137+
134138
return sql;
135139
}
136140

0 commit comments

Comments
 (0)