All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.mongodb.reactivestreams.client.internal.BatchCursorPublisher Maven / Gradle / Ivy

There is a newer version: 5.3.0-beta0
Show newest version
/*
 * Copyright 2008-present MongoDB, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.mongodb.reactivestreams.client.internal;

import com.mongodb.MongoNamespace;
import com.mongodb.ReadPreference;
import com.mongodb.internal.VisibleForTesting;
import com.mongodb.internal.async.AsyncBatchCursor;
import com.mongodb.internal.operation.AsyncOperations;
import com.mongodb.internal.operation.AsyncReadOperation;
import com.mongodb.lang.Nullable;
import com.mongodb.reactivestreams.client.ClientSession;
import org.bson.codecs.configuration.CodecRegistry;
import org.reactivestreams.Publisher;
import org.reactivestreams.Subscriber;
import reactor.core.publisher.Mono;

import java.util.function.Supplier;

import static com.mongodb.assertions.Assertions.assertNotNull;
import static com.mongodb.assertions.Assertions.notNull;

@VisibleForTesting(otherwise = VisibleForTesting.AccessModifier.PROTECTED)
public abstract class BatchCursorPublisher implements Publisher {
    private final ClientSession clientSession;
    private final MongoOperationPublisher mongoOperationPublisher;
    private Integer batchSize;

    BatchCursorPublisher(@Nullable final ClientSession clientSession, final MongoOperationPublisher mongoOperationPublisher) {
        this(clientSession, mongoOperationPublisher, null);
    }

    BatchCursorPublisher(@Nullable final ClientSession clientSession, final MongoOperationPublisher mongoOperationPublisher,
                         @Nullable final Integer batchSize) {
        this.clientSession = clientSession;
        this.mongoOperationPublisher = notNull("mongoOperationPublisher", mongoOperationPublisher);
        this.batchSize = batchSize;
    }

    abstract AsyncReadOperation> asAsyncReadOperation(int initialBatchSize);

    AsyncReadOperation> asAsyncFirstReadOperation() {
        return asAsyncReadOperation(1);
    }

    @Nullable
    ClientSession getClientSession() {
        return clientSession;
    }

    MongoOperationPublisher getMongoOperationPublisher() {
        return mongoOperationPublisher;
    }

    AsyncOperations getOperations() {
        return mongoOperationPublisher.getOperations();
    }

    MongoNamespace getNamespace() {
        return assertNotNull(mongoOperationPublisher.getNamespace());
    }

    ReadPreference getReadPreference() {
        return mongoOperationPublisher.getReadPreference();
    }

    CodecRegistry getCodecRegistry() {
        return mongoOperationPublisher.getCodecRegistry();
    }

    boolean getRetryReads() {
        return mongoOperationPublisher.getRetryReads();
    }

    Class getDocumentClass() {
        return mongoOperationPublisher.getDocumentClass();
    }


    @Nullable
    public Integer getBatchSize() {
        return batchSize;
    }

    public Publisher batchSize(final int batchSize) {
        this.batchSize = batchSize;
        return this;
    }

    public Publisher first() {
        return batchCursor(this::asAsyncFirstReadOperation)
                .flatMap(batchCursor -> {
                    batchCursor.setBatchSize(1);
                    return Mono.from(batchCursor.next())
                            .doOnTerminate(batchCursor::close)
                            .flatMap(results -> {
                                if (results == null || results.isEmpty()) {
                                    return Mono.empty();
                                }
                                return Mono.fromCallable(() -> results.get(0));
                            });
                });
    }

    @Override
    public void subscribe(final Subscriber subscriber) {
        new BatchCursorFlux<>(this).subscribe(subscriber);
    }

    public Mono> batchCursor(final int initialBatchSize) {
        return batchCursor(() -> asAsyncReadOperation(initialBatchSize));
    }

    Mono> batchCursor(final Supplier>> supplier) {
        return mongoOperationPublisher.createReadOperationMono(supplier, clientSession).map(BatchCursor::new);
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy