-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Introduce BsonUtil.mutableDeepCopy
#1081
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
Merged
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
d439e79
Make sure that `BsonDocument.clone` returns a mutable deep copy
stIncMale a525a64
Introduce Util.mutableDeepCopy
stIncMale 3134e4a
Rename `Util` to `BsonUtil`
stIncMale a34d0ff
`BsonUtil.mutableDeepCopy` does not need to be generic
stIncMale 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,73 @@ | ||
| /* | ||
| * 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.internal; | ||
|
|
||
| import org.bson.BsonArray; | ||
| import org.bson.BsonBinary; | ||
| import org.bson.BsonDocument; | ||
| import org.bson.BsonJavaScriptWithScope; | ||
| import org.bson.BsonValue; | ||
|
|
||
| /** | ||
| * <p>This class is not part of the public API and may be removed or changed at any time</p> | ||
| */ | ||
| public final class BsonUtil { | ||
| public static BsonDocument mutableDeepCopy(final BsonDocument original) { | ||
| BsonDocument copy = new BsonDocument(original.size()); | ||
| original.forEach((key, value) -> copy.put(key, mutableDeepCopy(value))); | ||
| return copy; | ||
| } | ||
|
|
||
| private static BsonArray mutableDeepCopy(final BsonArray original) { | ||
| BsonArray copy = new BsonArray(original.size()); | ||
| original.forEach(element -> copy.add(mutableDeepCopy(element))); | ||
| return copy; | ||
| } | ||
|
|
||
| private static BsonBinary mutableDeepCopy(final BsonBinary original) { | ||
| return new BsonBinary(original.getType(), original.getData().clone()); | ||
| } | ||
|
|
||
| private static BsonJavaScriptWithScope mutableDeepCopy(final BsonJavaScriptWithScope original) { | ||
| return new BsonJavaScriptWithScope(original.getCode(), mutableDeepCopy(original.getScope())); | ||
| } | ||
|
|
||
| private static <T extends BsonValue> T mutableDeepCopy(final T original) { | ||
| BsonValue copy; | ||
| switch (original.getBsonType()) { | ||
| case DOCUMENT: | ||
| copy = mutableDeepCopy(original.asDocument()); | ||
| break; | ||
| case ARRAY: | ||
| copy = mutableDeepCopy(original.asArray()); | ||
| break; | ||
| case BINARY: | ||
| copy = mutableDeepCopy(original.asBinary()); | ||
| break; | ||
| case JAVASCRIPT_WITH_SCOPE: | ||
| copy = mutableDeepCopy(original.asJavaScriptWithScope()); | ||
| break; | ||
| default: | ||
| copy = original; | ||
| } | ||
| @SuppressWarnings("unchecked") | ||
| T result = (T) copy; | ||
| return result; | ||
| } | ||
|
|
||
| private BsonUtil() { | ||
| } | ||
| } | ||
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,131 @@ | ||
| /* | ||
| * 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.internal; | ||
|
|
||
| import org.bson.BsonArray; | ||
| import org.bson.BsonBinary; | ||
| import org.bson.BsonDocument; | ||
| import org.bson.BsonDocumentWrapper; | ||
| import org.bson.BsonJavaScriptWithScope; | ||
| import org.bson.BsonValue; | ||
| import org.bson.RawBsonArray; | ||
| import org.bson.RawBsonDocument; | ||
| import org.bson.conversions.Bson; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import java.nio.charset.StandardCharsets; | ||
| import java.util.AbstractMap.SimpleImmutableEntry; | ||
| import java.util.Map.Entry; | ||
|
|
||
| import static java.util.Arrays.asList; | ||
| import static java.util.Collections.singletonList; | ||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertNotSame; | ||
|
|
||
| final class BsonUtilTest { | ||
| @Test | ||
| public void mutableDeepCopy() { | ||
| Entry<String, BsonBinary> originalBsonBinaryEntry = new SimpleImmutableEntry<>( | ||
| "bsonBinary", | ||
| new BsonBinary("bsonBinary".getBytes(StandardCharsets.UTF_8)) | ||
| ); | ||
| Entry<String, BsonJavaScriptWithScope> originalBsonJavaScriptWithScopeEntry = new SimpleImmutableEntry<>( | ||
| "bsonJavaScriptWithScopeEntry", | ||
| new BsonJavaScriptWithScope("\"use strict\";", new BsonDocument()) | ||
| ); | ||
| Entry<String, RawBsonDocument> originalRawBsonDocumentEntry = new SimpleImmutableEntry<>( | ||
| "rawBsonDocument", | ||
| RawBsonDocument.parse("{rawBsonDocument: 'rawBsonDocument_value'}") | ||
| ); | ||
| Entry<String, BsonDocumentWrapper<RawBsonDocument>> originalBsonDocumentWrapperEntry = new SimpleImmutableEntry<>( | ||
| "bsonDocumentWrapper", | ||
| new BsonDocumentWrapper<>(originalRawBsonDocumentEntry.getValue(), Bson.DEFAULT_CODEC_REGISTRY.get(RawBsonDocument.class)) | ||
| ); | ||
| Entry<String, BsonDocument> originalBsonDocumentEntry = new SimpleImmutableEntry<>( | ||
| "bsonDocument", | ||
| new BsonDocument() | ||
| .append(originalBsonBinaryEntry.getKey(), originalBsonBinaryEntry.getValue()) | ||
| .append(originalBsonJavaScriptWithScopeEntry.getKey(), originalBsonJavaScriptWithScopeEntry.getValue()) | ||
| .append(originalRawBsonDocumentEntry.getKey(), originalRawBsonDocumentEntry.getValue()) | ||
| .append(originalBsonDocumentWrapperEntry.getKey(), originalBsonDocumentWrapperEntry.getValue()) | ||
| ); | ||
| Entry<String, BsonArray> originalBsonArrayEntry = new SimpleImmutableEntry<>( | ||
| "bsonArray", | ||
| new BsonArray(singletonList(new BsonArray())) | ||
| ); | ||
| Entry<String, RawBsonArray> originalRawBsonArrayEntry = new SimpleImmutableEntry<>( | ||
| "rawBsonArray", | ||
| rawBsonArray( | ||
| originalBsonBinaryEntry.getValue(), | ||
| originalBsonJavaScriptWithScopeEntry.getValue(), | ||
| originalRawBsonDocumentEntry.getValue(), | ||
| originalBsonDocumentWrapperEntry.getValue(), | ||
| originalBsonDocumentEntry.getValue(), | ||
| originalBsonArrayEntry.getValue()) | ||
| ); | ||
| BsonDocument original = new BsonDocument() | ||
| .append(originalBsonBinaryEntry.getKey(), originalBsonBinaryEntry.getValue()) | ||
| .append(originalBsonJavaScriptWithScopeEntry.getKey(), originalBsonJavaScriptWithScopeEntry.getValue()) | ||
| .append(originalRawBsonDocumentEntry.getKey(), originalRawBsonDocumentEntry.getValue()) | ||
| .append(originalBsonDocumentWrapperEntry.getKey(), originalBsonDocumentWrapperEntry.getValue()) | ||
| .append(originalBsonDocumentEntry.getKey(), originalBsonDocumentEntry.getValue()) | ||
| .append(originalBsonArrayEntry.getKey(), originalBsonArrayEntry.getValue()) | ||
| .append(originalRawBsonArrayEntry.getKey(), originalRawBsonArrayEntry.getValue()); | ||
| BsonDocument copy = BsonUtil.mutableDeepCopy(original); | ||
| assertEqualNotSameAndMutable(original, copy); | ||
| original.forEach((key, value) -> assertEqualNotSameAndMutable(value, copy.get(key))); | ||
| // check nested document | ||
| String nestedDocumentKey = originalBsonDocumentEntry.getKey(); | ||
| BsonDocument originalNestedDocument = original.getDocument(nestedDocumentKey); | ||
| BsonDocument copyNestedDocument = copy.getDocument(nestedDocumentKey); | ||
| assertEqualNotSameAndMutable(originalNestedDocument, copyNestedDocument); | ||
| originalNestedDocument.forEach((key, value) -> assertEqualNotSameAndMutable(value, copyNestedDocument.get(key))); | ||
| // check nested array | ||
| String nestedArrayKey = originalRawBsonArrayEntry.getKey(); | ||
| BsonArray originalNestedArray = original.getArray(nestedArrayKey); | ||
| BsonArray copyNestedArray = copy.getArray(nestedArrayKey); | ||
| assertEqualNotSameAndMutable(originalNestedArray, copyNestedArray); | ||
| for (int i = 0; i < originalNestedArray.size(); i++) { | ||
| assertEqualNotSameAndMutable(originalNestedArray.get(i), copyNestedArray.get(i)); | ||
| } | ||
| } | ||
|
|
||
| private static RawBsonArray rawBsonArray(final BsonValue... elements) { | ||
| return (RawBsonArray) new RawBsonDocument( | ||
| new BsonDocument("a", new BsonArray(asList(elements))), Bson.DEFAULT_CODEC_REGISTRY.get(BsonDocument.class)) | ||
| .get("a"); | ||
| } | ||
|
|
||
| private static void assertEqualNotSameAndMutable(final Object expected, final Object actual) { | ||
| assertEquals(expected, actual); | ||
| assertNotSame(expected, actual); | ||
| Class<?> actualClass = actual.getClass(); | ||
| if (expected instanceof BsonDocument) { | ||
| assertEquals(BsonDocument.class, actualClass); | ||
| } else if (expected instanceof BsonArray) { | ||
| assertEquals(BsonArray.class, actualClass); | ||
| } else if (expected instanceof BsonBinary) { | ||
| assertEquals(BsonBinary.class, actualClass); | ||
| } else if (expected instanceof BsonJavaScriptWithScope) { | ||
| assertEquals(BsonJavaScriptWithScope.class, actualClass); | ||
| } else { | ||
| org.bson.assertions.Assertions.fail("Unexpected " + expected.getClass().toString()); | ||
| } | ||
| } | ||
|
|
||
| private BsonUtilTest() { | ||
| } | ||
| } |
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
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.