Titan Framework MongoDB深入理解1

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

  1. MongoDBConverter

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

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

     Document toDocument(T var1);

     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 toEntity(Document var1)该方法直接返回null,为一个默认方法,而 T toEntity(Document var1, Class var2)是将一个Document(MongoDB中的一条数据)转换为对应的Entity,最后一个方法 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 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 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 List queryByCondition(String a, Bson a, int a, int a, Bson a, Class 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 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 _query(String a, Bson a, int a, int a) {
            return a.getCollection(a).find(a).skip(a).limit(a).iterator();
        }

        protected MongoCursor _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 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 _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 {
         int totalCount(MongoDBQueryCondition var1);

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

         List find(MongoDBQueryCondition var1);

        GeneralMongodb.Finder withConverter(MongoDBConverter var1);
    }

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

         int totalCount(String var1, T var2);

         boolean delete(String var1, T var2);

         void add(String var1, List var2);

        GeneralMongodb.Curd withConverter(MongoDBConverter var1);

         List queryByCondition(MongoDBQueryCondition var1);

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

         void add(String var1, T var2);

         List queryAll(String var1, Class var2);

         boolean delete(String var1, String var2);

         T load(String var1, String var2, Class 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的实现类提供。

评论

暂无评论