pbfhogg 0.4.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
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
//! Merge multiple OSC files into a single OSC stream.
//!
//! Default mode preserves the full change stream in input order.
//! `--simplify` keeps only the last change per object (type + id).

use std::cell::Cell;
use std::collections::BTreeMap;
use std::fs::File;
use std::io::{self, BufReader, Seek, Write};
use std::path::Path;
use std::rc::Rc;
use std::time::{Duration, Instant};

use flate2::read::MultiGzDecoder;
use flate2::write::GzEncoder;
use quick_xml::events::{BytesDecl, BytesEnd, BytesStart, Event};
use quick_xml::name::QName;
use quick_xml::{Reader, Writer};
use rayon::prelude::*;

use crate::osc::write::{
    OwnedMember, OwnedMetadata, OwnedNode, OwnedRelation, OwnedWay,
    write_node_xml, write_way_xml, write_relation_xml, write_delete_xml,
};
use super::Result;
use crate::MemberId;

/// Wraps an `io::Read` and accumulates wall time spent in `read()` calls.
///
/// Used to attribute the gzip-decompress portion of `parse_osc_streaming`
/// and `parse_osc_into` separately from the surrounding `quick_xml` parse
/// machinery via the `merge_changes_decompress_ns` sidecar counter. The
/// shared `Rc<Cell<Duration>>` lets the wrapped reader live behind a
/// `Box<dyn io::Read>` while the outer parse function still recovers the
/// total at end-of-call.
struct TimedRead<R: io::Read> {
    inner: R,
    total: Rc<Cell<Duration>>,
}

impl<R: io::Read> io::Read for TimedRead<R> {
    fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
        let start = Instant::now();
        let result = self.inner.read(buf);
        self.total.set(self.total.get() + start.elapsed());
        result
    }
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum Action {
    Create,
    Modify,
    Delete,
}

#[derive(Clone, Debug)]
enum ChangeElement {
    Node(OwnedNode),
    Way(OwnedWay),
    Relation(OwnedRelation),
}

impl ChangeElement {
    fn key(&self) -> (u8, i64) {
        match self {
            Self::Node(n) => (0, n.id),
            Self::Way(w) => (1, w.id),
            Self::Relation(r) => (2, r.id),
        }
    }
}

#[derive(Clone, Debug)]
struct Change {
    action: Action,
    element: ChangeElement,
}

#[derive(Default)]
struct ChangeStream {
    changes: Vec<Change>,
}

impl ChangeStream {
    fn push(&mut self, action: Action, element: ChangeElement) {
        self.changes.push(Change { action, element });
    }
}

#[derive(Debug, Default)]
pub struct MergeChangesStats {
    pub files: usize,
    pub changes_in: u64,
    pub changes_out: u64,
    pub simplified: bool,
}

impl MergeChangesStats {
    pub fn print_summary(&self) {
        if self.simplified {
            eprintln!(
                "Merged {} files: {} input changes -> {} output changes (simplified)",
                self.files, self.changes_in, self.changes_out
            );
        } else {
            eprintln!("Merged {} files: {} changes", self.files, self.changes_out);
        }
    }
}

#[derive(Clone, Copy, PartialEq, Eq)]
enum Section {
    None,
    Create,
    Modify,
    Delete,
}

impl Section {
    fn as_action(self) -> Option<Action> {
        match self {
            Self::Create => Some(Action::Create),
            Self::Modify => Some(Action::Modify),
            Self::Delete => Some(Action::Delete),
            Self::None => None,
        }
    }
}

#[derive(Clone, Copy, PartialEq, Eq)]
enum ElemKind {
    Node,
    Way,
    Relation,
}

struct CurrentElem {
    kind: ElemKind,
    id: i64,
    decimicro_lat: i32,
    decimicro_lon: i32,
    metadata: Option<OwnedMetadata>,
    tags: Vec<(String, String)>,
    refs: Vec<i64>,
    members: Vec<OwnedMember>,
}

impl CurrentElem {
    fn new(kind: ElemKind, id: i64, metadata: Option<OwnedMetadata>) -> Self {
        Self {
            kind,
            id,
            decimicro_lat: 0,
            decimicro_lon: 0,
            metadata,
            tags: Vec::new(),
            refs: Vec::new(),
            members: Vec::new(),
        }
    }

    fn into_change_element(self) -> ChangeElement {
        match self.kind {
            ElemKind::Node => ChangeElement::Node(OwnedNode {
                id: self.id,
                decimicro_lat: self.decimicro_lat,
                decimicro_lon: self.decimicro_lon,
                tags: self.tags,
                metadata: self.metadata,
            }),
            ElemKind::Way => ChangeElement::Way(OwnedWay {
                id: self.id,
                refs: self.refs,
                tags: self.tags,
                metadata: self.metadata,
            }),
            ElemKind::Relation => ChangeElement::Relation(OwnedRelation {
                id: self.id,
                members: self.members,
                tags: self.tags,
                metadata: self.metadata,
            }),
        }
    }
}

#[hotpath::measure]
pub fn merge_changes(
    inputs: &[&Path],
    output: &Path,
    simplify: bool,
    jobs: Option<usize>,
) -> Result<MergeChangesStats> {
    if inputs.is_empty() {
        return Err("at least one input OSC file is required".into());
    }

    let worker_count = match jobs {
        Some(n) if n > 0 => n,
        _ => std::thread::available_parallelism()
            .map(std::num::NonZeroUsize::get)
            .unwrap_or(8),
    };

    crate::debug::emit_marker("MERGECHANGES_START");
    #[allow(clippy::cast_possible_wrap)]
    crate::debug::emit_counter("merge_changes_files", inputs.len() as i64);
    #[allow(clippy::cast_possible_wrap)]
    crate::debug::emit_counter("merge_changes_worker_count", worker_count as i64);
    let total_bytes_in: u64 = inputs
        .iter()
        .filter_map(|p| std::fs::metadata(p).ok())
        .map(|m| m.len())
        .sum();
    #[allow(clippy::cast_possible_wrap)]
    crate::debug::emit_counter("merge_changes_total_bytes_in", total_bytes_in as i64);

    // Build a scoped rayon pool when the caller passed an explicit `jobs`.
    // Pool controls par_iter and par_chunks thread count for both the
    // streaming and simplify paths. When `jobs` is None, the global rayon
    // pool is used (current default behaviour).
    let pool = if jobs.is_some() {
        Some(
            rayon::ThreadPoolBuilder::new()
                .num_threads(worker_count)
                .build()
                .map_err(|e| -> Box<dyn std::error::Error> { e.to_string().into() })?,
        )
    } else {
        None
    };

    // The closure converts errors to String so it is Send across the
    // pool.install boundary; the project's `BoxResult` wraps a non-Send
    // `Box<dyn Error>` which `rayon::ThreadPool::install` cannot bridge.
    let do_work = || -> std::result::Result<(u64, u64), String> {
        if simplify {
            let stream = build_simplify_stream(inputs)?;
            let changes_in = stream.changes.len() as u64;
            let changes_out = write_simplified(output, stream, worker_count)
                .map_err(|e| e.to_string())? as u64;
            Ok((changes_in, changes_out))
        } else {
            let changes_out =
                write_streaming(inputs, output).map_err(|e| e.to_string())?;
            Ok((changes_out, changes_out))
        }
    };

    let (changes_in, changes_out) = match &pool {
        Some(p) => p.install(do_work),
        None => do_work(),
    }
    .map_err(|s| -> Box<dyn std::error::Error> { s.into() })?;

    #[allow(clippy::cast_possible_wrap)]
    crate::debug::emit_counter("merge_changes_changes_out", changes_out as i64);
    if let Ok(meta) = std::fs::metadata(output) {
        #[allow(clippy::cast_possible_wrap)]
        crate::debug::emit_counter("merge_changes_total_bytes_out", meta.len() as i64);
    }
    crate::debug::emit_marker("MERGECHANGES_END");

    Ok(MergeChangesStats {
        files: inputs.len(),
        changes_in,
        changes_out,
        simplified: simplify,
    })
}

/// Build the simplify path's pre-dedupe `ChangeStream` from N inputs.
///
/// N <= 1: serial, same as the pre-parallel shape. N > 1: rayon
/// `par_iter` fan-out into per-input streams, concatenated in input order
/// so `write_simplified`'s "later inputs win" `BTreeMap` dedupe still
/// observes inputs in sequence order. Errors are returned as `String`
/// so the closure can be `Send` across the `rayon::ThreadPool::install`
/// boundary in `merge_changes`.
fn build_simplify_stream(inputs: &[&Path]) -> std::result::Result<ChangeStream, String> {
    if inputs.len() <= 1 {
        let mut stream = ChangeStream::default();
        for path in inputs {
            parse_one_into_stream(path, &mut stream).map_err(|e| e.to_string())?;
        }
        return Ok(stream);
    }

    for path in inputs {
        if let Ok(meta) = std::fs::metadata(path) {
            #[allow(clippy::cast_possible_wrap)]
            crate::debug::emit_counter("merge_changes_input_bytes", meta.len() as i64);
        }
    }

    crate::debug::emit_marker("MERGECHANGES_PARALLEL_PARSE_START");
    let streams: std::result::Result<Vec<ChangeStream>, String> = inputs
        .par_iter()
        .map(|path| -> std::result::Result<ChangeStream, String> {
            let mut s = ChangeStream::default();
            parse_osc_into(path, &mut s).map_err(|e| e.to_string())?;
            Ok(s)
        })
        .collect();
    let streams = streams?;
    crate::debug::emit_marker("MERGECHANGES_PARALLEL_PARSE_END");

    let mut combined = ChangeStream::default();
    let total_changes: usize = streams.iter().map(|s| s.changes.len()).sum();
    combined.changes.reserve(total_changes);
    for s in streams {
        let len_before = combined.changes.len();
        combined.changes.extend(s.changes);
        #[allow(clippy::cast_possible_wrap)]
        crate::debug::emit_counter(
            "merge_changes_changes_per_osc",
            (combined.changes.len() - len_before) as i64,
        );
    }
    Ok(combined)
}

