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

com.anwen.mongo.mapper.AbstractBaseMapper Maven / Gradle / Ivy

There is a newer version: 2.1.5
Show newest version
package com.anwen.mongo.mapper;

import com.anwen.mongo.aggregate.Aggregate;
import com.anwen.mongo.cache.global.InterceptorCache;
import com.anwen.mongo.cache.global.TenantCache;
import com.anwen.mongo.conditions.interfaces.condition.CompareCondition;
import com.anwen.mongo.conditions.query.QueryChainWrapper;
import com.anwen.mongo.conditions.query.QueryWrapper;
import com.anwen.mongo.conditions.update.UpdateChainWrapper;
import com.anwen.mongo.constant.SqlOperationConstant;
import com.anwen.mongo.enums.SpecialConditionEnum;
import com.anwen.mongo.execute.Execute;
import com.anwen.mongo.execute.ExecutorFactory;
import com.anwen.mongo.logging.Log;
import com.anwen.mongo.logging.LogFactory;
import com.anwen.mongo.logic.LogicDeleteHandler;
import com.anwen.mongo.manager.MongoPlusClient;
import com.anwen.mongo.mapping.MongoConverter;
import com.anwen.mongo.mapping.TypeReference;
import com.anwen.mongo.model.BaseLambdaQueryResult;
import com.anwen.mongo.model.MutablePair;
import com.anwen.mongo.model.PageParam;
import com.anwen.mongo.model.PageResult;
import com.anwen.mongo.toolkit.Filters;
import com.anwen.mongo.toolkit.*;
import com.mongodb.BasicDBObject;
import com.mongodb.bulk.BulkWriteResult;
import com.mongodb.client.AggregateIterable;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.model.*;
import com.mongodb.client.result.InsertManyResult;
import org.bson.Document;
import org.bson.conversions.Bson;

import java.io.Serializable;
import java.util.*;

import static com.anwen.mongo.handlers.condition.BuildCondition.condition;

/**
 * 抽象的baseMapper
 *
 * @author anwen
 * @date 2024/6/26 下午2:01
 */
public abstract class AbstractBaseMapper implements BaseMapper {

    private final Log log = LogFactory.getLog(AbstractBaseMapper.class);

    private final MongoPlusClient mongoPlusClient;

    private final MongoConverter mongoConverter;

    private final ExecutorFactory factory;

    private final LambdaOperate lambdaOperate = new LambdaOperate();

    public AbstractBaseMapper(MongoPlusClient mongoPlusClient, MongoConverter mongoConverter,ExecutorFactory factory) {
        this.mongoPlusClient = mongoPlusClient;
        this.mongoConverter = mongoConverter;
        this.factory = factory;
    }

    @Override
    public MongoPlusClient getMongoPlusClient() {
        return mongoPlusClient;
    }

    @Override
    public MongoConverter getMongoConverter() {
        return this.mongoConverter;
    }

    @Override
    public Execute getExecute(){
        return factory.getExecute();
    }

    @Override
    public  boolean save(String database, String collectionName, T entity) {
        try {
            Document document = new Document();
            mongoConverter.writeBySave(entity, document);
            InsertManyResult insertManyResult = factory.getExecute().executeSave(Collections.singletonList(document), mongoPlusClient.getCollection(database,collectionName));
            mongoConverter.reSetIdValue(entity, document);
            return insertManyResult.wasAcknowledged();
        } catch (Exception e) {
            log.error("save fail , error info : {}", e.getMessage(), e);
            throw e;
        }
    }

    @Override
    public  Boolean saveBatch(String database, String collectionName, Collection entityList) {
        try {
            Assert.notEmpty(entityList, "entityList can not be empty");
            List documentList = new ArrayList<>(entityList.size());
            mongoConverter.writeBySaveBatch(entityList, documentList);
            MongoCollection collection = mongoPlusClient.getCollection(database,collectionName);
            InsertManyResult insertManyResult = factory.getExecute().executeSave(documentList, collection);
            mongoConverter.batchReSetIdValue(entityList, documentList);
            return insertManyResult.getInsertedIds().size() == entityList.size();
        } catch (Exception e) {
            log.error("saveBatch fail , error info : {}", e.getMessage(), e);
            throw e;
        }
    }

    @Override
    public Long update(String database, String collectionName, Bson queryBasic, Bson updateBasic) {
        return factory.getExecute().executeUpdate(
                Collections.singletonList(new MutablePair<>(queryBasic, updateBasic)),
                mongoPlusClient.getCollection(database,collectionName)
        ).getModifiedCount();
    }

    @Override
    public Integer bulkWrite(String database, String collectionName, List> writeModelList) {
        Assert.notEmpty(writeModelList, "writeModelList can not be empty");
        BulkWriteResult bulkWriteResult = factory.getExecute().executeBulkWrite(writeModelList,mongoPlusClient.getCollection(database,collectionName));
        return bulkWriteResult.getModifiedCount() + bulkWriteResult.getInsertedCount();
    }

    @Override
    public  Boolean update(String database, String collectionName, T entity, QueryChainWrapper queryChainWrapper) {
        MutablePair updatePair = ConditionUtil.getUpdateCondition(queryChainWrapper.getCompareList(), entity,mongoConverter);
        return update(database,collectionName,updatePair.getLeft(),updatePair.getRight()) > 0;
    }

    @Override
    public boolean isExist(String database, String collectionName, Serializable id) {
        QueryWrapper wrapper = Wrappers.lambdaQuery().eq(SqlOperationConstant._ID, id);
        return isExist(database,collectionName,wrapper);
    }

    @Override
    public boolean isExist(String database, String collectionName, QueryChainWrapper queryChainWrapper) {
        BasicDBObject basicDBObject = condition().queryCondition(queryChainWrapper.getCompareList());
        return factory.getExecute().executeCount(basicDBObject,null,mongoPlusClient.getCollection(database,collectionName)) >= 1;
    }

    @Override
    public Boolean update(String database, String collectionName, UpdateChainWrapper updateChainWrapper) {
        MutablePair pair = condition().updateCondition(updateChainWrapper);
        BasicDBObject targetBasicDBObject = new BasicDBObject();
        mongoConverter.write(pair.getRight(),targetBasicDBObject);
        return update(database,collectionName,pair.getLeft(),targetBasicDBObject) >= 1;
    }

    @Override
    public Boolean remove(String database, String collectionName, UpdateChainWrapper updateChainWrapper) {
        return remove(database,collectionName,condition().queryCondition(updateChainWrapper.getCompareList())) >= 1;
    }

    @Override
    public Long remove(String database, String collectionName, Bson filter) {
        return factory.getExecute().executeRemove(filter,mongoPlusClient.getCollection(database, collectionName)).getDeletedCount();
    }

    @Override
    public long count(String database, String collectionName, QueryChainWrapper queryChainWrapper) {
        Execute execute = factory.getExecute();
        MongoCollection collection = mongoPlusClient.getCollection(database, collectionName);
        long line;
        if (canEstimatedDocumentCount(collection, queryChainWrapper)) {
            line = execute.estimatedDocumentCount(collection);
        } else {
            line = execute.executeCount(condition().queryCondition(queryChainWrapper.getCompareList()), null, collection);
        }
        return line;
    }

    /**
     * 判断是否可以使用 mongo  快速估计文档数量
     */
    private boolean canEstimatedDocumentCount(MongoCollection collection, QueryChainWrapper queryChainWrapper) {

        // 忽略逻辑删除 + 条件为空 + 忽略多租户
        return LogicDeleteHandler.close(collection)
                && (Objects.isNull(queryChainWrapper) || CollUtil.isEmpty(queryChainWrapper.getCompareList()))
                && (TenantCache.getIgnoreTenant() != null || InterceptorCache.getTenant() == null);

    }

