geolith 0.1.4

Shared library of the geolith builders: source readers, conflation, admin areas, tile encoding and the PMTiles and MBTiles writers
Documentation
//! GERS deduplication join — skip OSM features that have Overture bridge matches.
//!
//! When features exist in both OSM and Overture datasets, the GERS bridge
//! file identifies which OSM features have Overture equivalents. This module
//! provides [`GersDedup`] which tracks which OSM features should be skipped
//! in favour of their Overture counterparts.

use std::sync::atomic::{AtomicU64, Ordering};

use crate::conflation::bridge::BridgeIndex;

/// GERS-based deduplication filter for OSM features.
///
/// Wraps a [`BridgeIndex`] and tracks how many OSM features were skipped
/// (had a GERS match in Overture) vs kept (no match). Thread-safe via
/// atomic counters for use in parallel processing pipelines.
pub struct GersDedup {
    bridge: BridgeIndex,
    skipped: AtomicU64,
    kept: AtomicU64,
}

impl std::fmt::Debug for GersDedup {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("GersDedup")
            .field("bridge_entries", &self.bridge.len())
            .field("skipped", &self.skipped_count())
            .field("kept", &self.kept_count())
            .finish()
    }
}

impl GersDedup {
    /// Create a new deduplication filter from a loaded bridge index.
    pub const fn new(bridge: BridgeIndex) -> Self {
        Self {
            bridge,
            skipped: AtomicU64::new(0),
            kept: AtomicU64::new(0),
        }
    }

    /// Check whether an OSM feature should be skipped.
    ///
    /// Returns `true` if the OSM feature with the given `osm_id` has a
    /// corresponding Overture feature in the GERS bridge index. In that
    /// case, the Overture version should be preferred and the OSM version
    /// skipped.
    ///
    /// Also increments the appropriate atomic counter (skipped or kept).
    pub fn should_skip(&self, osm_id: u64) -> bool {
        if self.bridge.contains(osm_id) {
            self.skipped.fetch_add(1, Ordering::Relaxed);
            true
        } else {
            self.kept.fetch_add(1, Ordering::Relaxed);
            false
        }
    }

    /// Get the number of OSM features skipped so far.
    #[must_use]
    pub fn skipped_count(&self) -> u64 {
        self.skipped.load(Ordering::Relaxed)
    }

    /// Get the number of OSM features kept so far.
    #[must_use]
    pub fn kept_count(&self) -> u64 {
        self.kept.load(Ordering::Relaxed)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn make_dedup(entries: Vec<(u64, String)>) -> GersDedup {
        let bridge = BridgeIndex::from_entries(entries);
        GersDedup::new(bridge)
    }

    #[test]
    fn should_skip_matched_id() {
        let dedup = make_dedup(vec![(100, "uuid-100".to_string())]);
        assert!(dedup.should_skip(100));
    }

    #[test]
    fn should_not_skip_unmatched_id() {
        let dedup = make_dedup(vec![(100, "uuid-100".to_string())]);
        assert!(!dedup.should_skip(999));
    }

    #[test]
    fn skipped_count_increments() {
        let dedup = make_dedup(vec![
            (100, "uuid-100".to_string()),
            (200, "uuid-200".to_string()),
        ]);

        dedup.should_skip(100); // skipped
        dedup.should_skip(200); // skipped
        dedup.should_skip(300); // kept

        assert_eq!(dedup.skipped_count(), 2);
        assert_eq!(dedup.kept_count(), 1);
    }

    #[test]
    fn kept_count_increments() {
        let dedup = make_dedup(vec![(100, "uuid-100".to_string())]);

        dedup.should_skip(999); // kept
        dedup.should_skip(888); // kept
        dedup.should_skip(100); // skipped

        assert_eq!(dedup.kept_count(), 2);
        assert_eq!(dedup.skipped_count(), 1);
    }
}