public static TablesMeta getMetadata()

in holo-shipper/src/main/java/com/alibaba/hologres/shipper/utils/TablesMeta.java [21:144]


    public static TablesMeta getMetadata(JSONObject shipList, JSONObject blackList, boolean restoreOwner, boolean restorePriv, boolean restoreForeign, boolean restoreView, String metaContent, String dbName) {
        JSONObject dbMeta = JSON.parseObject(metaContent);
        JSONArray allTables = dbMeta.getJSONArray("tableInfo");
        TablesMeta tablesMeta = new TablesMeta();
        List<TableInfo> tableInfoList = new ArrayList<>();
        Set<String> tables = new HashSet<>();
        //找到所有符合shipList的表和他们的父表/子表
        for(int i=0; i<allTables.size(); i++) {
            JSONObject table = allTables.getJSONObject(i);
            String schemaName = table.getString("schemaname");
            String tableName = table.getString("tablename");
            String parentSchema = table.getString("parentschema");
            String parentTable = table.getString("parenttable");
            boolean isForeign = table.getBoolean("foreign");
            boolean isView = table.getBoolean("view");
            if ((!isView || restoreView) && (!isForeign || restoreForeign) && (AbstractDB.filterListContains(shipList, schemaName, tableName) || (parentSchema != null && AbstractDB.filterListContains(shipList, parentSchema, parentTable)))) {
                tables.add(schemaName + '.' + tableName);
                if (parentSchema != null ) {
                    tables.add(parentSchema + '.' + parentTable);
                }
            }
        }
        //根据blackList filter
        if(blackList != null) {
            for(int i=0; i<allTables.size(); i++) {
                JSONObject table = allTables.getJSONObject(i);
                String schemaName = table.getString("schemaname");
                String tableName = table.getString("tablename");
                String parentSchema = table.getString("parentschema");
                String parentTable = table.getString("parenttable");
                if (AbstractDB.filterListContains(blackList, schemaName, tableName) || (parentSchema != null && AbstractDB.filterListContains(blackList, parentSchema, parentTable))) {
                    tables.remove(schemaName + '.' + tableName);
                }
            }
        }
        for(int i=0; i<allTables.size(); i++) {
            JSONObject table = allTables.getJSONObject(i);
            String schemaName = table.getString("schemaname");
            String tableName = table.getString("tablename");
            if(tables.contains(schemaName + '.' + tableName)) {
                TableInfo tableInfo = new TableInfo();
                tableInfo.schemaName = schemaName;
                tableInfo.tableName = tableName;
                tableInfo.isPartitioned = table.getBoolean("partitioned");
                tableInfo.parentSchema = table.getString("parentschema");
                tableInfo.parentTable = table.getString("parenttable");
                tableInfo.isForeign = table.getBoolean("foreign");
                tableInfo.isView = table.getBoolean("view");
                tableInfoList.add(tableInfo);
            }
        }
        tablesMeta.tableInfoList = tableInfoList;
        //get spm and slpm info
        JSONObject spmInfo = dbMeta.getJSONObject("spmInfo");
        JSONObject slpmInfo = dbMeta.getJSONObject("slpmInfo");
        if(spmInfo != null) {
            restoreOwner = false;
            restorePriv = false;
            Map<String, List<String>> spmInfoMap = new HashMap<>();
            for(String groupName : spmInfo.keySet()) {
                List<String> members = jsonToList(spmInfo.getJSONArray(groupName));
                spmInfoMap.put(groupName, members);
            }
            tablesMeta.spmInfo = spmInfoMap;
        } else
            tablesMeta.spmInfo = null;
        if(slpmInfo != null) {
            restoreOwner = false;
            restorePriv = false;
            Map<String, List<String>> slpmInfoMap = new HashMap<>();
            List<String> roleGroups = new ArrayList<>();
            roleGroups.add(String.format("%s.admin", dbName));
            for(String schemaName: shipList.keySet()) {
                roleGroups.add(String.format("%s.%s.developer", dbName, schemaName));
                roleGroups.add(String.format("%s.%s.writer", dbName, schemaName));
                roleGroups.add(String.format("%s.%s.viewer", dbName, schemaName));
            }
            for(String groupName : roleGroups) {
                List<String> members = jsonToList(slpmInfo.getJSONArray(groupName));
                slpmInfoMap.put(groupName, members);
            }
            tablesMeta.slpmInfo = slpmInfoMap;
        } else
            tablesMeta.slpmInfo = null;
        //get owner info
        if(restoreOwner) {
            JSONObject allOwnerInfo = dbMeta.getJSONObject("ownerInfo");
            if(allOwnerInfo != null) {
                Map<String, String> ownerInfo = new HashMap<>();
                for(String table : tables) {
                    String owner = allOwnerInfo.getString(table);
                    if (owner != null)
                        ownerInfo.put(table, owner);
                }
                tablesMeta.ownerInfo = ownerInfo;
            }
            else {
                LOGGER.warn("Owner info was not dumped! will not restore owner for database " + dbName);
                tablesMeta.ownerInfo = null;
            }
        }
        else
            tablesMeta.ownerInfo = null;
        //get privileges info
        if(restorePriv) {
            JSONObject grantees = dbMeta.getJSONObject("granteesInfo");
            if(grantees != null) {
                Map<String, List<String>> granteesInfo = new HashMap<>();
                for(String table : tables) {
                    List<String> tableGrantees = jsonToList(grantees.getJSONArray(table));
                    granteesInfo.put(table, tableGrantees);
                }
                tablesMeta.granteesInfo = granteesInfo;
            }
            else {
                LOGGER.warn("Privileges info was not dumped! will not restore table privileges for database " + dbName);
                tablesMeta.granteesInfo = null;
            }
        }
        else
            tablesMeta.granteesInfo = null;
        LOGGER.info("Finished reading metadata for database " + dbName);
        return tablesMeta;
    }