    @Override
    public long recentPageCount(String database, String collectionName, List compareConditionList, Integer pageNum, Integer pageSize, Integer recentPageNum) {
        if (recentPageNum == null || !(recentPageNum <= 50 && recentPageNum >= 5)) {
            // 返回-1 表示不查询总条数
            return -1L;
        }
        //分页查询  不查询实际总条数  需要单独查询  是否有数据
        //如果recentPageNum = 10  第1-6页  总页数=10  从第7页开始 需要往后 + 4 页
        int limitParam = (pageNum < (recentPageNum / 2 + 1 + recentPageNum % 2) ? recentPageNum : (pageNum + (recentPageNum / 2 + recentPageNum % 2 - 1))) * pageSize;
        CountOptions countOptions = new CountOptions();
        countOptions.skip(limitParam).limit(1);
        MongoCollection collection = mongoPlusClient.getCollection(database, collectionName);
        long isExists = factory.getExecute().executeCount(condition().queryCondition(compareConditionList),countOptions, collection);
        //如果查询结果为空 则查询总条数,如果不为空则 limitParam为总条数
        if (isExists == 0) {
            // 查询真实总条数
            CountOptions countOptionsReal = new CountOptions();
            countOptionsReal.limit(limitParam);
            return factory.getExecute().executeCount(condition().queryCondition(compareConditionList),countOptions, collection);
        }
        return limitParam;
    }

    @Override
    public  List list(String database, String collectionName, Class rClazz) {
        return list(database,collectionName,new TypeReference(rClazz){});
    }

    @Override
    public  List list(String database, String collectionName, TypeReference typeReference) {
        FindIterable findIterable = factory.getExecute().executeQuery(null, null, null, Document.class, mongoPlusClient.getCollection(database, collectionName));
        return mongoConverter.read(findIterable, typeReference);
    }

    @Override
    public  List list(String database, String collectionName, QueryChainWrapper queryChainWrapper, Class rClazz) {
        return list(database,collectionName,queryChainWrapper,new TypeReference(rClazz){});
    }

    @Override
    public  List list(String database, String collectionName, QueryChainWrapper queryChainWrapper, TypeReference typeReference) {
        BaseLambdaQueryResult baseLambdaQuery = lambdaOperate.baseLambdaQuery(queryChainWrapper.getCompareList(),queryChainWrapper.getOrderList(),queryChainWrapper.getProjectionList(),queryChainWrapper.getBasicDBObjectList());
        FindIterable documentFindIterable = factory.getExecute().executeQuery(baseLambdaQuery.getCondition(), baseLambdaQuery.getProjection(), baseLambdaQuery.getSort(), Document.class, mongoPlusClient.getCollection(database, collectionName));
        return mongoConverter.read(documentFindIterable, typeReference);
    }

    @Override
    public  List aggregateList(String database, String collectionName, Aggregate aggregate, Class rClazz) {
        return aggregateList(database,collectionName,aggregate,new TypeReference(rClazz){});
    }

    @Override
    public  List aggregateList(String database, String collectionName, Aggregate aggregate, TypeReference typeReference) {
        List aggregateConditionList = aggregate.getAggregateConditionList();
        AggregateIterable aggregateIterable = factory.getExecute().executeAggregate(aggregateConditionList, Document.class, mongoPlusClient.getCollection(database, collectionName));
        AggregateUtil.aggregateOptions(aggregateIterable,aggregate.getAggregateOptions());
        return mongoConverter.read(aggregateIterable,typeReference);
    }

    @Override
    public  R one(String database, String collectionName, QueryChainWrapper queryChainWrapper, Class rClazz) {
        return one(database,collectionName,queryChainWrapper,new TypeReference(rClazz){});
    }

    @Override
    public  R one(String database, String collectionName, QueryChainWrapper queryChainWrapper, TypeReference typeReference) {
        BaseLambdaQueryResult baseLambdaQuery = lambdaOperate.baseLambdaQuery(queryChainWrapper.getCompareList(),queryChainWrapper.getOrderList(),queryChainWrapper.getProjectionList(),queryChainWrapper.getBasicDBObjectList());
        return mongoConverter.readDocument(factory.getExecute().executeQuery(baseLambdaQuery.getCondition(),baseLambdaQuery.getProjection(),baseLambdaQuery.getSort(), Document.class, mongoPlusClient.getCollection(database, collectionName)).limit(1),typeReference);
    }

    @Override
    public  PageResult page(String database, String collectionName, QueryChainWrapper queryChainWrapper, Integer pageNum, Integer pageSize, Class rClazz) {
        return page(database,collectionName,queryChainWrapper,pageNum,pageSize,new TypeReference(rClazz){});
    }

