-
Notifications
You must be signed in to change notification settings - Fork 429
Support u64 vertex ids in the bftree provider #1216
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
JordanMaples
wants to merge
2
commits into
main
Choose a base branch
from
jordanmaples/bftree_u64_ids
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| /* | ||
| * Copyright (c) Microsoft Corporation. | ||
| * Licensed under the MIT license. | ||
| */ | ||
|
|
||
| //! Vertex-id abstraction for the bf-tree provider. | ||
|
|
||
| use diskann::utils::{IntoUsize, VectorId}; | ||
| use diskann::{ANNError, ANNResult}; | ||
|
|
||
| /// Identifier type usable as a `BfTreeProvider` vertex id. | ||
| /// | ||
| /// This bundles the bounds the core algorithm requires of an id ([`VectorId`]) | ||
| /// with the ability to convert *to* `usize` ([`IntoUsize`], used to key the | ||
| /// per-vector stores) and *from* a zero-based index. The provider mints ids | ||
| /// densely from `0..total`, so it needs a way to build an `I` from a counter. | ||
| /// | ||
| /// Implemented for `u32` (the default, capping at ~4.29B vertices) and `u64` | ||
| /// (for billion-scale-and-beyond, larger-than-memory datasets). On a 64-bit | ||
| /// target `u64` covers every representable `usize`, so its conversions never | ||
| /// fail. | ||
| pub trait BfTreeId: VectorId + IntoUsize { | ||
| /// Build an id from a zero-based index, truncating on overflow. | ||
| /// | ||
| /// Only call this for indices already known to fit (e.g. ids drawn from | ||
| /// `0..total`, which the provider guarantees fit by construction). | ||
| fn from_index(index: usize) -> Self; | ||
|
|
||
| /// Build an id from a zero-based index, returning `None` if it does not fit. | ||
| fn try_from_index(index: usize) -> Option<Self>; | ||
| } | ||
|
|
||
| impl BfTreeId for u32 { | ||
| #[inline(always)] | ||
| fn from_index(index: usize) -> Self { | ||
| index as u32 | ||
| } | ||
|
|
||
| #[inline(always)] | ||
| fn try_from_index(index: usize) -> Option<Self> { | ||
| u32::try_from(index).ok() | ||
| } | ||
| } | ||
|
|
||
| impl BfTreeId for u64 { | ||
| #[inline(always)] | ||
| fn from_index(index: usize) -> Self { | ||
| index as u64 | ||
| } | ||
|
|
||
| #[inline(always)] | ||
| fn try_from_index(index: usize) -> Option<Self> { | ||
| u64::try_from(index).ok() | ||
| } | ||
| } | ||
|
|
||
| /// Validate that a provider holding `total` ids can represent every id in `0..total`. | ||
| /// | ||
| /// `BfTreeProvider::iter` mints ids densely via the infallible (truncating) | ||
| /// [`BfTreeId::from_index`]; callers must guarantee the range fits in `I`. This check | ||
| /// enforces that guarantee up front (at construction and load) so the truncating | ||
| /// conversion can never silently wrap a real id. | ||
| pub(crate) fn validate_id_capacity<I: BfTreeId>(total: usize) -> ANNResult<()> { | ||
| if let Some(last) = total.checked_sub(1) { | ||
| if I::try_from_index(last).is_none() { | ||
| return Err(ANNError::log_index_error(format!( | ||
| "provider capacity of {total} ids exceeds the maximum representable by the \ | ||
| {}-byte vertex id type", | ||
| std::mem::size_of::<I>() | ||
| ))); | ||
| } | ||
| } | ||
| Ok(()) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Suggestion: Try to avoid
IntoUsizeas a trait bound and just make this anas_indexmethod ofBfTreeId. It's a little more defensive and self contained. It may also be a good idea to drop theVectorIdbound as well and instead include the required traits directly, but that's perhaps less important sinceVectorIdstill pulls in way too much.