use std::collections::{BTreeMap, BTreeSet, HashMap};
use std::time::Instant;
use osmflow::nodes::PagedStore;
use osmflow::nodes::prelude::*;
use osmflow::osc::{self, Action, Change, Object, Replication, StateSource as _};
use osmflow::{Location, Timestamp};
use postgres::{Client, Transaction};
use tracing::{debug, info, warn};
use crate::copy::Rows;
use crate::db::{self, DbState, Table};
use crate::files::{read_diffs, set_locations, way_pairs};
use crate::index::{Pair, ReverseIndex};
use crate::{GeometryFormat, OpenMirror, SyncError};
#[derive(Debug, Clone)]
pub struct UpdateOpts {
pub max_diffs: u64,
pub until: Option<u64>,
}
impl Default for UpdateOpts {
fn default() -> Self {
Self {
max_diffs: 60,
until: None,
}
}
}
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct UpdateStats {
pub batches: u64,
pub diffs: u64,
pub rows: u64,
pub moved_ways: u64,
pub sequence: i64,
}
#[derive(Debug, Default)]
pub(crate) struct Changes {
nodes: BTreeMap<i64, (bool, osc::Node)>,
ways: BTreeMap<i64, (bool, osc::Way)>,
relations: BTreeMap<i64, (bool, osc::Relation)>,
}
impl Changes {
pub(crate) fn add(&mut self, changes: Vec<Change>) {
for c in changes {
let deleted = c.action == Action::Delete;
match c.object {
Object::Node(n) => self.nodes.insert(n.id, (deleted, n)).map(drop),
Object::Way(w) => self.ways.insert(w.id, (deleted, w)).map(drop),
Object::Relation(r) => self.relations.insert(r.id, (deleted, r)).map(drop),
};
}
}
pub(crate) fn locations(&self) -> impl Iterator<Item = (u64, Option<Location>)> + '_ {
self.nodes.iter().filter_map(|(&id, (deleted, n))| {
Some((
u64::try_from(id).ok()?,
if *deleted { None } else { n.location },
))
})
}
fn len(&self) -> usize {
self.nodes.len() + self.ways.len() + self.relations.len()
}
}
impl OpenMirror {
pub fn update(
&mut self,
replication: &Replication,
opts: &UpdateOpts,
) -> Result<UpdateStats, SyncError> {
let mut state = DbState::read(&mut self.client)?;
check_stream(&state, replication)?;
self.sync_index(&state)?;
let mut store = self.take_node_file(&state, replication)?;
if !self.temp_tables {
create_temp_tables(&mut self.client)?;
self.temp_tables = true;
}
let total = apply_diffs(
&mut self.client,
&mut store,
&self.index,
&mut state,
replication,
opts,
)?;
self.store = Some(store);
Ok(total)
}
}
fn apply_diffs(
client: &mut Client,
store: &mut PagedStore,
index: &ReverseIndex,
state: &mut DbState,
replication: &Replication,
opts: &UpdateOpts,
) -> Result<UpdateStats, SyncError> {
let latest = match opts.until {
Some(until) => until,
None => replication.state()?.sequence,
};
let mut total = UpdateStats {
sequence: state.sequence,
..UpdateStats::default()
};
let mut next = state.sequence.cast_unsigned() + 1;
while next <= latest {
let t = Instant::now();
let last = latest.min(next + opts.max_diffs.max(1) - 1);
let (changes, last_state) = read_diffs(replication, next..=last)?;
let read = t.elapsed();
let batch = Batch {
rev: state.rev + 1,
first: next.cast_signed(),
last: last.cast_signed(),
timestamp: last_state.map(|s| s.timestamp),
geometry: state.geometry,
};
let applied = apply(client, store, index, &changes, &batch)?;
info!(
rev = batch.rev,
sequences = format!("{next}..={last}"),
changes = changes.len(),
rows = applied.rows,
moved_ways = applied.moved_ways,
read_secs = read.as_secs_f32(),
secs = t.elapsed().as_secs_f32(),
"applied a batch"
);
state.rev = batch.rev;
total.batches += 1;
total.diffs += last + 1 - next;
total.rows += applied.rows;
total.moved_ways += applied.moved_ways;
total.sequence = batch.last;
next = last + 1;
}
Ok(total)
}
pub(crate) fn check_stream(state: &DbState, replication: &Replication) -> Result<(), SyncError> {
let Some(time) = state.timestamp else {
return Ok(());
};
let sequence = state.sequence.cast_unsigned();
let Some(theirs) = replication.get(sequence)? else {
let latest = replication.state()?.sequence;
if latest < sequence {
return Err(SyncError::state(format!(
"the replication stream ends at diff {latest}, before the database's {sequence}: it is not the \
database's stream, {}",
state.url.as_deref().unwrap_or("which it does not name")
)));
}
warn!(
"the replication stream has no state of diff {sequence}, the database's, so it cannot be checked to be \
the database's stream"
);
return Ok(());
};
if theirs.timestamp == time {
Ok(())
} else {
Err(SyncError::state(format!(
"the replication stream's diff {sequence} is at {}, but the database's is at {time}: the stream is \
not the database's, {}",
theirs.timestamp,
state.url.as_deref().unwrap_or("which it does not name")
)))
}
}
struct Batch {
rev: i64,
first: i64,
last: i64,
timestamp: Option<Timestamp>,
geometry: GeometryFormat,
}
#[derive(Default)]
struct Writes {
rows: Rows,
written: Vec<i64>,
deleted: Vec<i64>,
}
#[derive(Default)]
struct Plan {
nodes: Writes,
ways: Writes,
relations: Writes,
geoms: Rows,
moved_ways: Vec<i64>,
remove: Vec<Pair>,
add: Vec<Pair>,
}
fn apply(
client: &mut Client,
store: &mut PagedStore,
index: &ReverseIndex,
changes: &Changes,
batch: &Batch,
) -> Result<UpdateStats, SyncError> {
let t = Instant::now();
let moved = set_locations(store, changes)?;
let through = index.ways_of(moved.iter().copied())?;
let mut ids: Vec<i64> = changes
.ways
.keys()
.copied()
.chain(through.iter().map(|p| p.1.cast_signed()))
.collect();
ids.sort_unstable();
ids.dedup();
let old: HashMap<i64, Vec<i64>> = client
.query("SELECT id, nodes FROM osm.ways WHERE id = ANY($1)", &[&ids])?
.into_iter()
.map(|r| (r.get(0), r.get(1)))
.collect();
let read = t.elapsed();
let plan = plan(store, changes, &through, &old, batch);
let planned = t.elapsed();
let rows = write(client, changes, &plan, batch)?;
let written = t.elapsed();
store.commit(batch.rev.cast_unsigned())?;
index.apply(&plan.remove, &plan.add, batch.rev)?;
debug!(
moved_nodes = moved.len(),
read_secs = read.as_secs_f32(),
encode_secs = planned.saturating_sub(read).as_secs_f32(),
db_secs = written.saturating_sub(planned).as_secs_f32(),
files_secs = t.elapsed().saturating_sub(written).as_secs_f32(),
"batch steps"
);
Ok(UpdateStats {
rows,
moved_ways: plan.moved_ways.len() as u64,
..UpdateStats::default()
})
}
fn plan(
store: &PagedStore,
changes: &Changes,
through: &[Pair],
old: &HashMap<i64, Vec<i64>>,
batch: &Batch,
) -> Plan {
let (rev, format) = (batch.rev, batch.geometry);
let mut plan = Plan::default();
plan.nodes.rows = Rows::new(format);
plan.ways.rows = Rows::new(format);
plan.geoms = Rows::new(format);
for (&id, (deleted, w)) in &changes.ways {
if let Some(nodes) = old.get(&id) {
plan.remove.extend(way_pairs(id, nodes));
}
if !deleted {
plan.add.extend(way_pairs(id, &w.refs));
}
}
let mut moved_ways = BTreeSet::new();
for &(node, way) in through {
let id = way.cast_signed();
match old.get(&id) {
_ if changes.ways.contains_key(&id) => {}
Some(nodes) if nodes.contains(&node.cast_signed()) => {
moved_ways.insert(id);
}
_ => plan.remove.push((node, way)),
}
}
for pairs in [&mut plan.remove, &mut plan.add] {
pairs.sort_unstable();
pairs.dedup();
}
let mut reader = store.reader();
let mut locs = Vec::new();
for (&id, (deleted, n)) in &changes.nodes {
if *deleted || n.tags.is_empty() {
plan.nodes.deleted.push(id);
} else {
plan.nodes.written.push(id);
plan.nodes
.rows
.node(id, (&n.meta).into(), rev, n.tags.iter(), n.location);
}
}
for (&id, (deleted, w)) in &changes.ways {
if *deleted {
plan.ways.deleted.push(id);
} else {
plan.ways.written.push(id);
let complete = locate(&mut reader, &w.refs, &mut locs);
let line = complete.then_some(&locs[..]);
let nodes = w.refs.iter().copied();
plan.ways
.rows
.way(id, (&w.meta).into(), rev, w.tags.iter(), nodes, line);
}
}
for &id in &moved_ways {
let complete = locate(&mut reader, &old[&id], &mut locs);
plan.geoms
.row(2)
.i64(id)
.line(complete.then_some(&locs[..]));
}
plan.moved_ways = moved_ways.into_iter().collect();
for (&id, (deleted, r)) in &changes.relations {
if *deleted {
plan.relations.deleted.push(id);
} else {
plan.relations.written.push(id);
plan.relations
.rows
.relation(id, (&r.meta).into(), rev, r.tags.iter(), r.members());
}
}
plan
}
fn write(
client: &mut Client,
changes: &Changes,
plan: &Plan,
batch: &Batch,
) -> Result<u64, SyncError> {
let rev = batch.rev;
let mut tx = client.transaction()?;
let mut log = Rows::default();
let mut rows = 0;
for (table, kind, w) in [
(Table::Nodes, b'n', &plan.nodes),
(Table::Ways, b'w', &plan.ways),
(Table::Relations, b'r', &plan.relations),
] {
let gone = if w.deleted.is_empty() {
Vec::new()
} else {
tx.query(
&format!("DELETE FROM {table} WHERE id = ANY($1) RETURNING id"),
&[&w.deleted],
)?
};
for row in &gone {
log.row(4).i64(rev).char(kind).i64(row.get(0)).char(b'd');
}
upsert(&mut tx, table, &w.rows)?;
for &id in &w.written {
log.row(4).i64(rev).char(kind).i64(id).char(b'u');
}
rows += gone.len() as u64 + w.rows.count;
}
if plan.geoms.count > 0 {
db::copy(&mut tx, "osm_geoms", "id, geom", &plan.geoms)?;
tx.execute(
"UPDATE osm.ways w SET geom = g.geom, minor = w.minor + 1, rev = $1 FROM osm_geoms g WHERE w.id = g.id",
&[&rev],
)?;
for &id in &plan.moved_ways {
log.row(4).i64(rev).char(b'w').i64(id).char(b'g');
}
}
if log.count > 0 {
db::copy(&mut tx, "osm.changes", "rev, kind, id, action", &log)?;
}
let timestamp = batch.timestamp.map(Timestamp::as_millis);
let count = |n: usize| i64::try_from(n).unwrap_or(i64::MAX);
tx.execute(
"INSERT INTO osm.batches (rev, first_sequence, last_sequence, timestamp, nodes, ways, relations) \
VALUES ($1, $2, $3, to_timestamp($4::int8 / 1000.0), $5, $6, $7)",
&[
&rev,
&batch.first,
&batch.last,
×tamp,
&count(changes.nodes.len()),
&count(changes.ways.len()),
&count(changes.relations.len()),
],
)?;
tx.execute(
"UPDATE osm.state SET rev = $1, sequence = $2, timestamp = to_timestamp($3::int8 / 1000.0)",
&[&rev, &batch.last, ×tamp],
)?;
tx.commit()?;
Ok(rows)
}
fn create_temp_tables(client: &mut Client) -> Result<(), SyncError> {
let tables = [Table::Nodes, Table::Ways, Table::Relations].map(|t| {
format!(
"CREATE TEMP TABLE {} (LIKE {t}) ON COMMIT DELETE ROWS;",
temp_name(t)
)
});
client.batch_execute(&format!(
"CREATE TEMP TABLE osm_geoms ON COMMIT DELETE ROWS AS SELECT id, geom FROM osm.ways WITH NO DATA; {}",
tables.concat()
))?;
Ok(())
}
fn temp_name(table: Table) -> String {
<&str>::from(table).replace('.', "_")
}
fn upsert(tx: &mut Transaction<'_>, table: Table, rows: &Rows) -> Result<(), SyncError> {
if rows.count == 0 {
return Ok(());
}
let (list, tmp) = (table.columns(), temp_name(table));
let rest = list.strip_prefix("id, ").unwrap_or(list);
let new = rest.replace(", ", ", EXCLUDED.");
db::copy(tx, &tmp, list, rows)?;
tx.execute(&format!("INSERT INTO {table} ({list}) SELECT {list} FROM {tmp} ON CONFLICT (id) DO UPDATE SET ({rest}) = ROW(EXCLUDED.{new})"), &[])?;
Ok(())
}
fn locate(reader: &mut impl NodeReader, refs: &[i64], locs: &mut Vec<Location>) -> bool {
locs.clear();
refs.iter().all(|&r| {
u64::try_from(r)
.ok()
.and_then(|r| reader.get(r))
.map(|l| locs.push(l))
.is_some()
})
}