-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Optimize RawBsonDocument encode and decode #1913
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
Closed
Closed
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
cd52b6b
Optimize RawBsonDocument encode and decode by eliminating intermediat…
vbabanin 03978bf
Merge branch 'main' into JAVA-6133
vbabanin fc3726d
Refines RawBsonDocument encode optimization
vbabanin a543455
Merge branch 'main' into JAVA-6133
vbabanin 81ec442
Make pipe default method.
vbabanin a5790af
Add tests.
vbabanin 103f461
Merge branch 'main' into JAVA-6133
vbabanin 9f49d2d
Remove unused imports.
vbabanin 96a44ef
Merge branch 'main' into JAVA-6133
vbabanin d5006c0
Address PR review feedback and improve test coverage
vbabanin 54292e8
Change javadoc.
vbabanin 495fe2b
Update bson/src/main/org/bson/RawBsonDocument.java
rozza 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
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 |
|---|---|---|
|
|
@@ -356,5 +356,4 @@ public interface BsonWriter { | |
| * @param reader The source. | ||
| */ | ||
| void pipe(BsonReader reader); | ||
|
|
||
| } | ||
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
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
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
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,106 @@ | ||
| /* | ||
| * Copyright 2008-present MongoDB, Inc. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package org.bson; | ||
|
|
||
| import org.bson.codecs.BsonDocumentCodec; | ||
| import org.bson.codecs.EncoderContext; | ||
| import org.bson.io.BasicOutputBuffer; | ||
| import org.junit.jupiter.params.ParameterizedTest; | ||
| import org.junit.jupiter.api.Named; | ||
| import org.junit.jupiter.params.provider.Arguments; | ||
| import org.junit.jupiter.params.provider.MethodSource; | ||
|
|
||
| import java.util.Arrays; | ||
| import java.util.stream.Stream; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertArrayEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
|
||
| class RawBsonDocumentTest { | ||
|
|
||
| private static final BsonDocument DOCUMENT = new BsonDocument() | ||
| .append("a", new BsonInt32(1)) | ||
| .append("b", new BsonInt32(2)) | ||
| .append("c", new BsonDocument("x", BsonBoolean.TRUE)) | ||
| .append("d", new BsonArray(Arrays.asList( | ||
| new BsonDocument("y", BsonBoolean.FALSE), | ||
| new BsonArray(Arrays.asList(new BsonInt32(1)))))); | ||
|
|
||
| private static final byte[] DOCUMENT_BYTES = encodeDocument(); | ||
|
|
||
| static Stream<Arguments> backingArrayAccessors() { | ||
| int documentLength = DOCUMENT_BYTES.length; | ||
|
|
||
| Stream.Builder<Arguments> builder = Stream.builder(); | ||
| builder.add(Arguments.of(createFromDocument(), 0, documentLength)); | ||
| builder.add(Arguments.of(createFromByteArray(), 0, documentLength)); | ||
|
|
||
| for (int padding = 1; padding <= 2; padding++) { | ||
| builder.add(Arguments.of(createPaddedBefore(padding), padding, documentLength)); | ||
| builder.add(Arguments.of(createPaddedAfter(padding), 0, documentLength)); | ||
| builder.add(Arguments.of(createPaddedBoth(padding), padding, documentLength)); | ||
| } | ||
|
|
||
| return builder.build(); | ||
| } | ||
|
|
||
| @ParameterizedTest(name = "{0}, expectedOffset={1}, expectedLength={2}") | ||
| @MethodSource("backingArrayAccessors") | ||
| void shouldExposeBackingArrayOffsetAndLength(final RawBsonDocument rawDocument, | ||
| final int expectedOffset, | ||
| final int expectedLength) { | ||
| assertEquals(expectedOffset, rawDocument.getByteOffset()); | ||
| assertEquals(expectedLength, rawDocument.getByteLength()); | ||
| assertArrayEquals(DOCUMENT_BYTES, | ||
| Arrays.copyOfRange( | ||
| rawDocument.getBackingArray(), | ||
| rawDocument.getByteOffset(), | ||
| rawDocument.getByteOffset() + rawDocument.getByteLength())); | ||
| } | ||
|
|
||
| private static Named<RawBsonDocument> createFromDocument() { | ||
| return Named.of("from document", new RawBsonDocument(DOCUMENT, new BsonDocumentCodec())); | ||
| } | ||
|
|
||
| private static Named<RawBsonDocument> createFromByteArray() { | ||
| return Named.of("from byte array", new RawBsonDocument(DOCUMENT_BYTES)); | ||
| } | ||
|
|
||
| private static Named<RawBsonDocument> createPaddedBefore(final int padding) { | ||
| byte[] padded = new byte[DOCUMENT_BYTES.length + padding]; | ||
| System.arraycopy(DOCUMENT_BYTES, 0, padded, padding, DOCUMENT_BYTES.length); | ||
| return Named.of("padded before " + padding, new RawBsonDocument(padded, padding, DOCUMENT_BYTES.length)); | ||
| } | ||
|
|
||
| private static Named<RawBsonDocument> createPaddedAfter(final int padding) { | ||
| byte[] padded = new byte[DOCUMENT_BYTES.length + padding]; | ||
| System.arraycopy(DOCUMENT_BYTES, 0, padded, 0, DOCUMENT_BYTES.length); | ||
| return Named.of("padded after " + padding, new RawBsonDocument(padded, 0, DOCUMENT_BYTES.length)); | ||
| } | ||
|
|
||
| private static Named<RawBsonDocument> createPaddedBoth(final int padding) { | ||
| byte[] padded = new byte[DOCUMENT_BYTES.length + padding * 2]; | ||
| System.arraycopy(DOCUMENT_BYTES, 0, padded, padding, DOCUMENT_BYTES.length); | ||
| return Named.of("padded both " + padding, new RawBsonDocument(padded, padding, DOCUMENT_BYTES.length)); | ||
| } | ||
|
|
||
| private static byte[] encodeDocument() { | ||
| BasicOutputBuffer buffer = new BasicOutputBuffer(); | ||
| new BsonDocumentCodec().encode(new BsonBinaryWriter(buffer), DOCUMENT, EncoderContext.builder().build()); | ||
| return Arrays.copyOf(buffer.getInternalBuffer(), buffer.getPosition()); | ||
| } | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.