-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create
DualMessageSequences
abstraction
This makes `CommandMessage` generic for any command with two sequences. The new abstraction adds: * The sequence identifiers for each sequence. * A field name validator for both sequences. * A `List<BsonElement>`` for any extra elements required by the splitting logic, so that `txnNumber` doesn't have to be treated specially. Make `SplittablePayload` extend `OpMsgSequence`. This brings SplittablePayload closer in design to `DualMessageSequences`, reducing a potential source of confusion for future readers. JAVA-5529 Co-authored-by: Jeff Yemin <[email protected]>
- Loading branch information
Showing
14 changed files
with
264 additions
and
231 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
134 changes: 134 additions & 0 deletions
134
driver-core/src/main/com/mongodb/internal/connection/DualMessageSequences.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
/* | ||
* 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 org.bson.BsonElement; | ||
import org.bson.BsonWriter; | ||
import org.bson.FieldNameValidator; | ||
import org.bson.io.BsonOutput; | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* Two sequences that may either be coupled or independent. | ||
* <p> | ||
* This class is not part of the public API and may be removed or changed at any time.</p> | ||
*/ | ||
public abstract class DualMessageSequences extends MessageSequences { | ||
|
||
private final String firstSequenceId; | ||
private final FieldNameValidator firstFieldNameValidator; | ||
private final String secondSequenceId; | ||
private final FieldNameValidator secondFieldNameValidator; | ||
|
||
protected DualMessageSequences( | ||
final String firstSequenceId, | ||
final FieldNameValidator firstFieldNameValidator, | ||
final String secondSequenceId, | ||
final FieldNameValidator secondFieldNameValidator) { | ||
this.firstSequenceId = firstSequenceId; | ||
this.firstFieldNameValidator = firstFieldNameValidator; | ||
this.secondSequenceId = secondSequenceId; | ||
this.secondFieldNameValidator = secondFieldNameValidator; | ||
} | ||
|
||
FieldNameValidator getFirstFieldNameValidator() { | ||
return firstFieldNameValidator; | ||
} | ||
|
||
FieldNameValidator getSecondFieldNameValidator() { | ||
return secondFieldNameValidator; | ||
} | ||
|
||
String getFirstSequenceId() { | ||
return firstSequenceId; | ||
} | ||
|
||
String getSecondSequenceId() { | ||
return secondSequenceId; | ||
} | ||
|
||
protected abstract EncodeDocumentsResult encodeDocuments(WritersProviderAndLimitsChecker writersProviderAndLimitsChecker); | ||
|
||
/** | ||
* @see #tryWrite(WriteAction) | ||
*/ | ||
public interface WritersProviderAndLimitsChecker { | ||
/** | ||
* Provides writers to the specified {@link WriteAction}, | ||
* {@linkplain WriteAction#doAndGetBatchCount(OrdinaryAndStoredBsonWriters, BsonWriter) executes} it, | ||
* checks the {@linkplain MessageSettings limits}. | ||
* <p> | ||
* May be called multiple times per {@link #encodeDocuments(WritersProviderAndLimitsChecker)}.</p> | ||
*/ | ||
WriteResult tryWrite(WriteAction write); | ||
|
||
/** | ||
* @see #doAndGetBatchCount(OrdinaryAndStoredBsonWriters, BsonWriter) | ||
*/ | ||
interface WriteAction { | ||
/** | ||
* Writes documents to the sequences using the provided writers. | ||
* | ||
* @return The resulting batch count since the beginning of {@link #encodeDocuments(WritersProviderAndLimitsChecker)}. | ||
* It is generally allowed to be greater than {@link MessageSettings#getMaxBatchCount()}. | ||
*/ | ||
// VAKOTODO pass OrdinaryAndStoredBsonWriters for both first and second? | ||
int doAndGetBatchCount(OrdinaryAndStoredBsonWriters firstWriter, BsonWriter secondWriter); | ||
} | ||
|
||
interface OrdinaryAndStoredBsonWriters { | ||
BsonWriter getWriter(); | ||
|
||
/** | ||
* A {@link BsonWriter} to use for writing documents that are intended to be stored in a database. | ||
* Must write to the same {@linkplain BsonOutput output} as {@link #getWriter()} does. | ||
*/ | ||
BsonWriter getStoredDocumentWriter(); | ||
} | ||
|
||
enum WriteResult { | ||
FAIL_LIMIT_EXCEEDED, | ||
OK_LIMIT_REACHED, | ||
OK_LIMIT_NOT_REACHED | ||
} | ||
} | ||
|
||
public static final class EncodeDocumentsResult { | ||
private final boolean serverResponseRequired; | ||
private final List<BsonElement> extraElements; | ||
|
||
/** | ||
* @param extraElements See {@link #getExtraElements()}. | ||
*/ | ||
public EncodeDocumentsResult(final boolean serverResponseRequired, final List<BsonElement> extraElements) { | ||
this.serverResponseRequired = serverResponseRequired; | ||
this.extraElements = extraElements; | ||
} | ||
|
||
boolean isServerResponseRequired() { | ||
return serverResponseRequired; | ||
} | ||
|
||
/** | ||
* {@linkplain BsonElement Key/value pairs} to be added to the document contained in the {@code OP_MSG} section with payload type 0. | ||
*/ | ||
List<BsonElement> getExtraElements() { | ||
return extraElements; | ||
} | ||
} | ||
} |
Oops, something went wrong.