protected Repository( RepositorySetup configuration, String collectionName, Class<T> type) { this.configuration = checkNotNull(configuration, "configuration"); checkNotNull(collectionName, "collectionName"); checkNotNull(type, "type"); MongoCollection<T> collection = configuration.database.getCollection(collectionName, type); this.collection = collection.withCodecRegistry(configuration.codecRegistry); this.fieldNamingStrategy = configuration.fieldNamingStrategy; }
private static MongoCollection<Document> getChunksCollection(final MongoDatabase database, final String bucketName) { return database.getCollection(bucketName + ".chunks").withCodecRegistry(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()) ); }
@Before @SuppressWarnings("unchecked") public void setUp() throws Exception { MongoDatabase db = mock(MongoDatabase.class); MongoCollection<Entity> collection = mock(MongoCollection.class); when(db.getCollection(anyString(), any(Class.class))).thenReturn(collection); when(collection.withCodecRegistry(any(CodecRegistry.class))).thenReturn(collection); RepositorySetup setup = RepositorySetup.builder().database(db) .executor(MoreExecutors.newDirectExecutorService()) .gson(new GsonBuilder().registerTypeAdapterFactory(new GsonAdaptersEntity()).create()) .build(); this.repository = new EntityRepository(setup); FindIterable<Entity> iterable = mock(FindIterable.class); when(collection.find(any(Bson.class))).thenReturn(iterable); MongoCursor<Entity> cursor = mock(MongoCursor.class); when(iterable.iterator()).thenReturn(cursor); this.cursor = cursor; }
protected Repository( RepositorySetup configuration, String collectionName, Class<T> type) { this.configuration = checkNotNull(configuration, "configuration"); checkNotNull(collectionName, "collectionName"); checkNotNull(type, "type"); MongoCollection<T> collection = configuration.database.getCollection(collectionName, type); this.collection = collection.withCodecRegistry(configuration.codecRegistry); this.fieldNamingStrategy = configuration.fieldNamingStrategy; }
private static MongoCollection<Document> getChunksCollection(final MongoDatabase database, final String bucketName) { return database.getCollection(bucketName + ".chunks").withCodecRegistry(MongoClientSettings.getDefaultCodecRegistry()); }
private static MongoCollection<Document> getChunksCollection(final MongoDatabase database, final String bucketName) { return database.getCollection(bucketName + ".chunks").withCodecRegistry(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()) ); }
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 MongoCollection<MongoBlob> initBlobCollection(MongoDatabase db) { if (stream(db.listCollectionNames().spliterator(), false) .noneMatch(COLLECTION_BLOBS::equals)) { db.createCollection(COLLECTION_BLOBS); } // override the read preference configured with the MongoDB URI // and use the primary as default. Reading a blob will still // try a secondary first and then fallback to the primary. return db.getCollection(COLLECTION_BLOBS, MongoBlob.class) .withCodecRegistry(CODEC_REGISTRY) .withReadPreference(primary()); }
private MongoCollection<MongoBlob> initBlobCollection(MongoDatabase db) { if (stream(db.listCollectionNames().spliterator(), false) .noneMatch(COLLECTION_BLOBS::equals)) { db.createCollection(COLLECTION_BLOBS); } // override the read preference configured with the MongoDB URI // and use the primary as default. Reading a blob will still // try a secondary first and then fallback to the primary. return db.getCollection(COLLECTION_BLOBS, MongoBlob.class) .withCodecRegistry(CODEC_REGISTRY) .withReadPreference(primary()); }
@NotNull @Override public MongoCollection<TDocument> withCodecRegistry(@NotNull CodecRegistry codecRegistry) { return new MongoTestCollection<>(collection.withCodecRegistry(codecRegistry), beforeQueryException, beforeUpdateException, afterUpdateException); }
private JacksonMongoCollection( com.mongodb.client.MongoCollection<?> mongoCollection, ObjectMapper objectMapper, Class<T> valueClass, Class<?> view) { this.objectMapper = objectMapper == null ? DEFAULT_OBJECT_MAPPER : objectMapper; this.view = view; JacksonCodecRegistry jacksonCodecRegistry = new JacksonCodecRegistry(this.objectMapper, this.view); jacksonCodecRegistry.addCodecForClass(valueClass); this.mongoCollection = mongoCollection.withDocumentClass(valueClass).withCodecRegistry(jacksonCodecRegistry);; this.valueClass = valueClass; this.type = this.objectMapper.constructType(valueClass); }