1 /* 2 * Copyright (c) 2021 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 #ifndef RELATIONAL_SCHEMA_OBJECT_H 16 #define RELATIONAL_SCHEMA_OBJECT_H 17 #ifdef RELATIONAL_STORE 18 #include <map> 19 #include "data_value.h" 20 #include "ischema.h" 21 #include "json_object.h" 22 #include "parcel.h" 23 #include "schema_constant.h" 24 #include "table_info.h" 25 26 namespace DistributedDB { 27 using TableInfoMap = std::map<std::string, TableInfo, CaseInsensitiveComparator>; 28 class RelationalSchemaObject : public ISchema { 29 public: 30 RelationalSchemaObject() = default; 31 ~RelationalSchemaObject() override = default; 32 33 bool IsSchemaValid() const override; 34 35 SchemaType GetSchemaType() const override; 36 37 std::string ToSchemaString() const override; 38 39 // Should be called on an invalid SchemaObject, create new SchemaObject if need to reparse 40 int ParseFromSchemaString(const std::string &inSchemaString) override; 41 42 void AddRelationalTable(const TableInfo &table); 43 44 void RemoveRelationalTable(const std::string &tableName); 45 46 const TableInfoMap &GetTables() const; 47 48 std::vector<std::string> GetTableNames() const; 49 50 TableInfo GetTable(const std::string& tableName) const; 51 52 std::string GetSchemaVersion() const; 53 54 DistributedTableMode GetTableMode() const; 55 void SetTableMode(DistributedTableMode mode); 56 57 void InsertTrackerSchema(const TrackerSchema &schema); 58 void RemoveTrackerSchema(const TrackerSchema &schema); 59 TrackerTable GetTrackerTable(const std::string &tableName) const; 60 int ParseFromTrackerSchemaString(const std::string &inSchemaString); 61 const TableInfoMap &GetTrackerTables() const; 62 63 void SetReferenceProperty(const std::vector<TableReferenceProperty> &referenceProperty); 64 const std::vector<TableReferenceProperty> &GetReferenceProperty() const; 65 std::set<std::string> GetSharedTableForChangeTable(std::set<std::string> &changeTables) const; 66 std::set<std::string> CompareReferenceProperty(const std::vector<TableReferenceProperty> &others, 67 bool &isRefNotSet) const; 68 std::map<std::string, std::map<std::string, bool>> GetReachableRef(); 69 std::map<std::string, int> GetTableWeight(); 70 71 bool CheckDistributedSchemaChange(const DistributedSchema &schema); 72 void SetDistributedSchema(const DistributedSchema &schema); 73 DistributedSchema GetDistributedSchema() const; 74 75 bool IsNeedSkipSyncField(const FieldInfo &fieldInfo, const std::string &tableName, 76 bool ignoreTableNonExist = true) const; 77 78 std::vector<FieldInfo> GetSyncFieldInfo(const std::string &tableName, bool ignoreTableNonExist = true) const; 79 80 DistributedTable GetDistributedTable(const std::string &table) const; 81 82 std::map<std::string, bool> GetTableChangeStatus(const DistributedSchema &schema); 83 private: 84 int CompareAgainstSchemaObject(const std::string &inSchemaString, std::map<std::string, int> &cmpRst) const; 85 86 int CompareAgainstSchemaObject(const RelationalSchemaObject &inSchemaObject, 87 std::map<std::string, int> &cmpRst) const; 88 89 int ParseRelationalSchema(const JsonObject &inJsonObject); 90 int ParseCheckSchemaType(const JsonObject &inJsonObject); 91 int ParseCheckTableMode(const JsonObject &inJsonObject); 92 int ParseCheckSchemaVersion(const JsonObject &inJsonObject); 93 int ParseCheckSchemaTableDefine(const JsonObject &inJsonObject); 94 int ParseCheckTableInfo(const JsonObject &inJsonObject); 95 int ParseCheckTableName(const JsonObject &inJsonObject, TableInfo &resultTable); 96 int ParseCheckOriginTableName(const JsonObject &inJsonObject, TableInfo &resultTable); 97 int ParseCheckTableDefine(const JsonObject &inJsonObject, TableInfo &resultTable); 98 int ParseCheckTableFieldInfo(const JsonObject &inJsonObject, const FieldPath &path, FieldInfo &table); 99 int ParseCheckTableAutoInc(const JsonObject &inJsonObject, TableInfo &resultTable); 100 int ParseCheckSharedTableMark(const JsonObject &inJsonObject, TableInfo &resultTable); 101 int ParseCheckTableSyncType(const JsonObject &inJsonObject, TableInfo &resultTable); 102 int ParseCheckTableIndex(const JsonObject &inJsonObject, TableInfo &resultTable); 103 int ParseCheckTableUnique(const JsonObject &inJsonObject, TableInfo &resultTable); 104 int ParseCheckTablePrimaryKey(const JsonObject &inJsonObject, TableInfo &resultTable); 105 int ParseCheckReferenceProperty(const JsonObject &inJsonObject); // parse all reference 106 int ParseCheckReference(const JsonObject &inJsonObject); // parse one reference 107 // parse reference columns 108 int ParseCheckReferenceColumns(const JsonObject &inJsonObject, TableReferenceProperty &tableReferenceProperty); 109 // parse one reference column pair 110 int ParseCheckReferenceColumn(const JsonObject &inJsonObject, TableReferenceProperty &tableReferenceProperty); 111 int ParseDistributedVersion(const JsonObject &inJsonObject); // parse distributed version if need 112 int ParseDistributedSchema(const JsonObject &inJsonObject); // parse distributed schema if need 113 int ParseDistributedTables(const JsonObject &inJsonObject); // parse distributed tables if need 114 int ParseDistributedTable(const JsonObject &inJsonObject); // parse distributed table if need 115 116 void GenerateSchemaString(); 117 void GenerateTrackerSchemaString(); 118 std::string GetReferencePropertyString(); 119 std::string GetOneReferenceString(const TableReferenceProperty &reference); 120 int ParseTrackerSchema(const JsonObject &inJsonObject); 121 int ParseCheckTrackerTable(const JsonObject &inJsonObject); 122 int ParseCheckTrackerTableName(const JsonObject &inJsonObject, TrackerTable &resultTable); 123 int ParseCheckTrackerExtendName(const JsonObject &inJsonObject, TrackerTable &resultTable); 124 int ParseCheckTrackerName(const JsonObject &inJsonObject, TrackerTable &resultTable); 125 int ParseCheckTrackerAction(const JsonObject &inJsonObject, TrackerTable &resultTable); 126 void GenerateReachableRef(); 127 void GenerateTableInfoReferenced(); 128 void RefreshReachableRef(const TableReferenceProperty &referenceProperty); 129 void CalculateTableWeight(const std::set<std::string> &startNodes, 130 const std::map<std::string, std::set<std::string>> &nextNodes); 131 std::string GetDistributedSchemaString(); 132 133 static bool CheckDistributedFieldChange(const std::vector<DistributedField> &source, 134 const std::vector<DistributedField> &target); 135 static std::string GetOneDistributedTableString(const DistributedTable &table); 136 static int ParseDistributedFields(const JsonObject &inJsonObject, std::vector<DistributedField> &fields); 137 static int ParseDistributedField(const JsonObject &inJsonObject, DistributedField &field); 138 139 bool isValid_ = false; // set to true after parse success from string or add at least one relational table 140 SchemaType schemaType_ = SchemaType::RELATIVE; // Default RELATIVE 141 std::string schemaString_; // The minified and valid schemaString 142 std::string schemaVersion_ = std::string(SchemaConstant::SCHEMA_SUPPORT_VERSION_V2); // Default version 2.0 143 TableInfoMap tables_; 144 TableInfoMap trackerTables_; 145 std::vector<TableReferenceProperty> referenceProperty_; 146 std::map<std::string, std::map<std::string, bool>> reachableReference_; 147 std::map<std::string, int> tableWeight_; 148 DistributedSchema dbSchema_; 149 150 DistributedTableMode tableMode_ = DistributedTableMode::SPLIT_BY_DEVICE; 151 }; 152 } // namespace DistributedDB 153 #endif // RELATIONAL_STORE 154 #endif // RELATIONAL_SCHEMA_OBJECT_H