/// Wrap `parse_osc_into` in the `MERGECHANGES_PARSE_{START,END}` pair so
/// `brokkr sidecar --durations` can measure each input's parse wall, and
/// emit `merge_changes_changes_per_osc` on the post-parse stream length
/// delta so the per-input change count is observable in the sidecar.
fn parse_one_into_stream(path: &Path, stream: &mut ChangeStream) -> Result<()> {
    if let Ok(meta) = std::fs::metadata(path) {
        #[allow(clippy::cast_possible_wrap)]
        crate::debug::emit_counter("merge_changes_input_bytes", meta.len() as i64);
    }
    let len_before = stream.changes.len();
    crate::debug::emit_marker("MERGECHANGES_PARSE_START");
    let result = parse_osc_into(path, stream);
    crate::debug::emit_marker("MERGECHANGES_PARSE_END");
    #[allow(clippy::cast_possible_wrap)]
    crate::debug::emit_counter(
        "merge_changes_changes_per_osc",
        (stream.changes.len() - len_before) as i64,
    );
    result
}

#[hotpath::measure]
fn parse_osc_into(path: &Path, stream: &mut ChangeStream) -> Result<()> {
    let mut file = File::open(path)?;
    let mut magic = [0u8; 2];
    io::Read::read_exact(&mut file, &mut magic)?;
    file.seek(io::SeekFrom::Start(0))?;

    let decompress_total = Rc::new(Cell::new(Duration::ZERO));
    let reader: Reader<BufReader<Box<dyn io::Read>>> = if magic == [0x1f, 0x8b] {
        let timed = TimedRead {
            inner: MultiGzDecoder::new(file),
            total: Rc::clone(&decompress_total),
        };
        Reader::from_reader(BufReader::new(Box::new(timed)))
    } else {
        let timed = TimedRead {
            inner: file,
            total: Rc::clone(&decompress_total),
        };
        Reader::from_reader(BufReader::new(Box::new(timed)))
    };
    let mut reader = reader;
    reader.config_mut().trim_text(true);

    let mut section = Section::None;
    let mut current: Option<CurrentElem> = None;
    let mut buf = Vec::new();

    loop {
        match reader.read_event_into(&mut buf) {
            Ok(Event::Start(ref e)) => {
                handle_start_like(e, false, &mut section, &mut current, stream)?;
            }
            Ok(Event::Empty(ref e)) => {
                handle_start_like(e, true, &mut section, &mut current, stream)?;
            }
            Ok(Event::End(ref e)) => match e.name().as_ref() {
                b"create" | b"modify" | b"delete" => section = Section::None,
                b"node" | b"way" | b"relation" => {
                    finalize_current(section, &mut current, stream);
                }
                _ => {}
            },
            Ok(Event::Eof) => break,
            Ok(_) => {}
            Err(e) => return Err(Box::new(e)),
        }
        buf.clear();
    }

    #[allow(clippy::cast_possible_wrap, clippy::cast_possible_truncation)]
    crate::debug::emit_counter(
        "merge_changes_decompress_ns",
        decompress_total.get().as_nanos() as i64,
    );
    Ok(())
}

fn handle_start_like(
    e: &BytesStart<'_>,
    is_empty: bool,
    section: &mut Section,
    current: &mut Option<CurrentElem>,
    stream: &mut ChangeStream,
) -> Result<()> {
    match e.name().as_ref() {
        b"create" => *section = Section::Create,
        b"modify" => *section = Section::Modify,
        b"delete" => *section = Section::Delete,
        b"node" | b"way" | b"relation" => {
            let kind = match e.name().as_ref() {
                b"node" => ElemKind::Node,
                b"way" => ElemKind::Way,
                _ => ElemKind::Relation,
            };
            let id = parse_i64_attr(e, b"id")?;
            let metadata = parse_metadata(e);
            let mut elem = CurrentElem::new(kind, id, metadata);

            if kind == ElemKind::Node {
                let lat = parse_f64_attr_optional(e, b"lat").unwrap_or(0.0);
                let lon = parse_f64_attr_optional(e, b"lon").unwrap_or(0.0);
                #[allow(clippy::cast_possible_truncation)]
                {
                    elem.decimicro_lat = (lat * 1e7).round() as i32;
                    elem.decimicro_lon = (lon * 1e7).round() as i32;
                }
            }

            if is_empty || *section == Section::Delete {
                let action = section
                    .as_action()
                    .ok_or_else(|| "element outside create/modify/delete section".to_string())?;
                stream.push(action, elem.into_change_element());
            } else {
                *current = Some(elem);
            }
        }
        b"tag" => {
            if let Some(cur) = current {
                let k = parse_str_attr(e, b"k")?;
                let v = parse_str_attr(e, b"v")?;
                cur.tags.push((k, v));
            }
        }
        b"nd" => {
            if let Some(cur) = current
                && cur.kind == ElemKind::Way
            {
                let rf = parse_i64_attr(e, b"ref")?;
                cur.refs.push(rf);
            }
        }
        b"member" => {
            if let Some(cur) = current
                && cur.kind == ElemKind::Relation
            {
                let ref_id = parse_i64_attr(e, b"ref")?;
                let role = parse_str_attr_optional(e, b"role").unwrap_or_default();
                let member_id = match parse_str_attr(e, b"type")?.as_str() {
                    "node" => MemberId::Node(ref_id),
                    "way" => MemberId::Way(ref_id),
                    "relation" => MemberId::Relation(ref_id),
                    other => {
                        return Err(format!("unknown relation member type '{other}'").into());
                    }
                };
                cur.members.push(OwnedMember {
                    id: member_id,
                    role,
                });
            }
        }
        _ => {}
    }

    if is_empty {
        match e.name().as_ref() {
            b"create" | b"modify" | b"delete" => *section = Section::None,
            _ => {}
        }
    }

    Ok(())
}

fn finalize_current(
    section: Section,
    current: &mut Option<CurrentElem>,
    stream: &mut ChangeStream,
) {
    let Some(elem) = current.take() else {
        return;
    };
    if let Some(action) = section.as_action() {
        stream.push(action, elem.into_change_element());
    }
}

fn parse_i64_attr(e: &BytesStart<'_>, name: &[u8]) -> Result<i64> {
    for attr in e.attributes() {
        let attr = attr?;
        if attr.key == QName(name) {
            return Ok(std::str::from_utf8(&attr.value)?.parse::<i64>()?);
        }
    }
    Err(format!("missing attribute '{}'", String::from_utf8_lossy(name)).into())
}

fn parse_metadata(e: &BytesStart<'_>) -> Option<OwnedMetadata> {
    let version = parse_i32_attr_optional(e, b"version")?;
    Some(OwnedMetadata {
        version,
        timestamp: parse_str_attr_optional(e, b"timestamp").unwrap_or_default(),
        changeset: parse_str_attr_optional(e, b"changeset").unwrap_or_default(),
        uid: parse_str_attr_optional(e, b"uid").unwrap_or_default(),
        user: parse_str_attr_optional(e, b"user").unwrap_or_default(),
        visible: parse_str_attr_optional(e, b"visible").unwrap_or_default(),
    })
}

fn parse_i32_attr_optional(e: &BytesStart<'_>, name: &[u8]) -> Option<i32> {
    for attr in e.attributes().flatten() {
        if attr.key == QName(name) {
            let text = std::str::from_utf8(&attr.value).ok()?;
            let parsed = text.parse::<i32>().ok()?;
            return Some(parsed);
        }
    }
    None
}

fn parse_f64_attr_optional(e: &BytesStart<'_>, name: &[u8]) -> Option<f64> {
    for attr in e.attributes().flatten() {
        if attr.key == QName(name) {
            let text = std::str::from_utf8(&attr.value).ok()?;
            let parsed = text.parse::<f64>().ok()?;
            return Some(parsed);
        }
    }
    None
}

fn parse_str_attr(e: &BytesStart<'_>, name: &[u8]) -> Result<String> {
    for attr in e.attributes() {
        let attr = attr?;
        if attr.key == QName(name) {
            return Ok(attr.unescape_value()?.into_owned());
        }
    }
    Err(format!("missing attribute '{}'", String::from_utf8_lossy(name)).into())
}

fn parse_str_attr_optional(e: &BytesStart<'_>, name: &[u8]) -> Option<String> {
    for attr in e.attributes().flatten() {
        if attr.key == QName(name) {
            return attr.unescape_value().ok().map(std::borrow::Cow::into_owned);
        }
    }
    None
}

enum OscWriter<W: io::Write> {
    Gz(Box<Writer<GzEncoder<W>>>),
    Plain(Writer<W>),
}

impl<W: io::Write> OscWriter<W> {
    fn write_event(&mut self, event: Event<'_>) -> Result<()> {
        match self {
            Self::Gz(w) => w.write_event(event)?,
            Self::Plain(w) => w.write_event(event)?,
        }
        Ok(())
    }

    /// Closes the gzip stream (if Gz) and returns the inner writer.
    /// For the file-backed flow, prefer `finish()` which also flushes
    /// the BufWriter. For the in-memory worker flow, this is the way
    /// to recover the produced `Vec<u8>` of compressed (or plain) XML.
    fn into_inner(self) -> Result<W> {
        match self {
            Self::Gz(w) => {
                let gz = w.into_inner();
                Ok(gz.finish()?)
            }
            Self::Plain(w) => Ok(w.into_inner()),
        }
    }
}

impl OscWriter<io::BufWriter<File>> {
    fn from_file(output: &Path) -> Result<Self> {
        let file = File::create(output)?;
        let buf = io::BufWriter::new(file);
        let is_gz = output.to_str().is_some_and(|s| s.ends_with(".gz"));
        if is_gz {
            Ok(Self::Gz(Box::new(Writer::new_with_indent(
                GzEncoder::new(buf, flate2::Compression::fast()),
                b' ',
                2,
            ))))
        } else {
            Ok(Self::Plain(Writer::new_with_indent(buf, b' ', 2)))
        }
    }

    fn finish(self) -> Result<()> {
        match self {
            Self::Gz(w) => {
                let gz = w.into_inner();
                gz.finish()?;
            }
            Self::Plain(w) => {
                let mut buf = w.into_inner();
                io::Write::flush(&mut buf)?;
            }
        }
        Ok(())
    }
}

impl OscWriter<Vec<u8>> {
    /// In-memory `OscWriter` for parallel-drain workers. Each worker
    /// emits its OSC's XML into its own `Vec<u8>` buffer (gz-encoded if
    /// `is_gz`); main thread concatenates worker buffers in input order.
    /// Multi-member gzip is valid: the resulting output file is the
    /// concatenation of self-contained gzip streams, decoded as the
    /// concatenation of their decompressed contents.
    fn new_buf(is_gz: bool) -> Self {
        if is_gz {
            Self::Gz(Box::new(Writer::new_with_indent(
                GzEncoder::new(Vec::new(), flate2::Compression::fast()),
                b' ',
                2,
            )))
        } else {
            Self::Plain(Writer::new_with_indent(Vec::new(), b' ', 2))
        }
    }
}

#[hotpath::measure]
fn write_streaming(inputs: &[&Path], output: &Path) -> Result<u64> {
    let is_gz = output.to_str().is_some_and(|s| s.ends_with(".gz"));

    if inputs.len() <= 1 {
        // N <= 1: no parallelism available. Use the original serial
        // streaming path that emits XML during parse - one-pass, no
        // buffer-and-drain cost. Avoids the regression a forced
        // buffer-and-drain would inflict at single-OSC scale (drain pass
        // is pure overhead when there is no parse to overlap with).
        crate::debug::emit_marker("MERGECHANGES_WRITE_OPEN_START");
        let mut writer = OscWriter::from_file(output)?;

        writer.write_event(Event::Decl(BytesDecl::new("1.0", Some("UTF-8"), None)))?;
        let mut root = BytesStart::new("osmChange");
        root.push_attribute(("version", "0.6"));
        writer.write_event(Event::Start(root))?;
        crate::debug::emit_marker("MERGECHANGES_WRITE_OPEN_END");

        let mut open_action: Option<Action> = None;
        let mut count = 0u64;
        for path in inputs {
            if let Ok(meta) = std::fs::metadata(path) {
                #[allow(clippy::cast_possible_wrap)]
                crate::debug::emit_counter("merge_changes_input_bytes", meta.len() as i64);
            }
            let count_before = count;
            crate::debug::emit_marker("MERGECHANGES_PARSE_START");
            parse_osc_streaming(path, &mut writer, &mut open_action, &mut count)?;
            crate::debug::emit_marker("MERGECHANGES_PARSE_END");
            #[allow(clippy::cast_possible_wrap)]
            crate::debug::emit_counter(
                "merge_changes_changes_per_osc",
                (count - count_before) as i64,
            );
        }

        if let Some(prev) = open_action {
            writer.write_event(Event::End(BytesEnd::new(action_tag(prev))))?;
        }
        writer.write_event(Event::End(BytesEnd::new("osmChange")))?;
        crate::debug::emit_marker("MERGECHANGES_WRITE_FINISH_START");
        writer.finish()?;
        crate::debug::emit_marker("MERGECHANGES_WRITE_FINISH_END");
        return Ok(count);
    }

    // N > 1: parallel-drain. Each worker runs the full per-input pipeline
    // (parse + XML re-emit + gzip-compress) into its own in-memory buffer.
    // Main thread writes the prelude (XML decl + osmChange opening) as its
    // own gzip member, concatenates worker buffers in input order (also
    // self-contained gzip members), and writes the postlude (osmChange
    // closing) as its own gzip member. Multi-member gzip is valid; on
    // decompress the members concatenate to produce the full XML document.
    //
    // Why this beats parallel-parse + serial-drain (commit 43dd620,
    // UUID 07ee92ee, 235.8 s wall): the serial drain pass at planet
    // 7-OSC was 223 s, dominated by per-change `quick_xml::Writer`
    // emit cost (single-thread XML serialization of 26.3 M changes).
    // Moving the re-emit + gzip onto the worker threads parallelizes
    // that 223 s across the same N rayon workers already doing parse.
    for path in inputs {
        if let Ok(meta) = std::fs::metadata(path) {
            #[allow(clippy::cast_possible_wrap)]
            crate::debug::emit_counter("merge_changes_input_bytes", meta.len() as i64);
        }
    }

    crate::debug::emit_marker("MERGECHANGES_PARALLEL_EMIT_START");
    let chunks: std::result::Result<Vec<(Vec<u8>, u64)>, String> = inputs
        .par_iter()
        .map(|path| -> std::result::Result<(Vec<u8>, u64), String> {
            let mut writer = OscWriter::<Vec<u8>>::new_buf(is_gz);
            let mut open_action: Option<Action> = None;
            let mut count = 0u64;
            parse_osc_streaming(path, &mut writer, &mut open_action, &mut count)
                .map_err(|e| e.to_string())?;
            if let Some(prev) = open_action {
                writer
                    .write_event(Event::End(BytesEnd::new(action_tag(prev))))
                    .map_err(|e| e.to_string())?;
            }
            let bytes = writer.into_inner().map_err(|e| e.to_string())?;
            Ok((bytes, count))
        })
        .collect();
    let chunks = chunks.map_err(|e| -> Box<dyn std::error::Error> { e.into() })?;
    crate::debug::emit_marker("MERGECHANGES_PARALLEL_EMIT_END");

    crate::debug::emit_marker("MERGECHANGES_DRAIN_START");
    let prelude = build_prelude_bytes(is_gz)?;
    let postlude = build_postlude_bytes(is_gz)?;
    let file = File::create(output)?;
    let mut out = io::BufWriter::new(file);
    out.write_all(&prelude)?;
    let mut count = 0u64;
    for (bytes, n) in chunks {
        out.write_all(&bytes)?;
        count += n;
        #[allow(clippy::cast_possible_wrap)]
        crate::debug::emit_counter("merge_changes_changes_per_osc", n as i64);
    }
    out.write_all(&postlude)?;
    out.flush()?;
    crate::debug::emit_marker("MERGECHANGES_DRAIN_END");
    crate::debug::emit_marker("MERGECHANGES_WRITE_FINISH_START");
    crate::debug::emit_marker("MERGECHANGES_WRITE_FINISH_END");

    Ok(count)
}

/// Pre-build the XML prelude (`<?xml ?><osmChange version="0.6">`) as
/// a self-contained gzip member (or plain bytes if `is_gz` is false).
/// Used by the N > 1 parallel-drain path so the main thread can write
/// it directly to the output file before concatenating worker chunks.
fn build_prelude_bytes(is_gz: bool) -> Result<Vec<u8>> {
    let mut writer = OscWriter::<Vec<u8>>::new_buf(is_gz);
    writer.write_event(Event::Decl(BytesDecl::new("1.0", Some("UTF-8"), None)))?;
    let mut root = BytesStart::new("osmChange");
    root.push_attribute(("version", "0.6"));
    writer.write_event(Event::Start(root))?;
    writer.into_inner()
}

/// Pre-build the XML postlude (`</osmChange>`) as a self-contained
/// gzip member (or plain bytes if `is_gz` is false). Counterpart to
/// `build_prelude_bytes`.
fn build_postlude_bytes(is_gz: bool) -> Result<Vec<u8>> {
    let mut writer = OscWriter::<Vec<u8>>::new_buf(is_gz);
    writer.write_event(Event::End(BytesEnd::new("osmChange")))?;
    writer.into_inner()
}

#[hotpath::measure]
fn parse_osc_streaming<W: io::Write>(
    path: &Path,
    writer: &mut OscWriter<W>,
    open_action: &mut Option<Action>,
    count: &mut u64,
) -> Result<()> {
    let mut file = File::open(path)?;
    let mut magic = [0u8; 2];
    io::Read::read_exact(&mut file, &mut magic)?;
    file.seek(io::SeekFrom::Start(0))?;

    let decompress_total = Rc::new(Cell::new(Duration::ZERO));
    let reader: Reader<BufReader<Box<dyn io::Read>>> = if magic == [0x1f, 0x8b] {
        let timed = TimedRead {
            inner: MultiGzDecoder::new(file),
            total: Rc::clone(&decompress_total),
        };
        Reader::from_reader(BufReader::new(Box::new(timed)))
    } else {
        let timed = TimedRead {
            inner: file,
            total: Rc::clone(&decompress_total),
        };
        Reader::from_reader(BufReader::new(Box::new(timed)))
    };
    let mut reader = reader;
    reader.config_mut().trim_text(true);

    let mut section = Section::None;
    let mut current: Option<CurrentElem> = None;
    let mut buf = Vec::new();

    loop {
        match reader.read_event_into(&mut buf) {
            Ok(Event::Start(ref e)) => {
                handle_start_like_streaming(
                    e, false, &mut section, &mut current, writer, open_action, count,
                )?;
            }
            Ok(Event::Empty(ref e)) => {
                handle_start_like_streaming(
                    e, true, &mut section, &mut current, writer, open_action, count,
                )?;
            }
            Ok(Event::End(ref e)) => match e.name().as_ref() {
                b"create" | b"modify" | b"delete" => section = Section::None,
                b"node" | b"way" | b"relation" => {
                    if let Some(elem) = current.take()
                        && let Some(action) = section.as_action()
                    {
                        let change = Change {
                            action,
                            element: elem.into_change_element(),
                        };
                        emit_change(writer, open_action, &change)?;
                        *count += 1;
                    }
                }
                _ => {}
            },
            Ok(Event::Eof) => break,
            Ok(_) => {}
            Err(e) => return Err(Box::new(e)),
        }
        buf.clear();
    }

    #[allow(clippy::cast_possible_wrap, clippy::cast_possible_truncation)]
    crate::debug::emit_counter(
        "merge_changes_decompress_ns",
        decompress_total.get().as_nanos() as i64,
    );
    Ok(())
}

fn handle_start_like_streaming<W: io::Write>(
    e: &BytesStart<'_>,
    is_empty: bool,
    section: &mut Section,
    current: &mut Option<CurrentElem>,
    writer: &mut OscWriter<W>,
    open_action: &mut Option<Action>,
    count: &mut u64,
) -> Result<()> {
    match e.name().as_ref() {
        b"create" => *section = Section::Create,
        b"modify" => *section = Section::Modify,
        b"delete" => *section = Section::Delete,
        b"node" | b"way" | b"relation" => {
            let kind = match e.name().as_ref() {
                b"node" => ElemKind::Node,
                b"way" => ElemKind::Way,
                _ => ElemKind::Relation,
            };
            let id = parse_i64_attr(e, b"id")?;
            let metadata = parse_metadata(e);
            let mut elem = CurrentElem::new(kind, id, metadata);

            if kind == ElemKind::Node {
                let lat = parse_f64_attr_optional(e, b"lat").unwrap_or(0.0);
                let lon = parse_f64_attr_optional(e, b"lon").unwrap_or(0.0);
                #[allow(clippy::cast_possible_truncation)]
                {
                    elem.decimicro_lat = (lat * 1e7).round() as i32;
                    elem.decimicro_lon = (lon * 1e7).round() as i32;
                }
            }

            if is_empty || *section == Section::Delete {
                let action = section
                    .as_action()
                    .ok_or_else(|| "element outside create/modify/delete section".to_string())?;
                let change = Change {
                    action,
                    element: elem.into_change_element(),
                };
                emit_change(writer, open_action, &change)?;
                *count += 1;
            } else {
                *current = Some(elem);
            }
        }
        b"tag" => {
            if let Some(cur) = current {
                let k = parse_str_attr(e, b"k")?;
                let v = parse_str_attr(e, b"v")?;
                cur.tags.push((k, v));
            }
        }
        b"nd" => {
            if let Some(cur) = current
                && cur.kind == ElemKind::Way
            {
                let rf = parse_i64_attr(e, b"ref")?;
                cur.refs.push(rf);
            }
        }
        b"member" => {
            if let Some(cur) = current
                && cur.kind == ElemKind::Relation
            {
                let ref_id = parse_i64_attr(e, b"ref")?;
                let role = parse_str_attr_optional(e, b"role").unwrap_or_default();
                let member_id = match parse_str_attr(e, b"type")?.as_str() {
                    "node" => MemberId::Node(ref_id),
                    "way" => MemberId::Way(ref_id),
                    "relation" => MemberId::Relation(ref_id),
                    other => {
                        return Err(format!("unknown relation member type '{other}'").into());
                    }
                };
                cur.members.push(OwnedMember {
                    id: member_id,
                    role,
                });
            }
        }
        _ => {}
    }

    if is_empty {
        match e.name().as_ref() {
            b"create" | b"modify" | b"delete" => *section = Section::None,
            _ => {}
        }
    }

    Ok(())
}

fn emit_change<W: io::Write>(
    writer: &mut OscWriter<W>,
    open_action: &mut Option<Action>,
    change: &Change,
) -> Result<()> {
    if *open_action != Some(change.action) {
        if let Some(prev) = open_action.take() {
            writer.write_event(Event::End(BytesEnd::new(action_tag(prev))))?;
        }
        writer.write_event(Event::Start(BytesStart::new(action_tag(change.action))))?;
        *open_action = Some(change.action);
    }
    write_change_to(writer, change)
}

