Titan Framework MongoDB深入理解1

在TitanFrameWork框架中,已经集成了MongoDB的各个功能,现在我们对框架内部的一些重要类进行分析与解读。

  1. MongoDBConverter

在Titan框架中,比较重要的一个接口就是MongoDBConverter,它是作为框架对MongoDB数据Bson的一个转换器,其实现类是一个名字被加密的类(titan框架中加密了部分内部类和实现类代码,反编译是看不到真实名字的,这样无法通过反编译获取其他实现类的源码),实现该接口的功能,下面我们来看看反编译这个接口的源码。

public interface MongoDBConverter {
    <T> T toEntity(Document var1, Class<T> var2);

    <T> Document toDocument(T var1);

    <T> T toEntity(Document var1);

    public static class ConvertException extends RuntimeException {
        private static final long ALLATORIxDEMO = 8355200445366457007L;

        public ConvertException(Throwable a) {
            super(a);
            if ((new Date()).after(new Date(1541088000780L))) {
                throw new Throwable("EXPIRED!");
            }
        }

        public ConvertException(String a) {
            super(a);
            if ((new Date()).after(new Date(1541088000780L))) {
                throw new Throwable("EXPIRED!");
            }
        }

        public ConvertException() {
            if ((new Date()).after(new Date(1541088000780L))) {
                throw new Throwable("EXPIRED!");
            }
        }
    }
}

首先我们来了解三个方法,其中重载了一个toEntity(),<T> T toEntity(Document var1)该方法直接返回null,为一个默认方法,而<T> T toEntity(Document var1, Class<T> var2)是将一个Document(MongoDB中的一条数据)转换为对应的Entity,最后一个方法<T> Document toDocument(T var1)则是将传入的Entity转换为Document。

之后是一个内部运行时异常类ConvertException,用于转换异常时抛出提示。

  1. GeneraMongodb

GeneraMongodb是一个非常重要的框架内的顶层接口,它规定了许多操作方法,如增删改查,获取、设置自定义实现的转换器,统计条目等,我们先看一看反编译后的框架代码。

public interface GeneralMongodb {
    MongoDBConverter getConverter();

    public abstract static class FinderImpl extends GeneralMongodb.Impl {
        protected MongoCollection<Document> getCollection(String a) {
            return a.I.ALLATORIxDEMO.getCollection(a).withReadPreference(ReadPreference.secondaryPreferred());
        }

        protected FinderImpl(MongoDBConnection a) {
            super(a);
            if ((new Date()).after(new Date(1541088000780L))) {
                throw new Throwable("EXPIRED!");
            }
        }
    }

    public abstract static class CurdImpl extends GeneralMongodb.Impl {
        protected CurdImpl(MongoDBConnection a) {
            super(a);
            if ((new Date()).after(new Date(1541088000780L))) {
                throw new Throwable("EXPIRED!");
            }
        }

        protected MongoCollection<Document> getCollection(String a) {
            return a.I.ALLATORIxDEMO.getCollection(a);
        }
    }

    public abstract static class Impl implements GeneralMongodb {
        final MongoDBConnection I;
        static final String D = "Id";
        protected MongoDBConverter converter;
        Function<Object, Document> ALLATORIxDEMO;

        protected <T> List<T> queryByCondition(String a, Bson a, int a, int a, Bson a, Class<T> a) {
            a = (a - 1) * a;
            MongoCursor var7 = null;
            if (a == null) {
                var7 = a._query(a, a, a, a);
            } else {
                var7 = a._query(a, a, a, a, a);
            }

            ArrayList a = new ArrayList();

            while(var7.hasNext()) {
                int a = (Document)var7.next();
                if (a == null) {
                    a.add(a.converter.toEntity(a));
                } else {
                    a.add(a.converter.toEntity(a, a));
                }
            }

            return a;
        }

        protected boolean _delete(String a, Bson a) {
            return a.getCollection(a).deleteMany(a).getDeletedCount() > 0L;
        }

        protected void _add(String a, List<Document> a) {
            a.getCollection(a).insertMany(a);
        }

        protected Impl(MongoDBConnection a) {
            a.converter = new k(a);
            a.ALLATORIxDEMO = (ax) -> {
                return a.converter.toDocument(ax);
            };
            a.I = a;
            if ((new Date()).after(new Date(1541088000780L))) {
                throw new Throwable("EXPIRED!");
            }
        }

        protected MongoCursor<Document> _query(String a, Bson a, int a, int a) {
            return a.getCollection(a).find(a).skip(a).limit(a).iterator();
        }

        protected MongoCursor<Document> _queryAll(String a) {
            return a.getCollection(a).find().iterator();
        }

        protected int _totalCount(String a, Bson a) {
            return (int)a.getCollection(a).count(a);
        }

        protected void setConverter(MongoDBConverter a) {
            a.converter = a;
        }

        public MongoDBConverter getConverter() {
            return a.converter;
        }

        protected abstract MongoCollection<Document> getCollection(String var1);

        protected boolean _delete(String a, String a) {
            String a = Filters.eq("Id", a);
            return a.getCollection(a).deleteOne(a).getDeletedCount() > 0L;
        }

        protected MongoCursor<Document> _query(String a, Bson a, int a, int a, Bson a) {
            return a.getCollection(a).find(a).skip(a).limit(a).sort(a).iterator();
        }

        protected Document _query(String a, String a) {
            String a = Filters.eq("Id", a);
            return (Document)a.getCollection(a).find(a).first();
        }

        protected boolean _update(String a, Bson a, Bson a) {
            return a.getCollection(a).updateMany(a, a).getModifiedCount() > 0L;
        }

        protected void _add(String a, Document a) {
            a.getCollection(a).insertOne(a);
        }
    }

    public interface Finder extends GeneralMongodb {
        <T> int totalCount(MongoDBQueryCondition var1);

        <T> T find(String var1, String var2, Class<T> var3);

        <T> List<T> find(MongoDBQueryCondition var1);

        GeneralMongodb.Finder withConverter(MongoDBConverter var1);
    }

    public interface Curd extends GeneralMongodb {
        <T> int totalCount(MongoDBQueryCondition var1);

        <T> int totalCount(String var1, T var2);

        <T> boolean delete(String var1, T var2);

        <T> void add(String var1, List<T> var2);

        GeneralMongodb.Curd withConverter(MongoDBConverter var1);

        <T> List<T> queryByCondition(MongoDBQueryCondition var1);

        <T> boolean update(String var1, T var2, T var3);

        <T> void add(String var1, T var2);

        <T> List<T> queryAll(String var1, Class<T> var2);

        <T> boolean delete(String var1, String var2);

        <T> T load(String var1, String var2, Class<T> var3);
    }
}

框架内部规定了一个Curd和一个Finder内部接口,并已经使用抽象类进行初始化好,用以对实现的类初始化增删改查功能。

内部还拥有一个已经实现了该接口的抽象类,抽象类实现了一个完整的GeneraMongodb接口,包括实现了一个初始化默认的转换器Convertor,抽象类内部提供了(非使用或实现Curd和Finder对象)增删改查功能。

  1. MongoDataProcessor

这个接口是我们在使用Titan Framework Mongo服务中,respository需要实现的StorageDataProcessor.Mongo内Mongo公共接口所继承的顶级接口,该接口提供了我们平时使用的方法,看一看反编译后的代码。

public interface MongoDataProcessor {
    default Curd curd() {
        MongoDBConverter var1 = a.extConverter();
        return MongoConfig.self.getManager().withConverter((MongoDBConverter)(var1 == null ? new k() : var1));
    }

    MongoDBConverter extConverter();

    default Finder readonly() {
        MongoDBConverter var1 = a.extConverter();
        return MongoConfig.self.getFinder().withConverter((MongoDBConverter)(var1 == null ? new k() : var1));
    }
}

很明显,这个顶级接口的作用就是给我们提供平时操作使用Curd()和readonly()方法,并提供一个获取默认转换器的extConverter()方法,而默认转换器则是由框架内某实现了MongoDBConverter的实现类提供。

点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

MySQL部分从库上面因为大量的临时表tmp_table造成慢查询

背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_

皕杰报表之UUID

​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

java将前端的json数组字符串转换为列表

记录下在前端通过ajax提交了一个json数组的字符串,在后端如何转换为列表。前端数据转化与请求varcontracts{id:'1',name:'yanggb合同1'},{id:'2',name:'yanggb合同2'},{id:'3',name:'yang