1#![doc = include_str!("../README.md")]
2
3mod copy;
4mod db;
5mod error;
6mod files;
7mod import;
8mod index;
9mod rebuild;
10mod sort;
11mod update;
12
13use std::path::{Path, PathBuf};
14use std::{fmt, thread};
15
16use osmflow::nodes::PagedStore;
17use osmflow::nodes::prelude::*;
18use osmflow::osc::Replication;
19
20pub use crate::db::DbState;
21pub use crate::error::SyncError;
22pub use crate::import::{ImportOpts, ImportStats};
23pub use crate::rebuild::{Rebuild, RebuildStats};
24pub use crate::update::{UpdateOpts, UpdateStats};
25
26#[derive(Clone)]
28pub struct Mirror {
29 url: String,
30 dir: PathBuf,
31}
32
33impl fmt::Debug for Mirror {
35 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
36 f.debug_struct("Mirror")
37 .field("dir", &self.dir)
38 .finish_non_exhaustive()
39 }
40}
41
42#[derive(Debug, Clone, Copy, PartialEq, Eq)]
44pub struct FileStamp {
45 pub id: Option<i64>,
48 pub rev: i64,
50}
51
52#[derive(Debug)]
54pub struct Status {
55 pub db: DbState,
57 pub nodes: Result<FileStamp, SyncError>,
59 pub index: Result<FileStamp, SyncError>,
61}
62
63impl Mirror {
64 pub fn new(url: impl Into<String>, dir: impl Into<PathBuf>) -> Self {
67 Self {
68 url: url.into(),
69 dir: dir.into(),
70 }
71 }
72
73 pub fn import(
81 &self,
82 pbf: impl AsRef<Path>,
83 replication: Option<&Replication>,
84 opts: &ImportOpts,
85 ) -> Result<ImportStats, SyncError> {
86 import::import(self, pbf.as_ref(), replication, opts)
87 }
88
89 pub fn update(
92 &self,
93 replication: &Replication,
94 opts: &UpdateOpts,
95 ) -> Result<UpdateStats, SyncError> {
96 update::update(self, replication, opts)
97 }
98
99 pub fn rebuild(&self, from: &Rebuild<'_>) -> Result<RebuildStats, SyncError> {
101 rebuild::rebuild(self, from)
102 }
103
104 pub fn state(&self) -> Result<DbState, SyncError> {
106 DbState::read(&mut db::connect(&self.url)?)
107 }
108
109 pub fn status(&self) -> Result<Status, SyncError> {
111 let db = self.state()?;
112 let nodes = PagedStore::open_read_only(self.node_path())
113 .map(|s| FileStamp {
114 id: files::node_file_id(&s),
115 rev: s.stamp().cast_signed(),
116 })
117 .map_err(SyncError::from);
118 let index = index::ReverseIndex::open(&self.index_path())
119 .and_then(|i| i.stamp())
120 .map(|(id, rev)| FileStamp { id: Some(id), rev });
121 Ok(Status { db, nodes, index })
122 }
123
124 #[must_use]
126 pub fn node_path(&self) -> PathBuf {
127 self.dir.join("nodes.cache")
128 }
129
130 #[must_use]
132 pub fn index_path(&self) -> PathBuf {
133 self.dir.join("ways.redb")
134 }
135}
136
137fn default_connections() -> usize {
139 (thread::available_parallelism().map_or(1, usize::from) / 2).clamp(1, 8)
140}