/** * A {@code CodecRegistry} that combines the given {@code CodecRegistry} instances into a single registry. * * <p>The registries are checked in order until one returns a {@code Codec} for the requested {@code Class}.</p> * * <p>The created instance can handle cycles of {@code Codec} dependencies, i.e when the construction of a {@code Codec} for class A * requires the construction of a {@code Codec} for class B, and vice versa.</p> * <p>Any of the given registries that also implement {@code CodecProvider} will be treated as a {@code CodecProvider} instead of a * {@code CodecRegistry}, which will ensure proper resolution of any dependencies between registries.</p> * * @param registries the preferred registry for {@code Codec} lookups * * @return a {@code CodecRegistry} that combines the list of {@code CodecRegistry} instances into a single one */ public static CodecRegistry fromRegistries(final CodecRegistry... registries) { return fromRegistries(asList(registries)); }
PojoCodecImpl(final ClassModel<T> classModel, final CodecRegistry registry, final PropertyCodecRegistry propertyCodecRegistry, final DiscriminatorLookup discriminatorLookup, final ConcurrentMap<ClassModel<?>, Codec<?>> codecCache, final boolean specialized) { this.classModel = classModel; this.registry = fromRegistries(fromCodecs(this), registry); this.discriminatorLookup = discriminatorLookup; this.codecCache = codecCache; this.propertyCodecRegistry = propertyCodecRegistry; this.specialized = specialized; specialize(); }
private static MongoCollection<GridFSFile> getFilesCollection(final MongoDatabase database, final String bucketName) { return database.getCollection(bucketName + ".files", GridFSFile.class).withCodecRegistry( fromRegistries(database.getCodecRegistry(), MongoClientSettings.getDefaultCodecRegistry()) ); }
PojoCodecImpl(final ClassModel<T> classModel, final CodecRegistry codecRegistry, final List<PropertyCodecProvider> propertyCodecProviders, final DiscriminatorLookup discriminatorLookup) { this.classModel = classModel; this.registry = fromRegistries(fromCodecs(this), codecRegistry); this.discriminatorLookup = discriminatorLookup; this.codecCache = new ConcurrentHashMap<ClassModel<?>, Codec<?>>(); this.propertyCodecRegistry = new PropertyCodecRegistryImpl(this, registry, propertyCodecProviders); this.specialized = shouldSpecialize(classModel); specialize(); }
CodecRegistry codecRegistry = CodecRegistries.fromRegistries(GsonCodecs.codecRegistryFromGson(newGson));
private DatastoreImpl(final Morphia morphia, final Mapper mapper, final MongoClient mongoClient, final MongoDatabase database) { this.morphia = morphia; this.mapper = mapper; this.mongoClient = mongoClient; this.database = database.withCodecRegistry(CodecRegistries.fromRegistries( mongoClient.getMongoClientOptions().getCodecRegistry(), MongoClientSettings.getDefaultCodecRegistry())); this.db = mongoClient.getDB(database.getName()); this.defConcern = mongoClient.getWriteConcern(); this.indexHelper = new IndexHelper(mapper, database); }
ChangeStreamDocumentCodec(final Class<TResult> fullDocumentClass, final CodecRegistry codecRegistry) { ClassModelBuilder<ChangeStreamDocument> classModelBuilder = ClassModel.builder(ChangeStreamDocument.class); ((PropertyModelBuilder<TResult>) classModelBuilder.getProperty("fullDocument")).codec(codecRegistry.get(fullDocumentClass)); ((PropertyModelBuilder<OperationType>) classModelBuilder.getProperty("operationType")).codec(OPERATION_TYPE_CODEC); ClassModel<ChangeStreamDocument> changeStreamDocumentClassModel = classModelBuilder.build(); PojoCodecProvider provider = PojoCodecProvider.builder() .register(MongoNamespace.class) .register(UpdateDescription.class) .register(changeStreamDocumentClassModel) .build(); CodecRegistry registry = fromRegistries(fromProviders(provider, new BsonValueCodecProvider()), codecRegistry); this.codec = (Codec<ChangeStreamDocument<TResult>>) (Codec<? extends ChangeStreamDocument>) registry.get(ChangeStreamDocument.class); }
@Test public void basic() { LocalCodec codecAndStrategy = new LocalCodec(); CodecRegistry registry = CodecRegistries.fromRegistries(codecAndStrategy, MongoClient.getDefaultCodecRegistry()); RepositorySetup.FieldNamingStrategy strategy = new RepositorySetup.FieldNamingStrategy() { @Override public String translateName(Member member) { return "date".equals(member.getName()) ? "dateChanged" : member.getName(); } }; RepositorySetup setup = RepositorySetup.builder() .executor(MoreExecutors.newDirectExecutorService()) .database(context.database()) .codecRegistry(registry, strategy) .build(); SomebodyRepository repository = new SomebodyRepository(setup); ImmutableSomebody somebody = ImmutableSomebody.builder().id(new ObjectId()).date(new Date()).prop1("prop1").build(); repository.insert(somebody) .getUnchecked(); check(repository.findById(somebody.id()).fetchAll().getUnchecked()).hasAll(somebody); check(repository.find(repository.criteria().prop1(somebody.prop1())).fetchAll().getUnchecked()).hasAll(somebody); check(repository.find(repository.criteria().prop1("unknown")).fetchAll().getUnchecked()).isEmpty(); check(repository.find(repository.criteria().date(somebody.date())).fetchAll().getUnchecked()).hasAll(somebody); check(repository.find(repository.criteria().date(new Date(somebody.date().getTime() + 1))) .fetchAll().getUnchecked()).isEmpty(); }
/** * Merges the provided codec registry with the default codec registry. * * @param codecRegistry the codec registry to merge with the default registry. * @return the builder. */ public Builder withCodecRegistry(final CodecRegistry codecRegistry) { // We can't detect if their codecRegistry has any duplicate providers. There's also a chance // that putting ours first may prevent decoding of some of their classes if for example they // have their own way of decoding an Integer. this.codecRegistry = CodecRegistries.fromRegistries(BsonUtils.DEFAULT_CODEC_REGISTRY, codecRegistry); return this; }
/** * Applies the given codec registry to be used alongside the default codec registry. * * @param codecRegistry the codec registry to merge in. * @return an {@link StitchObjectMapper} with the merged codec registries. */ public StitchObjectMapper withCodecRegistry(final CodecRegistry codecRegistry) { // We can't detect if their codecRegistry has any duplicate providers. There's also a chance // that putting ours first may prevent decoding of some of their classes if for example they // have their own way of decoding an Integer. final CodecRegistry newReg = CodecRegistries.fromRegistries(BsonUtils.DEFAULT_CODEC_REGISTRY, codecRegistry); return new StitchObjectMapper(this, newReg); }
@Bean public CodecRegistry registry(Codec[] codecs) { return CodecRegistries.fromRegistries(MongoClient.getDefaultCodecRegistry(), CodecRegistries.fromCodecs(codecs)); }
/** * Configure the {@link MongoDatabase}, registering the additional codecs if configured. * @param database The database to configure * @return The configured database */ protected MongoDatabase checkAdditionalCodecs(MongoDatabase database) { return getAdditionalCodecRegistry() .map(r -> database.withCodecRegistry(CodecRegistries.fromRegistries(database.getCodecRegistry(), r))) .orElse(database); }
/** * Configure the {@link MongoDatabase}, registering the additional codecs if configured. * @param database The database to configure * @return The configured database */ protected MongoDatabase checkAdditionalCodecs(MongoDatabase database) { return getAdditionalCodecRegistry() .map(r -> database.withCodecRegistry(CodecRegistries.fromRegistries(database.getCodecRegistry(), r))) .orElse(database); }
private static MongoCollection<GridFSFile> getFilesCollection(final MongoDatabase database, final String bucketName) { return database.getCollection(bucketName + ".files", GridFSFile.class).withCodecRegistry( fromRegistries(database.getCodecRegistry(), MongoClientSettings.getDefaultCodecRegistry()) ); }
private static MongoCollection<GridFSFile> getFilesCollection(final MongoDatabase database, final String bucketName) { return database.getCollection(bucketName + ".files", GridFSFile.class).withCodecRegistry( fromRegistries(database.getCodecRegistry(), MongoClientSettings.getDefaultCodecRegistry()) ); }
protected I configure() { I datastore = getDatastore(); // codecs List<CodecRegistry> registries = new LinkedList<>(); if (!codecs.isEmpty()) { registries.add(CodecRegistries.fromCodecs(codecs)); } if (!codecProviders.isEmpty()) { registries.add(CodecRegistries.fromProviders(codecProviders)); } if (!registries.isEmpty()) { datastore.setAdditionalCodecRegistry(CodecRegistries.fromRegistries(registries)); } // init datastore.initialize(); return datastore; }
public <T> void addCodecForClass(Class<T> clazz) { JacksonEncoder<T> encoder = new JacksonEncoder<>(clazz, view, objectMapper); JacksonDecoder<T> decoder = new JacksonDecoder<>(clazz, view, objectMapper); JacksonCodec<T> jacksonCodec = new JacksonCodec<T>(encoder, decoder); codecRegistry = CodecRegistries.fromRegistries(codecRegistry, CodecRegistries.fromCodecs(jacksonCodec)); } }
/** * Retrieves the database with the specified {@code name} on the {@code client}. * The database will be created if it does not already exist. * * @param client the MongoDB client, connected on the server * @param name the name of the database * * @return the database */ @Nonnull private MongoDatabase createDatabase(MongoClient client, String name) { final CodecRegistry registry = fromRegistries( client.getMongoClientOptions().getCodecRegistry(), fromProviders(PojoCodecProvider.builder().automatic(true).build())); return client.getDatabase(name) .withCodecRegistry(registry) .withWriteConcern(WriteConcern.MAJORITY) .withReadConcern(ReadConcern.MAJORITY); } }
@Bean CodecRegistry codecRegistry() { PojoCodecProvider pojoCodecProvider = PojoCodecProvider .builder() .conventions(ImmutableList.of(CLASS_AND_PROPERTY_CONVENTION)) .register(Context.class) .register(Job.class) .register(TaskExecution.class) .register(Error.class) .register(Accessor.class) .build(); return fromRegistries( fromProviders( new PiperCodecProvider(), pojoCodecProvider, new EnumPropertyCodecProvider()), getDefaultCodecRegistry(), fromCodecs(new StringArrayCodec()) ); }
ChangeStreamDocumentCodec(final Class<TResult> fullDocumentClass, final CodecRegistry codecRegistry) { ClassModelBuilder<ChangeStreamDocument> classModelBuilder = ClassModel.builder(ChangeStreamDocument.class); ((PropertyModelBuilder<TResult>) classModelBuilder.getProperty("fullDocument")).codec(codecRegistry.get(fullDocumentClass)); ((PropertyModelBuilder<OperationType>) classModelBuilder.getProperty("operationType")).codec(OPERATION_TYPE_CODEC); ClassModel<ChangeStreamDocument> changeStreamDocumentClassModel = classModelBuilder.build(); PojoCodecProvider provider = PojoCodecProvider.builder() .register(MongoNamespace.class) .register(UpdateDescription.class) .register(changeStreamDocumentClassModel) .build(); CodecRegistry registry = fromRegistries(fromProviders(provider, new BsonValueCodecProvider()), codecRegistry); this.codec = (Codec<ChangeStreamDocument<TResult>>) (Codec<? extends ChangeStreamDocument>) registry.get(ChangeStreamDocument.class); }