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

com.landawn.abacus.util.AsyncMongoDBExecutor Maven / Gradle / Ivy

There is a newer version: 1.8.1
Show newest version
/*
 * Copyright (C) 2015 HaiYang Li
 *
 * 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.landawn.abacus.util;

import java.util.Collection;
import java.util.List;
import java.util.concurrent.Callable;

import org.bson.Document;
import org.bson.conversions.Bson;
import org.bson.types.ObjectId;

import com.landawn.abacus.DataSet;
import com.landawn.abacus.annotation.Beta;
import com.landawn.abacus.util.stream.Stream;
import com.mongodb.bulk.BulkWriteResult;
import com.mongodb.client.model.BulkWriteOptions;
import com.mongodb.client.model.CountOptions;
import com.mongodb.client.model.InsertManyOptions;
import com.mongodb.client.model.UpdateOptions;
import com.mongodb.client.model.WriteModel;
import com.mongodb.client.result.DeleteResult;
import com.mongodb.client.result.UpdateResult;

/**
 * Asynchronous MongoDBExecutor.
 * 
 * @since 0.8
 * 
 * @author Haiyang Li
 */
public final class AsyncMongoDBExecutor {
    private final MongoDBExecutor dbExecutor;
    private final AsyncExecutor asyncExecutor;

    AsyncMongoDBExecutor(final MongoDBExecutor dbExecutor, final AsyncExecutor asyncExecutor) {
        this.dbExecutor = dbExecutor;
        this.asyncExecutor = asyncExecutor;
    }

    public MongoDBExecutor sync() {
        return dbExecutor;
    }

    public CompletableFuture exists(final String collectionName, final String objectId) {
        return asyncExecutor.execute(new Callable() {
            @Override
            public Boolean call() throws Exception {
                return dbExecutor.exists(collectionName, objectId);
            }
        });
    }

    public CompletableFuture exists(final String collectionName, final ObjectId objectId) {
        return asyncExecutor.execute(new Callable() {
            @Override
            public Boolean call() throws Exception {
                return dbExecutor.exists(collectionName, objectId);
            }
        });
    }

    public CompletableFuture exists(final String collectionName, final Bson filter) {
        return asyncExecutor.execute(new Callable() {
            @Override
            public Boolean call() throws Exception {
                return dbExecutor.exists(collectionName, filter);
            }
        });
    }

    public CompletableFuture count(final String collectionName) {
        return asyncExecutor.execute(new Callable() {
            @Override
            public Long call() throws Exception {
                return dbExecutor.count(collectionName);
            }
        });
    }

    public CompletableFuture count(final String collectionName, final Bson filter) {
        return asyncExecutor.execute(new Callable() {
            @Override
            public Long call() throws Exception {
                return dbExecutor.count(collectionName, filter);
            }
        });
    }

    public CompletableFuture count(final String collectionName, final Bson filter, final CountOptions options) {
        return asyncExecutor.execute(new Callable() {
            @Override
            public Long call() throws Exception {
                return dbExecutor.count(collectionName, filter, options);
            }
        });
    }

    public CompletableFuture get(final String collectionName, final String objectId) {
        return asyncExecutor.execute(new Callable() {
            @Override
            public Document call() throws Exception {
                return dbExecutor.get(collectionName, objectId);
            }
        });
    }

    public CompletableFuture get(final String collectionName, final ObjectId objectId) {
        return asyncExecutor.execute(new Callable() {
            @Override
            public Document call() throws Exception {
                return dbExecutor.get(collectionName, objectId);
            }
        });
    }

    public  CompletableFuture get(final Class targetClass, final String collectionName, final String objectId) {
        return asyncExecutor.execute(new Callable() {
            @Override
            public T call() throws Exception {
                return dbExecutor.get(targetClass, collectionName, objectId);
            }
        });
    }

    public  CompletableFuture get(final Class targetClass, final String collectionName, final ObjectId objectId) {
        return asyncExecutor.execute(new Callable() {
            @Override
            public T call() throws Exception {
                return dbExecutor.get(targetClass, collectionName, objectId);
            }
        });
    }

    public  CompletableFuture get(final Class targetClass, final String collectionName, final String objectId,
            final Collection selectPropNames) {
        return asyncExecutor.execute(new Callable() {
            @Override
            public T call() throws Exception {
                return dbExecutor.get(targetClass, collectionName, objectId, selectPropNames);
            }
        });
    }

    public  CompletableFuture get(final Class targetClass, final String collectionName, final ObjectId objectId,
            final Collection selectPropNames) {
        return asyncExecutor.execute(new Callable() {
            @Override
            public T call() throws Exception {
                return dbExecutor.get(targetClass, collectionName, objectId, selectPropNames);
            }
        });
    }

    public  CompletableFuture> queryForSingleResult(final Class targetClass, final String collectionName, final String propName,
            final Bson filter) {
        return asyncExecutor.execute(new Callable>() {
            @Override
            public Nullable call() throws Exception {
                return dbExecutor.queryForSingleResult(targetClass, collectionName, propName, filter);
            }
        });
    }

    public CompletableFuture> queryForEntity(final String collectionName, final Bson filter) {
        return asyncExecutor.execute(new Callable>() {
            @Override
            public Optional call() throws Exception {
                return dbExecutor.queryForEntity(collectionName, filter);
            }
        });
    }

    public  CompletableFuture> queryForEntity(final Class targetClass, final String collectionName, final Bson filter) {
        return asyncExecutor.execute(new Callable>() {
            @Override
            public Optional call() throws Exception {
                return dbExecutor.queryForEntity(targetClass, collectionName, filter);
            }
        });
    }

    public  CompletableFuture> queryForEntity(final Class targetClass, final String collectionName, final Collection selectPropNames,
            final Bson filter) {
        return asyncExecutor.execute(new Callable>() {
            @Override
            public Optional call() throws Exception {
                return dbExecutor.queryForEntity(targetClass, collectionName, selectPropNames, filter);
            }
        });
    }

    public  CompletableFuture> queryForEntity(final Class targetClass, final String collectionName, final Collection selectPropNames,
            final Bson filter, final Bson sort) {
        return asyncExecutor.execute(new Callable>() {
            @Override
            public Optional call() throws Exception {
                return dbExecutor.queryForEntity(targetClass, collectionName, selectPropNames, filter, sort);
            }
        });
    }

    public  CompletableFuture> queryForEntity(final Class targetClass, final String collectionName, final Bson filter, final Bson sort,
            final Bson projection) {
        return asyncExecutor.execute(new Callable>() {
            @Override
            public Optional call() throws Exception {
                return dbExecutor.queryForEntity(targetClass, collectionName, filter, sort, projection);
            }
        });
    }

    public CompletableFuture> find(final String collectionName, final Bson filter) {
        return asyncExecutor.execute(new Callable>() {
            @Override
            public List call() throws Exception {
                return dbExecutor.find(collectionName, filter);
            }
        });
    }

    public  CompletableFuture> find(final Class targetClass, final String collectionName, final Bson filter) {
        return asyncExecutor.execute(new Callable>() {
            @Override
            public List call() throws Exception {
                return dbExecutor.find(targetClass, collectionName, filter);
            }
        });
    }

    public  CompletableFuture> find(final Class targetClass, final String collectionName, final Collection selectPropNames,
            final Bson filter) {
        return asyncExecutor.execute(new Callable>() {
            @Override
            public List call() throws Exception {
                return dbExecutor.find(targetClass, collectionName, selectPropNames, filter);
            }
        });
    }

    public  CompletableFuture> find(final Class targetClass, final String collectionName, final Collection selectPropNames,
            final Bson filter, final int offset, final int count) {
        return asyncExecutor.execute(new Callable>() {
            @Override
            public List call() throws Exception {
                return dbExecutor.find(targetClass, collectionName, selectPropNames, filter, offset, count);
            }
        });
    }

