Skip to content

fix: directory offset encoding#111

Merged
nyurik merged 3 commits intostadiamaps:mainfrom
michaelkirk:mkirk/fix-directory-offset-encoding
Apr 18, 2026
Merged

fix: directory offset encoding#111
nyurik merged 3 commits intostadiamaps:mainfrom
michaelkirk:mkirk/fix-directory-offset-encoding

Conversation

@michaelkirk
Copy link
Copy Markdown
Contributor

Note: I used an LLM to port the tests, but I visually inspected them and they appear equivalent.

Copilot AI review requested due to automatic review settings February 20, 2026 22:22
Comment thread src/directory.rs
let offset_to_write = if entry.offset == last_offset + u64::from(entry.length) {
let mut last_length = 0;
for (i, entry) in self.entries.iter().enumerate() {
let offset_to_write = if i > 0 && entry.offset == last_offset + u64::from(last_length) {
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment thread src/directory.rs Outdated
#[cfg(feature = "write")]
fn directory_roundtrip_gzip() {
let entries = vec![
DirEntry {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please add a helper for instantiating DirEntry, or maybe a new() fn, possibly with pub(crate) to not expose it -- so that all this becomes readable oneliners

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if you really want this readable by humans, you may instead make a test-only helper like fn entry_id_off_len_rl(...) ... but totally up to you

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

or DirEntry::from_id_off_len_rl(...) ?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would result in fewer lines of code, but I disagree that this would be a readability improvement. I admit it's subjective though. Is this a blocker for getting this bug fix merged?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would agree for the regular code, but the tests are already fairly verbose. I am ok to fix it myself if you want - up to you

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, thanks! Please fix up the test phrasing as you see fit.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nyurik - I'd like to see this bugfix merged. Can you apply your preferred test styling?

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes PMTiles directory offset varint encoding and expands directory-related unit tests to better align with upstream go-pmtiles behavior.

Changes:

  • Correct offset “same-as-previous+length” encoding to use the previous entry’s length (and avoid emitting 0 for the first entry).
  • Derive PartialEq for DirEntry to support equality assertions in tests.
  • Add multiple directory roundtrip and find_tile_id behavior tests (ported from go-pmtiles).

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/directory.rs Outdated
Comment thread src/directory.rs Outdated
Comment thread src/directory.rs Outdated
Comment thread src/directory.rs Outdated
Comment on lines +310 to +316

// Ported from go-pmtiles/pmtiles/directory_test.go:TestDirectoryRoundtrip (line 10)
#[test]
#[cfg(feature = "write")]
fn directory_roundtrip_gzip() {
let entries = vec![
DirEntry {
Copy link

Copilot AI Feb 20, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new roundtrip tests don’t exercise the specific offset-encoding bug fixed above (writing 0 when entry.offset == last_offset + entry.length instead of using the previous entry’s length). Add a case where the previous and current lengths differ and the old encoding would have produced an incorrect decoded offset (e.g., prev: offset=0,len=1; next: offset=2,len=2) to ensure this regression is covered.

Copilot uses AI. Check for mistakes.
@michaelkirk michaelkirk force-pushed the mkirk/fix-directory-offset-encoding branch from 4784d3a to 76d86fe Compare February 20, 2026 22:27
@codecov
Copy link
Copy Markdown

codecov Bot commented Feb 20, 2026

Codecov Report

❌ Patch coverage is 97.97980% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 80.88%. Comparing base (9983294) to head (c3df928).
⚠️ Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
src/directory.rs 97.97% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #111      +/-   ##
==========================================
+ Coverage   79.97%   80.88%   +0.91%     
==========================================
  Files          12       12              
  Lines        1648     1732      +84     
  Branches     1648     1732      +84     
==========================================
+ Hits         1318     1401      +83     
- Misses        225      226       +1     
  Partials      105      105              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link
Copy Markdown
Collaborator

@nyurik nyurik left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks!

@nyurik
Copy link
Copy Markdown
Collaborator

nyurik commented Apr 17, 2026

sigh, seems like there are some object-store related issues and the new tests... I tried fixing them, but it didn't really help. @michaelkirk could you take a look? I am not certain my fix is the right one

@michaelkirk michaelkirk force-pushed the mkirk/fix-directory-offset-encoding branch from 4f5c7e4 to 34d9b0a Compare April 17, 2026 21:34
@nyurik
Copy link
Copy Markdown
Collaborator

nyurik commented Apr 17, 2026

I wonder if tokio feature should be made always-on in object-store dep

@michaelkirk
Copy link
Copy Markdown
Contributor Author

I'm pulling out the "fix the build" stuff into another PR. Apparently it's still WIP #119

michaelkirk and others added 3 commits April 17, 2026 20:30
Note: 2 are currently failing:
    directory::tests::directory_roundtrip_gzip
    directory::tests::directory_roundtrip_no_compression
The contiguous-offset check compared against the current entry's length
instead of the previous entry's length, producing corrupt output when
consecutive entries had different lengths. Also add an `i > 0` guard so
the first entry always encodes its full offset (a value of 0 is reserved
to mean "contiguous with previous").
@nyurik nyurik force-pushed the mkirk/fix-directory-offset-encoding branch from 4c1fa6c to c3df928 Compare April 18, 2026 00:30
@nyurik nyurik changed the title Fix directory offset encoding fix: directory offset encoding Apr 18, 2026
@nyurik nyurik merged commit 1f03a20 into stadiamaps:main Apr 18, 2026
8 checks passed
nyurik pushed a commit that referenced this pull request Apr 18, 2026
## 🤖 New release

* `pmtiles`: 0.21.0 -> 0.22.0 (⚠ API breaking changes)

### ⚠ `pmtiles` breaking changes

```text
--- failure auto_trait_impl_removed: auto trait no longer implemented ---

Description:
A public type has stopped implementing one or more auto traits. This can break downstream code that depends on the traits being implemented.
        ref: https://doc.rust-lang.org/reference/special-types-and-traits.html#auto-traits
       impl: https://github.com/obi1kenobi/cargo-semver-checks/tree/v0.46.0/src/lints/auto_trait_impl_removed.ron

Failed in:
  type PmTilesWriter is no longer Send, in /tmp/.tmpQE4q9u/pmtiles-rs/src/writer/mod.rs:23
  type PmTilesWriter is no longer Sync, in /tmp/.tmpQE4q9u/pmtiles-rs/src/writer/mod.rs:23
  type PmTilesWriter is no longer UnwindSafe, in /tmp/.tmpQE4q9u/pmtiles-rs/src/writer/mod.rs:23
  type PmTilesWriter is no longer RefUnwindSafe, in /tmp/.tmpQE4q9u/pmtiles-rs/src/writer/mod.rs:23
  type PmTilesStreamWriter is no longer Send, in /tmp/.tmpQE4q9u/pmtiles-rs/src/writer/mod.rs:36
  type PmTilesStreamWriter is no longer Sync, in /tmp/.tmpQE4q9u/pmtiles-rs/src/writer/mod.rs:36
  type PmTilesStreamWriter is no longer UnwindSafe, in /tmp/.tmpQE4q9u/pmtiles-rs/src/writer/mod.rs:36
  type PmTilesStreamWriter is no longer RefUnwindSafe, in /tmp/.tmpQE4q9u/pmtiles-rs/src/writer/mod.rs:36
```

<details><summary><i><b>Changelog</b></i></summary><p>

<blockquote>

##
[0.22.0](v0.21.0...v0.22.0)
- 2026-04-18

### Fixed

- directory offset encoding
([#111](#111))

### Other

- update object_store dependency and fix build
([#119](#119))
- Configurable compression parameters via Compressor trait
([#112](#112))
</blockquote>


</p></details>

---
This PR was generated with
[release-plz](https://github.com/release-plz/release-plz/).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants