ByteBufBsonDocument and RawBsonDocument simplifications#1902
Open
rozza wants to merge 1 commit intomongodb:ByteBuffrom
Open
ByteBufBsonDocument and RawBsonDocument simplifications#1902rozza wants to merge 1 commit intomongodb:ByteBuffrom
ByteBufBsonDocument and RawBsonDocument simplifications#1902rozza wants to merge 1 commit intomongodb:ByteBuffrom
Conversation
### Rationale
`ByteBufBsonDocument#clone` used to return a `RawBsonDocument`.
The recent changes returned a normal `BsonDocument`, which is potentially
expensive depending on its usage.
The `ByteBufBsonDocument` changes also added complex iterator logic, when
`RawBsonDocument` deferred to `BsonDocument` iterators. As iteration is
essentially a hydrating mechanism, there is opportunity for improvements
for both implementations. By changing the `RawBsonDocument` iterators to
be more efficient, `ByteBufBsonDocument` can now utilize these efficiency
gains by proxy, relying on the `cachedDocument` iterators.
This change both reduces the complexity of `ByteBufBsonDocument` and relies
on an improved `RawBsonDocument` implementation.
### Summary of changes
* **`ByteBufBsonDocument`**:
* Simplify by returning `RawBsonDocument` from `toBsonDocument`, avoiding
full BSON deserialization. When there are no sequence fields, the body
bytes are cloned directly. When sequence fields exist,
`BsonBinaryWriter.pipe()` merges the body with sequence arrays efficiently.
* Use `toBsonDocument` for iterators. This eliminates the need for custom
iterators (`IteratorMode`, `CombinedIterator`, `createBodyIterator`, and
sequence iterators) since `entrySet`/`values`/`keySet` now delegate to the
cached `RawBsonDocument`.
* **`RawBsonDocument`**:
* Renamed `toBaseBsonDocument` to override the default `toBsonDocument`
implementation.
* Implemented the iterators so that they don't need to fully convert the
document to a `BsonDocument`.
* **Tests**:
* Updated `ByteBufBsonDocumentTest` iteration tests.
* Updated `ByteBufBsonArrayTest#fromValues` as `entrySet` now returns
`RawBsonDocument` instances.
JAVA-6010
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Rationale
ByteBufBsonDocument#cloneused to return aRawBsonDocument. The recent changes returned a normalBsonDocument, which is potentially expensive depending on its usage.The
ByteBufBsonDocumentchanges also added complex iterator logic, whenRawBsonDocumentdeferred toBsonDocumentiterators. As iteration is essentially a hydrating mechanism, there is opportunity for improvements for both implementations. By changing theRawBsonDocumentiterators to be more efficient,ByteBufBsonDocumentcan now utilize these efficiency gains by proxy, relying on thecachedDocumentiterators.This change both reduces the complexity of
ByteBufBsonDocumentand relies on an improvedRawBsonDocumentimplementation.Summary of changes
ByteBufBsonDocument:RawBsonDocumentfromtoBsonDocument, avoiding full BSON deserialization. When there are no sequence fields, the body bytes are cloned directly. When sequence fields exist,BsonBinaryWriter.pipe()merges the body with sequence arrays efficiently.toBsonDocumentfor iterators. This eliminates the need for custom iterators (IteratorMode,CombinedIterator,createBodyIterator, and sequence iterators) sinceentrySet/values/keySetnow delegate to the cachedRawBsonDocument.RawBsonDocument:toBaseBsonDocumentto override the defaulttoBsonDocumentimplementation.BsonDocument.Tests:
ByteBufBsonDocumentTestiteration tests.ByteBufBsonArrayTest#fromValuesasentrySetnow returnsRawBsonDocumentinstances.JAVA-6010