-
Notifications
You must be signed in to change notification settings - Fork 1.5k
ByteBufBsonDocument and RawBsonDocument simplifications
#1902
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
base: ByteBuf
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -35,7 +35,11 @@ | |
| import java.io.StringWriter; | ||
| import java.nio.ByteBuffer; | ||
| import java.nio.ByteOrder; | ||
| import java.util.AbstractMap; | ||
| import java.util.ArrayList; | ||
| import java.util.Collection; | ||
| import java.util.LinkedHashSet; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.NoSuchElementException; | ||
| import java.util.Set; | ||
|
|
@@ -225,17 +229,42 @@ public int size() { | |
|
|
||
| @Override | ||
| public Set<Entry<String, BsonValue>> entrySet() { | ||
| return toBaseBsonDocument().entrySet(); | ||
| List<Entry<String, BsonValue>> entries = new ArrayList<>(); | ||
| try (BsonBinaryReader bsonReader = createReader()) { | ||
| bsonReader.readStartDocument(); | ||
| while (bsonReader.readBsonType() != BsonType.END_OF_DOCUMENT) { | ||
| String key = bsonReader.readName(); | ||
| BsonValue value = RawBsonValueHelper.decode(bytes, bsonReader); | ||
| entries.add(new AbstractMap.SimpleImmutableEntry<>(key, value)); | ||
| } | ||
| } | ||
| return new LinkedHashSet<>(entries); | ||
| } | ||
|
|
||
| @Override | ||
| public Collection<BsonValue> values() { | ||
| return toBaseBsonDocument().values(); | ||
| List<BsonValue> values = new ArrayList<>(); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think I understand the logic , the entrySet returns a SET though looking at the previous implementation it can still return duplicates This test case is passing but fails in this branch because LinkedHashSet removes duplicate values, I believe we should preserve an already broken behavior and return duplicates |
||
| try (BsonBinaryReader bsonReader = createReader()) { | ||
| bsonReader.readStartDocument(); | ||
| while (bsonReader.readBsonType() != BsonType.END_OF_DOCUMENT) { | ||
| bsonReader.skipName(); | ||
| values.add(RawBsonValueHelper.decode(bytes, bsonReader)); | ||
| } | ||
| } | ||
| return new LinkedHashSet<>(values); | ||
| } | ||
|
|
||
| @Override | ||
| public Set<String> keySet() { | ||
| return toBaseBsonDocument().keySet(); | ||
| List<String> keys = new ArrayList<>(); | ||
| try (BsonBinaryReader bsonReader = createReader()) { | ||
| bsonReader.readStartDocument(); | ||
| while (bsonReader.readBsonType() != BsonType.END_OF_DOCUMENT) { | ||
| keys.add(bsonReader.readName()); | ||
| bsonReader.skipValue(); | ||
| } | ||
| } | ||
| return new LinkedHashSet<>(keys); | ||
| } | ||
|
|
||
| @Override | ||
|
|
@@ -318,12 +347,19 @@ public String toJson(final JsonWriterSettings settings) { | |
|
|
||
| @Override | ||
| public boolean equals(final Object o) { | ||
| return toBaseBsonDocument().equals(o); | ||
| return toBsonDocument().equals(o); | ||
| } | ||
|
|
||
| @Override | ||
| public int hashCode() { | ||
| return toBaseBsonDocument().hashCode(); | ||
| return toBsonDocument().hashCode(); | ||
| } | ||
|
|
||
| @Override | ||
| public BsonDocument toBsonDocument() { | ||
| try (BsonBinaryReader bsonReader = createReader()) { | ||
| return new BsonDocumentCodec().decode(bsonReader, DecoderContext.builder().build()); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
|
|
@@ -335,13 +371,6 @@ private BsonBinaryReader createReader() { | |
| return new BsonBinaryReader(new ByteBufferBsonInput(getByteBuffer())); | ||
| } | ||
|
|
||
| // Transform to an org.bson.BsonDocument instance | ||
| private BsonDocument toBaseBsonDocument() { | ||
| try (BsonBinaryReader bsonReader = createReader()) { | ||
| return new BsonDocumentCodec().decode(bsonReader, DecoderContext.builder().build()); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Write the replacement object. | ||
| * | ||
|
|
||
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.
the javadoc for
RawBsonDocumentsay it's immutable, does it make sense to store entrySet/values in memory instead of calculating them all from scratch ? Something like what java.lang.String does with a hashcode, keep hashcode as class field but calculates it only once during the firsthashCodecallNot mandatory