Skip to content

Simplify OverridableUuidRepresentationCodecRegistry #1014

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
Oct 13, 2022
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
14 changes: 1 addition & 13 deletions bson/src/main/org/bson/codecs/configuration/CodecRegistries.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

import org.bson.UuidRepresentation;
import org.bson.codecs.Codec;
import org.bson.internal.OverridableUuidRepresentationCodecRegistry;
import org.bson.internal.ProvidersCodecRegistry;

import java.util.List;
Expand All @@ -41,18 +40,7 @@ public final class CodecRegistries {
* @since 4.5
*/
public static CodecRegistry withUuidRepresentation(final CodecRegistry codecRegistry, final UuidRepresentation uuidRepresentation) {
if (codecRegistry instanceof OverridableUuidRepresentationCodecRegistry) {
OverridableUuidRepresentationCodecRegistry overridableUuidRepresentationCodecRegistry =
(OverridableUuidRepresentationCodecRegistry) codecRegistry;
if (overridableUuidRepresentationCodecRegistry.getUuidRepresentation().equals(uuidRepresentation)) {
return codecRegistry;
} else {
return new OverridableUuidRepresentationCodecRegistry(overridableUuidRepresentationCodecRegistry.getWrapped(),
uuidRepresentation);
}
} else {
return new OverridableUuidRepresentationCodecRegistry(codecRegistry, uuidRepresentation);
}
return fromProviders(new OverridableUuidRepresentationCodecProvider(codecRegistry, uuidRepresentation));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,41 +14,24 @@
* limitations under the License.
*/

package org.bson.internal;
package org.bson.codecs.configuration;

import org.bson.UuidRepresentation;
import org.bson.codecs.Codec;
import org.bson.codecs.OverridableUuidRepresentationCodec;
import org.bson.codecs.configuration.CodecProvider;
import org.bson.codecs.configuration.CodecRegistry;

import static org.bson.assertions.Assertions.notNull;

public class OverridableUuidRepresentationCodecRegistry implements CycleDetectingCodecRegistry {
final class OverridableUuidRepresentationCodecProvider implements CodecProvider {

private final CodecProvider wrapped;
private final CodecCache codecCache = new CodecCache();
private final UuidRepresentation uuidRepresentation;

public OverridableUuidRepresentationCodecRegistry(final CodecProvider wrapped, final UuidRepresentation uuidRepresentation) {
OverridableUuidRepresentationCodecProvider(final CodecProvider wrapped, final UuidRepresentation uuidRepresentation) {
this.uuidRepresentation = notNull("uuidRepresentation", uuidRepresentation);
this.wrapped = notNull("wrapped", wrapped);
}

public UuidRepresentation getUuidRepresentation() {
return uuidRepresentation;
}

public CodecProvider getWrapped() {
return wrapped;
}

@Override
public <T> Codec<T> get(final Class<T> clazz) {
return get(new ChildCodecRegistry<T>(this, clazz));
}


@Override
@SuppressWarnings({"unchecked"})
public <T> Codec<T> get(final Class<T> clazz, final CodecRegistry registry) {
Expand All @@ -59,19 +42,6 @@ public <T> Codec<T> get(final Class<T> clazz, final CodecRegistry registry) {
return codec;
}

@Override
@SuppressWarnings({"unchecked"})
public <T> Codec<T> get(final ChildCodecRegistry<T> context) {
if (!codecCache.containsKey(context.getCodecClass())) {
Codec<T> codec = wrapped.get(context.getCodecClass(), context);
if (codec instanceof OverridableUuidRepresentationCodec) {
codec = ((OverridableUuidRepresentationCodec<T>) codec).withUuidRepresentation(uuidRepresentation);
}
codecCache.put(context.getCodecClass(), codec);
}
return codecCache.getOrThrow(context.getCodecClass());
}

@Override
public boolean equals(final Object o) {
if (this == o) {
Expand All @@ -81,7 +51,7 @@ public boolean equals(final Object o) {
return false;
}

OverridableUuidRepresentationCodecRegistry that = (OverridableUuidRepresentationCodecRegistry) o;
OverridableUuidRepresentationCodecProvider that = (OverridableUuidRepresentationCodecProvider) o;

if (!wrapped.equals(that.wrapped)) {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import org.bson.codecs.IntegerCodec
import org.bson.codecs.LongCodec
import org.bson.codecs.UuidCodec
import org.bson.codecs.ValueCodecProvider
import org.bson.internal.OverridableUuidRepresentationCodecRegistry
import org.bson.internal.ProvidersCodecRegistry
import spock.lang.Specification

Expand Down Expand Up @@ -78,27 +77,18 @@ class CodeRegistriesSpecification extends Specification {
def 'withUuidRepresentation should apply uuid representation'() {
given:
def registry = fromProviders(new ValueCodecProvider());

when:
def registryWithStandard = withUuidRepresentation(registry, STANDARD)

then:
registryWithStandard instanceof OverridableUuidRepresentationCodecRegistry
(registryWithStandard as OverridableUuidRepresentationCodecRegistry).uuidRepresentation == STANDARD
(registryWithStandard as OverridableUuidRepresentationCodecRegistry).wrapped == registry

when:
def registryWithUnspecified = withUuidRepresentation(registryWithStandard, UNSPECIFIED)
def uuidCodec = registry.get(UUID) as UuidCodec

then:
registryWithUnspecified instanceof OverridableUuidRepresentationCodecRegistry
(registryWithUnspecified as OverridableUuidRepresentationCodecRegistry).uuidRepresentation == UNSPECIFIED
(registryWithUnspecified as OverridableUuidRepresentationCodecRegistry).wrapped == registry
uuidCodec.getUuidRepresentation() == UNSPECIFIED

when:
def registryWithUnspecifiedTwo = withUuidRepresentation(registryWithUnspecified, UNSPECIFIED)
uuidCodec = registryWithStandard.get(UUID) as UuidCodec

then:
registryWithUnspecifiedTwo === registryWithUnspecified
uuidCodec.getUuidRepresentation() == STANDARD
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ import com.mongodb.connection.ClusterSettings
import com.mongodb.internal.connection.Cluster
import org.bson.BsonDocument
import org.bson.Document
import org.bson.codecs.UuidCodec
import org.bson.codecs.ValueCodecProvider
import org.bson.codecs.configuration.CodecRegistry
import org.bson.internal.OverridableUuidRepresentationCodecRegistry
import org.bson.json.JsonObject
import spock.lang.Specification

Expand All @@ -37,6 +37,7 @@ import static com.mongodb.ReadPreference.secondary
import static com.mongodb.connection.ClusterConnectionMode.MULTIPLE
import static com.mongodb.connection.ClusterConnectionMode.SINGLE
import static java.util.concurrent.TimeUnit.MILLISECONDS
import static org.bson.UuidRepresentation.C_SHARP_LEGACY
import static org.bson.UuidRepresentation.STANDARD
import static org.bson.codecs.configuration.CodecRegistries.fromProviders
import static spock.util.matcher.HamcrestSupport.expect
Expand Down Expand Up @@ -345,17 +346,14 @@ class MongoClientSpecification extends Specification {
given:
def options = MongoClientOptions.builder()
.codecRegistry(codecRegistry)
.uuidRepresentation(STANDARD)
.uuidRepresentation(C_SHARP_LEGACY)
.build()

when:
def client = new MongoClient('localhost', options)
def registry = client.getCodecRegistry()

then:
registry instanceof OverridableUuidRepresentationCodecRegistry
(registry as OverridableUuidRepresentationCodecRegistry).uuidRepresentation == STANDARD
(registry as OverridableUuidRepresentationCodecRegistry).wrapped == codecRegistry
(client.getCodecRegistry().get(UUID) as UuidCodec).getUuidRepresentation() == C_SHARP_LEGACY

cleanup:
client?.close()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,17 @@ import com.mongodb.internal.client.model.changestream.ChangeStreamLevel
import com.mongodb.internal.connection.Cluster
import org.bson.BsonDocument
import org.bson.Document
import org.bson.codecs.BsonValueCodecProvider
import org.bson.codecs.UuidCodec
import org.bson.codecs.ValueCodecProvider
import org.bson.codecs.configuration.CodecRegistry
import org.bson.internal.OverridableUuidRepresentationCodecRegistry
import spock.lang.Specification

import static com.mongodb.CustomMatchers.isTheSameAs
import static com.mongodb.MongoClientSettings.getDefaultCodecRegistry
import static com.mongodb.ReadPreference.primary
import static com.mongodb.ReadPreference.secondary
import static com.mongodb.client.internal.TestHelper.execute
import static org.bson.UuidRepresentation.STANDARD
import static org.bson.UuidRepresentation.C_SHARP_LEGACY
import static org.bson.UuidRepresentation.UNSPECIFIED
import static org.bson.codecs.configuration.CodecRegistries.fromProviders
import static org.bson.codecs.configuration.CodecRegistries.withUuidRepresentation
Expand Down Expand Up @@ -198,20 +197,17 @@ class MongoClientSpecification extends Specification {

def 'should create registry reflecting UuidRepresentation'() {
given:
def codecRegistry = fromProviders([new BsonValueCodecProvider()])
def codecRegistry = fromProviders([new ValueCodecProvider()])
def settings = MongoClientSettings.builder()
.codecRegistry(codecRegistry)
.uuidRepresentation(STANDARD)
.uuidRepresentation(C_SHARP_LEGACY)
.build()

when:
def client = new MongoClientImpl(Stub(Cluster), null, settings, new TestOperationExecutor([]))
def registry = client.getCodecRegistry()

then:
registry instanceof OverridableUuidRepresentationCodecRegistry
(registry as OverridableUuidRepresentationCodecRegistry).uuidRepresentation == STANDARD
(registry as OverridableUuidRepresentationCodecRegistry).wrapped == codecRegistry
(client.getCodecRegistry().get(UUID) as UuidCodec).getUuidRepresentation() == C_SHARP_LEGACY

cleanup:
client?.close()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,11 @@ import org.bson.Document
import org.bson.codecs.BsonDocumentCodec
import org.bson.codecs.BsonValueCodecProvider
import org.bson.codecs.DocumentCodec
import org.bson.codecs.UuidCodec
import org.bson.codecs.ValueCodecProvider
import org.bson.codecs.configuration.CodecConfigurationException
import org.bson.codecs.configuration.CodecRegistries
import org.bson.conversions.Bson
import org.bson.internal.OverridableUuidRepresentationCodecRegistry
import spock.lang.Specification

import java.util.concurrent.TimeUnit
Expand All @@ -105,8 +105,8 @@ import static com.mongodb.internal.bulk.WriteRequest.Type.INSERT
import static com.mongodb.internal.bulk.WriteRequest.Type.REPLACE
import static com.mongodb.internal.bulk.WriteRequest.Type.UPDATE
import static java.util.concurrent.TimeUnit.MILLISECONDS
import static org.bson.UuidRepresentation.C_SHARP_LEGACY
import static org.bson.UuidRepresentation.JAVA_LEGACY
import static org.bson.UuidRepresentation.STANDARD
import static org.bson.codecs.configuration.CodecRegistries.fromProviders
import static spock.util.matcher.HamcrestSupport.expect

Expand Down Expand Up @@ -150,14 +150,12 @@ class MongoCollectionSpecification extends Specification {

when:
def collection = new MongoCollectionImpl(namespace, Document, codecRegistry, readPreference, ACKNOWLEDGED,
true, true, readConcern, STANDARD, null, executor).withCodecRegistry(newCodecRegistry)
true, true, readConcern, C_SHARP_LEGACY, null, executor).withCodecRegistry(newCodecRegistry)

then:
collection.getCodecRegistry() instanceof OverridableUuidRepresentationCodecRegistry
(collection.getCodecRegistry() as OverridableUuidRepresentationCodecRegistry).uuidRepresentation == STANDARD
(collection.getCodecRegistry() as OverridableUuidRepresentationCodecRegistry).wrapped == newCodecRegistry
(collection.getCodecRegistry().get(UUID) as UuidCodec).getUuidRepresentation() == C_SHARP_LEGACY
expect collection, isTheSameAs(new MongoCollectionImpl(namespace, Document, collection.getCodecRegistry(), readPreference,
ACKNOWLEDGED, true, true, readConcern, STANDARD, null, executor))
ACKNOWLEDGED, true, true, readConcern, C_SHARP_LEGACY, null, executor))
}

def 'should behave correctly when using withReadPreference'() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,17 @@ import org.bson.BsonInt32
import org.bson.Document
import org.bson.codecs.BsonValueCodecProvider
import org.bson.codecs.DocumentCodecProvider
import org.bson.codecs.UuidCodec
import org.bson.codecs.ValueCodecProvider
import org.bson.internal.OverridableUuidRepresentationCodecRegistry
import spock.lang.Specification

import static com.mongodb.CustomMatchers.isTheSameAs
import static com.mongodb.ReadPreference.primary
import static com.mongodb.ReadPreference.primaryPreferred
import static com.mongodb.ReadPreference.secondary
import static com.mongodb.client.internal.TestHelper.execute
import static org.bson.UuidRepresentation.C_SHARP_LEGACY
import static org.bson.UuidRepresentation.JAVA_LEGACY
import static org.bson.UuidRepresentation.STANDARD
import static org.bson.codecs.configuration.CodecRegistries.fromProviders
import static spock.util.matcher.HamcrestSupport.expect

Expand Down Expand Up @@ -100,15 +100,13 @@ class MongoDatabaseSpecification extends Specification {

when:
def database = new MongoDatabaseImpl(name, codecRegistry, readPreference, writeConcern, false, true, readConcern,
STANDARD, null, executor)
C_SHARP_LEGACY, null, executor)
.withCodecRegistry(newCodecRegistry)

then:
database.getCodecRegistry() instanceof OverridableUuidRepresentationCodecRegistry
(database.getCodecRegistry() as OverridableUuidRepresentationCodecRegistry).uuidRepresentation == STANDARD
(database.getCodecRegistry() as OverridableUuidRepresentationCodecRegistry).wrapped == newCodecRegistry
(database.getCodecRegistry().get(UUID) as UuidCodec).getUuidRepresentation() == C_SHARP_LEGACY
expect database, isTheSameAs(new MongoDatabaseImpl(name, database.getCodecRegistry(), readPreference, writeConcern,
false, true, readConcern, STANDARD, null, executor))
false, true, readConcern, C_SHARP_LEGACY, null, executor))
}

def 'should behave correctly when using withReadPreference'() {
Expand Down