    public  CompletableFuture> find(final Class targetClass, final String collectionName, final Collection selectPropNames,
            final Bson filter, final Bson sort) {
        return asyncExecutor.execute(new Callable>() {
            @Override
            public List call() throws Exception {
                return dbExecutor.find(targetClass, collectionName, selectPropNames, filter, sort);
            }
        });
    }

    public  CompletableFuture> find(final Class targetClass, final String collectionName, final Collection selectPropNames,
            final Bson filter, final Bson sort, final int offset, final int count) {
        return asyncExecutor.execute(new Callable>() {
            @Override
            public List call() throws Exception {
                return dbExecutor.find(targetClass, collectionName, selectPropNames, filter, sort, offset, count);
            }
        });
    }

    public  CompletableFuture> find(final Class targetClass, final String collectionName, final Bson filter, final Bson sort,
            final Bson projection) {
        return asyncExecutor.execute(new Callable>() {
            @Override
            public List call() throws Exception {
                return dbExecutor.find(targetClass, collectionName, filter, sort, projection);
            }
        });
    }

    public  CompletableFuture> find(final Class targetClass, final String collectionName, final Bson filter, final Bson sort,
            final Bson projection, final int offset, final int count) {
        return asyncExecutor.execute(new Callable>() {
            @Override
            public List call() throws Exception {
                return dbExecutor.find(targetClass, collectionName, filter, sort, projection, offset, count);
            }
        });
    }

    public CompletableFuture query(final String collectionName, final Bson filter) {
        return asyncExecutor.execute(new Callable() {
            @Override
            public DataSet call() throws Exception {
                return dbExecutor.query(collectionName, filter);
            }
        });
    }

    public  CompletableFuture query(final Class targetClass, final String collectionName, final Bson filter) {
        return asyncExecutor.execute(new Callable() {
            @Override
            public DataSet call() throws Exception {
                return dbExecutor.query(targetClass, collectionName, filter);
            }
        });
    }

    public  CompletableFuture query(final Class targetClass, final String collectionName, final Collection selectPropNames,
            final Bson filter) {
        return asyncExecutor.execute(new Callable() {
            @Override
            public DataSet call() throws Exception {
                return dbExecutor.query(targetClass, collectionName, selectPropNames, filter);
            }
        });
    }

    public  CompletableFuture query(final Class targetClass, final String collectionName, final Collection selectPropNames,
            final Bson filter, final int offset, final int count) {
        return asyncExecutor.execute(new Callable() {
            @Override
            public DataSet call() throws Exception {
                return dbExecutor.query(targetClass, collectionName, selectPropNames, filter, offset, count);
            }
        });
    }

    public  CompletableFuture query(final Class targetClass, final String collectionName, final Collection selectPropNames,
            final Bson filter, final Bson sort) {
        return asyncExecutor.execute(new Callable() {
            @Override
            public DataSet call() throws Exception {
                return dbExecutor.query(targetClass, collectionName, selectPropNames, filter, sort);
            }
        });
    }

    public  CompletableFuture query(final Class targetClass, final String collectionName, final Collection selectPropNames,
            final Bson filter, final Bson sort, final int offset, final int count) {
        return asyncExecutor.execute(new Callable() {
            @Override
            public DataSet call() throws Exception {
                return dbExecutor.query(targetClass, collectionName, selectPropNames, filter, sort, offset, count);
            }
        });
    }

    public  CompletableFuture query(final Class targetClass, final String collectionName, final Bson filter, final Bson sort,
            final Bson projection) {
        return asyncExecutor.execute(new Callable() {
            @Override
            public DataSet call() throws Exception {
                return dbExecutor.query(targetClass, collectionName, filter, sort, projection);
            }
        });
    }

