Skip to content

items(): support iterating over a key-prefix partition - #50

Merged
ThomasWaldmann merged 1 commit into
masterfrom
items-prefix
Aug 11, 2026
Merged

items(): support iterating over a key-prefix partition#50
ThomasWaldmann merged 1 commit into
masterfrom
items-prefix

Conversation

@ThomasWaldmann

Copy link
Copy Markdown
Member

Fixes #49.

Add optional prefix_bits / prefix arguments to HashTable.items() and HashTableNT.items() to iterate only over the items whose key starts with the given bit prefix.

  • The filter compares the key's first 4 bytes interpreted big-endian (as _get_index already does), so each partition is a contiguous range of the sorted key space.
  • Non-matching entries are skipped at C level without creating Python objects for them, so scanning the table once per partition is cheap.
  • prefix_bits=0 is the default (no filtering), so existing callers are unaffected.

As the keys are random bytes, this partitions the items into 2 ** prefix_bits roughly equally sized, disjoint sets, e.g. to process a huge hash table in batches with a small memory footprint — needed for borgbackup/borg#9886 (bounded-memory write_chunkindex_to_repo).

🤖 Generated with Claude Code

Add optional prefix_bits / prefix arguments to HashTable.items() and
HashTableNT.items() to iterate only over the items whose key starts with
the given bit prefix (first 4 key bytes interpreted big-endian, like
_get_index does, so a partition is a contiguous range of the sorted key
space).

Non-matching entries are skipped at C level without creating Python
objects for them, so scanning the table once per partition is cheap.

As the keys are random bytes, this partitions the items into
2 ** prefix_bits roughly equally sized, disjoint sets, e.g. to process a
huge hash table in batches with a small memory footprint
(see borgbackup/borg#9886).

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@ThomasWaldmann ThomasWaldmann added this to the 0.2.0 milestone Aug 11, 2026
@ThomasWaldmann
ThomasWaldmann merged commit f390cc3 into master Aug 11, 2026
2 checks passed
@ThomasWaldmann
ThomasWaldmann deleted the items-prefix branch August 11, 2026 14:38
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.

items(): support iterating over a key-prefix partition

1 participant