#[hotpath::measure]
fn write_simplified(
    output: &Path,
    stream: ChangeStream,
    worker_count: usize,
) -> Result<usize> {
    crate::debug::emit_marker("MERGECHANGES_SIMPLIFY_START");
    #[allow(clippy::cast_possible_wrap)]
    crate::debug::emit_counter("merge_changes_changes_in", stream.changes.len() as i64);
    let mut last_by_object: BTreeMap<(u8, i64), Change> = BTreeMap::new();
    for change in stream.changes {
        last_by_object.insert(change.element.key(), change);
    }

    let mut creates = Vec::new();
    let mut modifies = Vec::new();
    let mut deletes = Vec::new();
    for (_, change) in last_by_object {
        match change.action {
            Action::Create => creates.push(change),
            Action::Modify => modifies.push(change),
            Action::Delete => deletes.push(change),
        }
    }
    crate::debug::emit_marker("MERGECHANGES_SIMPLIFY_END");

    let total_out = creates.len() + modifies.len() + deletes.len();
    let is_gz = output.to_str().is_some_and(|s| s.ends_with(".gz"));

    // Mirror the streaming-path parallel-drain shape: split each
    // non-empty action group into chunks, parallel-emit each chunk as a
    // self-contained `<action>...</action>` gzip member, main thread
    // concatenates members in (group, chunk-index) order with the
    // shared prelude / postlude. Multiple consecutive same-action
    // sections are valid OSC; the per-chunk wrapping costs ~14 bytes
    // per chunk and is negligible against the win.
    //
    // 37fbe5b5 (parallel parse, serial write_simplified) clocked
    // planet 7-OSC --simplify at 220.9 s with the write_simplified
    // phase consuming ~197 s on a single thread - the same per-change
    // `quick_xml::Writer` + zlib ceiling that bottlenecked the
    // streaming path's abandoned parallel-parse-only stage. Same
    // mechanism, same fix.
    crate::debug::emit_marker("MERGECHANGES_PARALLEL_EMIT_START");
    let mut all_chunks: Vec<Vec<u8>> = Vec::new();
    for (action, group) in [
        (Action::Create, &creates),
        (Action::Modify, &modifies),
        (Action::Delete, &deletes),
    ] {
        if group.is_empty() {
            continue;
        }
        let chunk_len = group.len().div_ceil(worker_count).max(1);
        let chunks: std::result::Result<Vec<Vec<u8>>, String> = group
            .par_chunks(chunk_len)
            .map(|chunk| -> std::result::Result<Vec<u8>, String> {
                let mut writer = OscWriter::<Vec<u8>>::new_buf(is_gz);
                writer
                    .write_event(Event::Start(BytesStart::new(action_tag(action))))
                    .map_err(|e| e.to_string())?;
                for change in chunk {
                    write_change_to(&mut writer, change).map_err(|e| e.to_string())?;
                }
                writer
                    .write_event(Event::End(BytesEnd::new(action_tag(action))))
                    .map_err(|e| e.to_string())?;
                writer.into_inner().map_err(|e| e.to_string())
            })
            .collect();
        let chunks = chunks.map_err(|e| -> Box<dyn std::error::Error> { e.into() })?;
        all_chunks.extend(chunks);
    }
    crate::debug::emit_marker("MERGECHANGES_PARALLEL_EMIT_END");

    crate::debug::emit_marker("MERGECHANGES_DRAIN_START");
    let prelude = build_prelude_bytes(is_gz)?;
    let postlude = build_postlude_bytes(is_gz)?;
    let file = File::create(output)?;
    let mut out = io::BufWriter::new(file);
    out.write_all(&prelude)?;
    for bytes in &all_chunks {
        out.write_all(bytes)?;
    }
    out.write_all(&postlude)?;
    out.flush()?;
    crate::debug::emit_marker("MERGECHANGES_DRAIN_END");
    crate::debug::emit_marker("MERGECHANGES_WRITE_FINISH_START");
    crate::debug::emit_marker("MERGECHANGES_WRITE_FINISH_END");

    Ok(total_out)
}

fn action_tag(action: Action) -> &'static str {
    match action {
        Action::Create => "create",
        Action::Modify => "modify",
        Action::Delete => "delete",
    }
}

fn write_change_to<W: io::Write>(writer: &mut OscWriter<W>, change: &Change) -> Result<()> {
    let delete = change.action == Action::Delete;
    match writer {
        OscWriter::Gz(w) => write_change_element(w, &change.element, delete),
        OscWriter::Plain(w) => write_change_element(w, &change.element, delete),
    }
}

fn write_change_element<W: Write>(
    writer: &mut Writer<W>,
    element: &ChangeElement,
    delete: bool,
) -> Result<()> {
    if delete {
        let (tag, id, meta) = match element {
            ChangeElement::Node(n) => ("node", n.id, n.metadata.as_ref()),
            ChangeElement::Way(w) => ("way", w.id, w.metadata.as_ref()),
            ChangeElement::Relation(r) => ("relation", r.id, r.metadata.as_ref()),
        };
        write_delete_xml(writer, tag, id, meta)
    } else {
        match element {
            ChangeElement::Node(node) => write_node_xml(writer, node),
            ChangeElement::Way(way) => write_way_xml(writer, way),
            ChangeElement::Relation(rel) => write_relation_xml(writer, rel),
        }
    }
}