Mongo -实现自增id,数据完整性校验

摘要

Mongo 的free schema,提供了灵活的数据结构,和快速开发的能力,但是也造成了松散的数据组织形式。比如说有些字段不允许为null的,需要符合一定格式的。也就是数据库的校验,validator。这个功能在mongo 3.2才提供,之前是并没有的。这里提供一种基于MongoEvent的解决方案,来实现对于数据的校验。

mongo event

mongo 类图

  1. Mongo 提供了Event的类型MongoMappingEvent 类,然后其他的具体事件类型都继承这个类,比兔BeforeConvertEvent,BeforeConvertEvent,BeforeSaveEvent.

  2. AbstractMongoEventListener 抽象类事件方法,
    onBeforeConvertEvent(),onBeforeConvertEvent()方法,在ApplicationEvent类中提供了基于方法参数MongoMappingEvent去调用不同的事件方法

提供了Mongo Event的类型,以及处理方法

public void onApplicationEvent(MongoMappingEvent<?> event) {
            // 根据事件类型,来执行不同的方法
    if (event instanceof AfterLoadEvent) {
        AfterLoadEvent<?> afterLoadEvent = (AfterLoadEvent<?>) event;

        if (domainClass.isAssignableFrom(afterLoadEvent.getType())) {
            onAfterLoad((AfterLoadEvent<E>) event);
        }

        return;
    }

    if (event instanceof AbstractDeleteEvent) {

        Class<?> eventDomainType = ((AbstractDeleteEvent) event).getType();

        if (eventDomainType != null && domainClass.isAssignableFrom(eventDomainType)) {
            if (event instanceof BeforeDeleteEvent) {
                onBeforeDelete((BeforeDeleteEvent<E>) event);
            }
            if (event instanceof AfterDeleteEvent) {
                onAfterDelete((AfterDeleteEvent<E>) event);
            }
        }

        return;

    }

    Object source = event.getSource();

    // Check for matching domain type and invoke callbacks
    if (source != null && !domainClass.isAssignableFrom(source.getClass())) {
        return;
    }

    if (event instanceof BeforeConvertEvent) {
        onBeforeConvert((BeforeConvertEvent<E>) event);
    } else if (event instanceof BeforeSaveEvent) {
        onBeforeSave((BeforeSaveEvent<E>) event);
    } else if (event instanceof AfterSaveEvent) {
        onAfterSave((AfterSaveEvent<E>) event);
    } else if (event instanceof AfterConvertEvent) {
        onAfterConvert((AfterConvertEvent<E>) event);
    }
}
  1. 事件的publish 发生在mongoTemplate类中
    之前有介绍过,spring-data-mongo中所有的数据库操作都在mongoTemplate中,典型的insert操作
protected <T> void doInsert(String collectionName, T objectToSave, MongoWriter<T> writer) {

        initializeVersionProperty(objectToSave);
        // 调用onBeforeConvert
        maybeEmitEvent(new BeforeConvertEvent<T>(objectToSave, collectionName));
        assertUpdateableIdIfNotSet(objectToSave);

        DBObject dbDoc = toDbObject(objectToSave, writer);
        // 调用onBeforeSaveEvent
        maybeEmitEvent(new BeforeSaveEvent<T>(objectToSave, dbDoc, collectionName));
        Object id = insertDBObject(collectionName, dbDoc, objectToSave.getClass());

        populateIdIfNecessary(objectToSave, id);
        // 调用onAfterSaveEvent
        maybeEmitEvent(new AfterSaveEvent<T>(objectToSave, dbDoc, collectionName));
    }

实现mongo自增id

提供一个@MongoAutoId的注解,然后onBeforeConvert事件中进行转换。

package com.fs.mongo.dao;

import com.fs.mongo.annotation.MongoAutoId;
import com.fs.mongo.model.MongoId;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.FindAndModifyOptions;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.mapping.event.AbstractMongoEventListener;
import org.springframework.data.mongodb.core.mapping.event.BeforeConvertEvent;
import org.springframework.data.mongodb.core.mapping.event.BeforeSaveEvent;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Repository;
import org.springframework.util.ReflectionUtils;

/**
 * @author cnstonefang@gmail.com
 */
@Repository
public class MongoListener extends AbstractMongoEventListener<Object> {
    private static final Logger LOG = LoggerFactory.getLogger(MongoListener.class);

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public void onBeforeConvert(final BeforeConvertEvent<Object> event) {
        final Object source = event.getSource();
        if (source != null) {
            ReflectionUtils.doWithFields(source.getClass(), new ReflectionUtils.FieldCallback() {
                @Override
                public void doWith(final java.lang.reflect.Field field)
                        throws IllegalArgumentException, IllegalAccessException {
                    ReflectionUtils.makeAccessible(field);
                    if (field.isAnnotationPresent(MongoAutoId.class) && field.get(source) == null) {
                        field.set(source, getId(event.getCollectionName()));
                    }
                }
            });
        }
    }
    /**
     * 获取自增id
     * 这边是利用mongo的findAndModify的原子性实现的
     * 也可以使用redis来实现
     */
    private Long getId(final String collName) {
        final Query query = new Query().addCriteria(
                new Criteria(MongoId.FIELD_COLLNAME).is(collName));
        final Update update = new Update();
        update.inc(MongoId.FIELD_SEQID, 1);
        final FindAndModifyOptions options = new FindAndModifyOptions().upsert(true).returnNew
                (true);
        final MongoId sequence = mongoTemplate.findAndModify(query, update, options,
                MongoId.class);
        return sequence.getSeqId();
    }
}

完整代码

这里写图片描述

©️2020 CSDN 皮肤主题: Age of Ai 设计师: meimeiellie 返回首页
实付0元
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值