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

com.mongodb.reactivestreams.client.MongoCollectionImpl Maven / Gradle / Ivy

There is a newer version: 5.3.0
Show newest version
/*
 * Copyright 2014-2015 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;

import com.mongodb.Block;
import com.mongodb.MongoNamespace;
import com.mongodb.ReadConcern;
import com.mongodb.ReadPreference;
import com.mongodb.WriteConcern;
import com.mongodb.async.SingleResultCallback;
import com.mongodb.bulk.BulkWriteResult;
import com.mongodb.client.model.BulkWriteOptions;
import com.mongodb.client.model.CountOptions;
import com.mongodb.client.model.FindOneAndDeleteOptions;
import com.mongodb.client.model.FindOneAndReplaceOptions;
import com.mongodb.client.model.FindOneAndUpdateOptions;
import com.mongodb.client.model.IndexModel;
import com.mongodb.client.model.IndexOptions;
import com.mongodb.client.model.InsertManyOptions;
import com.mongodb.client.model.InsertOneOptions;
import com.mongodb.client.model.RenameCollectionOptions;
import com.mongodb.client.model.UpdateOptions;
import com.mongodb.client.model.WriteModel;
import com.mongodb.client.result.DeleteResult;
import com.mongodb.client.result.UpdateResult;
import org.bson.BsonDocument;
import org.bson.Document;
import org.bson.codecs.configuration.CodecRegistry;
import org.bson.conversions.Bson;
import org.reactivestreams.Publisher;

import java.util.List;

import static com.mongodb.assertions.Assertions.notNull;
import static com.mongodb.async.client.Observables.observe;
import static com.mongodb.async.client.Observables.observeAndFlatten;
import static com.mongodb.reactivestreams.client.PublisherHelper.voidToSuccessCallback;

class MongoCollectionImpl implements MongoCollection {

    private final com.mongodb.async.client.MongoCollection wrapped;

    MongoCollectionImpl(final com.mongodb.async.client.MongoCollection wrapped) {
        this.wrapped = notNull("wrapped", wrapped);
    }

    @Override
    public MongoNamespace getNamespace() {
        return wrapped.getNamespace();
    }

    @Override
    public Class getDocumentClass() {
        return wrapped.getDocumentClass();
    }

    @Override
    public CodecRegistry getCodecRegistry() {
        return wrapped.getCodecRegistry();
    }

    @Override
    public ReadPreference getReadPreference() {
        return wrapped.getReadPreference();
    }

    @Override
    public WriteConcern getWriteConcern() {
        return wrapped.getWriteConcern();
    }

    @Override
    public ReadConcern getReadConcern() {
        return wrapped.getReadConcern();
    }

    @Override
    public  MongoCollection withDocumentClass(final Class clazz) {
        return new MongoCollectionImpl(wrapped.withDocumentClass(clazz));
    }

    @Override
    public MongoCollection withCodecRegistry(final CodecRegistry codecRegistry) {
        return new MongoCollectionImpl(wrapped.withCodecRegistry(codecRegistry));
    }

    @Override
    public MongoCollection withReadPreference(final ReadPreference readPreference) {
        return new MongoCollectionImpl(wrapped.withReadPreference(readPreference));
    }

    @Override
    public MongoCollection withWriteConcern(final WriteConcern writeConcern) {
        return new MongoCollectionImpl(wrapped.withWriteConcern(writeConcern));
    }

    @Override
    public MongoCollection withReadConcern(final ReadConcern readConcern) {
        return new MongoCollectionImpl(wrapped.withReadConcern(readConcern));
    }

    @Override
    public Publisher count() {
        return count(new BsonDocument(), new CountOptions());
    }

    @Override
    public Publisher count(final Bson filter) {
        return count(filter, new CountOptions());
    }

    @Override
    public Publisher count(final Bson filter, final CountOptions options) {
        return new ObservableToPublisher(observe(new Block>() {
            @Override
            public void apply(final SingleResultCallback callback) {
                wrapped.count(filter, options, callback);
            }
        }));
    }

    @Override
    public  DistinctPublisher distinct(final String fieldName, final Class resultClass) {
        return distinct(fieldName, new BsonDocument(), resultClass);
    }

    @Override
    public  DistinctPublisher distinct(final String fieldName, final Bson filter, final Class resultClass) {
        return new DistinctPublisherImpl(wrapped.distinct(fieldName, resultClass)).filter(filter);
    }

    @Override
    public FindPublisher find() {
        return find(new BsonDocument(), getDocumentClass());
    }

    @Override
    public  FindPublisher find(final Class clazz) {
        return find(new BsonDocument(), clazz);
    }

    @Override
    public FindPublisher find(final Bson filter) {
        return find(filter, getDocumentClass());
    }

    @Override
    public  FindPublisher find(final Bson filter, final Class clazz) {
        return new FindPublisherImpl(wrapped.find(filter, clazz));
    }

    @Override
    public AggregatePublisher aggregate(final List pipeline) {
        return aggregate(pipeline, Document.class);
    }

    @Override
    public  AggregatePublisher aggregate(final List pipeline, final Class clazz) {
        return new AggregatePublisherImpl(wrapped.aggregate(pipeline, clazz));
    }

    @Override
    public MapReducePublisher mapReduce(final String mapFunction, final String reduceFunction) {
        return mapReduce(mapFunction, reduceFunction, Document.class);
    }

    @Override
    public  MapReducePublisher mapReduce(final String mapFunction, final String reduceFunction,
                                                           final Class clazz) {
        return new MapReducePublisherImpl(wrapped.mapReduce(mapFunction, reduceFunction, clazz));
    }

    @Override
    public Publisher bulkWrite(final List> requests) {
        return bulkWrite(requests, new BulkWriteOptions());
    }

    @Override
    public Publisher bulkWrite(final List> requests,
                                                final BulkWriteOptions options) {
        return new ObservableToPublisher(observe(new Block>(){
            @Override
            public void apply(final SingleResultCallback callback) {
                wrapped.bulkWrite(requests, options, callback);
            }
        }));
    }

    @Override
    public Publisher insertOne(final TDocument document) {
        return insertOne(document, new InsertOneOptions());
    }

    @Override
    public Publisher insertOne(final TDocument document, final InsertOneOptions options) {
        return new ObservableToPublisher(observe(new Block>() {
            @Override
            public void apply(final SingleResultCallback callback) {
                wrapped.insertOne(document, options, voidToSuccessCallback(callback));
            }
        }));
    }

    @Override
    public Publisher insertMany(final List documents) {
        return insertMany(documents, new InsertManyOptions());
    }

    @Override
    public Publisher insertMany(final List documents, final InsertManyOptions options) {
        return new ObservableToPublisher(observe(new Block>() {
            @Override
            public void apply(final SingleResultCallback callback) {
                wrapped.insertMany(documents, options, voidToSuccessCallback(callback));
            }
        }));
    }

    @Override
    public Publisher deleteOne(final Bson filter) {
        return new ObservableToPublisher(observe(new Block>() {
            @Override
            public void apply(final SingleResultCallback callback) {
                wrapped.deleteOne(filter, callback);
            }
        }));
    }

    @Override
    public Publisher deleteMany(final Bson filter) {
        return new ObservableToPublisher(observe(new Block>() {
            @Override
            public void apply(final SingleResultCallback callback) {
                wrapped.deleteMany(filter, callback);
            }
        }));
    }

    @Override
    public Publisher replaceOne(final Bson filter, final TDocument replacement) {
        return replaceOne(filter, replacement, new UpdateOptions());
    }

    @Override
    public Publisher replaceOne(final Bson filter, final TDocument replacement, final UpdateOptions options) {
        return new ObservableToPublisher(observe(new Block>() {
            @Override
            public void apply(final SingleResultCallback callback) {
                wrapped.replaceOne(filter, replacement, options, callback);
            }
        }));
    }

    @Override
    public Publisher updateOne(final Bson filter, final Bson update) {
        return updateOne(filter, update, new UpdateOptions());
    }

    @Override
    public Publisher updateOne(final Bson filter, final Bson update, final UpdateOptions options) {
        return new ObservableToPublisher(observe(new Block>() {
            @Override
            public void apply(final SingleResultCallback callback) {
                wrapped.updateOne(filter, update, options, callback);
            }
        }));
    }

    @Override
    public Publisher updateMany(final Bson filter, final Bson update) {
        return updateMany(filter, update, new UpdateOptions());
    }

    @Override
    public Publisher updateMany(final Bson filter, final Bson update, final UpdateOptions options) {
        return new ObservableToPublisher(observe(new Block>() {
            @Override
            public void apply(final SingleResultCallback callback) {
                wrapped.updateMany(filter, update, options, callback);
            }
        }));
    }

    @Override
    public Publisher findOneAndDelete(final Bson filter) {
        return findOneAndDelete(filter, new FindOneAndDeleteOptions());
    }

    @Override
    public Publisher findOneAndDelete(final Bson filter, final FindOneAndDeleteOptions options) {
        return new ObservableToPublisher(observe(new Block>() {
            @Override
            public void apply(final SingleResultCallback callback) {
                wrapped.findOneAndDelete(filter, options, callback);
            }
        }));
    }

    @Override
    public Publisher findOneAndReplace(final Bson filter, final TDocument replacement) {
        return findOneAndReplace(filter, replacement, new FindOneAndReplaceOptions());
    }

    @Override
    public Publisher findOneAndReplace(final Bson filter, final TDocument replacement, final FindOneAndReplaceOptions options) {
        return new ObservableToPublisher(observe(new Block>() {
            @Override
            public void apply(final SingleResultCallback callback) {
                wrapped.findOneAndReplace(filter, replacement, options, callback);
            }
        }));
    }

    @Override
    public Publisher findOneAndUpdate(final Bson filter, final Bson update) {
        return findOneAndUpdate(filter, update, new FindOneAndUpdateOptions());
    }

    @Override
    public Publisher findOneAndUpdate(final Bson filter, final Bson update, final FindOneAndUpdateOptions options) {
        return new ObservableToPublisher(observe(new Block>() {
            @Override
            public void apply(final SingleResultCallback callback) {
                wrapped.findOneAndUpdate(filter, update, options, callback);
            }
        }));
    }

    @Override
    public Publisher drop() {
        return new ObservableToPublisher(observe(new Block>() {
            @Override
            public void apply(final SingleResultCallback callback) {
                wrapped.drop(voidToSuccessCallback(callback));
            }
        }));
    }

    @Override
    public Publisher createIndex(final Bson key) {
        return createIndex(key, new IndexOptions());
    }

    @Override
    public Publisher createIndex(final Bson key, final IndexOptions options) {
        return new ObservableToPublisher(observe(new Block>() {
            @Override
            public void apply(final SingleResultCallback callback) {
                wrapped.createIndex(key, options, callback);
            }
        }));
    }

    @Override
    public Publisher createIndexes(final List indexes) {
        return new ObservableToPublisher(observeAndFlatten(new Block>>() {
            @Override
            public void apply(final SingleResultCallback> callback) {
                wrapped.createIndexes(indexes, callback);
            }
        }));
    }

    @Override
    public ListIndexesPublisher listIndexes() {
        return listIndexes(Document.class);
    }

    @Override
    public  ListIndexesPublisher listIndexes(final Class clazz) {
        return new ListIndexesPublisherImpl(wrapped.listIndexes(clazz));
    }

    @Override
    public Publisher dropIndex(final String indexName) {
        return new ObservableToPublisher(observe(new Block>() {
            @Override
            public void apply(final SingleResultCallback callback) {
                wrapped.dropIndex(indexName, voidToSuccessCallback(callback));
            }
        }));
    }

    @Override
    public Publisher dropIndex(final Bson keys) {
        return new ObservableToPublisher(observe(new Block>() {
            @Override
            public void apply(final SingleResultCallback callback) {
                wrapped.dropIndex(keys, voidToSuccessCallback(callback));
            }
        }));
    }

    @Override
    public Publisher dropIndexes() {
        return dropIndex("*");
    }

    @Override
    public Publisher renameCollection(final MongoNamespace newCollectionNamespace) {
        return renameCollection(newCollectionNamespace, new RenameCollectionOptions());
    }

    @Override
    public Publisher renameCollection(final MongoNamespace newCollectionNamespace, final RenameCollectionOptions options) {
        return new ObservableToPublisher(observe(new Block>() {
            @Override
            public void apply(final SingleResultCallback callback) {
                wrapped.renameCollection(newCollectionNamespace, options, voidToSuccessCallback(callback));
            }
        }));
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy