Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 41 additions & 12 deletions bson/src/main/org/bson/RawBsonDocument.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -225,17 +229,42 @@ public int size() {

@Override
public Set<Entry<String, BsonValue>> entrySet() {
return toBaseBsonDocument().entrySet();
List<Entry<String, BsonValue>> entries = new ArrayList<>();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the javadoc for RawBsonDocument say 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 first hashCode call

Not mandatory

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<>();
Copy link
Contributor

@strogiyotec strogiyotec Mar 5, 2026

Choose a reason for hiding this comment

The 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
Here is the test case using latest main without this change

    @Test
    void testLatestPR() {
        BsonDocument doc = new BsonDocument()
                .append("a", new BsonInt32(1))
                .append("b", new BsonInt32(1))
                .append("c", new BsonInt32(1));
        RawBsonDocument rawDoc = new RawBsonDocument(doc, new BsonDocumentCodec());

        // BsonDocument preserves all 3 values
        assertEquals(3, doc.values().size());

        // RawBsonDocument should too — but LinkedHashSet deduplicates them to 1
        assertEquals(3, rawDoc.values().size());
    }

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
Expand Down Expand Up @@ -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
Expand All @@ -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.
*
Expand Down
Loading