Skip to content

Do minor improvements to how we use field validators #1477

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion bson/src/main/org/bson/AbstractBsonWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public abstract class AbstractBsonWriter implements BsonWriter, Closeable {
* @param settings The writer settings.
*/
protected AbstractBsonWriter(final BsonWriterSettings settings) {
this(settings, new NoOpFieldNameValidator());
this(settings, NoOpFieldNameValidator.INSTANCE);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion bson/src/main/org/bson/BsonBinaryWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public BsonBinaryWriter(final BsonOutput bsonOutput) {
*/
public BsonBinaryWriter(final BsonWriterSettings settings, final BsonBinaryWriterSettings binaryWriterSettings,
final BsonOutput bsonOutput) {
this(settings, binaryWriterSettings, bsonOutput, new NoOpFieldNameValidator());
this(settings, binaryWriterSettings, bsonOutput, NoOpFieldNameValidator.INSTANCE);
}

/**
Expand Down
7 changes: 6 additions & 1 deletion bson/src/main/org/bson/NoOpFieldNameValidator.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@

package org.bson;

class NoOpFieldNameValidator implements FieldNameValidator {
final class NoOpFieldNameValidator implements FieldNameValidator {
static final NoOpFieldNameValidator INSTANCE = new NoOpFieldNameValidator();

private NoOpFieldNameValidator() {
}

@Override
public boolean validate(final String fieldName) {
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ private static CommandMessage getCommandMessage(final String database, final Bso
final InternalConnection internalConnection,
final ClusterConnectionMode clusterConnectionMode,
@Nullable final ServerApi serverApi) {
return new CommandMessage(new MongoNamespace(database, COMMAND_COLLECTION_NAME), command, new NoOpFieldNameValidator(), primary(),
return new CommandMessage(new MongoNamespace(database, COMMAND_COLLECTION_NAME), command, NoOpFieldNameValidator.INSTANCE, primary(),
MessageSettings
.builder()
// Note: server version will be 0.0 at this point when called from InternalConnectionInitializer,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ private boolean shouldStreamResponses(final ServerDescription currentServerDescr
private CommandMessage createCommandMessage(final BsonDocument command, final InternalConnection connection,
final ServerDescription currentServerDescription) {
return new CommandMessage(new MongoNamespace("admin", COMMAND_COLLECTION_NAME), command,
new NoOpFieldNameValidator(), primary(),
NoOpFieldNameValidator.INSTANCE, primary(),
MessageSettings.builder()
.maxWireVersion(connection.getDescription().getMaxWireVersion())
.build(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import com.mongodb.internal.connection.Connection;
import com.mongodb.internal.connection.OperationContext;
import com.mongodb.internal.operation.AsyncOperationHelper.AsyncCallableConnectionWithCallback;
import com.mongodb.internal.validator.NoOpFieldNameValidator;
import com.mongodb.lang.Nullable;
import org.bson.BsonDocument;
import org.bson.BsonTimestamp;
Expand All @@ -53,7 +54,6 @@
import static com.mongodb.internal.operation.CommandBatchCursorHelper.FIRST_BATCH;
import static com.mongodb.internal.operation.CommandBatchCursorHelper.MESSAGE_IF_CLOSED_AS_CURSOR;
import static com.mongodb.internal.operation.CommandBatchCursorHelper.NEXT_BATCH;
import static com.mongodb.internal.operation.CommandBatchCursorHelper.NO_OP_FIELD_NAME_VALIDATOR;
import static com.mongodb.internal.operation.CommandBatchCursorHelper.getKillCursorsCommand;
import static com.mongodb.internal.operation.CommandBatchCursorHelper.getMoreCommandDocument;
import static com.mongodb.internal.operation.CommandBatchCursorHelper.logCommandCursorResult;
Expand Down Expand Up @@ -177,7 +177,7 @@ private void getMoreLoop(final AsyncConnection connection, final ServerCursor se
final SingleResultCallback<List<T>> callback) {
connection.commandAsync(namespace.getDatabaseName(),
getMoreCommandDocument(serverCursor.getId(), connection.getDescription(), namespace, batchSize, comment),
NO_OP_FIELD_NAME_VALIDATOR, ReadPreference.primary(),
NoOpFieldNameValidator.INSTANCE, ReadPreference.primary(),
CommandResultDocumentCodec.create(decoder, NEXT_BATCH),
assertNotNull(resourceManager.getConnectionSource()).getOperationContext(),
(commandResult, t) -> {
Expand Down Expand Up @@ -334,7 +334,7 @@ private void killServerCursor(final MongoNamespace namespace, final ServerCursor
timeoutContext.resetToDefaultMaxTime();

localConnection.commandAsync(namespace.getDatabaseName(), getKillCursorsCommand(namespace, localServerCursor),
NO_OP_FIELD_NAME_VALIDATOR, ReadPreference.primary(), new BsonDocumentCodec(),
NoOpFieldNameValidator.INSTANCE, ReadPreference.primary(), new BsonDocumentCodec(),
operationContext, (r, t) -> callback.onResult(null, null));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ static <T> void executeCommandAsync(final AsyncWriteBinding binding,
Assertions.notNull("binding", binding);
SingleResultCallback<T> addingRetryableLabelCallback = addingRetryableLabelCallback(callback,
connection.getDescription().getMaxWireVersion());
connection.commandAsync(database, command, new NoOpFieldNameValidator(), ReadPreference.primary(), new BsonDocumentCodec(),
connection.commandAsync(database, command, NoOpFieldNameValidator.INSTANCE, ReadPreference.primary(), new BsonDocumentCodec(),
binding.getOperationContext(), transformingWriteCallback(transformer, connection, addingRetryableLabelCallback));
}

Expand Down Expand Up @@ -306,7 +306,7 @@ static <D, T> void createReadCommandAndExecuteAsync(
callback.onResult(null, e);
return;
}
connection.commandAsync(database, command, new NoOpFieldNameValidator(), source.getReadPreference(), decoder,
connection.commandAsync(database, command, NoOpFieldNameValidator.INSTANCE, source.getReadPreference(), decoder,
operationContext, transformingReadCallback(transformer, source, connection, callback));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@
import org.bson.codecs.configuration.CodecRegistry;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
Expand All @@ -69,6 +68,7 @@
import static com.mongodb.internal.operation.OperationHelper.LOGGER;
import static com.mongodb.internal.operation.OperationHelper.isRetryableWrite;
import static com.mongodb.internal.operation.WriteConcernHelper.createWriteConcernError;
import static java.util.Collections.singletonMap;
import static org.bson.codecs.configuration.CodecRegistries.fromProviders;

/**
Expand All @@ -77,7 +77,6 @@
public final class BulkWriteBatch {
private static final CodecRegistry REGISTRY = fromProviders(new BsonValueCodecProvider());
private static final Decoder<BsonDocument> DECODER = REGISTRY.get(BsonDocument.class);
private static final FieldNameValidator NO_OP_FIELD_NAME_VALIDATOR = new NoOpFieldNameValidator();

private final MongoNamespace namespace;
private final ConnectionDescription connectionDescription;
Expand Down Expand Up @@ -279,15 +278,15 @@ BulkWriteBatch getNextBatch() {

FieldNameValidator getFieldNameValidator() {
if (batchType == UPDATE || batchType == REPLACE) {
Map<String, FieldNameValidator> rootMap = new HashMap<>();
Map<String, FieldNameValidator> rootMap;
if (batchType == REPLACE) {
rootMap.put("u", new ReplacingDocumentFieldNameValidator());
rootMap = singletonMap("u", ReplacingDocumentFieldNameValidator.INSTANCE);
} else {
rootMap.put("u", new UpdateFieldNameValidator());
rootMap = singletonMap("u", new UpdateFieldNameValidator());
}
return new MappedFieldNameValidator(NO_OP_FIELD_NAME_VALIDATOR, rootMap);
return new MappedFieldNameValidator(NoOpFieldNameValidator.INSTANCE, rootMap);
} else {
return NO_OP_FIELD_NAME_VALIDATOR;
return NoOpFieldNameValidator.INSTANCE;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import com.mongodb.internal.binding.ConnectionSource;
import com.mongodb.internal.connection.Connection;
import com.mongodb.internal.connection.OperationContext;
import com.mongodb.internal.validator.NoOpFieldNameValidator;
import com.mongodb.lang.Nullable;
import org.bson.BsonDocument;
import org.bson.BsonTimestamp;
Expand All @@ -52,7 +53,6 @@
import static com.mongodb.internal.operation.CommandBatchCursorHelper.MESSAGE_IF_CLOSED_AS_CURSOR;
import static com.mongodb.internal.operation.CommandBatchCursorHelper.MESSAGE_IF_CLOSED_AS_ITERATOR;
import static com.mongodb.internal.operation.CommandBatchCursorHelper.NEXT_BATCH;
import static com.mongodb.internal.operation.CommandBatchCursorHelper.NO_OP_FIELD_NAME_VALIDATOR;
import static com.mongodb.internal.operation.CommandBatchCursorHelper.getKillCursorsCommand;
import static com.mongodb.internal.operation.CommandBatchCursorHelper.getMoreCommandDocument;
import static com.mongodb.internal.operation.CommandBatchCursorHelper.logCommandCursorResult;
Expand Down Expand Up @@ -237,7 +237,7 @@ private void getMore() {
assertNotNull(
connection.command(namespace.getDatabaseName(),
getMoreCommandDocument(serverCursor.getId(), connection.getDescription(), namespace, batchSize, comment),
NO_OP_FIELD_NAME_VALIDATOR,
NoOpFieldNameValidator.INSTANCE,
ReadPreference.primary(),
CommandResultDocumentCodec.create(decoder, NEXT_BATCH),
assertNotNull(resourceManager.getConnectionSource()).getOperationContext())));
Expand Down Expand Up @@ -374,7 +374,7 @@ private void killServerCursor(final MongoNamespace namespace, final ServerCursor
timeoutContext.resetToDefaultMaxTime();

localConnection.command(namespace.getDatabaseName(), getKillCursorsCommand(namespace, localServerCursor),
NO_OP_FIELD_NAME_VALIDATOR, ReadPreference.primary(), new BsonDocumentCodec(), operationContext);
NoOpFieldNameValidator.INSTANCE, ReadPreference.primary(), new BsonDocumentCodec(), operationContext);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,13 @@
import com.mongodb.MongoQueryException;
import com.mongodb.ServerCursor;
import com.mongodb.connection.ConnectionDescription;
import com.mongodb.internal.validator.NoOpFieldNameValidator;
import com.mongodb.lang.Nullable;
import org.bson.BsonArray;
import org.bson.BsonDocument;
import org.bson.BsonInt32;
import org.bson.BsonInt64;
import org.bson.BsonString;
import org.bson.BsonValue;
import org.bson.FieldNameValidator;

import static com.mongodb.internal.operation.DocumentHelper.putIfNotNull;
import static com.mongodb.internal.operation.OperationHelper.LOGGER;
Expand All @@ -42,7 +40,6 @@ final class CommandBatchCursorHelper {

static final String FIRST_BATCH = "firstBatch";
static final String NEXT_BATCH = "nextBatch";
static final FieldNameValidator NO_OP_FIELD_NAME_VALIDATOR = new NoOpFieldNameValidator();
static final String MESSAGE_IF_CLOSED_AS_CURSOR = "Cursor has been closed";
static final String MESSAGE_IF_CLOSED_AS_ITERATOR = "Iterator has been closed";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public FindAndDeleteOperation<T> let(@Nullable final BsonDocument variables) {
}

protected FieldNameValidator getFieldNameValidator() {
return new NoOpFieldNameValidator();
return NoOpFieldNameValidator.INSTANCE;
}

protected void specializeCommand(final BsonDocument commandDocument, final ConnectionDescription connectionDescription) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,9 @@
import org.bson.FieldNameValidator;
import org.bson.codecs.Decoder;

import java.util.HashMap;
import java.util.Map;

import static com.mongodb.assertions.Assertions.notNull;
import static com.mongodb.internal.operation.DocumentHelper.putIfTrue;
import static java.util.Collections.singletonMap;

/**
* An operation that atomically finds and replaces a single document.
Expand Down Expand Up @@ -133,9 +131,9 @@ public FindAndReplaceOperation<T> let(@Nullable final BsonDocument variables) {
}

protected FieldNameValidator getFieldNameValidator() {
Map<String, FieldNameValidator> map = new HashMap<>();
map.put("update", new ReplacingDocumentFieldNameValidator());
return new MappedFieldNameValidator(new NoOpFieldNameValidator(), map);
return new MappedFieldNameValidator(
NoOpFieldNameValidator.INSTANCE,
singletonMap("update", ReplacingDocumentFieldNameValidator.INSTANCE));
}

protected void specializeCommand(final BsonDocument commandDocument, final ConnectionDescription connectionDescription) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,12 @@
import org.bson.FieldNameValidator;
import org.bson.codecs.Decoder;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.mongodb.assertions.Assertions.notNull;
import static com.mongodb.internal.operation.DocumentHelper.putIfNotNull;
import static com.mongodb.internal.operation.DocumentHelper.putIfTrue;
import static java.util.Collections.singletonMap;

/**
* An operation that atomically finds and updates a single document.
Expand Down Expand Up @@ -161,9 +160,7 @@ public FindAndUpdateOperation<T> let(@Nullable final BsonDocument variables) {
}

protected FieldNameValidator getFieldNameValidator() {
Map<String, FieldNameValidator> map = new HashMap<>();
map.put("update", new UpdateFieldNameValidator());
return new MappedFieldNameValidator(new NoOpFieldNameValidator(), map);
return new MappedFieldNameValidator(NoOpFieldNameValidator.INSTANCE, singletonMap("update", new UpdateFieldNameValidator()));
}

protected void specializeCommand(final BsonDocument commandDocument, final ConnectionDescription connectionDescription) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@
import org.bson.BsonDocument;
import org.bson.BsonString;
import org.bson.BsonValue;
import org.bson.FieldNameValidator;

import java.util.List;
import java.util.Optional;
Expand Down Expand Up @@ -77,7 +76,6 @@
* <p>This class is not part of the public API and may be removed or changed at any time</p>
*/
public class MixedBulkWriteOperation implements AsyncWriteOperation<BulkWriteResult>, WriteOperation<BulkWriteResult> {
private static final FieldNameValidator NO_OP_FIELD_NAME_VALIDATOR = new NoOpFieldNameValidator();
private final MongoNamespace namespace;
private final List<? extends WriteRequest> writeRequests;
private final boolean ordered;
Expand Down Expand Up @@ -408,14 +406,14 @@ private boolean handleMongoWriteConcernWithResponseExceptionAsync(final RetrySta

@Nullable
private BsonDocument executeCommand(final OperationContext operationContext, final Connection connection, final BulkWriteBatch batch) {
return connection.command(namespace.getDatabaseName(), batch.getCommand(), NO_OP_FIELD_NAME_VALIDATOR, null, batch.getDecoder(),
return connection.command(namespace.getDatabaseName(), batch.getCommand(), NoOpFieldNameValidator.INSTANCE, null, batch.getDecoder(),
operationContext, shouldAcknowledge(batch, operationContext.getSessionContext()),
batch.getPayload(), batch.getFieldNameValidator());
}

private void executeCommandAsync(final OperationContext operationContext, final AsyncConnection connection, final BulkWriteBatch batch,
final SingleResultCallback<BsonDocument> callback) {
connection.commandAsync(namespace.getDatabaseName(), batch.getCommand(), NO_OP_FIELD_NAME_VALIDATOR, null, batch.getDecoder(),
connection.commandAsync(namespace.getDatabaseName(), batch.getCommand(), NoOpFieldNameValidator.INSTANCE, null, batch.getDecoder(),
operationContext, shouldAcknowledge(batch, operationContext.getSessionContext()),
batch.getPayload(), batch.getFieldNameValidator(), callback);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ static <T> T executeCommand(final WriteBinding binding, final String database, f
commandCreator.create(binding.getOperationContext(),
source.getServerDescription(),
connection.getDescription()),
new NoOpFieldNameValidator(), primary(), BSON_DOCUMENT_CODEC, binding.getOperationContext())),
NoOpFieldNameValidator.INSTANCE, primary(), BSON_DOCUMENT_CODEC, binding.getOperationContext())),
connection));
}

Expand All @@ -219,7 +219,7 @@ static <D, T> T executeCommand(final WriteBinding binding, final String database
final Decoder<D> decoder, final CommandWriteTransformer<D, T> transformer) {
return withSourceAndConnection(binding::getWriteConnectionSource, false, (source, connection) ->
transformer.apply(assertNotNull(
connection.command(database, command, new NoOpFieldNameValidator(), primary(), decoder,
connection.command(database, command, NoOpFieldNameValidator.INSTANCE, primary(), decoder,
binding.getOperationContext())), connection));
}

Expand All @@ -228,7 +228,7 @@ static <T> T executeCommand(final WriteBinding binding, final String database, f
final Connection connection, final CommandWriteTransformer<BsonDocument, T> transformer) {
notNull("binding", binding);
return transformer.apply(assertNotNull(
connection.command(database, command, new NoOpFieldNameValidator(), primary(), BSON_DOCUMENT_CODEC,
connection.command(database, command, NoOpFieldNameValidator.INSTANCE, primary(), BSON_DOCUMENT_CODEC,
binding.getOperationContext())),
connection);
}
Expand Down Expand Up @@ -295,7 +295,7 @@ static <D, T> T createReadCommandAndExecute(
BsonDocument command = commandCreator.create(operationContext, source.getServerDescription(),
connection.getDescription());
retryState.attach(AttachmentKeys.commandDescriptionSupplier(), command::getFirstKey, false);
return transformer.apply(assertNotNull(connection.command(database, command, new NoOpFieldNameValidator(),
return transformer.apply(assertNotNull(connection.command(database, command, NoOpFieldNameValidator.INSTANCE,
source.getReadPreference(), decoder, operationContext)), source, connection);
}

Expand Down
Loading