Skip to content

Commit 3c3e677

Browse files
authored
Simplify RequestMessage class hierarchy (#1787)
* Remove EncodingMetadata abstraction and pull the remains into CommandMessage * Pull the writeDocuments method into CommandMessage and simplify its parameters JAVA-5944
1 parent 4f6e2e2 commit 3c3e677

File tree

3 files changed

+24
-48
lines changed

3 files changed

+24
-48
lines changed

driver-core/src/main/com/mongodb/internal/connection/CommandMessage.java

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import org.bson.BsonString;
3636
import org.bson.ByteBuf;
3737
import org.bson.FieldNameValidator;
38+
import org.bson.io.BsonOutput;
3839

3940
import java.io.ByteArrayOutputStream;
4041
import java.io.UnsupportedEncodingException;
@@ -55,6 +56,8 @@
5556
import static com.mongodb.connection.ServerType.STANDALONE;
5657
import static com.mongodb.internal.connection.BsonWriterHelper.appendElementsToDocument;
5758
import static com.mongodb.internal.connection.BsonWriterHelper.backpatchLength;
59+
import static com.mongodb.internal.connection.BsonWriterHelper.createBsonBinaryWriter;
60+
import static com.mongodb.internal.connection.BsonWriterHelper.encodeUsingRegistry;
5861
import static com.mongodb.internal.connection.BsonWriterHelper.writeDocumentsOfDualMessageSequences;
5962
import static com.mongodb.internal.connection.BsonWriterHelper.writePayload;
6063
import static com.mongodb.internal.connection.ByteBufBsonDocument.createList;
@@ -77,16 +80,20 @@ public final class CommandMessage extends RequestMessage {
7780
*/
7881
private static final byte PAYLOAD_TYPE_1_DOCUMENT_SEQUENCE = 1;
7982

83+
private static final int UNINITIALIZED_POSITION = -1;
84+
8085
private final BsonDocument command;
8186
private final FieldNameValidator commandFieldNameValidator;
8287
private final ReadPreference readPreference;
8388
private final boolean exhaustAllowed;
8489
private final MessageSequences sequences;
8590
private final boolean responseExpected;
8691
private final String database;
92+
private int firstDocumentPosition = UNINITIALIZED_POSITION;
93+
8794
/**
8895
* {@code null} iff either {@link #sequences} is not of the {@link DualMessageSequences} type,
89-
* or it is of that type, but it has not been {@linkplain #encodeMessageBodyWithMetadata(ByteBufferBsonOutput, OperationContext) encoded}.
96+
* or it is of that type, but it has not been {@linkplain #encodeMessageBody(ByteBufferBsonOutput, OperationContext) encoded}.
9097
*/
9198
@Nullable
9299
private Boolean dualMessageSequencesRequireResponse;
@@ -145,7 +152,7 @@ BsonDocument getCommandDocument(final ByteBufferBsonOutput bsonOutput) {
145152
try {
146153
CompositeByteBuf byteBuf = new CompositeByteBuf(byteBuffers);
147154
try {
148-
byteBuf.position(getEncodingMetadata().getFirstDocumentPosition());
155+
byteBuf.position(firstDocumentPosition);
149156
ByteBufBsonDocument byteBufBsonDocument = createOne(byteBuf);
150157

151158
// If true, it means there is at least one `PAYLOAD_TYPE_1_DOCUMENT_SEQUENCE` section in the OP_MSG
@@ -223,9 +230,8 @@ boolean isResponseExpected() {
223230
}
224231

225232
@Override
226-
protected EncodingMetadata encodeMessageBodyWithMetadata(final ByteBufferBsonOutput bsonOutput, final OperationContext operationContext) {
227-
int commandStartPosition = useOpMsg() ? writeOpMsg(bsonOutput, operationContext) : writeOpQuery(bsonOutput);
228-
return new EncodingMetadata(commandStartPosition);
233+
protected void encodeMessageBody(final ByteBufferBsonOutput bsonOutput, final OperationContext operationContext) {
234+
this.firstDocumentPosition = useOpMsg() ? writeOpMsg(bsonOutput, operationContext) : writeOpQuery(bsonOutput);
229235
}
230236

231237
@SuppressWarnings("try")
@@ -237,7 +243,7 @@ private int writeOpMsg(final ByteBufferBsonOutput bsonOutput, final OperationCon
237243
int commandStartPosition = bsonOutput.getPosition();
238244
List<BsonElement> extraElements = getExtraElements(operationContext);
239245

240-
int commandDocumentSizeInBytes = writeDocument(command, bsonOutput, commandFieldNameValidator);
246+
int commandDocumentSizeInBytes = writeCommand(bsonOutput);
241247
if (sequences instanceof SplittablePayload) {
242248
appendElementsToDocument(bsonOutput, commandStartPosition, extraElements);
243249
SplittablePayload payload = (SplittablePayload) sequences;
@@ -288,7 +294,7 @@ private int writeOpQuery(final ByteBufferBsonOutput bsonOutput) {
288294
elements = new ArrayList<>(3);
289295
addServerApiElements(elements);
290296
}
291-
writeDocument(command, bsonOutput, commandFieldNameValidator);
297+
writeCommand(bsonOutput);
292298
appendElementsToDocument(bsonOutput, commandStartPosition, elements);
293299
return commandStartPosition;
294300
}
@@ -416,6 +422,13 @@ public String getDatabase() {
416422
return database;
417423
}
418424

425+
private int writeCommand(final BsonOutput bsonOutput) {
426+
BsonBinaryWriter writer = createBsonBinaryWriter(bsonOutput, commandFieldNameValidator, getSettings());
427+
int documentStart = bsonOutput.getPosition();
428+
encodeUsingRegistry(writer, command);
429+
return bsonOutput.getPosition() - documentStart;
430+
}
431+
419432
@FunctionalInterface
420433
private interface FinishOpMsgSectionWithPayloadType1 extends AutoCloseable {
421434
void close();

driver-core/src/main/com/mongodb/internal/connection/CompressedMessage.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class CompressedMessage extends RequestMessage {
3636
}
3737

3838
@Override
39-
protected EncodingMetadata encodeMessageBodyWithMetadata(final ByteBufferBsonOutput bsonOutput, final OperationContext operationContext) {
39+
protected void encodeMessageBody(final ByteBufferBsonOutput bsonOutput, final OperationContext operationContext) {
4040
bsonOutput.writeInt32(wrappedOpcode.getValue());
4141
bsonOutput.writeInt32(getWrappedMessageSize(wrappedMessageBuffers) - MESSAGE_HEADER_LENGTH);
4242
bsonOutput.writeByte(compressor.getId());
@@ -45,8 +45,6 @@ protected EncodingMetadata encodeMessageBodyWithMetadata(final ByteBufferBsonOut
4545
.position(getFirstWrappedMessageBuffer(wrappedMessageBuffers).position() + MESSAGE_HEADER_LENGTH);
4646

4747
compressor.compress(wrappedMessageBuffers, bsonOutput);
48-
49-
return new EncodingMetadata(0);
5048
}
5149

5250
private static int getWrappedMessageSize(final List<ByteBuf> wrappedMessageBuffers) {

driver-core/src/main/com/mongodb/internal/connection/RequestMessage.java

Lines changed: 3 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,12 @@
1616

1717
package com.mongodb.internal.connection;
1818

19-
import org.bson.BsonBinaryWriter;
20-
import org.bson.BsonDocument;
21-
import org.bson.FieldNameValidator;
2219
import org.bson.io.BsonOutput;
2320

2421
import java.util.concurrent.atomic.AtomicInteger;
2522

2623
import static com.mongodb.assertions.Assertions.notNull;
2724
import static com.mongodb.internal.connection.BsonWriterHelper.backpatchLength;
28-
import static com.mongodb.internal.connection.BsonWriterHelper.createBsonBinaryWriter;
29-
import static com.mongodb.internal.connection.BsonWriterHelper.encodeUsingRegistry;
3025

3126
/**
3227
* Abstract base class for all MongoDB Wire Protocol request messages.
@@ -40,19 +35,7 @@ abstract class RequestMessage {
4035
private final MessageSettings settings;
4136
private final int id;
4237
private final OpCode opCode;
43-
private EncodingMetadata encodingMetadata;
4438

45-
static class EncodingMetadata {
46-
private final int firstDocumentPosition;
47-
48-
EncodingMetadata(final int firstDocumentPosition) {
49-
this.firstDocumentPosition = firstDocumentPosition;
50-
}
51-
52-
public int getFirstDocumentPosition() {
53-
return firstDocumentPosition;
54-
}
55-
}
5639
/**
5740
* Gets the next available unique message identifier.
5841
*
@@ -109,18 +92,8 @@ public void encode(final ByteBufferBsonOutput bsonOutput, final OperationContext
10992
notNull("operationContext", operationContext);
11093
int messageStartPosition = bsonOutput.getPosition();
11194
writeMessagePrologue(bsonOutput);
112-
EncodingMetadata encodingMetadata = encodeMessageBodyWithMetadata(bsonOutput, operationContext);
95+
encodeMessageBody(bsonOutput, operationContext);
11396
backpatchLength(messageStartPosition, bsonOutput);
114-
this.encodingMetadata = encodingMetadata;
115-
}
116-
117-
/**
118-
* Gets the encoding metadata from the last attempt to encode this message.
119-
*
120-
* @return Get metadata from the last attempt to encode this message. Returns null if there has not yet been an attempt.
121-
*/
122-
public EncodingMetadata getEncodingMetadata() {
123-
return encodingMetadata;
12497
}
12598

12699
/**
@@ -138,16 +111,8 @@ protected void writeMessagePrologue(final BsonOutput bsonOutput) {
138111
/**
139112
* Encode the message body to the given output.
140113
*
141-
* @param bsonOutput the output
114+
* @param bsonOutput the output
142115
* @param operationContext the session context
143-
* @return the encoding metadata
144116
*/
145-
protected abstract EncodingMetadata encodeMessageBodyWithMetadata(ByteBufferBsonOutput bsonOutput, OperationContext operationContext);
146-
147-
protected int writeDocument(final BsonDocument document, final BsonOutput bsonOutput, final FieldNameValidator validator) {
148-
BsonBinaryWriter writer = createBsonBinaryWriter(bsonOutput, validator, getSettings());
149-
int documentStart = bsonOutput.getPosition();
150-
encodeUsingRegistry(writer, document);
151-
return bsonOutput.getPosition() - documentStart;
152-
}
117+
protected abstract void encodeMessageBody(ByteBufferBsonOutput bsonOutput, OperationContext operationContext);
153118
}

0 commit comments

Comments
 (0)