/** * Creates new {@link ExpressionEvaluatingParameterBinder} * * @param expressionParser must not be {@literal null}. * @param evaluationContextProvider must not be {@literal null}. */ public ExpressionEvaluatingParameterBinder(SpelExpressionParser expressionParser, QueryMethodEvaluationContextProvider evaluationContextProvider) { Assert.notNull(expressionParser, "ExpressionParser must not be null!"); Assert.notNull(evaluationContextProvider, "EvaluationContextProvider must not be null!"); this.expressionParser = expressionParser; this.evaluationContextProvider = evaluationContextProvider; this.codecRegistryProvider = () -> MongoClient.getDefaultCodecRegistry(); }
static DBObjectCodec getCommandCodec() { return new DBObjectCodec(getDefaultCodecRegistry()); } }
DBObjectCodec getDefaultDBObjectCodec() { return new DBObjectCodec(MongoClient.getDefaultCodecRegistry(), DBObjectCodec.getDefaultBsonTypeClassMap(), getObjectFactory()); }
/** * Create the record makers using the supplied components. * * @param filters the filter configuration; may not be null * @param source the connector's source information; may not be null * @param topicSelector the selector for topic names; may not be null * @param recorder the potentially blocking consumer function to be called for each generated record; may not be null */ public RecordMakers(Filters filters, SourceInfo source, TopicSelector<CollectionId> topicSelector, BlockingConsumer<SourceRecord> recorder, boolean emitTombstonesOnDelete) { this.filters = filters; this.source = source; this.topicSelector = topicSelector; JsonWriterSettings writerSettings = new JsonWriterSettings(JsonMode.STRICT, "", ""); // most compact JSON Encoder<Document> encoder = MongoClient.getDefaultCodecRegistry().get(Document.class); this.valueTransformer = (doc) -> doc.toJson(writerSettings, encoder); this.recorder = recorder; this.emitTombstonesOnDelete = emitTombstonesOnDelete; }
/** * Convert criteria to mongo query (for testing). Currently using reflection (since exposed * only in tests). * * @return Query (as {@link BsonDocument}) which will be sent to mongo. */ public static BsonDocument extractQuery(Repositories.Criteria criteria) { Preconditions.checkNotNull(criteria, "criteria"); try { final Field field = criteria.getClass().getDeclaredField("constraint"); field.setAccessible(true); Constraints.Constraint constraint = (Constraints.Constraint) field.get(criteria); return Support.convertToBson(constraint) .toBsonDocument(Document.class, MongoClient.getDefaultCodecRegistry()); } catch (NoSuchFieldException e) { throw new RuntimeException("private field 'constraint' not found in " + criteria, e); } catch (IllegalAccessException e) { throw new RuntimeException(e); } }
public static DBObject toDBObject(final BsonDocument document) { return MongoClient.getDefaultCodecRegistry().get(DBObject.class).decode(new BsonDocumentReader(document), DecoderContext.builder().build()); }
MongoClient.getDefaultCodecRegistry() );
@Before public void setUp() throws Exception { MongoDatabase database = new Fongo("myname").getDatabase("foo"); this.collection = database.getCollection("jackson").withDocumentClass(BsonDocument.class); SimpleModule module = new SimpleModule(); // for our local serializers of Date and ObjectId module.addDeserializer(Date.class, new DateDeserializer()); module.addSerializer(new DateSerializer()); module.addDeserializer(ObjectId.class, new ObjectIdDeserializer()); module.addSerializer(new ObjectIdSerializer()); module.addDeserializer(UUID.class, new UUIDDeserializer(UuidRepresentation.JAVA_LEGACY)); ObjectMapper mapper = new ObjectMapper() // to support bson types like: Document, BsonValue etc. .registerModule(JacksonCodecs.module(MongoClient.getDefaultCodecRegistry())) .registerModule(new GuavaModule()) .registerModule(module); RepositorySetup setup = RepositorySetup.builder() .database(database) .codecRegistry(JacksonCodecs.registryFromMapper(mapper)) .executor(MoreExecutors.newDirectExecutorService()) .build(); this.repository = new JacksonRepository(setup); }
BulkWriteResult executeBulkWriteOperation(final boolean ordered, final Boolean bypassDocumentValidation, final List<WriteRequest> writeRequests, final WriteConcern writeConcern) { try { return translateBulkWriteResult(executor.execute(new MixedBulkWriteOperation(getNamespace(), translateWriteRequestsToNew(writeRequests), ordered, writeConcern, false) .bypassDocumentValidation(bypassDocumentValidation), getReadConcern()), getObjectCodec()); } catch (MongoBulkWriteException e) { throw BulkWriteHelper.translateBulkWriteException(e, MongoClient.getDefaultCodecRegistry().get(DBObject.class)); } }
/** * Creates new {@link ExpressionEvaluatingParameterBinder} * * @param expressionParser must not be {@literal null}. * @param evaluationContextProvider must not be {@literal null}. */ public ExpressionEvaluatingParameterBinder(SpelExpressionParser expressionParser, QueryMethodEvaluationContextProvider evaluationContextProvider) { Assert.notNull(expressionParser, "ExpressionParser must not be null!"); Assert.notNull(evaluationContextProvider, "EvaluationContextProvider must not be null!"); this.expressionParser = expressionParser; this.evaluationContextProvider = evaluationContextProvider; this.codecRegistryProvider = () -> MongoClient.getDefaultCodecRegistry(); }
private MongoClient createMongoClient(ServerConfiguration configuration) { List<String> serverUrls = configuration.getServerUrls(); if (serverUrls.isEmpty()) { throw new ConfigurationException("server host is not set"); } List<ServerAddress> serverAddresses = new LinkedList<>(); if (SshTunnelingConfiguration.isEmpty(configuration.getSshTunnelingConfiguration())) { for (String serverUrl : serverUrls) { ServerConfiguration.HostAndPort hostAndPort = ServerConfiguration.extractHostAndPort(serverUrl); serverAddresses.add(new ServerAddress(hostAndPort.host, hostAndPort.port)); } } else { serverAddresses.add(new ServerAddress(DEFAULT_TUNNEL_LOCAL_HOST, DEFAULT_TUNNEL_LOCAL_PORT)); } MongoClientOptions options = MongoClientOptions.builder() .sslEnabled(configuration.isSslConnection()) .readPreference(configuration.getReadPreference()) .codecRegistry(MongoClient.getDefaultCodecRegistry()) .build(); if (StringUtils.isEmpty(configuration.getUsername())) { return new MongoClient(serverAddresses, options); } else { MongoCredential credential = getMongoCredential(configuration); return new MongoClient(serverAddresses, Collections.singletonList(credential), options); } }
@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(); }
@Bean @ConditionalOnMissingBean(CodecRegistry.class) public CodecRegistry codecRegistry() { return MongoClient.getDefaultCodecRegistry(); }
@Bean @ConditionalOnMissingBean(CodecRegistry.class) public CodecRegistry codecRegistry() { return MongoClient.getDefaultCodecRegistry(); }
public static BsonDocument toBsonDocument(Map<String, Object> map) { Document d = new Document(map); return d.toBsonDocument(BsonDocument.class, MongoClient.getDefaultCodecRegistry()); }
DBObjectCodec getDefaultDBObjectCodec() { return new DBObjectCodec(MongoClient.getDefaultCodecRegistry(), DBObjectCodec.getDefaultBsonTypeClassMap(), getObjectFactory()); }
@Override public QueryResult nativeGet(Query query, QueryOptions options) { Bson bson = parseQuery(query); logger.debug("query: {}", bson.toBsonDocument(Document.class, MongoClient.getDefaultCodecRegistry()) .toJson()); return mongoDBCollection.find(bson, options); }
@Override public QueryResult nativeGet(Query query, QueryOptions options) { Bson bson = parseQuery(query); // options.put(MongoDBCollection.SKIP_COUNT, true); logger.debug("query: {}", bson.toBsonDocument(Document.class, MongoClient.getDefaultCodecRegistry()) .toJson()); return mongoDBCollection.find(bson, options); }
@Override public QueryResult<Variant> get(Query query, QueryOptions options) { Bson bson = parseQuery(query); QueryOptions parsedOptions = parseQueryOptions(options, query); parsedOptions = addPrivateExcludeOptions(parsedOptions, PRIVATE_CLINICAL_FIELDS); logger.debug("query: {}", bson.toBsonDocument(Document.class, MongoClient.getDefaultCodecRegistry()).toJson()); logger.debug("queryOptions: {}", options.toJson()); return mongoDBCollection.find(bson, null, Variant.class, parsedOptions); }
@Override public QueryResult nativeGet(Query query, QueryOptions inputOptions) { Bson bson = parseQuery(query); QueryOptions options = addPrivateExcludeOptions(new QueryOptions(inputOptions)); logger.debug("query: {}", bson.toBsonDocument(Document.class, MongoClient.getDefaultCodecRegistry()) .toJson()); return mongoDBCollection.find(bson, options); }