Newer
Older
newfiber-termite / newfiber-termites-business / newfiber-termites-biz / src / main / java / com / newfiber / termite / schedule / MongoIndexSchedule.java
package com.newfiber.termite.schedule;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.mongodb.BasicDBObject;
import java.util.List;
import java.util.Set;
import javax.annotation.Resource;
import com.mongodb.client.model.IndexOptions;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.index.IndexInfo;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class MongoIndexSchedule {

    @Resource
    private MongoTemplate mongoTemplate;

    /** 每天执行一次创建索引 */
    @Scheduled(cron = "30 3 6 * * ? ")
    public void createIndex() {

        log.info("MongoIndexSchedule.createIndex");
        long startLong = System.currentTimeMillis();
        try{

            //1 查询mongodb库中所有的名 集合
            long startTime = System.currentTimeMillis();
            Set<String> setList = mongoTemplate.getCollectionNames();
            if(CollectionUtils.isEmpty(setList)){
                log.info("该mongodb库中 没有任何集合");
                return;
            }
            log.info("查询mongodb 库中所有的名 集合数size:" + setList.size() + ",耗时:" + (System.currentTimeMillis()-startTime) + "毫秒");

            //新建索引的字段 date_1 升序索引
            String fieldDate = "date";
            for (String strST:setList) {
                //2.查询表中已存在的所有索引
                startTime = System.currentTimeMillis();
                List<IndexInfo> indexInfo = mongoTemplate.indexOps(strST).getIndexInfo();
                log.info("本次查询集合中索引数量 表名 ST=" + strST + "|索引数量size:" + indexInfo.size() + "|耗时:" + (System.currentTimeMillis()-startTime) + "毫秒");

                //白蚁设备历史数据 创建date_1 升序索引 s.get("name").equals("date_1")
                long countTt = indexInfo.stream().filter(s->s.getName().equals("date_1")).count();
                if (countTt <= 0) {
                    startTime = System.currentTimeMillis();
                    BasicDBObject indexDate = new BasicDBObject();
                    //创建 date 升序索引
                    indexDate.put(fieldDate,1);
                    //是否在后台创建索引。如果为 true,MongoDB 将在后台创建索引,这样就不会阻塞其他数据库操作
                    IndexOptions indexOptions = new IndexOptions();
                    indexOptions.background(true);
                    mongoTemplate.getDb().getCollection(strST).createIndex(indexDate,indexOptions);
                    log.info("本次创建date升序索引 表名 ST="+strST+ "|耗时" + (System.currentTimeMillis()-startTime) + "毫秒");

                    //mongoTemplate.getDb().getCollection(strST).dropIndex("tt_-1");//删除索引
                    //mongoTemplate.getDb().getCollection(strST).dropIndex(indexTT);//删除索引
                    //mongoTemplate.getDb().getCollection(strST).createIndex(new Document(fieldTt,-1));
                    //mongoTemplate.getDb().getCollection(strST).createIndex(new Document(fieldTt,-1),indexOptions);
                }
            }
            log.info("白蚁设备历史数据createIndex 完成|执行创建索引耗时= "+((double)(System.currentTimeMillis()-startLong)/1000) +"秒");
        }catch (Exception e){
            log.error("定时任务创建索引异常 异常信息:{}",e);
        }
    }

}