    @Override
    public  PageResult page(String database, String collectionName, QueryChainWrapper queryChainWrapper, Integer pageNum, Integer pageSize, TypeReference typeReference) {
        BaseLambdaQueryResult baseLambdaQuery = lambdaOperate.baseLambdaQuery(queryChainWrapper.getCompareList(),queryChainWrapper.getOrderList(),queryChainWrapper.getProjectionList(),queryChainWrapper.getBasicDBObjectList());
        MongoCollection collection = mongoPlusClient.getCollection(database, collectionName);
        long count;
        if (canEstimatedDocumentCount(collection, queryChainWrapper)) {
            count = factory.getExecute().estimatedDocumentCount(collection);
        } else {
            count = count(database, collectionName, queryChainWrapper);
        }
        FindIterable iterable = factory.getExecute().executeQuery(baseLambdaQuery.getCondition(), baseLambdaQuery.getProjection(), baseLambdaQuery.getSort(), Document.class, collection);
        return lambdaOperate.getLambdaQueryResultPage(iterable, count, new PageParam(pageNum, pageSize), typeReference, mongoConverter);
    }

    @Override
    public  List pageList(String database, String collectionName, QueryChainWrapper queryChainWrapper, Integer pageNum, Integer pageSize, Class rClazz) {
        return pageList(database,collectionName,queryChainWrapper,pageNum,pageSize,new TypeReference(rClazz){});
    }

    @Override
    public  List pageList(String database, String collectionName, QueryChainWrapper queryChainWrapper, Integer pageNum, Integer pageSize, TypeReference typeReference) {
        BaseLambdaQueryResult baseLambdaQuery = lambdaOperate.baseLambdaQuery(queryChainWrapper.getCompareList(),queryChainWrapper.getOrderList(),queryChainWrapper.getProjectionList(),queryChainWrapper.getBasicDBObjectList());
        FindIterable iterable = factory.getExecute().executeQuery(baseLambdaQuery.getCondition(), baseLambdaQuery.getProjection(), baseLambdaQuery.getSort(), Document.class, mongoPlusClient.getCollection(database, collectionName));
        return mongoConverter.read(iterable.skip((pageNum - 1) * pageSize).limit(pageSize), typeReference);
    }

    @Override
    public  PageResult page(String database, String collectionName, QueryChainWrapper queryChainWrapper, Integer pageNum, Integer pageSize, Integer recentPageNum, Class rClazz) {
        return page(database,collectionName,queryChainWrapper,pageNum,pageSize,recentPageNum,new TypeReference(rClazz){});
    }

    @Override
    public  PageResult page(String database, String collectionName, QueryChainWrapper queryChainWrapper, Integer pageNum, Integer pageSize, Integer recentPageNum, TypeReference typeReference) {
        BaseLambdaQueryResult baseLambdaQuery = lambdaOperate.baseLambdaQuery(queryChainWrapper.getCompareList(),queryChainWrapper.getOrderList(),queryChainWrapper.getProjectionList(),queryChainWrapper.getBasicDBObjectList());
        MongoCollection collection = mongoPlusClient.getCollection(database, collectionName);
        long count;
        if (canEstimatedDocumentCount(collection, queryChainWrapper)) {
            count = factory.getExecute().estimatedDocumentCount(collection);
        } else {
            count = recentPageCount(database, collectionName, queryChainWrapper.getCompareList(), pageNum, pageSize, recentPageNum);
        }
        FindIterable iterable = factory.getExecute().executeQuery(baseLambdaQuery.getCondition(), baseLambdaQuery.getProjection(), baseLambdaQuery.getSort(), Document.class, collection);
        return lambdaOperate.getLambdaQueryResultPage(iterable, count,new PageParam(pageNum,pageSize),typeReference,mongoConverter);
    }

    @Override
    public  List getByIds(String database, String collectionName, Collection ids, Class rClazz) {
        return getByIds(database,collectionName,ids,new TypeReference(rClazz){});
    }

    @Override
    public  List getByIds(String database, String collectionName, Collection ids, TypeReference typeReference) {
        FindIterable iterable = factory.getExecute().executeQuery(BsonUtil.getIdsCondition(ids),null,null, Document.class, mongoPlusClient.getCollection(database, collectionName));
        return mongoConverter.read(iterable, typeReference);
    }

    @Override
    public  R getById(String database, String collectionName, Serializable id, Class rClazz) {
        return getById(database,collectionName,id,new TypeReference(rClazz){});
    }

    @Override
    public  R getById(String database, String collectionName, Serializable id, TypeReference typeReference) {
        BasicDBObject queryBasic = new BasicDBObject(SqlOperationConstant._ID, new BasicDBObject(SpecialConditionEnum.EQ.getCondition(), ObjectIdUtil.getObjectIdValue(id)));
        return mongoConverter.read(factory.getExecute().executeQuery(queryBasic,null,null, Document.class, mongoPlusClient.getCollection(database, collectionName)).first(),typeReference);
    }


    @Override
    public  List queryCommand(String database, String collectionName, String command, Class rClazz) {
        return queryCommand(database,collectionName,command,new TypeReference(rClazz){});
    }

    @Override
    public  List queryCommand(String database, String collectionName, String command, TypeReference typeReference) {
        FindIterable iterable = factory.getExecute().executeQuery(BasicDBObject.parse(command),null,null, Document.class, mongoPlusClient.getCollection(database, collectionName));
        return mongoConverter.read(iterable,typeReference);
    }

    @Override
    public  List getByColumn(String database, String collectionName, String column, Object value, Class rClazz) {
        return getByColumn(database,collectionName,column,value,new TypeReference(rClazz){});
    }

    @Override
    public  List getByColumn(String database, String collectionName, String column, Object value, TypeReference typeReference) {
        Bson filter = Filters.eq(column, ObjectIdUtil.getObjectIdValue(value));
        return mongoConverter.read(factory.getExecute().executeQuery(filter,null,null, Document.class, mongoPlusClient.getCollection(database, collectionName)),typeReference);
    }

    @Override
    public long count(String database, String collectionName) {
        MongoCollection collection = mongoPlusClient.getCollection(database, collectionName);
        Execute execute = factory.getExecute();
        long line;
        if (canEstimatedDocumentCount(collection, null)){
            line = execute.estimatedDocumentCount(collection);
        } else {
            line = execute.executeCount(null,null,collection);
        }
        return line;
    }

    @Override
    public String createIndex(String database, String collectionName, Bson bson) {
        return factory.getExecute().doCreateIndex(bson,mongoPlusClient.getCollection(database, collectionName));
    }

    @Override
    public String createIndex(String database, String collectionName, Bson bson, IndexOptions indexOptions) {
        return factory.getExecute().doCreateIndex(bson,indexOptions,mongoPlusClient.getCollection(database, collectionName));
    }

    @Override
    public List createIndexes(String database, String collectionName, List indexes) {
        return factory.getExecute().doCreateIndexes(indexes,mongoPlusClient.getCollection(database, collectionName));
    }

    @Override
    public List createIndexes(String database, String collectionName, List indexes, CreateIndexOptions createIndexOptions) {
        return factory.getExecute().doCreateIndexes(indexes,createIndexOptions,mongoPlusClient.getCollection(database, collectionName));
    }

    @Override
    public List listIndexes(String database, String collectionName) {
        return factory.getExecute().doListIndexes(mongoPlusClient.getCollection(database, collectionName));
    }

    @Override
    public void dropIndex(String database, String collectionName, String indexName) {
        factory.getExecute().doDropIndex(indexName,mongoPlusClient.getCollection(database, collectionName));
    }

    @Override
    public void dropIndex(String database, String collectionName, String indexName, DropIndexOptions dropIndexOptions) {
        factory.getExecute().doDropIndex(indexName,dropIndexOptions,mongoPlusClient.getCollection(database, collectionName));
    }

    @Override
    public void dropIndex(String database, String collectionName, Bson keys) {
        factory.getExecute().doDropIndex(keys,mongoPlusClient.getCollection(database, collectionName));
    }

    @Override
    public void dropIndex(String database, String collectionName, Bson keys, DropIndexOptions dropIndexOptions) {
        factory.getExecute().doDropIndex(keys,dropIndexOptions,mongoPlusClient.getCollection(database, collectionName));
    }

    @Override
    public void dropIndexes(String database, String collectionName) {
        factory.getExecute().doDropIndexes(mongoPlusClient.getCollection(database, collectionName));
    }

    @Override
    public void dropIndexes(String database, String collectionName, DropIndexOptions dropIndexOptions) {
        factory.getExecute().doDropIndexes(dropIndexOptions,mongoPlusClient.getCollection(database, collectionName));
    }

}