#![doc = include_str!("../README.md")]
mod copy;
mod db;
mod error;
mod files;
mod follow;
mod import;
mod index;
mod rebuild;
mod sort;
mod stop;
mod tls;
mod update;
use std::path::{Path, PathBuf};
use std::{fmt, thread};
use osmflow::nodes::PagedStore;
use osmflow::nodes::prelude::*;
use osmflow::osc::Replication;
use postgres::Client;
pub use crate::db::{DbState, GeometryFormat};
pub use crate::error::SyncError;
pub use crate::follow::{FollowError, FollowOpts};
pub use crate::import::{ImportOpts, ImportStats};
pub use crate::rebuild::{Rebuild, RebuildStats};
#[cfg(feature = "signals")]
pub use crate::stop::{Signal, SignalError, SignalGuard};
pub use crate::stop::{Stop, StopReason};
pub use crate::tls::TlsError;
pub use crate::update::{UpdateOpts, UpdateStats};
#[derive(Clone)]
pub struct Mirror {
url: String,
dir: PathBuf,
}
impl fmt::Debug for Mirror {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Mirror")
.field("dir", &self.dir)
.finish_non_exhaustive()
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct FileStamp {
pub id: Option<i64>,
pub rev: i64,
}
#[derive(Debug)]
pub struct Status {
pub db: DbState,
pub nodes: Result<FileStamp, SyncError>,
pub index: Result<FileStamp, SyncError>,
}
impl Mirror {
pub fn new(url: impl Into<String>, dir: impl Into<PathBuf>) -> Self {
Self {
url: url.into(),
dir: dir.into(),
}
}
pub fn import(
&self,
pbf: impl AsRef<Path>,
replication: Option<&Replication>,
opts: &ImportOpts,
) -> Result<ImportStats, SyncError> {
import::import(self, pbf.as_ref(), replication, opts)
}
pub fn update(
&self,
replication: &Replication,
opts: &UpdateOpts,
) -> Result<UpdateStats, SyncError> {
self.open()?.update(replication, opts)
}
pub fn open(&self) -> Result<OpenMirror, SyncError> {
let mut client = db::connect(&self.url)?;
let state = DbState::read(&mut client)?;
let mut open = OpenMirror {
mirror: self.clone(),
client,
index: index::ReverseIndex::open(&self.index_path())?,
store: None,
temp_tables: false,
};
open.sync_index(&state)?;
Ok(open)
}
pub fn rebuild(&self, from: &Rebuild<'_>) -> Result<RebuildStats, SyncError> {
rebuild::rebuild(self, from)
}
pub fn state(&self) -> Result<DbState, SyncError> {
DbState::read(&mut db::connect(&self.url)?)
}
pub fn status(&self) -> Result<Status, SyncError> {
let db = self.state()?;
let nodes = PagedStore::open_read_only(self.node_path())
.map(|s| FileStamp {
id: files::node_file_id(&s),
rev: s.stamp().cast_signed(),
})
.map_err(SyncError::from);
let index = index::ReverseIndex::open(&self.index_path())
.and_then(|i| i.stamp())
.map(|(id, rev)| FileStamp { id: Some(id), rev });
Ok(Status { db, nodes, index })
}
#[must_use]
pub fn node_path(&self) -> PathBuf {
self.dir.join("nodes.cache")
}
#[must_use]
pub fn index_path(&self) -> PathBuf {
self.dir.join("ways.redb")
}
}
pub struct OpenMirror {
mirror: Mirror,
client: Client,
index: index::ReverseIndex,
store: Option<PagedStore>,
temp_tables: bool,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct NodeWay {
pub node: i64,
pub way: i64,
}
impl OpenMirror {
pub fn ways_through(&self, nodes: &[i64]) -> Result<Vec<NodeWay>, SyncError> {
let nodes = nodes.iter().filter_map(|&n| u64::try_from(n).ok());
Ok(self
.index
.ways_of(nodes)?
.into_iter()
.map(|(node, way)| NodeWay {
node: node.cast_signed(),
way: way.cast_signed(),
})
.collect())
}
}
fn default_connections() -> usize {
(thread::available_parallelism().map_or(1, usize::from) / 2).clamp(1, 8)
}