From 39514c13638a7d40e2783420e4fe47032a90ab60 Mon Sep 17 00:00:00 2001 From: Jeff Yemin Date: Thu, 4 May 2023 11:46:57 -0400 Subject: [PATCH 1/7] Remove unused method --- .../connection/ByteBufBsonDocument.java | 19 ------------------- 1 file changed, 19 deletions(-) diff --git a/driver-core/src/main/com/mongodb/internal/connection/ByteBufBsonDocument.java b/driver-core/src/main/com/mongodb/internal/connection/ByteBufBsonDocument.java index 163a1c4e99..4034d85f36 100644 --- a/driver-core/src/main/com/mongodb/internal/connection/ByteBufBsonDocument.java +++ b/driver-core/src/main/com/mongodb/internal/connection/ByteBufBsonDocument.java @@ -22,7 +22,6 @@ import org.bson.BsonReader; import org.bson.BsonType; import org.bson.ByteBuf; -import org.bson.ByteBufNIO; import org.bson.RawBsonDocument; import org.bson.codecs.BsonDocumentCodec; import org.bson.codecs.DecoderContext; @@ -34,7 +33,6 @@ import java.io.InvalidObjectException; import java.io.ObjectInputStream; import java.io.StringWriter; -import java.nio.ByteOrder; import java.util.ArrayList; import java.util.List; @@ -43,23 +41,6 @@ class ByteBufBsonDocument extends AbstractByteBufBsonDocument { private final transient ByteBuf byteBuf; - static List createList(final ResponseBuffers responseBuffers) { - int numDocuments = responseBuffers.getReplyHeader().getNumberReturned(); - ByteBuf documentsBuffer = responseBuffers.getBodyByteBuffer(); - documentsBuffer.order(ByteOrder.LITTLE_ENDIAN); - List documents = new ArrayList<>(numDocuments); - while (documents.size() < numDocuments) { - int documentSizeInBytes = documentsBuffer.getInt(); - documentsBuffer.position(documentsBuffer.position() - 4); - ByteBuf documentBuffer = documentsBuffer.duplicate(); - documentBuffer.limit(documentBuffer.position() + documentSizeInBytes); - documents.add(new ByteBufBsonDocument(new ByteBufNIO(documentBuffer.asNIO()))); - documentBuffer.release(); - documentsBuffer.position(documentsBuffer.position() + documentSizeInBytes); - } - return documents; - } - static List createList(final ByteBufferBsonOutput bsonOutput, final int startPosition) { List duplicateByteBuffers = bsonOutput.getByteBuffers(); CompositeByteBuf outputByteBuf = new CompositeByteBuf(duplicateByteBuffers); From 95e5161ec7687182797a9479432bd0f4a2883781 Mon Sep 17 00:00:00 2001 From: Jeff Yemin Date: Mon, 15 May 2023 17:35:24 +0200 Subject: [PATCH 2/7] Suppress warning --- .../com/mongodb/internal/connection/ByteBufBsonDocument.java | 1 + 1 file changed, 1 insertion(+) diff --git a/driver-core/src/main/com/mongodb/internal/connection/ByteBufBsonDocument.java b/driver-core/src/main/com/mongodb/internal/connection/ByteBufBsonDocument.java index 4034d85f36..a29ef85281 100644 --- a/driver-core/src/main/com/mongodb/internal/connection/ByteBufBsonDocument.java +++ b/driver-core/src/main/com/mongodb/internal/connection/ByteBufBsonDocument.java @@ -118,6 +118,7 @@ T findInDocument(final Finder finder) { return finder.notFound(); } + @SuppressWarnings("MethodDoesntCallSuperMethod") @Override public BsonDocument clone() { byte[] clonedBytes = new byte[byteBuf.remaining()]; From c768fbdaa2ef28fd3cb89d3acd46328fe8829125 Mon Sep 17 00:00:00 2001 From: Jeff Yemin Date: Mon, 15 May 2023 17:45:03 +0200 Subject: [PATCH 3/7] Inline abstract superclass --- .../AbstractByteBufBsonDocument.java | 238 ------------------ .../connection/ByteBufBsonDocument.java | 223 +++++++++++++++- 2 files changed, 214 insertions(+), 247 deletions(-) delete mode 100644 driver-core/src/main/com/mongodb/internal/connection/AbstractByteBufBsonDocument.java diff --git a/driver-core/src/main/com/mongodb/internal/connection/AbstractByteBufBsonDocument.java b/driver-core/src/main/com/mongodb/internal/connection/AbstractByteBufBsonDocument.java deleted file mode 100644 index 4d344401fb..0000000000 --- a/driver-core/src/main/com/mongodb/internal/connection/AbstractByteBufBsonDocument.java +++ /dev/null @@ -1,238 +0,0 @@ -/* - * 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 com.mongodb.internal.connection; - -import com.mongodb.lang.Nullable; -import org.bson.BsonDocument; -import org.bson.BsonReader; -import org.bson.BsonValue; -import org.bson.codecs.BsonValueCodecProvider; -import org.bson.codecs.DecoderContext; -import org.bson.codecs.configuration.CodecRegistry; - -import java.util.Collection; -import java.util.Map; -import java.util.NoSuchElementException; -import java.util.Set; - -import static com.mongodb.assertions.Assertions.assertNotNull; -import static com.mongodb.assertions.Assertions.notNull; -import static org.bson.codecs.BsonValueCodecProvider.getClassForBsonType; -import static org.bson.codecs.configuration.CodecRegistries.fromProviders; - -abstract class AbstractByteBufBsonDocument extends BsonDocument { - private static final long serialVersionUID = 1L; - private static final CodecRegistry REGISTRY = fromProviders(new BsonValueCodecProvider()); - - @Override - public void clear() { - throw new UnsupportedOperationException("ByteBufBsonDocument instances are immutable"); - } - - @Override - public BsonValue put(final String key, final BsonValue value) { - throw new UnsupportedOperationException("ByteBufBsonDocument instances are immutable"); - } - - @Override - public BsonDocument append(final String key, final BsonValue value) { - throw new UnsupportedOperationException("ByteBufBsonDocument instances are immutable"); - } - - @Override - public void putAll(final Map m) { - throw new UnsupportedOperationException("ByteBufBsonDocument instances are immutable"); - } - - @Override - public BsonValue remove(final Object key) { - throw new UnsupportedOperationException("ByteBufBsonDocument instances are immutable"); - } - - @Override - public boolean isEmpty() { - return assertNotNull(findInDocument(new Finder() { - @Override - public Boolean find(final BsonReader bsonReader) { - return false; - } - - @Override - public Boolean notFound() { - return true; - } - })); - } - - @Override - public int size() { - return assertNotNull(findInDocument(new Finder() { - private int size; - - @Override - @Nullable - public Integer find(final BsonReader bsonReader) { - size++; - bsonReader.readName(); - bsonReader.skipValue(); - return null; - } - - @Override - public Integer notFound() { - return size; - } - })); - } - - @Override - public Set> entrySet() { - return toBaseBsonDocument().entrySet(); - } - - @Override - public Collection values() { - return toBaseBsonDocument().values(); - } - - @Override - public Set keySet() { - return toBaseBsonDocument().keySet(); - } - - @Override - public boolean containsKey(final Object key) { - if (key == null) { - throw new IllegalArgumentException("key can not be null"); - } - - Boolean containsKey = findInDocument(new Finder() { - @Override - public Boolean find(final BsonReader bsonReader) { - if (bsonReader.readName().equals(key)) { - return true; - } - bsonReader.skipValue(); - return null; - } - - @Override - public Boolean notFound() { - return false; - } - }); - return containsKey != null ? containsKey : false; - } - - @Override - public boolean containsValue(final Object value) { - Boolean containsValue = findInDocument(new Finder() { - @Override - public Boolean find(final BsonReader bsonReader) { - bsonReader.skipName(); - if (deserializeBsonValue(bsonReader).equals(value)) { - return true; - } - return null; - } - - @Override - public Boolean notFound() { - return false; - } - }); - return containsValue != null ? containsValue : false; - } - - @Nullable - @Override - public BsonValue get(final Object key) { - notNull("key", key); - return findInDocument(new Finder() { - @Override - public BsonValue find(final BsonReader bsonReader) { - if (bsonReader.readName().equals(key)) { - return deserializeBsonValue(bsonReader); - } - bsonReader.skipValue(); - return null; - } - - @Nullable - @Override - public BsonValue notFound() { - return null; - } - }); - } - - /** - * Gets the first key in this document. - * - * @return the first key in this document - * @throws java.util.NoSuchElementException if the document is empty - */ - public String getFirstKey() { - return assertNotNull(findInDocument(new Finder() { - @Override - public String find(final BsonReader bsonReader) { - return bsonReader.readName(); - } - - @Override - public String notFound() { - throw new NoSuchElementException(); - } - })); - } - - interface Finder { - @Nullable - T find(BsonReader bsonReader); - @Nullable - T notFound(); - } - - @Nullable abstract T findInDocument(Finder finder); - - //AbstractBsonReader getBsonReader(); - - abstract BsonDocument toBaseBsonDocument(); - - @Override - public boolean equals(final Object o) { - if (this == o) { - return true; - } - return toBaseBsonDocument().equals(o); - } - - @Override - public int hashCode() { - return toBaseBsonDocument().hashCode(); - } - - private BsonValue deserializeBsonValue(final BsonReader bsonReader) { - return REGISTRY.get(getClassForBsonType(bsonReader.getCurrentBsonType())).decode(bsonReader, DecoderContext.builder().build()); - } - - // see https://docs.oracle.com/javase/6/docs/platform/serialization/spec/output.html - Object writeReplace() { - return toBaseBsonDocument(); - } - -} diff --git a/driver-core/src/main/com/mongodb/internal/connection/ByteBufBsonDocument.java b/driver-core/src/main/com/mongodb/internal/connection/ByteBufBsonDocument.java index a29ef85281..bc9725342e 100644 --- a/driver-core/src/main/com/mongodb/internal/connection/ByteBufBsonDocument.java +++ b/driver-core/src/main/com/mongodb/internal/connection/ByteBufBsonDocument.java @@ -21,10 +21,13 @@ import org.bson.BsonDocument; import org.bson.BsonReader; import org.bson.BsonType; +import org.bson.BsonValue; import org.bson.ByteBuf; import org.bson.RawBsonDocument; import org.bson.codecs.BsonDocumentCodec; +import org.bson.codecs.BsonValueCodecProvider; import org.bson.codecs.DecoderContext; +import org.bson.codecs.configuration.CodecRegistry; import org.bson.io.ByteBufferBsonInput; import org.bson.json.JsonMode; import org.bson.json.JsonWriter; @@ -34,11 +37,22 @@ import java.io.ObjectInputStream; import java.io.StringWriter; import java.util.ArrayList; +import java.util.Collection; import java.util.List; +import java.util.Map; +import java.util.NoSuchElementException; +import java.util.Set; -class ByteBufBsonDocument extends AbstractByteBufBsonDocument { +import static com.mongodb.assertions.Assertions.assertNotNull; +import static com.mongodb.assertions.Assertions.notNull; +import static org.bson.codecs.BsonValueCodecProvider.getClassForBsonType; +import static org.bson.codecs.configuration.CodecRegistries.fromProviders; + +class ByteBufBsonDocument extends BsonDocument { private static final long serialVersionUID = 2L; + private static final CodecRegistry REGISTRY = fromProviders(new BsonValueCodecProvider()); + private final transient ByteBuf byteBuf; static List createList(final ByteBufferBsonOutput bsonOutput, final int startPosition) { @@ -99,6 +113,14 @@ public BsonReader asBsonReader() { return new BsonBinaryReader(new ByteBufferBsonInput(byteBuf.duplicate())); } + @SuppressWarnings("MethodDoesntCallSuperMethod") + @Override + public BsonDocument clone() { + byte[] clonedBytes = new byte[byteBuf.remaining()]; + byteBuf.get(byteBuf.position(), clonedBytes); + return new RawBsonDocument(clonedBytes); + } + @Nullable T findInDocument(final Finder finder) { ByteBuf duplicateByteBuf = byteBuf.duplicate(); @@ -118,14 +140,6 @@ T findInDocument(final Finder finder) { return finder.notFound(); } - @SuppressWarnings("MethodDoesntCallSuperMethod") - @Override - public BsonDocument clone() { - byte[] clonedBytes = new byte[byteBuf.remaining()]; - byteBuf.get(byteBuf.position(), clonedBytes); - return new RawBsonDocument(clonedBytes); - } - int getSizeInBytes() { return byteBuf.getInt(byteBuf.position()); } @@ -143,6 +157,197 @@ BsonDocument toBaseBsonDocument() { this.byteBuf = byteBuf; } + @Override + public void clear() { + throw new UnsupportedOperationException("ByteBufBsonDocument instances are immutable"); + } + + @Override + public BsonValue put(final String key, final BsonValue value) { + throw new UnsupportedOperationException("ByteBufBsonDocument instances are immutable"); + } + + @Override + public BsonDocument append(final String key, final BsonValue value) { + throw new UnsupportedOperationException("ByteBufBsonDocument instances are immutable"); + } + + @Override + public void putAll(final Map m) { + throw new UnsupportedOperationException("ByteBufBsonDocument instances are immutable"); + } + + @Override + public BsonValue remove(final Object key) { + throw new UnsupportedOperationException("ByteBufBsonDocument instances are immutable"); + } + + @Override + public boolean isEmpty() { + return assertNotNull(findInDocument(new Finder() { + @Override + public Boolean find(final BsonReader bsonReader) { + return false; + } + + @Override + public Boolean notFound() { + return true; + } + })); + } + + @Override + public int size() { + return assertNotNull(findInDocument(new Finder() { + private int size; + + @Override + @Nullable + public Integer find(final BsonReader bsonReader) { + size++; + bsonReader.readName(); + bsonReader.skipValue(); + return null; + } + + @Override + public Integer notFound() { + return size; + } + })); + } + + @Override + public Set> entrySet() { + return toBaseBsonDocument().entrySet(); + } + + @Override + public Collection values() { + return toBaseBsonDocument().values(); + } + + @Override + public Set keySet() { + return toBaseBsonDocument().keySet(); + } + + @Override + public boolean containsKey(final Object key) { + if (key == null) { + throw new IllegalArgumentException("key can not be null"); + } + + Boolean containsKey = findInDocument(new Finder() { + @Override + public Boolean find(final BsonReader bsonReader) { + if (bsonReader.readName().equals(key)) { + return true; + } + bsonReader.skipValue(); + return null; + } + + @Override + public Boolean notFound() { + return false; + } + }); + return containsKey != null ? containsKey : false; + } + + @Override + public boolean containsValue(final Object value) { + Boolean containsValue = findInDocument(new Finder() { + @Override + public Boolean find(final BsonReader bsonReader) { + bsonReader.skipName(); + if (deserializeBsonValue(bsonReader).equals(value)) { + return true; + } + return null; + } + + @Override + public Boolean notFound() { + return false; + } + }); + return containsValue != null ? containsValue : false; + } + + @Nullable + @Override + public BsonValue get(final Object key) { + notNull("key", key); + return findInDocument(new Finder() { + @Override + public BsonValue find(final BsonReader bsonReader) { + if (bsonReader.readName().equals(key)) { + return deserializeBsonValue(bsonReader); + } + bsonReader.skipValue(); + return null; + } + + @Nullable + @Override + public BsonValue notFound() { + return null; + } + }); + } + + /** + * Gets the first key in this document. + * + * @return the first key in this document + * @throws java.util.NoSuchElementException if the document is empty + */ + public String getFirstKey() { + return assertNotNull(findInDocument(new Finder() { + @Override + public String find(final BsonReader bsonReader) { + return bsonReader.readName(); + } + + @Override + public String notFound() { + throw new NoSuchElementException(); + } + })); + } + + interface Finder { + @Nullable + T find(BsonReader bsonReader); + @Nullable + T notFound(); + } + + @Override + public boolean equals(final Object o) { + if (this == o) { + return true; + } + return toBaseBsonDocument().equals(o); + } + + @Override + public int hashCode() { + return toBaseBsonDocument().hashCode(); + } + + private BsonValue deserializeBsonValue(final BsonReader bsonReader) { + return REGISTRY.get(getClassForBsonType(bsonReader.getCurrentBsonType())).decode(bsonReader, DecoderContext.builder().build()); + } + + // see https://docs.oracle.com/javase/6/docs/platform/serialization/spec/output.html + Object writeReplace() { + return toBaseBsonDocument(); + } + // see https://docs.oracle.com/javase/6/docs/platform/serialization/spec/input.html private void readObject(final ObjectInputStream stream) throws InvalidObjectException { throw new InvalidObjectException("Proxy required"); From d20adbf688c8e2eeb1c677b1b60cbd7631b8ee4a Mon Sep 17 00:00:00 2001 From: Jeff Yemin Date: Mon, 15 May 2023 17:48:46 +0200 Subject: [PATCH 4/7] Remove unnecessary equal/hashCode override --- .../internal/connection/ByteBufBsonDocument.java | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/driver-core/src/main/com/mongodb/internal/connection/ByteBufBsonDocument.java b/driver-core/src/main/com/mongodb/internal/connection/ByteBufBsonDocument.java index bc9725342e..88497142ad 100644 --- a/driver-core/src/main/com/mongodb/internal/connection/ByteBufBsonDocument.java +++ b/driver-core/src/main/com/mongodb/internal/connection/ByteBufBsonDocument.java @@ -326,19 +326,6 @@ interface Finder { T notFound(); } - @Override - public boolean equals(final Object o) { - if (this == o) { - return true; - } - return toBaseBsonDocument().equals(o); - } - - @Override - public int hashCode() { - return toBaseBsonDocument().hashCode(); - } - private BsonValue deserializeBsonValue(final BsonReader bsonReader) { return REGISTRY.get(getClassForBsonType(bsonReader.getCurrentBsonType())).decode(bsonReader, DecoderContext.builder().build()); } From 88593b185d469f408e3db7456f0303e5016f4bcc Mon Sep 17 00:00:00 2001 From: Jeff Yemin Date: Wed, 31 May 2023 15:59:56 -0400 Subject: [PATCH 5/7] Update driver-core/src/main/com/mongodb/internal/connection/ByteBufBsonDocument.java Co-authored-by: Valentin Kovalenko --- .../com/mongodb/internal/connection/ByteBufBsonDocument.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/driver-core/src/main/com/mongodb/internal/connection/ByteBufBsonDocument.java b/driver-core/src/main/com/mongodb/internal/connection/ByteBufBsonDocument.java index 88497142ad..483cd6d5d0 100644 --- a/driver-core/src/main/com/mongodb/internal/connection/ByteBufBsonDocument.java +++ b/driver-core/src/main/com/mongodb/internal/connection/ByteBufBsonDocument.java @@ -319,7 +319,7 @@ public String notFound() { })); } - interface Finder { + private interface Finder { @Nullable T find(BsonReader bsonReader); @Nullable From f9d21e5d6205f5d3ace67f2e092595fe2c5778ba Mon Sep 17 00:00:00 2001 From: Jeff Yemin Date: Wed, 31 May 2023 16:00:01 -0400 Subject: [PATCH 6/7] Update driver-core/src/main/com/mongodb/internal/connection/ByteBufBsonDocument.java Co-authored-by: Valentin Kovalenko --- .../com/mongodb/internal/connection/ByteBufBsonDocument.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/driver-core/src/main/com/mongodb/internal/connection/ByteBufBsonDocument.java b/driver-core/src/main/com/mongodb/internal/connection/ByteBufBsonDocument.java index 483cd6d5d0..cd6a36802e 100644 --- a/driver-core/src/main/com/mongodb/internal/connection/ByteBufBsonDocument.java +++ b/driver-core/src/main/com/mongodb/internal/connection/ByteBufBsonDocument.java @@ -48,7 +48,7 @@ import static org.bson.codecs.BsonValueCodecProvider.getClassForBsonType; import static org.bson.codecs.configuration.CodecRegistries.fromProviders; -class ByteBufBsonDocument extends BsonDocument { +final class ByteBufBsonDocument extends BsonDocument { private static final long serialVersionUID = 2L; private static final CodecRegistry REGISTRY = fromProviders(new BsonValueCodecProvider()); From d593b47bf14b124a3e01705e44c67c05c76c8039 Mon Sep 17 00:00:00 2001 From: Jeff Yemin Date: Wed, 31 May 2023 16:00:08 -0400 Subject: [PATCH 7/7] Update driver-core/src/main/com/mongodb/internal/connection/ByteBufBsonDocument.java Co-authored-by: Valentin Kovalenko --- .../com/mongodb/internal/connection/ByteBufBsonDocument.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/driver-core/src/main/com/mongodb/internal/connection/ByteBufBsonDocument.java b/driver-core/src/main/com/mongodb/internal/connection/ByteBufBsonDocument.java index cd6a36802e..676dba2d78 100644 --- a/driver-core/src/main/com/mongodb/internal/connection/ByteBufBsonDocument.java +++ b/driver-core/src/main/com/mongodb/internal/connection/ByteBufBsonDocument.java @@ -331,7 +331,7 @@ private BsonValue deserializeBsonValue(final BsonReader bsonReader) { } // see https://docs.oracle.com/javase/6/docs/platform/serialization/spec/output.html - Object writeReplace() { + private Object writeReplace() { return toBaseBsonDocument(); }