    public  CompletableFuture query(final Class targetClass, final String collectionName, final Bson filter, final Bson sort,
            final Bson projection, final int offset, final int count) {
        return asyncExecutor.execute(new Callable() {
            @Override
            public DataSet call() throws Exception {
                return dbExecutor.query(targetClass, collectionName, filter, sort, projection, offset, count);
            }
        });
    }

    public CompletableFuture> stream(final String collectionName, final Bson filter) {
        return asyncExecutor.execute(new Callable>() {
            @Override
            public Stream call() throws Exception {
                return dbExecutor.stream(collectionName, filter);
            }
        });
    }

    public  CompletableFuture> stream(final Class targetClass, final String collectionName, final Bson filter) {
        return asyncExecutor.execute(new Callable>() {
            @Override
            public Stream call() throws Exception {
                return dbExecutor.stream(targetClass, collectionName, filter);
            }
        });
    }

    public  CompletableFuture> stream(final Class targetClass, final String collectionName, final Collection selectPropNames,
            final Bson filter) {
        return asyncExecutor.execute(new Callable>() {
            @Override
            public Stream call() throws Exception {
                return dbExecutor.stream(targetClass, collectionName, selectPropNames, filter);
            }
        });
    }

    public  CompletableFuture> stream(final Class targetClass, final String collectionName, final Collection selectPropNames,
            final Bson filter, final int offset, final int count) {
        return asyncExecutor.execute(new Callable>() {
            @Override
            public Stream call() throws Exception {
                return dbExecutor.stream(targetClass, collectionName, selectPropNames, filter, offset, count);
            }
        });
    }

    public  CompletableFuture> stream(final Class targetClass, final String collectionName, final Collection selectPropNames,
            final Bson filter, final Bson sort) {
        return asyncExecutor.execute(new Callable>() {
            @Override
            public Stream call() throws Exception {
                return dbExecutor.stream(targetClass, collectionName, selectPropNames, filter, sort);
            }
        });
    }

    public  CompletableFuture> stream(final Class targetClass, final String collectionName, final Collection selectPropNames,
            final Bson filter, final Bson sort, final int offset, final int count) {
        return asyncExecutor.execute(new Callable>() {
            @Override
            public Stream call() throws Exception {
                return dbExecutor.stream(targetClass, collectionName, selectPropNames, filter, sort, offset, count);
            }
        });
    }

    public  CompletableFuture> stream(final Class targetClass, final String collectionName, final Bson filter, final Bson sort,
            final Bson projection) {
        return asyncExecutor.execute(new Callable>() {
            @Override
            public Stream call() throws Exception {
                return dbExecutor.stream(targetClass, collectionName, filter, sort, projection);
            }
        });
    }

    public  CompletableFuture> stream(final Class targetClass, final String collectionName, final Bson filter, final Bson sort,
            final Bson projection, final int offset, final int count) {
        return asyncExecutor.execute(new Callable>() {
            @Override
            public Stream call() throws Exception {
                return dbExecutor.stream(targetClass, collectionName, filter, sort, projection, offset, count);
            }
        });
    }

    public CompletableFuture insert(final String collectionName, final Object obj) {
        return asyncExecutor.execute(new Callable() {
            @Override
            public Void call() throws Exception {
                dbExecutor.insert(collectionName, obj);
                return null;
            }
        });
    }

    public CompletableFuture insert(final String collectionName, final Collection objList) {
        return asyncExecutor.execute(new Callable() {
            @Override
            public Void call() throws Exception {
                dbExecutor.insert(collectionName, objList);
                return null;
            }
        });
    }

    public CompletableFuture insert(final String collectionName, final Collection objList, final InsertManyOptions options) {
        return asyncExecutor.execute(new Callable() {
            @Override
            public Void call() throws Exception {
                dbExecutor.insert(collectionName, objList, options);
                return null;
            }
        });
    }

    public CompletableFuture update(final String collectionName, final String objectId, final Object update) {
        return asyncExecutor.execute(new Callable() {
            @Override
            public UpdateResult call() throws Exception {
                return dbExecutor.update(collectionName, objectId, update);
            }
        });
    }

