pbfhogg 0.5.0

Fast OpenStreetMap PBF reader and writer for Rust. Read, write, and merge .osm.pbf files with pipelined parallel decoding.
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
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
//! Merge multiple sorted PBF files into one. Equivalent to `osmium merge`.
//!
//! Two-pass blob-level merge:
//!   Pass 1: Build blob index from all input files
//!   Pass 2: Write in sorted order - passthrough for non-overlapping blobs,
//!           decode + sweep merge with dedup for overlapping blobs.
//!
//! Exact duplicates (same type, ID, version) across input files are removed.
//! Common when merging geographic extracts that share border elements.

use std::cmp::Reverse;
use std::collections::BinaryHeap;
use std::fs::File;
use std::io::{self, Read, Seek, SeekFrom};
use std::path::Path;

use crate::Element;
use crate::blob::{
    BlobKind, decode_blob_to_headerblock, decode_blob_to_primitiveblock, decompress_blob_data_into,
    parse_blob_header_with_index,
};
use crate::blob_meta::{BlobIndex, ElemKind, scan_block_ids};
use crate::block_builder::BlockBuilder;
use crate::file_reader::FileReader;
use crate::file_writer::FileWriter;
use crate::writer::{Compression, PbfWriter, reframe_raw_with_index};

use crate::BoxResult as Result;
use crate::commands::{
    HeaderOverrides, build_output_header, flush_block, require_indexdata, require_sorted,
    writer_from_header_bytes,
};
use crate::owned::{
    OwnedNode, OwnedRelation, OwnedWay, read_dense_node, read_node, read_relation, read_way,
    write_single_node, write_single_relation, write_single_way,
};

/// Statistics from a multi-PBF merge operation.
pub struct MergePbfStats {
    pub nodes: u64,
    pub ways: u64,
    pub relations: u64,
    pub blobs_passthrough: u64,
    pub blobs_rewritten: u64,
    pub blobs_total: u64,
    pub duplicates_removed: u64,
}

impl MergePbfStats {
    pub fn print_summary(&self) {
        let total = self.nodes + self.ways + self.relations;
        eprintln!(
            "Merged {total} elements: {} nodes, {} ways, {} relations \
             ({} blobs: {} passthrough, {} rewritten, {} duplicates removed)",
            self.nodes,
            self.ways,
            self.relations,
            self.blobs_total,
            self.blobs_passthrough,
            self.blobs_rewritten,
            self.duplicates_removed,
        );
    }
}

/// Options for the multi-PBF merge command.
pub struct MergePbfOptions {
    pub compression: Compression,
    pub direct_io: bool,
    pub io_uring: bool,
    pub force: bool,
}

// ---------------------------------------------------------------------------
// Blob index entry
// ---------------------------------------------------------------------------

struct BlobEntry {
    file_offset: u64,
    frame_len: u64,
    index: BlobIndex,
    has_indexdata: bool,
    tagdata: Option<Box<[u8]>>,
    file_index: usize,
}

trait HasId {
    fn id(&self) -> i64;
    fn version(&self) -> i32;
}

impl HasId for OwnedNode {
    fn id(&self) -> i64 {
        self.id
    }
    fn version(&self) -> i32 {
        self.metadata.as_ref().map_or(0, |m| m.version)
    }
}

impl HasId for OwnedWay {
    fn id(&self) -> i64 {
        self.id
    }
    fn version(&self) -> i32 {
        self.metadata.as_ref().map_or(0, |m| m.version)
    }
}

impl HasId for OwnedRelation {
    fn id(&self) -> i64 {
        self.id
    }
    fn version(&self) -> i32 {
        self.metadata.as_ref().map_or(0, |m| m.version)
    }
}

// ---------------------------------------------------------------------------
// Public API
// ---------------------------------------------------------------------------

/// Merge multiple sorted PBF files into one, deduplicating exact duplicates.
///
/// All inputs must be sorted (Sort.Type_then_ID). Uses blob-level index for
/// fast passthrough of non-overlapping blobs; overlapping blobs are decoded,
/// merged, and deduped via a streaming sweep.
#[allow(clippy::too_many_lines)]
#[hotpath::measure]
pub fn merge_pbf(
    inputs: &[&Path],
    output: &Path,
    opts: &MergePbfOptions,
    overrides: &HeaderOverrides,
) -> Result<MergePbfStats> {
    if inputs.is_empty() {
        return Err("no input files specified".into());
    }

    // Validate inputs have indexdata
    for input in inputs {
        require_indexdata(
            input,
            opts.direct_io,
            opts.force,
            "input PBF has no blob-level indexdata. Without indexdata, every blob must be \
             decompressed to scan element IDs (significantly slower).",
        )?;
    }

    crate::debug::emit_marker("MERGEPBF_START");
    // Pass 1: Build blob index from all files
    eprintln!("Pass 1: indexing blobs...");
    let (header, mut entries) = build_blob_index(inputs, opts.direct_io)?;
    crate::commands::warn_locations_on_ways_loss(&header);
    eprintln!(
        "  {} OSMData blobs indexed from {} files",
        entries.len(),
        inputs.len()
    );

    // Sort by (type, osm_id)
    entries.sort_by_key(|e| {
        (
            type_order(e.index.kind),
            crate::osm_id::blob_osm_first_key(e.index.min_id, e.index.max_id),
        )
    });

    // Detect overlaps
    let overlaps = detect_overlaps(&entries);
    let overlap_count = overlaps.iter().filter(|&&b| b).count();
    if overlap_count > 0 {
        eprintln!("  {overlap_count} blobs in overlap runs (decode + merge + dedup)");
    }

    // Pass 2: Write in sorted order
    eprintln!("Pass 2: writing merged output...");
    #[allow(clippy::redundant_closure_for_method_calls)]
    let header_bytes = build_output_header(&header, false, overrides, |hb| hb.sorted())?;
    let mut writer = writer_from_header_bytes(
        output,
        opts.compression,
        &header_bytes,
        opts.direct_io,
        opts.io_uring,
    )?;

    // Open all input files for random access
    let mut files: Vec<File> = inputs.iter().map(File::open).collect::<io::Result<_>>()?;

    let mut stats = MergePbfStats {
        nodes: 0,
        ways: 0,
        relations: 0,
        blobs_passthrough: 0,
        blobs_rewritten: 0,
        blobs_total: entries.len() as u64,
        duplicates_removed: 0,
    };

    let mut bb = BlockBuilder::new();
    let mut frame_buf: Vec<u8> = Vec::new();

    let mut i = 0;
    while i < entries.len() {
        if overlaps[i] {
            let start = i;
            let run_kind = entries[i].index.kind;
            // Overlap runs must contain exactly one element kind.
            // `detect_overlaps` only sets `overlaps[j]` based on same-kind
            // adjacency, so the only way a kind boundary lands mid-run
            // here is when two same-kind overlap-runs sit adjacent in
            // file order (e.g. a node overlap-pair followed immediately
            // by a way overlap-pair). Grouping those into one
            // `write_overlap_run` call hands `entries[0].index.kind` to
            // `sweep_merge_dedup`, whose kind-gated extract closure then
            // silently drops every element whose kind doesn't match -
            // the exact shape of the `merge_pbf([A, A])` regression
            // where ways and relations disappeared.
            while i < entries.len() && overlaps[i] && entries[i].index.kind == run_kind {
                i += 1;
            }
            write_overlap_run(
                &entries[start..i],
                &mut files,
                &mut bb,
                &mut writer,
                &mut stats,
            )?;
        } else {
            write_passthrough_blob(
                &entries[i],
                &mut files[entries[i].file_index],
                &mut writer,
                &mut frame_buf,
            )?;
            count_entry(&entries[i], &mut stats);
            stats.blobs_passthrough += 1;
            i += 1;
        }
    }

    flush_block(&mut bb, &mut writer)?;
    writer.flush()?;
    crate::debug::emit_marker("MERGEPBF_END");
    Ok(stats)
}

// ---------------------------------------------------------------------------
// Pass 1: Build blob index from multiple files
// ---------------------------------------------------------------------------

