Skip to content

Commit ca5271d

Browse files
authored
Reorganize NettyBufferProvider, NettyByteBuf and improve docs (#1149)
1 parent 897565c commit ca5271d

File tree

7 files changed

+67
-46
lines changed

7 files changed

+67
-46
lines changed

driver-core/src/main/com/mongodb/connection/BufferProvider.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,15 @@
1616

1717
package com.mongodb.connection;
1818

19+
import com.mongodb.annotations.ThreadSafe;
1920
import org.bson.ByteBuf;
2021

2122
/**
2223
* A provider of instances of ByteBuf.
2324
*
2425
* @since 3.0
2526
*/
27+
@ThreadSafe
2628
public interface BufferProvider {
2729
/**
2830
* Gets a buffer with the givens capacity.

driver-core/src/main/com/mongodb/connection/netty/NettyBufferProvider.java

Lines changed: 0 additions & 40 deletions
This file was deleted.

driver-core/src/main/com/mongodb/connection/netty/NettyStream.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import com.mongodb.connection.SocketSettings;
3030
import com.mongodb.connection.SslSettings;
3131
import com.mongodb.connection.Stream;
32+
import com.mongodb.internal.connection.netty.NettyByteBuf;
3233
import com.mongodb.lang.Nullable;
3334
import io.netty.bootstrap.Bootstrap;
3435
import io.netty.buffer.ByteBufAllocator;

driver-core/src/main/com/mongodb/connection/netty/NettyByteBuf.java renamed to driver-core/src/main/com/mongodb/internal/connection/netty/NettyByteBuf.java

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,29 +14,41 @@
1414
* limitations under the License.
1515
*/
1616

17-
package com.mongodb.connection.netty;
17+
package com.mongodb.internal.connection.netty;
1818

1919
import org.bson.ByteBuf;
2020

2121
import java.nio.ByteBuffer;
2222
import java.nio.ByteOrder;
2323

24-
final class NettyByteBuf implements ByteBuf {
24+
/**
25+
* <p>This class is not part of the public API and may be removed or changed at any time</p>
26+
*/
27+
public final class NettyByteBuf implements ByteBuf {
2528

2629
private io.netty.buffer.ByteBuf proxied;
2730
private boolean isWriting = true;
2831

32+
/**
33+
* @param proxied This constructor stores a reference to {@code proxied} in the heap memory
34+
* but does not {@linkplain io.netty.buffer.ByteBuf#retain() retain} {@code proxied}.
35+
* A caller may have to do that depending on the
36+
* <a href="https://jira.mongodb.org/browse/JAVA-3964">reference counting approach</a> he uses.
37+
*/
2938
@SuppressWarnings("deprecation")
30-
NettyByteBuf(final io.netty.buffer.ByteBuf proxied) {
39+
public NettyByteBuf(final io.netty.buffer.ByteBuf proxied) {
3140
this.proxied = proxied.order(ByteOrder.LITTLE_ENDIAN);
3241
}
3342

34-
NettyByteBuf(final io.netty.buffer.ByteBuf proxied, final boolean isWriting) {
43+
/**
44+
* @param proxied See {@link #NettyByteBuf(io.netty.buffer.ByteBuf)}.
45+
*/
46+
private NettyByteBuf(final io.netty.buffer.ByteBuf proxied, final boolean isWriting) {
3547
this(proxied);
3648
this.isWriting = isWriting;
3749
}
3850

39-
io.netty.buffer.ByteBuf asByteBuf() {
51+
public io.netty.buffer.ByteBuf asByteBuf() {
4052
return proxied;
4153
}
4254

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* Copyright 2008-present MongoDB, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
/**
18+
* Contains <a href="https://netty.io/">Netty</a>-specific program elements.
19+
*/
20+
@NonNullApi
21+
package com.mongodb.internal.connection.netty;
22+
23+
import com.mongodb.lang.NonNullApi;

driver-core/src/test/unit/com/mongodb/connection/netty/ByteBufSpecification.groovy

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package com.mongodb.connection.netty
1818

19+
import com.mongodb.internal.connection.netty.NettyByteBuf
1920
import io.netty.buffer.ByteBufAllocator
2021
import org.bson.ByteBufNIO
2122
import spock.lang.Specification

driver-core/src/test/unit/com/mongodb/internal/connection/ByteBufSpecification.groovy

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,11 @@
1616

1717
package com.mongodb.internal.connection
1818

19-
import com.mongodb.connection.netty.NettyBufferProvider
19+
import com.mongodb.connection.BufferProvider
20+
import com.mongodb.internal.connection.netty.NettyByteBuf
21+
import io.netty.buffer.ByteBufAllocator
22+
import io.netty.buffer.PooledByteBufAllocator
23+
import org.bson.ByteBuf
2024
import spock.lang.Specification
2125

2226
class ByteBufSpecification extends Specification {
@@ -234,4 +238,22 @@ class ByteBufSpecification extends Specification {
234238
where:
235239
provider << [new NettyBufferProvider(), new SimpleBufferProvider()]
236240
}
241+
242+
static final class NettyBufferProvider implements BufferProvider {
243+
private final ByteBufAllocator allocator
244+
245+
NettyBufferProvider() {
246+
allocator = PooledByteBufAllocator.DEFAULT
247+
}
248+
249+
@Override
250+
ByteBuf getBuffer(final int size) {
251+
io.netty.buffer.ByteBuf buffer = allocator.directBuffer(size, size)
252+
try {
253+
new NettyByteBuf(buffer.retain())
254+
} finally {
255+
buffer.release();
256+
}
257+
}
258+
}
237259
}

0 commit comments

Comments
 (0)