    public CompletableFuture update(final String collectionName, final ObjectId objectId, final Object update) {
        return asyncExecutor.execute(new Callable() {
            @Override
            public UpdateResult call() throws Exception {
                return dbExecutor.update(collectionName, objectId, update);
            }
        });
    }

    public CompletableFuture updateOne(final String collectionName, final Bson filter, final Object update) {
        return asyncExecutor.execute(new Callable() {
            @Override
            public UpdateResult call() throws Exception {
                return dbExecutor.updateOne(collectionName, filter, update);
            }
        });
    }

    public CompletableFuture updateOne(final String collectionName, final Bson filter, final Object update, final UpdateOptions options) {
        return asyncExecutor.execute(new Callable() {
            @Override
            public UpdateResult call() throws Exception {
                return dbExecutor.updateOne(collectionName, filter, update, options);
            }
        });
    }

    public CompletableFuture updateAll(final String collectionName, final Bson filter, final Object update) {
        return asyncExecutor.execute(new Callable() {
            @Override
            public UpdateResult call() throws Exception {
                return dbExecutor.updateAll(collectionName, filter, update);
            }
        });
    }

    public CompletableFuture updateAll(final String collectionName, final Bson filter, final Object update, final UpdateOptions options) {
        return asyncExecutor.execute(new Callable() {
            @Override
            public UpdateResult call() throws Exception {
                return dbExecutor.updateAll(collectionName, filter, update, options);
            }
        });
    }

    public CompletableFuture replace(final String collectionName, final String objectId, final Object replacement) {
        return asyncExecutor.execute(new Callable() {
            @Override
            public UpdateResult call() throws Exception {
                return dbExecutor.replace(collectionName, objectId, replacement);
            }
        });
    }

    public CompletableFuture replace(final String collectionName, final ObjectId objectId, final Object replacement) {
        return asyncExecutor.execute(new Callable() {
            @Override
            public UpdateResult call() throws Exception {
                return dbExecutor.replace(collectionName, objectId, replacement);
            }
        });
    }

    public CompletableFuture replaceOne(final String collectionName, final Bson filter, final Object replacement) {
        return asyncExecutor.execute(new Callable() {
            @Override
            public UpdateResult call() throws Exception {
                return dbExecutor.replaceOne(collectionName, filter, replacement);
            }
        });
    }

    public CompletableFuture replaceOne(final String collectionName, final Bson filter, final Object replacement, final UpdateOptions options) {
        return asyncExecutor.execute(new Callable() {
            @Override
            public UpdateResult call() throws Exception {
                return dbExecutor.replaceOne(collectionName, filter, replacement, options);
            }
        });
    }

    public CompletableFuture delete(final String collectionName, final String objectId) {
        return asyncExecutor.execute(new Callable() {
            @Override
            public DeleteResult call() throws Exception {
                return dbExecutor.delete(collectionName, objectId);
            }
        });
    }

    public CompletableFuture delete(final String collectionName, final ObjectId objectId) {
        return asyncExecutor.execute(new Callable() {
            @Override
            public DeleteResult call() throws Exception {
                return dbExecutor.delete(collectionName, objectId);
            }
        });
    }

    public CompletableFuture deleteOne(final String collectionName, final Bson filter) {
        return asyncExecutor.execute(new Callable() {
            @Override
            public DeleteResult call() throws Exception {
                return dbExecutor.deleteOne(collectionName, filter);
            }
        });
    }

    public CompletableFuture deleteAll(final String collectionName, final Bson filter) {
        return asyncExecutor.execute(new Callable() {
            @Override
            public DeleteResult call() throws Exception {
                return dbExecutor.deleteAll(collectionName, filter);
            }
        });
    }

    public CompletableFuture bulkInsert(final String collectionName, final Collection entities) {
        return asyncExecutor.execute(new Callable() {
            @Override
            public Integer call() throws Exception {
                return dbExecutor.bulkInsert(collectionName, entities);
            }
        });
    }