fn build_blob_index(
    inputs: &[&Path],
    direct_io: bool,
) -> Result<(crate::HeaderBlock, Vec<BlobEntry>)> {
    let mut entries = Vec::new();
    let mut header: Option<crate::HeaderBlock> = None;
    let mut decompress_buf: Vec<u8> = Vec::new();
    let mut header_buf: Vec<u8> = Vec::new();
    let mut blob_buf: Vec<u8> = Vec::new();

    for (file_idx, input) in inputs.iter().enumerate() {
        let mut reader = FileReader::open(input, direct_io)?;
        let mut file_offset: u64 = 0;

        loop {
            let frame_start = file_offset;

            let mut len_buf = [0u8; 4];
            match reader.read_exact(&mut len_buf) {
                Ok(()) => {}
                Err(e) if e.kind() == io::ErrorKind::UnexpectedEof => break,
                Err(e) => return Err(e.into()),
            }
            #[allow(clippy::cast_possible_truncation)]
            let header_len = u32::from_be_bytes(len_buf) as usize;

            header_buf.resize(header_len, 0);
            reader.read_exact(&mut header_buf)?;

            let (blob_type, data_size, raw_index, tagdata) =
                parse_blob_header_with_index(&header_buf)?;
            let index = raw_index.as_ref().and_then(|d| BlobIndex::deserialize(d));
            let has_indexdata = index.is_some();

            let frame_len = (4 + header_len + data_size) as u64;
            file_offset += frame_len;

            match &blob_type {
                BlobKind::OsmHeader => {
                    blob_buf.resize(data_size, 0);
                    reader.read_exact(&mut blob_buf)?;
                    let hdr = decode_blob_to_headerblock(&blob_buf)?;
                    require_sorted(&hdr, input, &format!("Input file {}", input.display()))?;
                    if header.is_none() {
                        header = Some(hdr);
                    }
                }
                BlobKind::OsmData if has_indexdata => {
                    reader.skip(data_size as u64)?;
                    #[allow(clippy::unwrap_used)]
                    entries.push(BlobEntry {
                        file_offset: frame_start,
                        frame_len,
                        index: index.unwrap(),
                        has_indexdata,
                        tagdata,
                        file_index: file_idx,
                    });
                }
                BlobKind::OsmData => {
                    blob_buf.resize(data_size, 0);
                    reader.read_exact(&mut blob_buf)?;
                    decompress_blob_data_into(&blob_buf, &mut decompress_buf)?;
                    let blob_index = scan_block_ids(&decompress_buf).ok_or_else(|| {
                        io::Error::new(io::ErrorKind::InvalidData, "failed to scan block IDs")
                    })?;
                    entries.push(BlobEntry {
                        file_offset: frame_start,
                        frame_len,
                        index: blob_index,
                        has_indexdata,
                        tagdata,
                        file_index: file_idx,
                    });
                }
                _ => {
                    reader.skip(data_size as u64)?;
                }
            }
        }
    }

    let header = header.ok_or_else(|| {
        io::Error::new(
            io::ErrorKind::InvalidData,
            "no OSMHeader blob found in any input file",
        )
    })?;

    Ok((header, entries))
}

// ---------------------------------------------------------------------------
// Sort + overlap detection
// ---------------------------------------------------------------------------

fn type_order(kind: ElemKind) -> u8 {
    match kind {
        ElemKind::Node => 0,
        ElemKind::Way => 1,
        ElemKind::Relation => 2,
    }
}

fn detect_overlaps(entries: &[BlobEntry]) -> Vec<bool> {
    let mut overlaps = vec![false; entries.len()];
    for i in 0..entries.len().saturating_sub(1) {
        if entries[i].index.kind == entries[i + 1].index.kind
            && crate::osm_id::blob_osm_last_key(entries[i].index.min_id, entries[i].index.max_id)
                >= crate::osm_id::blob_osm_first_key(
                    entries[i + 1].index.min_id,
                    entries[i + 1].index.max_id,
                )
        {
            overlaps[i] = true;
            overlaps[i + 1] = true;
        }
    }
    overlaps
}

// ---------------------------------------------------------------------------
// Pass 2: Passthrough write
// ---------------------------------------------------------------------------

fn write_passthrough_blob(
    entry: &BlobEntry,
    file: &mut File,
    writer: &mut PbfWriter<FileWriter>,
    frame_buf: &mut Vec<u8>,
) -> Result<()> {
    read_frame_into(file, entry, frame_buf)?;
    if entry.has_indexdata {
        writer.write_raw(frame_buf)?;
    } else {
        let blob_bytes = extract_blob_bytes(frame_buf)?;
        let reframed = reframe_raw_with_index(
            blob_bytes,
            &entry.index.serialize(),
            entry.tagdata.as_deref(),
        )?;
        writer.write_raw(&reframed)?;
    }
    Ok(())
}

fn read_frame_into(file: &mut File, entry: &BlobEntry, buf: &mut Vec<u8>) -> io::Result<()> {
    file.seek(SeekFrom::Start(entry.file_offset))?;
    #[allow(clippy::cast_possible_truncation)]
    let len = entry.frame_len as usize;
    buf.clear();
    buf.resize(len, 0);
    file.read_exact(buf)?;
    Ok(())
}

fn extract_blob_bytes(frame: &[u8]) -> Result<&[u8]> {
    if frame.len() < 4 {
        return Err(io::Error::new(io::ErrorKind::InvalidData, "frame too short").into());
    }
    #[allow(clippy::cast_possible_truncation)]
    let header_len = u32::from_be_bytes([frame[0], frame[1], frame[2], frame[3]]) as usize;
    let blob_start = 4 + header_len;
    if blob_start > frame.len() {
        return Err(io::Error::new(io::ErrorKind::InvalidData, "invalid header length").into());
    }
    Ok(&frame[blob_start..])
}

fn count_entry(entry: &BlobEntry, stats: &mut MergePbfStats) {
    match entry.index.kind {
        ElemKind::Node => stats.nodes += entry.index.count,
        ElemKind::Way => stats.ways += entry.index.count,
        ElemKind::Relation => stats.relations += entry.index.count,
    }
}

// ---------------------------------------------------------------------------
// Pass 2: Overlap run rewrite with dedup
// ---------------------------------------------------------------------------

fn write_overlap_run(
    entries: &[BlobEntry],
    files: &mut [File],
    bb: &mut BlockBuilder,
    writer: &mut PbfWriter<FileWriter>,
    stats: &mut MergePbfStats,
) -> Result<()> {
    let kind = entries[0].index.kind;
    match kind {
        ElemKind::Node => {
            let (c, d) = sweep_merge_dedup(
                entries,
                files,
                bb,
                writer,
                |e, heap| match e {
                    Element::DenseNode(dn) => heap.push(Reverse(read_dense_node(dn))),
                    Element::Node(n) => heap.push(Reverse(read_node(n))),
                    _ => {}
                },
                write_single_node,
            )?;
            stats.nodes += c;
            stats.duplicates_removed += d;
        }
        ElemKind::Way => {
            let (c, d) = sweep_merge_dedup(
                entries,
                files,
                bb,
                writer,
                |e, heap| {
                    if let Element::Way(w) = e {
                        heap.push(Reverse(read_way(w)));
                    }
                },
                write_single_way,
            )?;
            stats.ways += c;
            stats.duplicates_removed += d;
        }
        ElemKind::Relation => {
            let (c, d) = sweep_merge_dedup(
                entries,
                files,
                bb,
                writer,
                |e, heap| {
                    if let Element::Relation(r) = e {
                        heap.push(Reverse(read_relation(r)));
                    }
                },
                write_single_relation,
            )?;
            stats.relations += c;
            stats.duplicates_removed += d;
        }
    };
    stats.blobs_rewritten += entries.len() as u64;
    Ok(())
}

// ---------------------------------------------------------------------------
// Sweep merge per element type - with dedup
// ---------------------------------------------------------------------------

/// Generic sweep merge: heap-based merge of overlapping blob entries with
/// dedup (skip elements with same id + version as the just-emitted element).
/// Returns (elements_written, duplicates_removed).
#[cfg_attr(feature = "hotpath", hotpath::measure)]
fn sweep_merge_dedup<T: Ord + HasId>(
    entries: &[BlobEntry],
    files: &mut [File],
    bb: &mut BlockBuilder,
    writer: &mut PbfWriter<FileWriter>,
    mut extract: impl FnMut(&Element<'_>, &mut BinaryHeap<Reverse<T>>),
    mut write_elem: impl FnMut(&T, &mut BlockBuilder, &mut PbfWriter<FileWriter>) -> Result<()>,
) -> Result<(u64, u64)> {
    let mut heap: BinaryHeap<Reverse<T>> = BinaryHeap::new();
    let mut frame_buf: Vec<u8> = Vec::new();
    let mut count: u64 = 0;
    let mut deduped: u64 = 0;

    for entry in entries {
        flush_heap_below_dedup(
            &mut heap,
            crate::osm_id::blob_osm_first_id(entry.index.min_id, entry.index.max_id),
            &mut deduped,
            |elem| {
                write_elem(&elem, bb, writer)?;
                count += 1;
                Ok(())
            },
        )?;

        read_frame_into(&mut files[entry.file_index], entry, &mut frame_buf)?;
        let blob_bytes = extract_blob_bytes(&frame_buf)?;
        let block = decode_blob_to_primitiveblock(blob_bytes)?;
        for element in block.elements() {
            extract(&element, &mut heap);
        }
    }

    // Drain remaining with dedup
    while let Some(Reverse(elem)) = heap.pop() {
        let eid = elem.id();
        let ever = elem.version();
        write_elem(&elem, bb, writer)?;
        count += 1;
        while heap
            .peek()
            .is_some_and(|Reverse(e)| e.id() == eid && e.version() == ever)
        {
            heap.pop();
            deduped += 1;
        }
    }
    flush_block(bb, writer)?;
    Ok((count, deduped))
}

/// Flush elements from the min-heap whose ID is below `below`, with dedup.
fn flush_heap_below_dedup<T: Ord + HasId>(
    heap: &mut BinaryHeap<Reverse<T>>,
    below: i64,
    dedup_count: &mut u64,
    mut emit: impl FnMut(T) -> Result<()>,
) -> Result<()> {
    while heap
        .peek()
        .is_some_and(|Reverse(e)| crate::osm_id::osm_id_cmp(e.id(), below).is_lt())
    {
        if let Some(Reverse(element)) = heap.pop() {
            let eid = element.id();
            let ever = element.version();
            emit(element)?;
            // Skip exact duplicates (same id + version)
            while heap
                .peek()
                .is_some_and(|Reverse(e)| e.id() == eid && e.version() == ever)
            {
                heap.pop();
                *dedup_count += 1;
            }
        }
    }
    Ok(())
}