Commit 945bdd7
### 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>
1 parent 98a264b commit 945bdd7
6 files changed
Lines changed: 543 additions & 849 deletions
File tree
- bson/src
- main/org/bson
- test/unit/org/bson
- driver-core/src
- main/com/mongodb/internal/connection
- test/unit/com/mongodb/internal/connection
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
35 | 35 | | |
36 | 36 | | |
37 | 37 | | |
| 38 | + | |
| 39 | + | |
38 | 40 | | |
| 41 | + | |
| 42 | + | |
39 | 43 | | |
40 | 44 | | |
41 | 45 | | |
| |||
123 | 127 | | |
124 | 128 | | |
125 | 129 | | |
126 | | - | |
127 | | - | |
128 | | - | |
129 | | - | |
130 | | - | |
131 | | - | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
132 | 137 | | |
133 | 138 | | |
134 | 139 | | |
| |||
225 | 230 | | |
226 | 231 | | |
227 | 232 | | |
228 | | - | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
229 | 243 | | |
230 | 244 | | |
231 | 245 | | |
232 | 246 | | |
233 | | - | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
234 | 256 | | |
235 | 257 | | |
236 | 258 | | |
237 | 259 | | |
238 | | - | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
239 | 269 | | |
240 | 270 | | |
241 | 271 | | |
| |||
318 | 348 | | |
319 | 349 | | |
320 | 350 | | |
321 | | - | |
| 351 | + | |
322 | 352 | | |
323 | 353 | | |
324 | 354 | | |
325 | 355 | | |
326 | | - | |
| 356 | + | |
| 357 | + | |
| 358 | + | |
| 359 | + | |
| 360 | + | |
| 361 | + | |
| 362 | + | |
| 363 | + | |
327 | 364 | | |
328 | 365 | | |
329 | 366 | | |
| |||
335 | 372 | | |
336 | 373 | | |
337 | 374 | | |
338 | | - | |
339 | | - | |
340 | | - | |
341 | | - | |
342 | | - | |
343 | | - | |
344 | | - | |
345 | 375 | | |
346 | 376 | | |
347 | 377 | | |
| |||
0 commit comments