osmflow-sync 0.13.0

Imports OpenStreetMap PBF files into PostgreSQL and keeps them current from replication diffs
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
//! The import: a PBF file into the `osm` schema, the node file and the reverse index.

use std::hash::{BuildHasher as _, Hasher as _, RandomState};
use std::path::Path;
use std::sync::atomic::{AtomicU64, Ordering};
use std::time::{Instant, SystemTime};
use std::{fs, io, thread};

use osmflow::nodes::PagedStore;
use osmflow::nodes::prelude::*;
use osmflow::osc::{COMMIT_DELAY, Replication, Start, StateSource, plan};
use osmflow::{Location, PbfError, PbfReader, Select, Timestamp};
use postgres::Client;
use tracing::{debug, info, warn};

use crate::copy::Rows;
use crate::db::{self, DbState, SCHEMA_VERSION, Table, Writers};
use crate::files::{ID_KEY, remove_node_file};
use crate::index::{self, ReverseIndex};
use crate::sort::Sorter;
use crate::{Mirror, SyncError};

/// The revision of the imported data.
pub(crate) const IMPORT_REV: i64 = 1;

/// How to import.
#[derive(Debug, Clone)]
pub struct ImportOpts {
    /// Drop an existing `osm` schema first; without it, one is an error.
    pub replace: bool,
    /// The URL of the replication stream that continues the file, kept for updates, instead of the one its header
    /// names.
    pub replication_url: Option<String>,
    /// The stream's sequence the file is up to, instead of the one its header gives. Without either, it is found from
    /// the file's timestamp in the stream, as for a planet file.
    pub sequence: Option<u64>,
    /// The database connections rows are copied on, at once.
    pub connections: usize,
}

impl Default for ImportOpts {
    fn default() -> Self {
        Self {
            replace: false,
            replication_url: None,
            sequence: None,
            connections: crate::default_connections(),
        }
    }
}

/// What an import loaded.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct ImportStats {
    /// Nodes in the node file.
    pub nodes: u64,
    /// Rows of `osm.nodes`: the tagged nodes.
    pub tagged_nodes: u64,
    /// Rows of `osm.ways`.
    pub ways: u64,
    /// Rows of `osm.relations`.
    pub relations: u64,
    /// (node, way) pairs in the reverse index.
    pub pairs: u64,
}

pub(crate) fn import(
    m: &Mirror,
    pbf: &Path,
    replication: Option<&Replication>,
    opts: &ImportOpts,
) -> Result<ImportStats, SyncError> {
    let reader = PbfReader::open(pbf)?;
    let header = reader.header();
    let header_sequence = match header.replication_sequence.map(i64::cast_unsigned) {
        Some(h) if !same_url(opts, header.replication_base_url.as_ref()) => {
            header_stream(h, header.replication_timestamp, replication)?
        }
        h => h,
    };
    let (sequence, timestamp) = start(
        opts.sequence,
        header_sequence,
        header.replication_timestamp,
        replication,
    )?;
    let url = opts
        .replication_url
        .clone()
        .or_else(|| header.replication_base_url.clone());

    let mut client = db::connect(&m.url)?;
    create_schema(&mut client, opts.replace)?;
    fs::create_dir_all(&m.dir)?;
    remove_node_file(&m.node_path())?;
    index::remove(&m.index_path())?;
    let id = random_id();

    let t = Instant::now();
    let mut store = PagedStore::open(m.node_path())?;
    let mut stats = ImportStats {
        nodes: reader.load_nodes(&mut store)?,
        ..ImportStats::default()
    };
    store.preload()?;
    info!(
        nodes = stats.nodes,
        secs = t.elapsed().as_secs_f32(),
        "loaded the node file"
    );

    let t = Instant::now();
    let pairs = copy_objects(
        &reader,
        &store,
        &m.url,
        opts.connections,
        &mut stats,
        &m.dir,
    )?;
    info!(
        tagged_nodes = stats.tagged_nodes,
        ways = stats.ways,
        relations = stats.relations,
        secs = t.elapsed().as_secs_f32(),
        "copied the objects"
    );

    // The files and the database are finished at once: the index on this thread, the tables on connections of
    // their own
    let t = Instant::now();
    let (index, tables) = thread::scope(|s| {
        let tables = s.spawn(|| finish_tables(&m.url));
        let t = Instant::now();
        let index = pairs.finish().map_err(SyncError::from).and_then(|sorted| {
            ReverseIndex::create(
                &m.index_path(),
                id,
                IMPORT_REV,
                sorted.map(|p| p.map_err(SyncError::from)),
            )
        });
        debug!(secs = t.elapsed().as_secs_f32(), "built the reverse index");
        (
            index,
            tables
                .join()
                .unwrap_or_else(|_| Err(SyncError::state("finishing the tables panicked"))),
        )
    });
    stats.pairs = index?.1;
    tables?;
    store.set_metadata(ID_KEY, &id.to_le_bytes())?;
    store.commit(IMPORT_REV.cast_unsigned())?;
    info!(
        pairs = stats.pairs,
        secs = t.elapsed().as_secs_f32(),
        "built the reverse index and the primary keys"
    );

    let position = DbState {
        id,
        rev: IMPORT_REV,
        url,
        sequence,
        timestamp,
    };
    record(&mut client, &position, &stats)?;
    Ok(stats)
}

/// Records the import's batch and the database's state, which marks the import done.
fn record(client: &mut Client, at: &DbState, loaded: &ImportStats) -> Result<(), SyncError> {
    let timestamp = at.timestamp.map(Timestamp::as_millis);
    let mut tx = client.transaction()?;
    tx.execute(
        "INSERT INTO osm.batches (rev, first_sequence, last_sequence, timestamp, nodes, ways, relations) \
         VALUES ($1, $2, $2, to_timestamp($3::int8 / 1000.0), $4, $5, $6)",
        &[
            &at.rev,
            &at.sequence,
            &timestamp,
            &loaded.tagged_nodes.cast_signed(),
            &loaded.ways.cast_signed(),
            &loaded.relations.cast_signed(),
        ],
    )?;
    tx.execute(
        "INSERT INTO osm.state (id, version, rev, url, sequence, timestamp) \
         VALUES ($1, $2, $3, $4, $5, to_timestamp($6::int8 / 1000.0))",
        &[
            &at.id,
            &SCHEMA_VERSION,
            &at.rev,
            &at.url,
            &at.sequence,
            &timestamp,
        ],
    )?;
    tx.commit()?;
    Ok(())
}

/// Whether the stream is the one the header names: none other given, or the same URL.
fn same_url(opts: &ImportOpts, header_url: Option<&String>) -> bool {
    opts.replication_url
        .as_ref()
        .is_none_or(|u| Some(u) == header_url)
}

/// The header's sequence `h`, if it is of `replication`, another URL than the header's: a copy of the header's stream
/// has the header's time at `h`, or no state of it, as a directory of only the diffs after the file, and a stream not
/// given cannot say otherwise. Another stream's state at `h` has another time, and its sequence is found from the
/// file's time instead.
fn header_stream(
    h: u64,
    time: Option<Timestamp>,
    replication: Option<&Replication>,
) -> Result<Option<u64>, SyncError> {
    let Some(r) = replication else {
        return Ok(Some(h));
    };
    Ok(match r.get(h)? {
        Some(state) if Some(state.timestamp) != time => None,
        _ => Some(h),
    })
}

/// The sequence the file is up to, and the time of its state.
///
/// An extract's header gives its sequence on its own stream (`header`), which is exact: a later one given would skip
/// changes and is an error. Without it, as for a planet file, it is found or checked in the stream from the file's
/// timestamp: the file has the changes made up to then, and a change can show in a diff up to [`COMMIT_DELAY`] after
/// it is made, so a sequence after the last diff before then is an error. An earlier sequence only repeats changes the
/// file has, which applying again changes nothing in the end, so it is only warned of.
fn start(
    given: Option<u64>,
    header: Option<u64>,
    file_time: Option<Timestamp>,
    replication: Option<&Replication>,
) -> Result<(i64, Option<Timestamp>), SyncError> {
    // The last sequence the file is known to have, and its state's time
    let bound = match (header, replication, file_time) {
        (Some(h), ..) => Some((h, file_time)),
        (None, Some(r), Some(at)) => {
            let sources: [&dyn StateSource; 1] = [r];
            let first = plan(&sources, Start::Snapshot { at }, None)?
                .first()
                .map(|s| s.from);
            let since = first.map_or_else(|| r.state(), Ok)?;
            Some((since.sequence, Some(since.timestamp)))
        }
        (None, ..) => None,
    };
    match (given, bound) {
        (None, Some((b, time))) => {
            if header.is_none() {
                info!(sequence = b, "found the file's sequence from its timestamp");
            }
            Ok((b.cast_signed(), time))
        }
        (Some(g), Some((b, time))) if g == b => Ok((g.cast_signed(), time)),
        (Some(g), Some((b, _))) if g > b => Err(SyncError::state(match header {
            Some(_) => format!(
                "the header says the file is up to diff {b}, so diffs {} to {g} would be skipped; give sequence \
                 {b} or earlier",
                b + 1
            ),
            None => format!(
                "the file has the changes made up to {}, and a change can show in a diff {} minutes after it is \
                 made, so diffs {} to {g} may have changes it does not have; give sequence {b} or earlier",
                file_time.unwrap_or_default(),
                COMMIT_DELAY.as_secs() / 60,
                b + 1,
            ),
        })),
        (Some(g), bound) => {
            if let Some((b, _)) = bound {
                warn!(
                    "sequence {g} is {} diffs before the file's {b}: until they are applied, the database has older \
                     versions of the objects they change, and nodes they move add to their ways' minor",
                    b - g
                );
            }
            let time = replication
                .map(|r| r.sequence_state(g))
                .transpose()?
                .map(|s| s.timestamp);
            Ok((g.cast_signed(), time))
        }
        (None, None) => Err(SyncError::state(if file_time.is_some() {
            "the header gives no replication sequence; give the replication stream, to find it from the file's \
             timestamp, or the sequence"
        } else {
            "the header gives neither a replication sequence nor a timestamp; give the sequence"
        })),
    }
}

/// Creates the `osm` schema, after dropping an existing one if `replace`.
fn create_schema(client: &mut Client, replace: bool) -> Result<(), SyncError> {
    if replace {
        client.batch_execute("DROP SCHEMA IF EXISTS osm CASCADE")?;
    } else if client
        .query_one("SELECT to_regnamespace('osm') IS NOT NULL", &[])?
        .get(0)
    {
        return Err(SyncError::state(
            "the database already has an osm schema: replace it, or update it",
        ));
    }
    client.batch_execute(include_str!("schema.sql"))?;
    Ok(())
}

/// Copies the tagged nodes, the ways and the relations into their tables, and returns the reverse index's pairs, to
/// sort with temporary files in `dir`.
fn copy_objects(
    reader: &PbfReader,
    store: &PagedStore,
    url: &str,
    connections: usize,
    stats: &mut ImportStats,
    dir: &Path,
) -> Result<Sorter, SyncError> {
    let writers = Writers::start(url, connections)?;
    let pairs = Sorter::new(dir)?;
    let counts = [const { AtomicU64::new(0) }; 3];
    let read = reader.par_resolve(Select::all(), store, |block, locator| {
        let mut rows = [Rows::default(), Rows::default(), Rows::default()];
        for n in block.nodes().filter(|n| n.tags().len() > 0) {
            let loc = Location::new(n.decimicro_lat(), n.decimicro_lon());
            rows[0].node(n.id(), n.info().into(), IMPORT_REV, n.tags(), Some(loc));
        }
        let mut block_pairs = Vec::new();
        let mut locs = Vec::new();
        for w in block.ways() {
            locs.clear();
            let complete = w
                .refs()
                .all(|r| locator.location(r).map(|l| locs.push(l)).is_some());
            block_pairs.extend(
                w.refs()
                    .filter_map(|n| Some((u64::try_from(n).ok()?, w.id().cast_unsigned()))),
            );
            let line = complete.then_some(&locs[..]);
            rows[1].way(
                w.id(),
                w.info().into(),
                IMPORT_REV,
                w.tags(),
                w.refs(),
                line,
            );
        }
        for r in block.relations() {
            let members: Vec<_> = r.members().collect();
            rows[2].relation(r.id(), r.info().into(), IMPORT_REV, r.tags(), &members);
        }
        let mut sent = true;
        for ((rows, count), table) in
            rows.into_iter()
                .zip(&counts)
                .zip([Table::Nodes, Table::Ways, Table::Relations])
        {
            count.fetch_add(rows.count, Ordering::Relaxed);
            sent &= writers.send(table, rows);
        }
        pairs.extend(block_pairs).map_err(PbfError::Io)?;
        if sent {
            Ok(())
        } else {
            Err(PbfError::Io(io::Error::other(
                "the database connections stopped",
            )))
        }
    });
    // A failed connection stops the read too, so its error goes first
    writers.finish()?;
    read?;
    [stats.tagged_nodes, stats.ways, stats.relations] = counts.map(AtomicU64::into_inner);
    Ok(pairs)
}

/// Adds the primary keys and analyzes, each table on a connection of its own. Autovacuum, off during the load,
/// vacuums them later.
fn finish_tables(url: &str) -> Result<(), SyncError> {
    thread::scope(|s| {
        let tables = [Table::Nodes, Table::Ways, Table::Relations].map(|t| {
            s.spawn(move || -> Result<(), SyncError> {
                let mut client = db::connect(url)?;
                let name = t.name();
                for sql in [
                    format!("ALTER TABLE {name} ADD PRIMARY KEY (id)"),
                    format!("ANALYZE {name}"),
                    format!("ALTER TABLE {name} RESET (autovacuum_enabled)"),
                ] {
                    let t = Instant::now();
                    client.batch_execute(&sql)?;
                    debug!(secs = t.elapsed().as_secs_f32(), sql, "finished");
                }
                Ok(())
            })
        });
        tables.into_iter().try_for_each(|t| {
            t.join()
                .unwrap_or_else(|_| Err(SyncError::state("finishing a table panicked")))
        })
    })
}

/// A random positive identity for a new database. `RandomState` is keyed from the system's random source, once per
/// thread and then stepped; the time is hashed too, to tell apart imports that start alike.
fn random_id() -> i64 {
    let mut h = RandomState::new().build_hasher();
    let now = SystemTime::now().duration_since(SystemTime::UNIX_EPOCH);
    h.write_u128(now.map_or(0, |d| d.as_nanos()));
    (h.finish() >> 1).cast_signed()
}

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

    /// A stream whose diffs 1705 to 1709 are at 20:08, 20:14, 20:20, 20:21:01 and 20:22.
    fn stream(dir: &Path) -> Replication {
        let times = ["20:08:00", "20:14:00", "20:20:00", "20:21:01", "20:22:00"];
        fs::create_dir_all(dir.join("000/001")).unwrap();
        for (n, time) in (705..).zip(times) {
            let state = format!(
                "sequenceNumber={}\ntimestamp=2025-04-23T{}Z\n",
                1000 + n,
                time.replace(':', "\\:")
            );
            fs::write(dir.join(format!("000/001/{n}.state.txt")), &state).unwrap();
            if n == 709 {
                fs::write(dir.join("state.txt"), &state).unwrap();
            }
        }
        Replication::local(dir)
    }

    #[test]
    fn the_sequence_is_found_or_checked() {
        let dir = tempfile::tempdir().unwrap();
        let r = stream(dir.path());
        let at = |t: &str| format!("2025-04-23T{t}Z").parse::<Timestamp>().unwrap();
        let made = Some(at("20:21:01"));
        // As the header gives it, given again or not, without reading the stream; a later one is an error
        assert_eq!(start(None, Some(1708), made, None).unwrap(), (1708, made));
        assert_eq!(
            start(Some(1708), Some(1708), made, Some(&r)).unwrap(),
            (1708, made)
        );
        let err = start(Some(1709), Some(1708), made, Some(&r))
            .unwrap_err()
            .to_string();
        assert!(err.contains("give sequence 1708"), "{err}");
        assert_eq!(
            start(Some(1707), Some(1708), made, Some(&r)).unwrap(),
            (1707, Some(at("20:20:00")))
        );
        // From the file's time: changes made after 20:15:01 can be in diff 1707
        let found = (1706, Some(at("20:14:00")));
        assert_eq!(start(None, None, made, Some(&r)).unwrap(), found);
        assert_eq!(start(Some(1706), None, made, Some(&r)).unwrap(), found);
        let err = start(Some(1708), None, made, Some(&r))
            .unwrap_err()
            .to_string();
        assert!(err.contains("diffs 1707 to 1708"), "{err}");
        start(None, None, made, None).unwrap_err();
        assert_eq!(start(Some(1708), None, None, None).unwrap(), (1708, None));
    }

    #[test]
    fn a_copy_of_the_headers_stream_keeps_its_sequence() {
        let dir = tempfile::tempdir().unwrap();
        let r = stream(dir.path());
        let at = |t: &str| Some(format!("2025-04-23T{t}Z").parse::<Timestamp>().unwrap());
        // The same time at 1708, or no state of 1710: the header's stream; another time at 1708: another stream
        assert_eq!(
            header_stream(1708, at("20:21:01"), Some(&r)).unwrap(),
            Some(1708)
        );
        assert_eq!(
            header_stream(1710, at("20:23:00"), Some(&r)).unwrap(),
            Some(1710)
        );
        assert_eq!(header_stream(1708, at("20:30:00"), Some(&r)).unwrap(), None);
    }
}