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!");
            }
        }
    }
}spa

 

首先咱們來了解三個方法,其中重載了一個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);
    }
}ip

 

框架內部規定了一個Curd和一個Finder內部接口,並已經使用抽象類進行初始化好,用以對實現的類初始化增刪改查功能。ci

內部還擁有一個已經實現了該接口的抽象類,抽象類實現了一個完整的GeneraMongodb接口,包括實現了一個初始化默認的轉換器Convertor,抽象類內部提供了(非使用或實現Curd和Finder對象)增刪改查功能。get

  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的實現類提供。

相關文章
相關標籤/搜索