From 9bbd6ac6b0f56c4f651a3cee84f267f2488356ab Mon Sep 17 00:00:00 2001 From: Valentin Kovalenko Date: Thu, 22 Jun 2023 17:33:15 -0600 Subject: [PATCH] Reorganize `NettyBufferProvider`, `NettyByteBuf` and improve internal docs --- .../mongodb/connection/BufferProvider.java | 2 + .../connection/netty/NettyBufferProvider.java | 40 ------------------- .../mongodb/connection/netty/NettyStream.java | 1 + .../connection/netty/NettyByteBuf.java | 22 +++++++--- .../connection/netty/package-info.java | 23 +++++++++++ .../netty/ByteBufSpecification.groovy | 1 + .../connection/ByteBufSpecification.groovy | 24 ++++++++++- 7 files changed, 67 insertions(+), 46 deletions(-) delete mode 100644 driver-core/src/main/com/mongodb/connection/netty/NettyBufferProvider.java rename driver-core/src/main/com/mongodb/{ => internal}/connection/netty/NettyByteBuf.java (86%) create mode 100644 driver-core/src/main/com/mongodb/internal/connection/netty/package-info.java diff --git a/driver-core/src/main/com/mongodb/connection/BufferProvider.java b/driver-core/src/main/com/mongodb/connection/BufferProvider.java index 711410af8e2..4f0fd0daa5b 100644 --- a/driver-core/src/main/com/mongodb/connection/BufferProvider.java +++ b/driver-core/src/main/com/mongodb/connection/BufferProvider.java @@ -16,6 +16,7 @@ package com.mongodb.connection; +import com.mongodb.annotations.ThreadSafe; import org.bson.ByteBuf; /** @@ -23,6 +24,7 @@ * * @since 3.0 */ +@ThreadSafe public interface BufferProvider { /** * Gets a buffer with the givens capacity. diff --git a/driver-core/src/main/com/mongodb/connection/netty/NettyBufferProvider.java b/driver-core/src/main/com/mongodb/connection/netty/NettyBufferProvider.java deleted file mode 100644 index e7adb0e6c20..00000000000 --- a/driver-core/src/main/com/mongodb/connection/netty/NettyBufferProvider.java +++ /dev/null @@ -1,40 +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.connection.netty; - -import com.mongodb.connection.BufferProvider; -import io.netty.buffer.ByteBufAllocator; -import io.netty.buffer.PooledByteBufAllocator; -import org.bson.ByteBuf; - -final class NettyBufferProvider implements BufferProvider { - - private final ByteBufAllocator allocator; - - NettyBufferProvider() { - allocator = PooledByteBufAllocator.DEFAULT; - } - - NettyBufferProvider(final ByteBufAllocator allocator) { - this.allocator = allocator; - } - - @Override - public ByteBuf getBuffer(final int size) { - return new NettyByteBuf(allocator.directBuffer(size, size)); - } -} diff --git a/driver-core/src/main/com/mongodb/connection/netty/NettyStream.java b/driver-core/src/main/com/mongodb/connection/netty/NettyStream.java index a0dc8f3020a..052bf7bfd67 100644 --- a/driver-core/src/main/com/mongodb/connection/netty/NettyStream.java +++ b/driver-core/src/main/com/mongodb/connection/netty/NettyStream.java @@ -29,6 +29,7 @@ import com.mongodb.connection.SocketSettings; import com.mongodb.connection.SslSettings; import com.mongodb.connection.Stream; +import com.mongodb.internal.connection.netty.NettyByteBuf; import com.mongodb.lang.Nullable; import io.netty.bootstrap.Bootstrap; import io.netty.buffer.ByteBufAllocator; diff --git a/driver-core/src/main/com/mongodb/connection/netty/NettyByteBuf.java b/driver-core/src/main/com/mongodb/internal/connection/netty/NettyByteBuf.java similarity index 86% rename from driver-core/src/main/com/mongodb/connection/netty/NettyByteBuf.java rename to driver-core/src/main/com/mongodb/internal/connection/netty/NettyByteBuf.java index 28860e43e1b..074e77de04f 100644 --- a/driver-core/src/main/com/mongodb/connection/netty/NettyByteBuf.java +++ b/driver-core/src/main/com/mongodb/internal/connection/netty/NettyByteBuf.java @@ -14,29 +14,41 @@ * limitations under the License. */ -package com.mongodb.connection.netty; +package com.mongodb.internal.connection.netty; import org.bson.ByteBuf; import java.nio.ByteBuffer; import java.nio.ByteOrder; -final class NettyByteBuf implements ByteBuf { +/** + *

This class is not part of the public API and may be removed or changed at any time

+ */ +public final class NettyByteBuf implements ByteBuf { private io.netty.buffer.ByteBuf proxied; private boolean isWriting = true; + /** + * @param proxied This constructor stores a reference to {@code proxied} in the heap memory + * but does not {@linkplain io.netty.buffer.ByteBuf#retain() retain} {@code proxied}. + * A caller may have to do that depending on the + * reference counting approach he uses. + */ @SuppressWarnings("deprecation") - NettyByteBuf(final io.netty.buffer.ByteBuf proxied) { + public NettyByteBuf(final io.netty.buffer.ByteBuf proxied) { this.proxied = proxied.order(ByteOrder.LITTLE_ENDIAN); } - NettyByteBuf(final io.netty.buffer.ByteBuf proxied, final boolean isWriting) { + /** + * @param proxied See {@link #NettyByteBuf(io.netty.buffer.ByteBuf)}. + */ + private NettyByteBuf(final io.netty.buffer.ByteBuf proxied, final boolean isWriting) { this(proxied); this.isWriting = isWriting; } - io.netty.buffer.ByteBuf asByteBuf() { + public io.netty.buffer.ByteBuf asByteBuf() { return proxied; } diff --git a/driver-core/src/main/com/mongodb/internal/connection/netty/package-info.java b/driver-core/src/main/com/mongodb/internal/connection/netty/package-info.java new file mode 100644 index 00000000000..f19b029d031 --- /dev/null +++ b/driver-core/src/main/com/mongodb/internal/connection/netty/package-info.java @@ -0,0 +1,23 @@ +/* + * 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. + */ + +/** + * Contains Netty-specific program elements. + */ +@NonNullApi +package com.mongodb.internal.connection.netty; + +import com.mongodb.lang.NonNullApi; diff --git a/driver-core/src/test/unit/com/mongodb/connection/netty/ByteBufSpecification.groovy b/driver-core/src/test/unit/com/mongodb/connection/netty/ByteBufSpecification.groovy index 35026ba5724..d2ab8ebf9dc 100644 --- a/driver-core/src/test/unit/com/mongodb/connection/netty/ByteBufSpecification.groovy +++ b/driver-core/src/test/unit/com/mongodb/connection/netty/ByteBufSpecification.groovy @@ -16,6 +16,7 @@ package com.mongodb.connection.netty +import com.mongodb.internal.connection.netty.NettyByteBuf import io.netty.buffer.ByteBufAllocator import org.bson.ByteBufNIO import spock.lang.Specification diff --git a/driver-core/src/test/unit/com/mongodb/internal/connection/ByteBufSpecification.groovy b/driver-core/src/test/unit/com/mongodb/internal/connection/ByteBufSpecification.groovy index 55520dd0d7d..e8b2f27f618 100644 --- a/driver-core/src/test/unit/com/mongodb/internal/connection/ByteBufSpecification.groovy +++ b/driver-core/src/test/unit/com/mongodb/internal/connection/ByteBufSpecification.groovy @@ -16,7 +16,11 @@ package com.mongodb.internal.connection -import com.mongodb.connection.netty.NettyBufferProvider +import com.mongodb.connection.BufferProvider +import com.mongodb.internal.connection.netty.NettyByteBuf +import io.netty.buffer.ByteBufAllocator +import io.netty.buffer.PooledByteBufAllocator +import org.bson.ByteBuf import spock.lang.Specification class ByteBufSpecification extends Specification { @@ -234,4 +238,22 @@ class ByteBufSpecification extends Specification { where: provider << [new NettyBufferProvider(), new SimpleBufferProvider()] } + + static final class NettyBufferProvider implements BufferProvider { + private final ByteBufAllocator allocator + + NettyBufferProvider() { + allocator = PooledByteBufAllocator.DEFAULT + } + + @Override + ByteBuf getBuffer(final int size) { + io.netty.buffer.ByteBuf buffer = allocator.directBuffer(size, size) + try { + new NettyByteBuf(buffer.retain()) + } finally { + buffer.release(); + } + } + } }