Reassemble only complete envelopes, advancing the cursor after a successful page. Retrying an RPC cannot append its bytes twice.
(
envelopes: &mut Vec<Vec<u8>>,
partial: &mut Vec<u8>,
cursor: &mut pb::PageCursor,
cutoff: u64,
page: pb::FrozenPage,
)
| 518 | /// Reassemble only complete envelopes, advancing the cursor after a |
| 519 | /// successful page. Retrying an RPC cannot append its bytes twice. |
| 520 | fn append_frozen_page( |
| 521 | envelopes: &mut Vec<Vec<u8>>, |
| 522 | partial: &mut Vec<u8>, |
| 523 | cursor: &mut pb::PageCursor, |
| 524 | cutoff: u64, |
| 525 | page: pb::FrozenPage, |
| 526 | ) -> anyhow::Result<()> { |
| 527 | let next = page |
| 528 | .next |
| 529 | .ok_or_else(|| anyhow!("frozen page without a continuation cursor"))?; |
| 530 | let end = pb::PageCursor { |
| 531 | seq: cutoff, |
| 532 | offset: 0, |
| 533 | }; |
| 534 | if cursor_cmp(&next, cursor) != std::cmp::Ordering::Greater |
| 535 | || cursor_cmp(&next, &end) == std::cmp::Ordering::Greater |
| 536 | { |
| 537 | return Err(anyhow!("invalid frozen journal page continuation")); |
| 538 | } |
| 539 | let mut position = *cursor; |
| 540 | for fragment in page.fragments { |
| 541 | let fragment_cursor = fragment |
| 542 | .cursor |
| 543 | .ok_or_else(|| anyhow!("frozen fragment without a cursor"))?; |
| 544 | // Legacy journal records can leave sequence gaps, but never inside an envelope. |
| 545 | if fragment_cursor.seq >= cutoff |
| 546 | || cursor_cmp(&fragment_cursor, &position) == std::cmp::Ordering::Less |
| 547 | || (fragment_cursor != position |
| 548 | && (position.offset != 0 || fragment_cursor.offset != 0)) |
| 549 | || (!fragment.complete && fragment.bytes.is_empty()) |
| 550 | { |
| 551 | return Err(anyhow!("invalid frozen journal fragment cursor")); |
| 552 | } |
| 553 | partial.extend_from_slice(&fragment.bytes); |
| 554 | position = if fragment.complete { |
| 555 | envelopes.push(std::mem::take(partial)); |
| 556 | pb::PageCursor { |
| 557 | seq: fragment_cursor.seq + 1, |
| 558 | offset: 0, |
| 559 | } |
| 560 | } else { |
| 561 | pb::PageCursor { |
| 562 | seq: fragment_cursor.seq, |
| 563 | offset: partial.len() as u64, |
| 564 | } |
| 565 | }; |
| 566 | } |
| 567 | if cursor_cmp(&next, &position) == std::cmp::Ordering::Less |
| 568 | || (next != position && (position.offset != 0 || next.offset != 0)) |
| 569 | || (next == end && !partial.is_empty()) |
| 570 | { |
| 571 | return Err(anyhow!("incomplete frozen journal page")); |
| 572 | } |
| 573 | *cursor = next; |
| 574 | Ok(()) |
| 575 | } |
| 576 | |
| 577 | // --------------------------------------------------------------------------- |