    public CompletableFuture bulkInsert(final String collectionName, final Collection entities, final BulkWriteOptions options) {
        return asyncExecutor.execute(new Callable() {
            @Override
            public Integer call() throws Exception {
                return dbExecutor.bulkInsert(collectionName, entities, options);
            }
        });
    }

    public CompletableFuture bulkWrite(final String collectionName, final List> requests) {
        return asyncExecutor.execute(new Callable() {
            @Override
            public BulkWriteResult call() throws Exception {
                return dbExecutor.bulkWrite(collectionName, requests);
            }
        });
    }

    public CompletableFuture bulkWrite(final String collectionName, final List> requests,
            final BulkWriteOptions options) {
        return asyncExecutor.execute(new Callable() {
            @Override
            public BulkWriteResult call() throws Exception {
                return dbExecutor.bulkWrite(collectionName, requests, options);
            }
        });
    }

    public  CompletableFuture> distinct(final Class targetClass, final String collectionName, final String fieldName) {
        return asyncExecutor.execute(new Callable>() {
            @Override
            public Stream call() throws Exception {
                return dbExecutor.distinct(targetClass, collectionName, fieldName);
            }
        });
    }

    public  CompletableFuture> distinct(final Class targetClass, final String collectionName, final String fieldName, final Bson filter) {
        return asyncExecutor.execute(new Callable>() {
            @Override
            public Stream call() throws Exception {
                return dbExecutor.distinct(targetClass, collectionName, fieldName, filter);
            }
        });
    }

    @Beta
    public CompletableFuture> groupBy(final String collectionName, final String fieldName) {
        return asyncExecutor.execute(new Callable>() {
            @Override
            public Stream call() throws Exception {
                return dbExecutor.groupBy(collectionName, fieldName);
            }
        });
    }

    @Beta
    public CompletableFuture> groupBy(final String collectionName, final Collection fieldNames) {
        return asyncExecutor.execute(new Callable>() {
            @Override
            public Stream call() throws Exception {
                return dbExecutor.groupBy(collectionName, fieldNames);
            }
        });
    }

    @Beta
    public CompletableFuture> groupByAndCount(final String collectionName, final String fieldName) {
        return asyncExecutor.execute(new Callable>() {
            @Override
            public Stream call() throws Exception {
                return dbExecutor.groupByAndCount(collectionName, fieldName);
            }
        });
    }

    @Beta
    public CompletableFuture> groupByAndCount(final String collectionName, final Collection fieldNames) {
        return asyncExecutor.execute(new Callable>() {
            @Override
            public Stream call() throws Exception {
                return dbExecutor.groupByAndCount(collectionName, fieldNames);
            }
        });
    }

    public CompletableFuture> aggregate(final String collectionName, final List pipeline) {
        return asyncExecutor.execute(new Callable>() {
            @Override
            public Stream call() throws Exception {
                return dbExecutor.aggregate(collectionName, pipeline);
            }
        });
    }

    public  CompletableFuture> aggregate(final Class targetClass, final String collectionName, final List pipeline) {
        return asyncExecutor.execute(new Callable>() {
            @Override
            public Stream call() throws Exception {
                return dbExecutor.aggregate(targetClass, collectionName, pipeline);
            }
        });
    }

    public CompletableFuture> mapReduce(final String collectionName, final String mapFunction, final String reduceFunction) {
        return asyncExecutor.execute(new Callable>() {
            @Override
            public Stream call() throws Exception {
                return dbExecutor.mapReduce(collectionName, mapFunction, reduceFunction);
            }
        });
    }

    public  CompletableFuture> mapReduce(final Class targetClass, final String collectionName, final String mapFunction,
            final String reduceFunction) {
        return asyncExecutor.execute(new Callable>() {
            @Override
            public Stream call() throws Exception {
                return dbExecutor.mapReduce(targetClass, collectionName, mapFunction, reduceFunction);
            }
        });
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy